About this lesson
REST Assured JSON schema validation: check a user with matchesJsonSchema()
Learn REST AssuredAdvancedJSON Schema Validation
How it works
Field-by-field assertions check the values you thought of. A JSON Schema checks the shape of the whole document: which fields must exist, their types, ranges and whether anything extra is allowed. REST Assured supports it through the json-schema-validator module. The static matchesJsonSchema() method from io.restassured.module.jsv.JsonSchemaValidator turns schema text into a Hamcrest matcher, and that matcher goes inside then().body() like any other.
The testkru user schema is draft-07. It describes an object, requires id, name, email, age and city, types id and age as integers with a minimum of zero for age, marks email with the email format, and sets additionalProperties to false. So a user record passes only if it has those five fields and nothing more, which is exactly what /users/1 returns.
Failures are detailed. Checking the product body against the user schema produced one AssertionError that listed both broken rules: properties not allowed by the schema, naming the three extra keys, and missing required properties, naming the three absent ones. Each entry carries a JSON pointer and the keyword that failed. Pointing the same schema at the full /users list fails differently, because the instance is an array where the schema expects an object.
About the assignment
Download the user schema, validate the body of user 1 against it, and print a confirmation once the check passes.
What you will do
- Send a GET for
/schema/userand keep the body as text withasString(). - Send a GET for
/users/1and, afterthen(), callbody()withmatchesJsonSchema()given that schema string. - Print the confirmation sentence from the expected output only after that assertion line, so it is reached only when the body matches.
Common mistakes
Passing the schema URL instead of its text
The String overload expects the schema document itself. Given a URL string, it tried to parse it as JSON and threw a
JsonSchemaValidationExceptioncaused by a Jackson parse error on the token https.Validating the list endpoint
The schema describes one user. Running it against
/usersfails at the root with a type error, because the response is an array of users.Asserting without then()
A matcher checks nothing until something applies it. Building it without passing it to
body()afterthen()leaves the response unchecked, and the confirmation prints regardless.
In a real test suite
Teams usually keep schemas in src/test/resources and load them with matchesJsonSchemaInClasspath(), so the contract is versioned with the tests. A schema check per endpoint catches renamed fields, type changes and surprise extra properties in one assertion, while a few value checks cover the business rules.
Related lessons
- Product Schema ValidationValidates a product against a schema with nested object rules.
- Deserialize Response to POJOMaps user 1 onto a POJO, where Jackson rejects unknown keys, another strict structural check.
- Validate Multiple FieldsChains several
body()checks on one response, the field-by-field approach a schema generalises.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// Step 1: Fetch the user JSON schema
// TODO: GET /schema/user and get the response as a String
// Step 2: Validate user 1 against the schema
// TODO: GET /users/1 and use .body(matchesJsonSchema(schema))
// TODO: Print confirmation
}
}