use of org.everit.json.schema.ValidationException in project vorto by eclipse.
the class DataValidation method main.
public static void main(String[] args) {
ModelId presenceCapability = ModelId.fromPrettyFormat("com.ipso.smartobjects.Presence:0.0.1");
JSONObject jsonSchema = getSchemaForCapability(presenceCapability);
JSONObject jsonInput = getJsonInput();
Schema schema = SchemaLoader.load(jsonSchema);
try {
schema.validate(jsonInput);
System.out.println("Congrats. The JSON is valid");
} catch (ValidationException validationException) {
System.err.println("JSON is not valid according to schema");
validationException.printStackTrace();
}
}
use of org.everit.json.schema.ValidationException in project microservice_framework by CJSCommonPlatform.
the class SchemaCatalogAwareJsonSchemaValidator method doValidate.
private void doValidate(final String envelopeJson, final String actionName, final MediaType mediaType) {
final Optional<Schema> schema = schemaIdMappingCache.schemaIdFor(mediaType).flatMap(schemaCatalogService::findSchema);
if (schema.isPresent()) {
logger.info(format("Performing schema validation with catalog schema for action '%s' and mediaType '%s", actionName, mediaType));
final JSONObject payload = payloadExtractor.extractPayloadFrom(envelopeJson);
try {
schema.get().validate(payload);
} catch (final ValidationException ex) {
throw new JsonSchemaValidationException(ex.getMessage(), ex);
}
} else {
fileBasedJsonSchemaValidator.validateWithoutSchemaCatalog(envelopeJson, actionName);
}
}
use of org.everit.json.schema.ValidationException in project microservice_framework by CJSCommonPlatform.
the class FileBasedJsonSchemaValidator method validateWithoutSchemaCatalog.
/**
* Validate a JSON payload against the correct schema for the given message type name. If the
* JSON contains metadata, this is removed first. Schemas are cached for reuse.
*
* @param envelopeJson the payload to validate
* @param actionName the message type name
*/
public void validateWithoutSchemaCatalog(final String envelopeJson, final String actionName) {
logger.info("Falling back to file based schema lookup, no catalog schema found for: {}", actionName);
final JSONObject payload = payloadExtractor.extractPayloadFrom(envelopeJson);
try {
schemaOf(actionName).validate(payload);
} catch (final ValidationException ex) {
throw new JsonSchemaValidationException(ex.getMessage(), ex);
}
}
use of org.everit.json.schema.ValidationException in project microservice_framework by CJSCommonPlatform.
the class SubscriptionDescriptorParser method read.
public SubscriptionDescriptor read(final Path absolutePath) {
try {
subscriptionDescriptorFileValidator.validate(absolutePath);
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()).registerModule(new Jdk8Module()).registerModule(new ParameterNamesModule(PROPERTIES));
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
return mapper.readValue(absolutePath.toFile(), SubscriptionDescriptorDef.class).getSubscriptionDescriptor();
} catch (final NoSuchFileException e) {
throw new SubscriptionDescriptorException(format("No such subscriptions YAML file %s ", absolutePath), e);
} catch (final ValidationException e) {
throw new SubscriptionDescriptorException(format("Failed to validate subscriptions yaml file %s ", absolutePath), e);
} catch (final IOException e) {
throw new SubscriptionDescriptorException(format("Failed to read subscriptions yaml file %s ", absolutePath), e);
}
}
use of org.everit.json.schema.ValidationException in project microservice_framework by CJSCommonPlatform.
the class SubscriptionDescriptorFileValidatorTest method shouldNotThrowExceptionOnCorrectSubscriptionYaml.
@Test
public void shouldNotThrowExceptionOnCorrectSubscriptionYaml() throws IOException {
try {
final SubscriptionDescriptorFileValidator subscriptionDescriptorFileValidator = new SubscriptionDescriptorFileValidator(new YamlFileToJsonObjectConverter());
subscriptionDescriptorFileValidator.validate(getFromClasspath("subscription.yaml"));
} catch (ValidationException e) {
fail("Unexpected validation exception");
}
}
Aggregations