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)));
}
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");
}
};
}
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");
}
};
}
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);
}
});
}
Aggregations