@Override JsonNode createSchemaInstance(String input) throws IOException { return JsonLoader.fromString(input); } }.create(schema);
public ProcessingReport validate(String jsonText) throws IOException { ProcessingReport report; JsonNode toValidate = JsonLoader.fromString(jsonText); try { report = this.schema.validate(toValidate); } catch (ProcessingException e) { throw new IOException("Error validating json text : " + e.getMessage()); } return report; } }
@Override protected boolean matchesSafely(String content) { try { JsonNode contentAsJsonNode = JsonLoader.fromString(content); JsonSchemaFactory jsonSchemaFactory = instanceSettings.jsonSchemaFactory(); Schema loadedSchema = loadSchema(schema, instanceSettings); final JsonSchema jsonSchema; if (loadedSchema.hasType(JsonNode.class)) { jsonSchema = jsonSchemaFactory.getJsonSchema(JsonNode.class.cast(loadedSchema.schema)); } else if (loadedSchema.hasType(String.class)) { jsonSchema = jsonSchemaFactory.getJsonSchema(String.class.cast(loadedSchema.schema)); } else { throw new RuntimeException("Internal error when loading schema from factory. Type was " + loadedSchema.schema.getClass().getName()); } if (instanceSettings.shouldUseCheckedValidation()) { report = jsonSchema.validate(contentAsJsonNode); } else { report = jsonSchema.validateUnchecked(contentAsJsonNode); } return report.isSuccess(); } catch (Exception e) { throw new JsonSchemaValidationException(e); } }
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"); }
protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws IOException, ProcessingException { 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"); }
@Override JsonNode createSchemaInstance(String input) throws IOException { return JsonLoader.fromString(input); } }.create(schema);
/** * Get a Jackson JsonNode representation for Json object. * @param jsonText The Json object as a string * @return The Jackson JsonNode corresponding to json object string * @throws IOException if json string representation cannot be parsed */ public static JsonNode getJsonNode(String jsonText) throws IOException { return JsonLoader.fromString(jsonText); }
@Override public JsonNode getRawDatabaseConfiguration() { // write the value back to a string and reload ObjectMapper objectMapper = new ObjectMapper(); String rawJson = null; try { rawJson = objectMapper.writeValueAsString(databaseConfiguration); return JsonLoader.fromString(rawJson); } catch (Exception e) { throw new RuntimeException("Error roundtripping json configuration", e); } }
@Override public JsonNode getRawDatabaseConfiguration() { // write the value back to a string and reload ObjectMapper objectMapper = new ObjectMapper(); String rawJson = null; try { rawJson = objectMapper.writeValueAsString(databaseConfiguration); return JsonLoader.fromString(rawJson); } catch (Exception e) { throw new RuntimeException("Error roundtripping json configuration", e); } }
@Override public final ValidationResult validate(String json) throws IOException { JsonNode jsonNode = JsonLoader.fromString(json); return getValidationResult(jsonNode); }
private JsonSchema loadSchema(URL schemaUrl) { String schema; try { schema = IOUtils.toString(schemaUrl); } catch (IOException e) { throw new IllegalStateException("Unable to read from schema URL", e); } try { JsonNode schemaNode = JsonLoader.fromString(schema); return factory.getJsonSchema(schemaNode); } catch (IOException e) { throw new IllegalStateException("Unable to deserialize schema", e); } catch (ProcessingException e) { throw new IllegalStateException("Unable to build schema from JSON", e); } }
/** * validateJsonData * @param jsonSchema * @param jsonData * @return * @throws Exception */ 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); System.out.println(report); return report.toString().contains("success"); }
/** * validateJsonData * @param jsonSchema * @param jsonData * @return * @throws Exception */ 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); System.out.println(report); return report.toString().contains("success"); }
public ObjectNode getResourceObject(String id) { String src = getResource(id); if (src == null) { return null; } try { JsonNode node = JsonLoader.fromString(src); if (!node.isObject()) { throw new IllegalArgumentException("Bad json. Root not an object"); } return (ObjectNode) JsonSchemaResolver.resolveSchemaReference(node); } catch (Exception e) { throw new UnsupportedOperationException(resourceNameBase + "." + id, e); } } }
private boolean isValid(String dsl) throws ProcessingException, IOException { JsonNode jsonNode = JsonLoader.fromString(dsl); ProcessingReport report; report = schema.validate(jsonNode); if (!report.isSuccess()) { throw new RuntimeException("Invalid Dsl :\\n" + report.toString()); } return report.isSuccess(); }
@Override public void setup(OperatorContext context) { try { if (jsonSchema != null) { JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonNode schemaNode = JsonLoader.fromString(jsonSchema); schema = factory.getJsonSchema(schemaNode); } objMapper = new ObjectMapper(); objMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); } catch (ProcessingException | IOException e) { DTThrowable.wrapIfChecked(e); } }
@Override public void setup(OperatorContext context) { try { if (jsonSchema != null) { JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonNode schemaNode = JsonLoader.fromString(jsonSchema); schema = factory.getJsonSchema(schemaNode); } objMapper = new ObjectMapper(); objMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); } catch (ProcessingException | IOException e) { DTThrowable.wrapIfChecked(e); } }
private void checkMandatoryJson (Context ctx) throws BLException { for (Map.Entry<String,JsonSchema> entry : mandatoryJson.entrySet()) { String value = ctx.getString(entry.getKey()); ProcessingReport report; if (value != null) { try { JsonSchema schema = entry.getValue(); JsonNode node = JsonLoader.fromString(value); report = schema.validate(node); } catch(Exception ex) { throw new BLException(ex.getMessage()); } assertTrue(report.isSuccess(), report.toString()); } } }
private JsonSchema loadSchema(URL schemaUrl) { String schema; try { schema = IOUtils.toString(schemaUrl); } catch (IOException e) { throw new IllegalStateException("Unable to read from schema URL", e); } try { JsonNode schemaNode = JsonLoader.fromString(schema); return JsonSchemaFactory.newBuilder() .setReportProvider(new ListReportProvider(LogLevel.ERROR, LogLevel.FATAL)) .freeze().getJsonSchema(schemaNode); } catch (IOException e) { throw new IllegalStateException("Unable to deserialize schema", e); } catch (ProcessingException e) { throw new IllegalStateException("Unable to build schema from JSON", e); } }
private JsonNode loadValidatorNode(ClassLoader loader) { String schema = resourceFeatureMetaInfo.getResource((isSubOperation() ? (parent.getName() + "." + id) : id) + ".validator.jsonSchema"); if (schema != null) { try { return JsonSchemaResolver.resolveSchemaReference(JsonLoader.fromString(schema), loader); } catch (Exception e) { throw new RuntimeException("Invalid schema for operation signature [" + resourceFeatureMetaInfo.getReportableFeatureName() + "].[" + id + "]", e); } } return null; }