Last active
July 17, 2018 12:14
-
-
Save ttomsu/53437f215e342dcb63d0 to your computer and use it in GitHub Desktop.
Testing out JSON Schema validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add these to orca's `build.gradle` file under `subprojects.dependencies` | |
compile "com.fasterxml.jackson.module:jackson-module-jsonSchema:2.7.0" | |
compile "com.github.fge:json-schema-validator:2.2.6" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2016 Google, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License") | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.netflix.spinnaker.orca.kato.pipeline.support | |
import com.fasterxml.jackson.databind.JsonNode | |
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator | |
import com.github.fge.jackson.JsonLoader | |
import com.github.fge.jsonschema.core.report.ProcessingReport | |
import com.github.fge.jsonschema.main.JsonSchemaFactory | |
import com.netflix.spinnaker.orca.jackson.OrcaObjectMapper | |
import groovy.transform.Canonical | |
import groovy.util.logging.Slf4j | |
import spock.lang.Shared | |
import spock.lang.Specification | |
@Slf4j | |
class JsonSchemaValidationSpec extends Specification { | |
@Canonical | |
static class Foo { | |
// Tests do not pass if required is true, because schema generator has incorrect output: | |
// See https://github.com/FasterXML/jackson-module-jsonSchema/issues/15 | |
// | |
// @JsonProperty(required = true) | |
String bar | |
int baz | |
} | |
@Shared OrcaObjectMapper mapper | |
@Shared String fooSchema | |
@Shared String fooAsJsonString | |
def setupSpec() { | |
// Why should I craft the JSON schema by hand when a tool will do it for me? | |
mapper = new OrcaObjectMapper() | |
JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper); | |
com.fasterxml.jackson.module.jsonSchema.JsonSchema jacksonJsonSchema = generator.generateSchema(Foo.class); | |
fooSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jacksonJsonSchema) | |
log.info fooSchema | |
Foo testFoo = new Foo(bar: "duftler", baz: 42) | |
fooAsJsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(testFoo) | |
log.info fooAsJsonString | |
} | |
def "validate json string against schema"() { | |
given: | |
// Create the schema object to validate against. | |
com.github.fge.jsonschema.main.JsonSchema fgeSchema = JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(fooSchema)) | |
// Create the JSON to be validated. | |
JsonNode toBeValidated = JsonLoader.fromString(fooAsJsonString) | |
when: | |
ProcessingReport report = fgeSchema.validate(toBeValidated) | |
then: | |
report.isSuccess() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16:20:00.994 [main] INFO c.n.s.o.k.p.s.JsonSchemaValidationSpec - { | |
"type" : "object", | |
"id" : "urn:jsonschema:com:netflix:spinnaker:orca:kato:pipeline:support:JsonSchemaValidationSpec:Foo", | |
"properties" : { | |
"bar" : { | |
"type" : "string" | |
}, | |
"baz" : { | |
"type" : "integer" | |
} | |
} | |
} | |
16:20:01.010 [main] INFO c.n.s.o.k.p.s.JsonSchemaValidationSpec - { | |
"bar" : "duftler", | |
"baz" : 42 | |
} | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ajordens, @duftler - PTAL