Package io.restassured.module.jsv
Class JsonSchemaValidator
java.lang.Object
org.hamcrest.BaseMatcher<T>
org.hamcrest.TypeSafeMatcher<String>
io.restassured.module.jsv.JsonSchemaValidator
- All Implemented Interfaces:
org.hamcrest.Matcher<String>,org.hamcrest.SelfDescribing
A Hamcrest matcher that can be used to validate that a JSON document matches a given JSON schema.
Typical use-case in REST Assured:
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));
The matchesJsonSchemaInClasspath(String) is defined in this class and validates that the response body of the request to "/products"
matches the products-schema.json schema located in classpath. It's also possible to supply some settings, for example the JsonSchemaFactory
that the matcher will use when validating the schema:
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV4).freeze()).freeze();
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(jsonSchemaFactory));
or:
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json").using(settings().with().checkedValidation(false)))
where "settings" is found in JsonSchemaValidatorSettings.settings().
It's also possible to specify static configuration that is reused for all matcher invocations. For example if you never wish to use checked validation you can configure
that JsonSchemaValidator like this:
JsonSchemaValidator.settings = settings().with().checkedValidation(false);This means that
get("/products").then().assertThat().body(matchesJsonSchemaInClasspath("products-schema.json"));
will use unchecked validation (since it was configured statically).
To reset the JsonSchemaValidator to its default state you can call reset():
JsonSchemaValidator.reset();
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic JsonSchemaValidatorSettingsDefault json schema factory instance -
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeTo(org.hamcrest.Description description) static JsonSchemaValidatormatchesJsonSchema(File file) Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.static JsonSchemaValidatormatchesJsonSchema(InputStream schema) Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.static JsonSchemaValidatormatchesJsonSchema(Reader schema) Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.static JsonSchemaValidatormatchesJsonSchema(String schema) Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.static JsonSchemaValidatormatchesJsonSchema(URI uri) Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema loaded by the supplied URI.static JsonSchemaValidatormatchesJsonSchema(URL url) static JsonSchemaValidatormatchesJsonSchemaInClasspath(String pathToSchemaInClasspath) Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.protected booleanmatchesSafely(String content) static voidreset()Reset the staticsettingstonull.org.hamcrest.Matcher<?>using(com.github.fge.jsonschema.main.JsonSchemaFactory jsonSchemaFactory) Validate the JSON document using the suppliedjsonSchemaFactoryinstance.org.hamcrest.Matcher<?>using(JsonSchemaValidatorSettings jsonSchemaValidatorSettings) Validate the JSON document using the suppliedjsonSchemaValidatorSettingsinstance.Methods inherited from class org.hamcrest.TypeSafeMatcher
describeMismatch, describeMismatchSafely, matchesMethods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, isNotNull, toString
-
Field Details
-
settings
Default json schema factory instance
-
-
Method Details
-
matchesJsonSchema
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.- Parameters:
schema- The string defining the JSON schema- Returns:
- A Hamcrest matcher
-
matchesJsonSchemaInClasspath
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.- Parameters:
pathToSchemaInClasspath- The string that points to a JSON schema in classpath.- Returns:
- A Hamcrest matcher
-
matchesJsonSchema
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.- Parameters:
schema- The input stream that points to a JSON schema- Returns:
- A Hamcrest matcher
-
matchesJsonSchema
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.- Parameters:
schema- The reader that points to a JSON schema- Returns:
- A Hamcrest matcher
-
matchesJsonSchema
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.- Parameters:
file- The file that points to a JSON schema- Returns:
- A Hamcrest matcher
-
matchesJsonSchema
-
matchesJsonSchema
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema loaded by the supplied URI.Note: Converts the URI to a URL and loads this URL.
- Parameters:
uri- The URI that points to a JSON schema- Returns:
- A Hamcrest matcher
-
using
public org.hamcrest.Matcher<?> using(com.github.fge.jsonschema.main.JsonSchemaFactory jsonSchemaFactory) Validate the JSON document using the suppliedjsonSchemaFactoryinstance.- Parameters:
jsonSchemaFactory- The json schema factory instance to use.- Returns:
- A Hamcrest matcher
-
using
Validate the JSON document using the suppliedjsonSchemaValidatorSettingsinstance.- Parameters:
jsonSchemaValidatorSettings- The json schema validator settings instance to use.- Returns:
- A Hamcrest matcher
-
matchesSafely
- Specified by:
matchesSafelyin classorg.hamcrest.TypeSafeMatcher<String>
-
describeTo
public void describeTo(org.hamcrest.Description description) -
reset
public static void reset()Reset the staticsettingstonull.
-