Java Code Examples for org.everit.json.schema.Schema#validate()

The following examples show how to use org.everit.json.schema.Schema#validate() . 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: ConfigurationPropagationTest.java    From json-schema with Apache License 2.0 6 votes vote down vote up
@Test
public void configurationPropagationTest() throws URISyntaxException {
    SchemaLoader loader = SchemaLoader.builder()
            .schemaClient(SchemaClient.classPathAwareClient())
            .nullableSupport(true)
            .useDefaults(true)
            .registerSchemaByURI(new URI("urn:uuid:85946d9c-b896-496c-a7ac-6835f4b59f63"), LOADER.readObj("schema-by-urn.json"))
            .addFormatValidator(new CustomFormatValidatorTest.EvenCharNumValidator())
            .schemaJson(LOADER.readObj("schema.json"))
            .build();
    Schema actual = loader.load().build();
    JSONObject instance = LOADER.readObj("instance.json");
    try {
        actual.validate(instance);
        fail("did not throw validation exception");
    } catch (ValidationException e) {
        assertThat(e.toJSON(), sameJsonAs(LOADER.readObj("expected-exception.json")));
    }
    assertEquals(42, instance.get("propWithDefault"));
}
 
Example 2
Source File: CapabilitySchemaValidator.java    From AppiumTestDistribution with GNU General Public License v3.0 6 votes vote down vote up
public void validateCapabilitySchema(JSONObject capability) {
    try {
        isPlatformInEnv();
        InputStream inputStream = getClass().getResourceAsStream(getPlatform());
        JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
        Schema schema = SchemaLoader.load(rawSchema);
        schema.validate(new JSONObject(capability.toString()));
        validateRemoteHosts();
    } catch (ValidationException e) {
        if (e.getCausingExceptions().size() > 1) {
            e.getCausingExceptions().stream()
                .map(ValidationException::getMessage)
                .forEach(System.out::println);
        } else {
            LOGGER.info(e.getErrorMessage());
        }

        throw new ValidationException("Capability json provided is missing the above schema");
    }
}
 
Example 3
Source File: Validator.java    From aws-cloudformation-resource-schema with Apache License 2.0 5 votes vote down vote up
@Override
public void validateObject(final JSONObject modelObject, final JSONObject definitionSchemaObject) throws ValidationException {
    final SchemaLoaderBuilder loader = getSchemaLoader(definitionSchemaObject);

    try {
        final Schema schema = loader.build().load().build();
        schema.validate(modelObject); // throws a ValidationException if this object is invalid
    } catch (final org.everit.json.schema.ValidationException e) {
        throw ValidationException.newScrubbedException(e);
    }
}
 
Example 4
Source File: JsonSchemaTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean validateJson(String source, Schema schema) throws FileNotFoundException, IOException {
    JSONObject jo = new JSONObject(source); // jo = (JsonObject) new com.google.gson.JsonParser().parse(source);
    try {
      schema.validate(jo);
      return true;
    } catch (ValidationException e) {
      System.out.println(e.getMessage());
      return false;
    }
//        e.getCausingExceptions().stream()
////            .map(ValidationException::getMessage)
////            .forEach(System.out::println);
  }
 
Example 5
Source File: Validator.java    From daq with Apache License 2.0 5 votes vote down vote up
private void validateMessage(Schema schema, Object message) {
  final String stringMessage;
  try {
    stringMessage = OBJECT_MAPPER.writeValueAsString(message);
  } catch (Exception e) {
    throw new RuntimeException("While converting to string", e);
  }
  schema.validate(new JSONObject(new JSONTokener(stringMessage)));
}
 
Example 6
Source File: Validator.java    From daq with Apache License 2.0 5 votes vote down vote up
private void validateFile(File targetFile, Schema schema) {
  try (InputStream targetStream = new FileInputStream(targetFile)) {
    schema.validate(new JSONObject(new JSONTokener(targetStream)));
  } catch (Exception e) {
    throw new RuntimeException("Against input " + targetFile, e);
  }
}
 
Example 7
Source File: EventValidatorBuilder.java    From nakadi with MIT License 5 votes vote down vote up
private Optional<ValidationError> validateSchemaConformance(final Schema schema, final JSONObject evt) {
    try {
        schema.validate(evt);
        return Optional.empty();
    } catch (final ValidationException e) {
        final StringBuilder builder = new StringBuilder();
        recursiveCollectErrors(e, builder);
        return Optional.of(new ValidationError(builder.toString()));
    }
}
 
Example 8
Source File: JsonValidator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Validates that a JSON string conforms to a {@link Schema}.
 *
 * @param json the JSON string to check
 * @param schema the {@link Schema} that the JSON string should conform to
 * @throws JsonValidationException if the JSON doesn't conform to the schema, containing a {@link
 *     ConstraintViolation} for each message
 */
public void validate(String json, Schema schema) {
  try {
    schema.validate(new JSONObject(json));
  } catch (ValidationException validationException) {
    throw new JsonValidationException(
        validationException.getAllMessages().stream()
            .map(ConstraintViolation::new)
            .collect(toSet()));
  }
}
 
Example 9
Source File: JSONSchemaUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test(expected = ValidationException.class)
public void givenInvalidInput_whenValidating_thenInvalid() {

    JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json")));
    JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_invalid.json")));

    Schema schema = SchemaLoader.load(jsonSchema);
    schema.validate(jsonSubject);
}
 
Example 10
Source File: JSONSchemaUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenValidInput_whenValidating_thenValid() {

    JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json")));
    JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_valid.json")));

    Schema schema = SchemaLoader.load(jsonSchema);
    schema.validate(jsonSubject);
}
 
Example 11
Source File: JsonSchemaVerificationSteps.java    From pandaria with MIT License 4 votes vote down vote up
private void validateJsonSchema(Object actual, String schemaJson) throws IOException {
    SchemaLoader loader = SchemaLoader.builder().schemaJson(new JSONObject(schemaJson)).build();
    Schema schema = loader.load().build();
    schema.validate(toJSONAware(actual));
}