Search in sources :

Example 1 with ValidationException

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();
    }
}
Also used : ValidationException(org.everit.json.schema.ValidationException) JSONObject(org.json.JSONObject) Schema(org.everit.json.schema.Schema) ModelId(org.eclipse.vorto.repository.api.ModelId)

Example 2 with ValidationException

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);
    }
}
Also used : ValidationException(org.everit.json.schema.ValidationException) JSONObject(org.json.JSONObject) Schema(org.everit.json.schema.Schema)

Example 3 with ValidationException

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);
    }
}
Also used : ValidationException(org.everit.json.schema.ValidationException) JSONObject(org.json.JSONObject)

Example 4 with ValidationException

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);
    }
}
Also used : SubscriptionDescriptorException(uk.gov.justice.subscription.SubscriptionDescriptorException) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) ValidationException(org.everit.json.schema.ValidationException) SubscriptionDescriptorDef(uk.gov.justice.subscription.domain.SubscriptionDescriptorDef) NoSuchFileException(java.nio.file.NoSuchFileException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with ValidationException

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");
    }
}
Also used : ValidationException(org.everit.json.schema.ValidationException) SubscriptionDescriptorFileValidator(uk.gov.justice.subscription.file.read.SubscriptionDescriptorFileValidator) YamlFileToJsonObjectConverter(uk.gov.justice.subscription.file.read.YamlFileToJsonObjectConverter) Test(org.junit.Test)

Aggregations

ValidationException (org.everit.json.schema.ValidationException)15 Schema (org.everit.json.schema.Schema)8 JSONObject (org.json.JSONObject)8 Test (org.junit.Test)6 IOException (java.io.IOException)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 ArrayList (java.util.ArrayList)2 JSONObject (org.json.simple.JSONObject)2 MediaType (uk.gov.justice.services.core.mapping.MediaType)2 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)2 SubscriptionDescriptorFileValidator (uk.gov.justice.subscription.file.read.SubscriptionDescriptorFileValidator)2 YamlFileToJsonObjectConverter (uk.gov.justice.subscription.file.read.YamlFileToJsonObjectConverter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)1 Gson (com.google.gson.Gson)1 Config (com.typesafe.config.Config)1 AaiDocument (io.apicurio.datamodels.asyncapi.models.AaiDocument)1 InputStream (java.io.InputStream)1