Search in sources :

Example 1 with ObjectSchema

use of org.everit.json.schema.ObjectSchema in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaPropertyMatcher method getObjectSchemaFromFile.

private static ObjectSchema getObjectSchemaFromFile(final String pathToJsonSchema) {
    final String jsonSchema = getJsonContentFrom(pathToJsonSchema);
    final Schema rawSchema = SchemaLoader.load(new JSONObject(new JSONTokener(jsonSchema)));
    return Optional.of(rawSchema).filter(ObjectSchema.class::isInstance).map(ObjectSchema.class::cast).orElseThrow(() -> new IllegalArgumentException(format("Schema found in file %s is invalid.", pathToJsonSchema)));
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) ObjectSchema(org.everit.json.schema.ObjectSchema) ArraySchema(org.everit.json.schema.ArraySchema) ReferenceSchema(org.everit.json.schema.ReferenceSchema) ObjectSchema(org.everit.json.schema.ObjectSchema) Schema(org.everit.json.schema.Schema)

Example 2 with ObjectSchema

use of org.everit.json.schema.ObjectSchema in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaPropertyMatcher method hasRequiredProperty.

/**
 * Matcher to validate if a given property is a required (non-optional) property in the given json schema
 *
 * @param jsonpathToRequiredProperty json path of the property that should be a required property in the schema (i.e. case.offences.offenceId)
 * @return matcher
 */
