com.github.fge.jsonschema.core.report.ProcessingReport Java Examples
The following examples show how to use
com.github.fge.jsonschema.core.report.ProcessingReport.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataExportRecipeValidatorTest.java From TomboloDigitalConnector with MIT License | 6 votes |
@Test public void testValidateWithValidBuilder() throws Exception { DataExportSpecificationBuilder spec = DataExportSpecificationBuilder.withGeoJsonExporter().addSubjectSpecification( new SubjectSpecificationBuilder("uk.gov.ons", "lsoa").setMatcher("label", "E01002766")) .addSubjectSpecification( new SubjectSpecificationBuilder("uk.gov.ons", "localAuthority").setMatcher("label", "E08000035")) .addDatasourceSpecification("uk.org.tombolo.importer.ons.CensusImporter", "qs103ew", "") .addFieldSpecification( FieldBuilder.wrapperField("attributes", Arrays.asList( FieldBuilder.fractionOfTotal("percentage_under_1_years_old_label") .addDividendAttribute("uk.gov.ons", "CL_0000053_2") // number under one year old .setDivisorAttribute("uk.gov.ons", "CL_0000053_1") // total population )) ); ProcessingReport report = DataExportRecipeValidator.validate(new StringReader(spec.toJSONString())); assertTrue("Spec is valid", report.isSuccess()); }
Example #2
Source File: ExtensionSchemaValidationTest.java From syndesis with Apache License 2.0 | 6 votes |
@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()); }
Example #3
Source File: ExtensionSchemaValidationTest.java From syndesis with Apache License 2.0 | 6 votes |
@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()); }
Example #4
Source File: SyntaxProcessing.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 6 votes |
@Override protected JsonNode buildResult(final String input) throws IOException, ProcessingException { final ObjectNode ret = FACTORY.objectNode(); final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, input); final JsonNode schemaNode = ret.remove(INPUT); if (invalidSchema) return ret; final ProcessingReport report = VALIDATOR.validateSchema(schemaNode); final boolean success = report.isSuccess(); ret.put(VALID, success); ret.put(RESULTS, JacksonUtils.prettyPrint(buildReport(report))); return ret; }
Example #5
Source File: OpenApiSchemaValidator.java From syndesis with Apache License 2.0 | 6 votes |
/** * Validates given specification Json and add validation errors and warnings to given open api info model builder. * @param specRoot the specification as Json root. * @param modelBuilder the model builder receiving all validation errors and warnings. */ default void validateJSonSchema(JsonNode specRoot, OpenApiModelInfo.Builder modelBuilder) { try { final ProcessingReport report = getSchema().validate(specRoot); final List<Violation> errors = new ArrayList<>(); final List<Violation> warnings = new ArrayList<>(); for (final ProcessingMessage message : report) { final boolean added = append(errors, message, Optional.of("error")); if (!added) { append(warnings, message, Optional.empty()); } } modelBuilder.addAllErrors(errors); modelBuilder.addAllWarnings(warnings); } catch (ProcessingException ex) { LoggerFactory.getLogger(OpenApiSchemaValidator.class).error("Unable to load the schema file embedded in the artifact", ex); modelBuilder.addError(new Violation.Builder() .error("error").property("") .message("Unable to load the OpenAPI schema file embedded in the artifact") .build()); } }
Example #6
Source File: Index.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 6 votes |
private static JsonNode buildResult(final String rawSchema, final String rawData) throws IOException { final ObjectNode ret = JsonNodeFactory.instance.objectNode(); final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, rawSchema); final boolean invalidData = fillWithData(ret, INPUT2, INVALID_INPUT2, rawData); final JsonNode schemaNode = ret.remove(INPUT); final JsonNode data = ret.remove(INPUT2); if (invalidSchema || invalidData) return ret; final ProcessingReport report = VALIDATOR.validateUnchecked(schemaNode, data); final boolean success = report.isSuccess(); ret.put(VALID, success); final JsonNode node = ((AsJson) report).asJson(); ret.put(RESULTS, JacksonUtils.prettyPrint(node)); return ret; }
Example #7
Source File: JJSchemaProcessing.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 6 votes |
@Override protected JsonNode buildResult(final String input) throws IOException { final ValueHolder<String> holder = ValueHolder.hold("source", input); final ProcessingReport report = new ListProcessingReport(); final ProcessingResult<ValueHolder<SchemaTree>> result = ProcessingResult.uncheckedResult(PROCESSOR, report, holder); final ProcessingReport processingReport = result.getReport(); final ObjectNode ret = FACTORY.objectNode(); final boolean success = processingReport.isSuccess(); ret.put(VALID, success); final JsonNode content = success ? result.getResult().getValue().getBaseNode() : buildReport(result.getReport()); ret.put(RESULTS, JacksonUtils.prettyPrint(content)); return ret; }
Example #8
Source File: ValidationService.java From yaml-json-validator-maven-plugin with Apache License 2.0 | 6 votes |
private void validateAgainstSchema(final JsonNode spec, final ValidationResult validationResult) { if (schema == null) { return; } try { final ProcessingReport report = schema.validate(spec); if (!report.isSuccess()) { validationResult.encounteredError(); } for (final ProcessingMessage processingMessage : report) { validationResult.addMessage(processingMessage.toString()); } } catch (final ProcessingException e) { validationResult.addMessage(e.getMessage()); validationResult.encounteredError(); } }
Example #9
Source File: JsonSchemaValidationTest.java From microprofile-health with Apache License 2.0 | 6 votes |
/** * Verifies that the JSON object returned by the implementation is following the * JSON schema defined by the specification */ @Test @RunAsClient public void testPayloadJsonVerifiesWithTheSpecificationSchema() throws Exception { Response response = getUrlHealthContents(); Assert.assertEquals(response.getStatus(), 200); JsonObject json = readJson(response); ObjectMapper mapper = new ObjectMapper(); JsonNode schemaJson = mapper.readTree(Thread.currentThread() .getContextClassLoader().getResourceAsStream("health-check-schema.json")); final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); final JsonSchema schema = factory.getJsonSchema(schemaJson); ProcessingReport report = schema.validate(toJsonNode(json)); Assert.assertTrue(report.isSuccess(), "Returned Health JSON does not validate against the specification schema"); }
Example #10
Source File: SchemaValidator.java From swagger-inflector with Apache License 2.0 | 6 votes |
public static boolean validate(Object argument, String schema, Direction direction) { try { JsonNode schemaObject = Json.mapper().readTree(schema); JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonNode content = Json.mapper().convertValue(argument, JsonNode.class); com.github.fge.jsonschema.main.JsonSchema jsonSchema = factory.getJsonSchema(schemaObject); ProcessingReport report = jsonSchema.validate(content); if(!report.isSuccess()) { if(direction.equals(Direction.INPUT)) { LOGGER.warn("input: " + content.toString() + "\n" + "does not match schema: \n" + schema); } else { LOGGER.warn("response: " + content.toString() + "\n" + "does not match schema: \n" + schema); } } return report.isSuccess(); } catch (Exception e) { LOGGER.error("can't validate model against schema", e); } return true; }
Example #11
Source File: JsonSchemaValidator.java From microcks with Apache License 2.0 | 6 votes |
/** * Validate a Json object representing by its text against a schema object representing byt its * text too. Validation is a deep one: its pursue checking children nodes on a failed parent. Validation * is respectful of Json schema spec semantics regarding additional or unknown attributes: schema must * explicitely set <code>additionalProperties</code> to false if you want to consider unknown attributes * as validation errors. It returns a list of validation error messages. * @param schemaNode The Json schema specification as a Jackson node * @param jsonNode The Json object as a Jackson node * @return The list of validation failures. If empty, json object is valid ! * @throws ProcessingException if json node does not represent valid Schema */ public static List<String> validateJson(JsonNode schemaNode, JsonNode jsonNode) throws ProcessingException { List<String> errors = new ArrayList<>(); final JsonSchema jsonSchemaNode = extractJsonSchemaNode(schemaNode); // Ask for a deep check to get a full error report. ProcessingReport report = jsonSchemaNode.validate(jsonNode, true); if (!report.isSuccess()) { for (ProcessingMessage processingMessage : report) { errors.add(processingMessage.getMessage()); } } return errors; }
Example #12
Source File: SchemaValidationTest.java From swagger-inflector with Apache License 2.0 | 6 votes |
@Test public void testValidation() throws Exception { String schemaAsString = "{\n" + " \"properties\": {\n" + " \"id\": {\n" + " \"type\": \"integer\",\n" + " \"format\": \"int64\"\n" + " }\n" + " }\n" + "}"; JsonNode schemaObject = Json.mapper().readTree(schemaAsString); JsonNode content = Json.mapper().readValue("{\n" + " \"id\": 123\n" + "}", JsonNode.class); JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); com.github.fge.jsonschema.main.JsonSchema schema = factory.getJsonSchema(schemaObject); ProcessingReport report = schema.validate(content); assertTrue(report.isSuccess()); }
Example #13
Source File: LEEFParserTest.java From metron with Apache License 2.0 | 5 votes |
protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws Exception { final JsonNode d = JsonLoader.fromString(jsonData); final JsonNode s = JsonLoader.fromString(jsonSchema); final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonValidator v = factory.getValidator(); ProcessingReport report = v.validate(s, d); return report.toString().contains("success"); }
Example #14
Source File: AbstractSchemaValidatorTest.java From elucidate-server with MIT License | 5 votes |
protected void validateJson(String jsonFileName) throws IOException, ProcessingException, JsonLdError { JsonNode schema = getSchema(); assertNotNull(schema); String jsonStr = getJson(jsonFileName); assertNotNull(jsonStr); Object jsonObj = JsonUtils.fromString(jsonStr); List<Object> expandedJson = JsonLdProcessor.expand(jsonObj); jsonStr = JsonUtils.toString(expandedJson); JsonNode json = JsonLoader.fromString(jsonStr); JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator(); ProcessingReport processingReport = jsonValidator.validate(schema, json); assertNotNull(processingReport); if (!processingReport.isSuccess()) { ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode(); assertNotNull(jsonArray); Iterator<ProcessingMessage> iterator = processingReport.iterator(); while (iterator.hasNext()) { ProcessingMessage processingMessage = iterator.next(); jsonArray.add(processingMessage.asJson()); } String errorJson = JsonUtils.toPrettyString(jsonArray); assertNotNull(errorJson); fail(errorJson); } }
Example #15
Source File: JsonSchemaAssertions.java From Bastion with GNU General Public License v3.0 | 5 votes |
private void assertResponseConformsToSchema(JsonNode response) throws ProcessingException, IOException { ProcessingReport validationReport = JsonSchemaFactory.byDefault() .getJsonSchema(getExpectedSchema()).validate(response); if (!validationReport.isSuccess()) { String messages = StreamSupport.stream(validationReport.spliterator(), false) .map(ProcessingMessage::getMessage) .collect(Collectors.joining(", ")); Assert.fail(String.format("Actual response body is not as specified. The following message(s) where produced during validation; %s.", messages)); } }
Example #16
Source File: JsonValidator.java From Poseidon with Apache License 2.0 | 5 votes |
private void validate(String schemaPath, String filePath) throws IOException, ProcessingException { JsonNode schema = JsonLoader.fromResource(schemaPath); JsonNode json = JsonLoader.fromPath(filePath); com.github.fge.jsonschema.main.JsonValidator validator = JsonSchemaFactory.byDefault().getValidator(); ProcessingReport report = validator.validate(schema, json); if ((report == null) || !report.isSuccess()) { logger.error("Invalid JSON"); if (report != null) { throw new ProcessingException(report.toString()); } else { throw new ProcessingException("JSON validation report is null for " + filePath); } } }
Example #17
Source File: ErrorProcessor.java From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 | 5 votes |
/** * Returns set of {@link SwaggerError} created from a validation report. * * @param report * to process * @return set of validation errors */ public Set<SwaggerError> processReport(ProcessingReport report) { final Set<SwaggerError> errors = new HashSet<>(); if (report != null) { for (Iterator<ProcessingMessage> it = report.iterator(); it.hasNext();) { errors.addAll(processMessage(it.next())); } } return errors; }
Example #18
Source File: CEFParserTest.java From metron with Apache License 2.0 | 5 votes |
protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws Exception { final JsonNode d = JsonLoader.fromString(jsonData); final JsonNode s = JsonLoader.fromString(jsonSchema); final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonValidator v = factory.getValidator(); ProcessingReport report = v.validate(s, d); return report.toString().contains("success"); }
Example #19
Source File: BenderConfig.java From bender with Apache License 2.0 | 5 votes |
public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema) throws ConfigurationException { ProcessingReport report; try { /* * Create object */ JsonNode node = objectMapper.readTree(data); /* * Create JSON schema */ JsonNode jsonSchema = benderSchema.getSchema(); /* * Validate */ final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); final JsonSchema schema = factory.getJsonSchema(jsonSchema); report = schema.validate(node); } catch (IOException | ProcessingException ioe) { throw new ConfigurationException("unable to validate config", ioe); } if (report.isSuccess()) { return true; } else { throw new ConfigurationException("invalid config file", report.iterator().next().asException()); } }
Example #20
Source File: JsonPatch.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 5 votes |
private static JsonNode buildResult(final String rawPatch, final String rawData) throws IOException { final ObjectNode ret = JsonNodeFactory.instance.objectNode(); final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, rawPatch); final boolean invalidData = fillWithData(ret, INPUT2, INVALID_INPUT2, rawData); final JsonNode patchNode = ret.remove(INPUT); final JsonNode data = ret.remove(INPUT2); if (invalidSchema || invalidData) return ret; final JsonPatchInput input = new JsonPatchInput(patchNode, data); final ProcessingReport report = new ListProcessingReport(); final ProcessingResult<ValueHolder<JsonNode>> result = ProcessingResult.uncheckedResult(PROCESSOR, report, input); final boolean success = result.isSuccess(); ret.put(VALID, success); final JsonNode node = result.isSuccess() ? result.getResult() .getValue() : buildReport(result.getReport()); ret.put(RESULTS, JacksonUtils.prettyPrint(node)); return ret; }
Example #21
Source File: JsonPatch.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 5 votes |
private static JsonNode buildReport(final ProcessingReport report) { final ArrayNode ret = JacksonUtils.nodeFactory().arrayNode(); for (final ProcessingMessage message: report) ret.add(message.asJson()); return ret; }
Example #22
Source File: Processing.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 5 votes |
protected static JsonNode buildReport(final ProcessingReport report) { final ArrayNode ret = FACTORY.arrayNode(); for (final ProcessingMessage message: report) ret.add(message.asJson()); return ret; }
Example #23
Source File: AvroProcessing.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected JsonNode buildResult(final String input) throws IOException, ProcessingException { final ObjectNode ret = FACTORY.objectNode(); final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, input); final JsonNode schemaNode = ret.remove(INPUT); if (invalidSchema) return ret; final JsonTree tree = new SimpleJsonTree(schemaNode); final ValueHolder<JsonTree> holder = ValueHolder.hold(tree); final ProcessingReport report = new ListProcessingReport(); final ProcessingResult<ValueHolder<SchemaTree>> result = ProcessingResult.uncheckedResult(PROCESSOR, report, holder); final boolean success = result.isSuccess(); final JsonNode content = success ? result.getResult().getValue().getBaseNode() : buildReport(result.getReport()); ret.put(VALID, success); ret.put(RESULTS, JacksonUtils.prettyPrint(content)); return ret; }
Example #24
Source File: Schema2PojoProcessing.java From json-schema-validator-demo with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected JsonNode buildResult(final String input) throws IOException, ProcessingException { final ObjectNode ret = FACTORY.objectNode(); final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, input); final JsonNode schemaNode = ret.remove(INPUT); if (invalidSchema) return ret; final SchemaTree tree = new CanonicalSchemaTree(SchemaKey.anonymousKey(), schemaNode); final ValueHolder<SchemaTree> holder = ValueHolder.hold("schema", tree); final ProcessingReport report = new ListProcessingReport(); final ProcessingResult<ValueHolder<String>> result = ProcessingResult.uncheckedResult(PROCESSOR, report, holder); final boolean success = result.isSuccess(); ret.put(VALID, success); final String content = success ? result.getResult().getValue() : JacksonUtils.prettyPrint(buildReport(result.getReport())); ret.put(RESULTS, content); return ret; }
Example #25
Source File: JSONValidator.java From arctic-sea with Apache License 2.0 | 5 votes |
public ProcessingReport validate(JsonNode node, String schema) { JsonSchema jsonSchema; try { jsonSchema = getJsonSchemaFactory().getJsonSchema(schema); } catch (ProcessingException ex) { throw new IllegalArgumentException("Unknown schema: " + schema, ex); } return jsonSchema.validateUnchecked(node); }
Example #26
Source File: JsonSchemaUtils.java From TestHub with MIT License | 5 votes |
/** * 将需要验证的JsonNode 与 JsonSchema标准对象 进行比较 * * @param schema schema标准对象 * @param data 需要比对的Schema对象 */ private static void assertJsonSchema(JsonNode schema, JsonNode data) { ProcessingReport report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); if (!report.isSuccess()) { for (ProcessingMessage aReport : report) { Reporter.log(aReport.getMessage(), true); } } Assert.assertTrue(report.isSuccess()); }
Example #27
Source File: ExtensionSchemaValidationTest.java From syndesis with Apache License 2.0 | 5 votes |
@Test public void validateStepExtensionTest() 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(ExtensionConverter.getCurrentSchemaVersion()) .addAction(new StepAction.Builder() .id("action-1") .name("action-1-name") .description("Action 1 Description") .pattern(Action.Pattern.From) .descriptor(new StepDescriptor.Builder() .entrypoint("direct:hello") .kind(StepAction.Kind.ENDPOINT) .build()) .build()) .build(); JsonNode tree = converter.toPublicExtension(extension); ProcessingReport report = schema.validate(tree); assertFalse(report.toString(), report.iterator().hasNext()); Extension extensionClone = converter.toInternalExtension(tree); assertEquals(extensionClone, extension); }
Example #28
Source File: ODataSerializerTest.java From syndesis with Apache License 2.0 | 5 votes |
private static void validateResultAgainstSchema(JsonNode jsonResultNode, JsonNode schemaNode) throws ProcessingException { String schemaURI = "http://json-schema.org/schema#"; LoadingConfiguration loadingConfiguration = LoadingConfiguration.newBuilder() .preloadSchema(schemaURI, schemaNode) .freeze(); JsonSchema jsonSchema = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(loadingConfiguration) .freeze() .getJsonSchema(schemaURI); ProcessingReport report = jsonSchema.validate(jsonResultNode); assertTrue(report.isSuccess()); }
Example #29
Source File: SchemaUtils.java From karate with MIT License | 5 votes |
public static boolean isValid(String json, String schema) throws Exception { JsonNode schemaNode = JsonLoader.fromString(schema); JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonSchema jsonSchema = factory.getJsonSchema(schemaNode); JsonNode jsonNode = JsonLoader.fromString(json); ProcessingReport report = jsonSchema.validate(jsonNode); logger.debug("report: {}", report); return report.isSuccess(); }
Example #30
Source File: FieldDecoderTest.java From arctic-sea with Apache License 2.0 | 5 votes |
protected SweField validateWithValueAndDecode(ObjectNode json, boolean withValue) throws DecodingException { ProcessingReport report = validator.validate(json, withValue ? SchemaConstants.Common.FIELD_WITH_VALUE : SchemaConstants.Common.FIELD); if (!report.isSuccess()) { System.err.println(validator.encode(report, json)); fail("Invalid generated field!"); } return decoder.decode(json); }