Skip to content
CodeKruPro

User Schema Validation

56/62

User Schema Validation

JSON Schema defines the structure a JSON response must follow. REST Assured can validate responses against schemas:

import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchema;

// 1. download the schema as a String
// 2. request the resource and pass the schema to matchesJsonSchema() inside .body()

The /schema/user endpoint provides the JSON Schema for a user, and /users/1 should conform to it. If a field is missing or has the wrong type, the assertion error lists every rule the response broke.

Task: Fetch the schema and validate user 1 against it.

Expected output:

User 1 matches the User JSON schema

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

  1. Send a GET for /schema/user and keep the body as text with asString().
  2. Send a GET for /users/1 and, after then(), call body() with matchesJsonSchema() given that schema string.
  3. 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 JsonSchemaValidationException caused by a Jackson parse error on the token https.

  • Validating the list endpoint

    The schema describes one user. Running it against /users fails 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() after then() 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.

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.

Main.java
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
    }
}