org.mockserver.matchers.JsonSchemaMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockserver-core Show documentation
Show all versions of mockserver-core Show documentation
Functionality used by all MockServer modules for matching and expectations
package org.mockserver.matchers;
import org.mockserver.validator.jsonschema.JsonSchemaValidator;
/**
* See http://json-schema.org/
*
* @author jamesdbloom
*/
public class JsonSchemaMatcher extends BodyMatcher {
private final String schema;
private JsonSchemaValidator jsonSchemaValidator;
public JsonSchemaMatcher(String schema) {
this.schema = schema;
jsonSchemaValidator = new JsonSchemaValidator(schema);
}
protected String[] fieldsExcludedFromEqualsAndHashCode() {
return new String[]{"logger", "jsonSchemaValidator"};
}
public boolean matches(String matched) {
boolean result = false;
try {
String validation = jsonSchemaValidator.isValid(matched);
result = validation.isEmpty();
if (!result) {
logger.trace("Failed to perform JSON match \"{}\" with schema \"{}\" because {}", matched, this.schema, validation);
}
} catch (Exception e) {
logger.trace("Failed to perform JSON match \"{}\" with schema \"{}\" because {}", matched, this.schema, e.getMessage());
}
return reverseResultIfNot(result);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy