Search in sources :

Example 1 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project useful-java-links by Vedenin.

the class JsonSchemaValidatorHelloWorld method main.

public static void main(final String... args) throws IOException, ProcessingException {
    final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    final JsonSchema schema = factory.getJsonSchema(fstabSchema);
    ProcessingReport report;
    report = schema.validate(good);
    System.out.println(report);
    report = schema.validate(bad);
    System.out.println(report);
    report = schema.validate(bad2);
    System.out.println(report);
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project KaiZen-OpenAPI-Editor by RepreZen.

the class Validator method validateAgainstSchema.

public Set<SwaggerError> validateAgainstSchema(ErrorProcessor processor, JsonNode schemaAsJson, JsonNode documentAsJson) {
    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder().setLoadingConfiguration(loadingConfiguration).freeze();
    final Set<SwaggerError> errors = Sets.newHashSet();
    JsonSchema schema = null;
    try {
        schema = factory.getJsonSchema(schemaAsJson);
    } catch (ProcessingException e) {
        YEditLog.logException(e);
        return errors;
    }
    try {
        ProcessingReport report = schema.validate(documentAsJson, true);
        errors.addAll(processor.processReport(report));
    } catch (ProcessingException e) {
        errors.addAll(processor.processMessage(e.getProcessingMessage()));
    }
    return errors;
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Example 3 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project IPK-BrAPI-Validator by plantbreeding.

the class SchemaValidator method validate.

/**
 * Validate an instance with a schema
 *
 * @param path           Path to the schema.
 * @param instanceString String with the JSON to be validated
 * @return Processing report of the validation.
 * @throws IOException         Thrown when reading the schema is not possible.
 * @throws ProcessingException Thrown when validating the instance.
 */
public ProcessingReport validate(String path, String instanceString) throws ProcessingException, IOException {
    JsonNode instance = NODE_READER.fromInputStream(new ByteArrayInputStream(instanceString.getBytes(Charset.defaultCharset())));
    final JsonSchema schemaNode = validator.getJsonSchema("resource:" + path);
    // Unchecked, deepCheck
    return schemaNode.validateUnchecked(instance, true);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project syndesis by syndesisio.

the class ExtensionSchemaValidationTest method addSchemaVersionInPublicModelExtensionTest.

@Test
public void addSchemaVersionInPublicModelExtensionTest() throws ProcessingException, IOException {
    String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
    ExtensionConverter converter = new DefaultExtensionConverter();
    ObjectNode tree = OBJECT_MAPPER.createObjectNode().put("extensionId", "my-extension").put("name", "Name").put("description", "Description").put("version", "1.0.0");
    ProcessingReport report = schema.validate(tree);
    assertFalse(report.toString(), report.iterator().hasNext());
    Extension extension = converter.toInternalExtension(tree);
    assertEquals(ExtensionConverter.getCurrentSchemaVersion(), extension.getSchemaVersion());
}
Also used : Extension(io.syndesis.common.model.extension.Extension) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) Test(org.junit.Test)

Example 5 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project syndesis by syndesisio.

the class ExtensionSchemaValidationTest method upgradePublicModelExtensionTest.

@Test
public void upgradePublicModelExtensionTest() throws ProcessingException, IOException {
    String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
    ExtensionConverter converter = new DefaultExtensionConverter();
    Extension extension = new Extension.Builder().extensionId("my-extension").name("Name").description("Description").version("1.0.0").schemaVersion("old-V0.1").extensionType(Extension.Type.Steps).build();
    JsonNode tree = converter.toPublicExtension(extension);
    ProcessingReport report = schema.validate(tree);
    assertFalse(report.toString(), report.iterator().hasNext());
    Extension extensionClone = converter.toInternalExtension(tree);
    assertNotEquals(extensionClone, extension);
    assertEquals(ExtensionConverter.getCurrentSchemaVersion(), extensionClone.getSchemaVersion());
}
Also used : Extension(io.syndesis.common.model.extension.Extension) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

JsonSchema (com.github.fge.jsonschema.main.JsonSchema)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)8 JsonSchemaFactory (com.github.fge.jsonschema.main.JsonSchemaFactory)7 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)4 Extension (io.syndesis.common.model.extension.Extension)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ParserException (com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserException)1 Swagger (io.swagger.models.Swagger)1 SwaggerParser (io.swagger.parser.SwaggerParser)1 StepDescriptor (io.syndesis.common.model.action.StepDescriptor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 MalformedURLException (java.net.MalformedURLException)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1