Skip to content
CodeKruPro

Validate a response against a JSON schema

Automation taskMediumJava only

Interviewers ask: "Schema validation or field-by-field assertions?" Practise matchesJsonSchemaInClasspath here (it reads the schema file from your project), then explain why good suites use both: the schema checks shape, the assertions check values.

User Schema Validation

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