@Test public void forUserSchema() throws Exception { String schema = "{\"$schema\":\"http: JsonSchemaValidator schemaValidator = JsonSchemaValidator.forUserSchema(schema); schemaValidator.validateJson(JsonHandler.createObjectNode() .put("_id", "MyId") .put("Title", "MyTitle") ); assertThatThrownBy(() -> schemaValidator.validateJson(JsonHandler.createObjectNode() .put("Title", "MyTitle") ) ).isInstanceOf(JsonSchemaValidationException.class); }
public static JsonSchemaValidator forUserSchema(String schemaJsonAsString) throws InvalidJsonSchemaException { try { JsonNode schemaAsJson = JsonHandler.getFromString(schemaJsonAsString); ProcessingReport pr = JSON_SCHEMA_FACTORY.getSyntaxValidator().validateSchema(schemaAsJson); if (!pr.isSuccess()) { throw new InvalidJsonSchemaException("External Schema is not valid"); } JsonSchema jsonSchema = JSON_SCHEMA_FACTORY.getJsonSchema(schemaAsJson); return new JsonSchemaValidator(jsonSchema); } catch (InvalidParseOperationException | ProcessingException e) { throw new InvalidJsonSchemaException("External Schema is not valid", e); } }
JsonSchemaValidator { public static JsonSchemaValidator forUserSchema(String schemaJsonAsString) throws InvalidJsonSchemaException { try { JsonNode schemaAsJson = JsonHandler.getFromString(schemaJsonAsString); ProcessingReport pr = JSON_SCHEMA_FACTORY.getSyntaxValidator().validateSchema(schemaAsJson); if (!pr.isSuccess()) { throw new InvalidJsonSchemaException("External Schema is not valid"); } JsonSchema jsonSchema = JSON_SCHEMA_FACTORY.getJsonSchema(schemaAsJson); return new JsonSchemaValidator(jsonSchema); } catch (InvalidParseOperationException | ProcessingException e) { throw new InvalidJsonSchemaException("External Schema is not valid", e); } } }
JsonSchemaValidator { public static JsonSchemaValidator forUserSchema(String schemaJsonAsString) throws InvalidJsonSchemaException { try { JsonNode schemaAsJson = JsonHandler.getFromString(schemaJsonAsString); ProcessingReport pr = JSON_SCHEMA_FACTORY.getSyntaxValidator().validateSchema(schemaAsJson); if (!pr.isSuccess()) { throw new InvalidJsonSchemaException("External Schema is not valid"); } JsonSchema jsonSchema = JSON_SCHEMA_FACTORY.getJsonSchema(schemaAsJson); return new JsonSchemaValidator(jsonSchema); } catch (InvalidParseOperationException | ProcessingException e) { throw new InvalidJsonSchemaException("External Schema is not valid", e); } } private  JsonSchemaValidator(JsonSchema jsonSchema); }
JsonSchemaValidator { public static JsonSchemaValidator forUserSchema(String schemaJsonAsString) throws InvalidJsonSchemaException { try { JsonNode schemaAsJson = JsonHandler.getFromString(schemaJsonAsString); ProcessingReport pr = JSON_SCHEMA_FACTORY.getSyntaxValidator().validateSchema(schemaAsJson); if (!pr.isSuccess()) { throw new InvalidJsonSchemaException("External Schema is not valid"); } JsonSchema jsonSchema = JSON_SCHEMA_FACTORY.getJsonSchema(schemaAsJson); return new JsonSchemaValidator(jsonSchema); } catch (InvalidParseOperationException | ProcessingException e) { throw new InvalidJsonSchemaException("External Schema is not valid", e); } } private  JsonSchemaValidator(JsonSchema jsonSchema); static JsonSchemaValidator forBuiltInSchema(String schemaFilename); static JsonSchemaValidator forUserSchema(String schemaJsonAsString); void validateJson(JsonNode jsonNode); }
JsonSchemaValidator { public static JsonSchemaValidator forUserSchema(String schemaJsonAsString) throws InvalidJsonSchemaException { try { JsonNode schemaAsJson = JsonHandler.getFromString(schemaJsonAsString); ProcessingReport pr = JSON_SCHEMA_FACTORY.getSyntaxValidator().validateSchema(schemaAsJson); if (!pr.isSuccess()) { throw new InvalidJsonSchemaException("External Schema is not valid"); } JsonSchema jsonSchema = JSON_SCHEMA_FACTORY.getJsonSchema(schemaAsJson); return new JsonSchemaValidator(jsonSchema); } catch (InvalidParseOperationException | ProcessingException e) { throw new InvalidJsonSchemaException("External Schema is not valid", e); } } private  JsonSchemaValidator(JsonSchema jsonSchema); static JsonSchemaValidator forBuiltInSchema(String schemaFilename); static JsonSchemaValidator forUserSchema(String schemaJsonAsString); void validateJson(JsonNode jsonNode);  }