Iterator<ProcessingMessage> iter = report.iterator(); while (iter.hasNext()) { ProcessingMessage pm = iter.next();
/** * 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; }
/** * Validates input node against given schema. Returns NULL if no errors * reported, else returns string representing violations. * * @param schema the json schema (see #loadSchema) * @param node the json node to validate * @return null if there are no errors, else string with all errors and * warnings * @throws ProcessingException */ public static String jsonSchemaValidation(JsonSchema schema, JsonNode node) throws ProcessingException, JsonProcessingException { ProcessingReport report = schema.validate(node); Iterator<ProcessingMessage> i = report.iterator(); StringBuilder buff = new StringBuilder(); while (!report.isSuccess() && i != null && i.hasNext()) { ProcessingMessage pm = i.next(); // attempting to pretty print the json ObjectMapper mapper = new ObjectMapper(); String prettyPrintJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pm.asJson()); buff.append(prettyPrintJson).append("\n\n"); } return report.isSuccess() ? null : buff.toString(); }
/** * Validates input node against given schema. Returns NULL if no errors * reported, else returns string representing violations. * * @param schema the json schema (see #loadSchema) * @param node the json node to validate * @return null if there are no errors, else string with all errors and * warnings * @throws ProcessingException */ public static String jsonSchemaValidation(JsonSchema schema, JsonNode node) throws ProcessingException, JsonProcessingException { ProcessingReport report = schema.validate(node); Iterator<ProcessingMessage> i = report.iterator(); StringBuilder buff = new StringBuilder(); while (!report.isSuccess() && i != null && i.hasNext()) { ProcessingMessage pm = i.next(); // attempting to pretty print the json ObjectMapper mapper = new ObjectMapper(); String prettyPrintJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pm.asJson()); buff.append(prettyPrintJson).append("\n\n"); } return report.isSuccess() ? null : buff.toString(); }
/** * Validates input node against given schema. Returns NULL if no errors * reported, else returns string representing violations. * * @param schema the json schema (see #loadSchema) * @param node the json node to validate * @return null if there are no errors, else string with all errors and * warnings * @throws ProcessingException */ public static String jsonSchemaValidation(JsonSchema schema, JsonNode node) throws ProcessingException, JsonProcessingException { ProcessingReport report = schema.validate(node); Iterator<ProcessingMessage> i = report.iterator(); StringBuilder buff = new StringBuilder(); while (!report.isSuccess() && i != null && i.hasNext()) { ProcessingMessage pm = i.next(); // attempting to pretty print the json ObjectMapper mapper = new ObjectMapper(); String prettyPrintJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pm.asJson()); buff.append(prettyPrintJson).append("\n\n"); } return report.isSuccess() ? null : buff.toString(); }
} else { StringBuffer result = new StringBuffer("Validation against Json schema failed: \n"); Iterator<ProcessingMessage> itr = report.iterator(); while (itr.hasNext()) { ProcessingMessage message = (ProcessingMessage) itr.next();
private void validate(JsonNode rootNode) { final JsonSchemaFactory factory; try { factory = JsonSchemaFactory.byDefault(); factory.getJsonSchema(rootNode); } catch (ProcessingException e) { throw new IllegalStateException("Invalid schema json was generated", e); } catch (ExceptionInInitializerError | NoClassDefFoundError e) { return; // this should only happen from test code. JsonSchemaFactory not easily mocked } ProcessingReport report = factory.getSyntaxValidator().validateSchema(rootNode); if (!report.isSuccess()) { throw new IllegalStateException("Invalid schema json was generated" + report.iterator().next().getMessage()); } }
public boolean validateJson(JsonObject json) throws Exception { // Convert the JsonObject (or String) to a internal node final JsonNode instance = JsonLoader.fromString(json.toString()); // Load the JsonSchema final JsonNode schema = JsonLoader.fromResource("fileNameOfYourJsonSchema"); // Create a validator which uses the latest JSON schema draft final JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); final JsonValidator validator = factory.getValidator(); // Validate the JSON object final ProcessingReport report = validator.validate(schema, instance); // optional error output final Iterator<ProcessingMessage> iterator = report.iterator(); while( iterator.hasNext() ) { final ProcessingMessage message = iterator.next(); System.out.println(message.getMessage()); // more verbose information are available via message.getJson() } return report.isSuccess(); }
report = schema.validate(data); if (report != null && !report.isSuccess()) { Iterator<ProcessingMessage> iter = report.iterator(); StringBuilder s = new StringBuilder(); while (iter.hasNext()) {
report = schema.validate(data); if (report != null && !report.isSuccess()) { Iterator<ProcessingMessage> iter = report.iterator(); StringBuilder s = new StringBuilder(); while (iter.hasNext()) {
Iterator<ProcessingMessage> iterator = report.iterator(); List<String> errors = Lists.newArrayList(); while (iterator.hasNext())
Iterator<ProcessingMessage> iterator = report.iterator(); List<String> errors = Lists.newArrayList(); while (iterator.hasNext())
@Test public void uncheckedReportCorrectlyReportsException() throws ProcessingException { final ProcessingException exception = new ProcessingException(MSG); when(processor.process(anyReport(), any(In.class))) .thenThrow(exception); when(report.iterator()) .thenReturn(Iterators.<ProcessingMessage>emptyIterator()); final ProcessingResult<Out> result = ProcessingResult.uncheckedResult(processor, report, input); assertFalse(result.isSuccess()); final ProcessingReport r = result.getReport(); final List<ProcessingMessage> list = Lists.newArrayList(r); assertFalse(list.isEmpty()); final ProcessingMessage message = list.get(0); assertMessage(message).hasMessage(MSG).hasLevel(LogLevel.FATAL); }
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()); } }
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()); } }
@Test public void addSchemaVersionInPublicModelExtensionTest() throws ProcessingException { String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json"; JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema); ExtensionConverter converter = new DefaultExtensionConverter(); ObjectNode tree = Json.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()); }
Iterator<ProcessingMessage> itrErrorMessages = report.iterator();
@Test public void upgradePublicModelExtensionTest() throws ProcessingException { 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()); }
@Test public void validateStepExtensionTest() throws ProcessingException { 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") .uses(OptionalInt.empty()) .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); }