public class JsonSchemaValidator extends org.hamcrest.TypeSafeMatcher<String>
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();
Modifier and Type | Field and Description |
---|---|
static JsonSchemaValidatorSettings |
settings
Default json schema factory instance
|
Modifier and Type | Method and Description |
---|---|
void |
describeTo(org.hamcrest.Description description) |
static JsonSchemaValidator |
matchesJsonSchema(File file)
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.
|
static JsonSchemaValidator |
matchesJsonSchema(InputStream schema)
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.
|
static JsonSchemaValidator |
matchesJsonSchema(Reader schema)
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.
|
static JsonSchemaValidator |
matchesJsonSchema(String schema)
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.
|
static JsonSchemaValidator |
matchesJsonSchema(URI uri)
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema loaded by the supplied URI.
|
static JsonSchemaValidator |
matchesJsonSchema(URL url) |
static JsonSchemaValidator |
matchesJsonSchemaInClasspath(String pathToSchemaInClasspath)
Creates a Hamcrest matcher that validates that a JSON document conforms to the JSON schema provided to this method.
|
protected boolean |
matchesSafely(String content) |
static void |
reset()
Reset the static
settings to null . |
org.hamcrest.Matcher<?> |
using(com.github.fge.jsonschema.main.JsonSchemaFactory jsonSchemaFactory)
Validate the JSON document using the supplied
jsonSchemaFactory instance. |
org.hamcrest.Matcher<?> |
using(JsonSchemaValidatorSettings jsonSchemaValidatorSettings)
Validate the JSON document using the supplied
jsonSchemaValidatorSettings instance. |
describeMismatch, describeMismatchSafely, matches
public static JsonSchemaValidatorSettings settings
public static JsonSchemaValidator matchesJsonSchema(String schema)
schema
- The string defining the JSON schemapublic static JsonSchemaValidator matchesJsonSchemaInClasspath(String pathToSchemaInClasspath)
pathToSchemaInClasspath
- The string that points to a JSON schema in classpath.public static JsonSchemaValidator matchesJsonSchema(InputStream schema)
schema
- The input stream that points to a JSON schemapublic static JsonSchemaValidator matchesJsonSchema(Reader schema)
schema
- The reader that points to a JSON schemapublic static JsonSchemaValidator matchesJsonSchema(File file)
file
- The file that points to a JSON schemapublic static JsonSchemaValidator matchesJsonSchema(URL url)
public static JsonSchemaValidator matchesJsonSchema(URI uri)
Note: Converts the URI to a URL and loads this URL.
uri
- The URI that points to a JSON schemapublic org.hamcrest.Matcher<?> using(com.github.fge.jsonschema.main.JsonSchemaFactory jsonSchemaFactory)
jsonSchemaFactory
instance.jsonSchemaFactory
- The json schema factory instance to use.public org.hamcrest.Matcher<?> using(JsonSchemaValidatorSettings jsonSchemaValidatorSettings)
jsonSchemaValidatorSettings
instance.jsonSchemaValidatorSettings
- The json schema validator settings instance to use.protected boolean matchesSafely(String content)
matchesSafely
in class org.hamcrest.TypeSafeMatcher<String>
public void describeTo(org.hamcrest.Description description)
public static void reset()
settings
to null
.Copyright © 2010–2017. All rights reserved.