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

public class JsonSchemaValidator extends org.hamcrest.TypeSafeMatcher<String>
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 Details

  • Method Details

    • matchesJsonSchema

      public static JsonSchemaValidator matchesJsonSchema(String schema)
      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

      public static JsonSchemaValidator matchesJsonSchemaInClasspath(String pathToSchemaInClasspath)
      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

      public static JsonSchemaValidator matchesJsonSchema(InputStream schema)
      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

      public static JsonSchemaValidator matchesJsonSchema(Reader schema)
      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

      public static JsonSchemaValidator matchesJsonSchema(File file)
      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

      public static JsonSchemaValidator matchesJsonSchema(URL url)
    • matchesJsonSchema

      public 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.

      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 supplied jsonSchemaFactory instance.
      Parameters:
      jsonSchemaFactory - The json schema factory instance to use.
      Returns:
      A Hamcrest matcher
    • using

      public org.hamcrest.Matcher<?> using(JsonSchemaValidatorSettings jsonSchemaValidatorSettings)
      Validate the JSON document using the supplied jsonSchemaValidatorSettings instance.
      Parameters:
      jsonSchemaValidatorSettings - The json schema validator settings instance to use.
      Returns:
      A Hamcrest matcher
    • matchesSafely

      protected boolean matchesSafely(String content)
      Specified by:
      matchesSafely in class org.hamcrest.TypeSafeMatcher<String>
    • describeTo

      public void describeTo(org.hamcrest.Description description)
    • reset

      public static void reset()
      Reset the static settings to null.