public static Matcher<String> hasRequiredProperty(final String jsonpathToRequiredProperty) {
    return new TypeSafeMatcher<String>() {

        private String pathToJsonFile = null;

        @Override
        protected boolean matchesSafely(final String pathToJsonFile) {
            this.pathToJsonFile = pathToJsonFile;
            final JsonPath jsonPath = JsonPath.of(jsonpathToRequiredProperty);
            final ObjectSchema parentObjectSchema = getObjectSchemaFromFile(pathToJsonFile);
            return getObjectSchemaWithPath(parentObjectSchema, jsonPath).getRequiredProperties().contains(jsonPath.getProperty());
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("property ").appendValue(jsonpathToRequiredProperty).appendText(" should be a required property in JSON Schema ").appendValue(pathToJsonFile);
        }

        @Override
        protected void describeMismatchSafely(final String pathToJsonFile, final Description mismatchDescription) {
            mismatchDescription.appendText("property ").appendValue(jsonpathToRequiredProperty).appendText(" is not a required property");
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ObjectSchema(org.everit.json.schema.ObjectSchema)

Example 3 with ObjectSchema

use of org.everit.json.schema.ObjectSchema in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaPropertyMatcher method hasProperty.

/**
 * Matcher to validate if a given property exists in the given json schema
 *
 * @param jsonpathToRequiredProperty json path of the property that should exist in the schema (i.e. case.offences.offenceId)
 * @return matcher
 */
public static Matcher<String> hasProperty(final String jsonpathToRequiredProperty) {
    return new TypeSafeMatcher<String>() {

        private String pathToJsonFile = null;

        @Override
        protected boolean matchesSafely(final String pathToJsonFile) {
            this.pathToJsonFile = pathToJsonFile;
            final JsonPath jsonPath = JsonPath.of(jsonpathToRequiredProperty);
            final ObjectSchema parentObjectSchema = getObjectSchemaFromFile(pathToJsonFile);
            return getObjectSchemaWithPath(parentObjectSchema, jsonPath).getPropertySchemas().containsKey(jsonPath.getProperty());
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("property ").appendValue(jsonpathToRequiredProperty).appendText(" should exist in JSON Schema ").appendValue(pathToJsonFile);
        }

        @Override
        protected void describeMismatchSafely(final String pathToJsonFile, final Description mismatchDescription) {
            mismatchDescription.appendText("property ").appendValue(jsonpathToRequiredProperty).appendText(" doesn't exist in schema");
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ObjectSchema(org.everit.json.schema.ObjectSchema)

Example 4 with ObjectSchema

use of org.everit.json.schema.ObjectSchema in project nakadi by zalando.

the class SchemaDiff method recursiveCheck.

static void recursiveCheck(final Schema originalIn, final Schema updateIn, final SchemaDiffState state) {
    if (originalIn == null && updateIn == null) {
        return;
    }
    if (updateIn == null) {
        state.addChange(SCHEMA_REMOVED);
        return;
    }
    final Schema original;
    final Schema update;
    if (!originalIn.getClass().equals(updateIn.getClass())) {
        // Tricky part. EmptySchema is the same as an empty ObjectSchema.
        if (originalIn instanceof EmptySchema && updateIn instanceof ObjectSchema) {
            original = replaceWithEmptyObjectSchema(originalIn);
            update = updateIn;
        } else {
            state.addChange(TYPE_CHANGED);
            return;
        }
    } else {
        original = originalIn;
        update = updateIn;
    }
    state.analyzeSchema(originalIn, () -> {
        if (!Objects.equals(original.getId(), update.getId())) {
            state.addChange(ID_CHANGED);
        }
        if (!Objects.equals(original.getTitle(), update.getTitle())) {
            state.addChange(TITLE_CHANGED);
        }
        if (!Objects.equals(original.getDescription(), update.getDescription())) {
            state.addChange(DESCRIPTION_CHANGED);
        }
        if (original instanceof StringSchema) {
            StringSchemaDiff.recursiveCheck((StringSchema) original, (StringSchema) update, state);
        } else if (original instanceof NumberSchema) {
            NumberSchemaDiff.recursiveCheck((NumberSchema) original, (NumberSchema) update, state);
        } else if (original instanceof EnumSchema) {
            EnumSchemaDiff.recursiveCheck((EnumSchema) original, (EnumSchema) update, state);
        } else if (original instanceof CombinedSchema) {
            CombinedSchemaDiff.recursiveCheck((CombinedSchema) original, (CombinedSchema) update, state);
        } else if (original instanceof ObjectSchema) {
            ObjectSchemaDiff.recursiveCheck((ObjectSchema) original, (ObjectSchema) update, state);
        } else if (original instanceof ArraySchema) {
            ArraySchemaDiff.recursiveCheck((ArraySchema) original, (ArraySchema) update, state);
        } else if (original instanceof ReferenceSchema) {
            ReferenceSchemaDiff.recursiveCheck((ReferenceSchema) original, (ReferenceSchema) update, state);
        }
    });
}
Also used : ReferenceSchema(org.everit.json.schema.ReferenceSchema) ArraySchema(org.everit.json.schema.ArraySchema) EmptySchema(org.everit.json.schema.EmptySchema) ObjectSchema(org.everit.json.schema.ObjectSchema) EmptySchema(org.everit.json.schema.EmptySchema) NumberSchema(org.everit.json.schema.NumberSchema) CombinedSchema(org.everit.json.schema.CombinedSchema) ArraySchema(org.everit.json.schema.ArraySchema) ReferenceSchema(org.everit.json.schema.ReferenceSchema) EnumSchema(org.everit.json.schema.EnumSchema) ObjectSchema(org.everit.json.schema.ObjectSchema) StringSchema(org.everit.json.schema.StringSchema) Schema(org.everit.json.schema.Schema) EnumSchema(org.everit.json.schema.EnumSchema) CombinedSchema(org.everit.json.schema.CombinedSchema) StringSchema(org.everit.json.schema.StringSchema) NumberSchema(org.everit.json.schema.NumberSchema)

Aggregations

ObjectSchema (org.everit.json.schema.ObjectSchema)4 ArraySchema (org.everit.json.schema.ArraySchema)2 ReferenceSchema (org.everit.json.schema.ReferenceSchema)2 Schema (org.everit.json.schema.Schema)2 Description (org.hamcrest.Description)2 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)2 CombinedSchema (org.everit.json.schema.CombinedSchema)1 EmptySchema (org.everit.json.schema.EmptySchema)1 EnumSchema (org.everit.json.schema.EnumSchema)1 NumberSchema (org.everit.json.schema.NumberSchema)1 StringSchema (org.everit.json.schema.StringSchema)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1