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);
}
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;
}
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);
}
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());
}
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());
}
Aggregations