Skip to content
CodeKruPro

Product Schema Validation

57/62

Product Schema Validation

The product schema is more complex — it validates nested category and specs objects.

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

Expected output:

Product 1 matches the Product JSON schema

About this lesson

REST Assured nested JSON schema: validate objects inside a product

Learn REST AssuredAdvancedJSON Schema Validation

How it works

Product data is richer than a user record, and so is its schema. The testkru product schema requires five top-level fields, id, name, price, category and specs, and disallows any others. price must be a number no lower than zero. category is itself an object that requires an integer id and a string name, and specs is an object that requires weight and color as strings plus inStock as a boolean.

Nested rules report nested locations. In an offline check with the same schema, changing inStock to a string produced an error whose instance pointer was /specs/inStock and whose schema pointer was /properties/specs/properties/inStock, stating that a string was found where a boolean was allowed. A negative price and a missing stock flag in the same body produced two separate entries, one for the minimum and one for the missing property. The pointers tell you exactly which part of the payload to look at.

Note what the schema does not say. additionalProperties is false only at the top level. The nested category and specs objects leave it unset, so an extra key inside them still passes. A schema is only as strict as the rules written into it.

About the assignment

Fetch the product schema, validate product 1 against it, and print a confirmation when the body conforms.

What you will do

  1. Send a GET for /schema/product and store the body as a String with asString().
  2. Send a GET for /products/1, call then(), and assert with body() and matchesJsonSchema() given the product schema text.
  3. Print the confirmation line from the expected output after the assertion so it only appears when validation passes.

Common mistakes

  • Reusing the user schema

    Copying the previous lesson's path validates the product against the user contract. The error lists the unwanted keys category, price and specs and the missing age, city and email.

  • Assuming nested objects are closed

    Expecting an unknown key inside specs to fail will surprise you here. Add additionalProperties set to false inside each nested object when the contract really is closed.

  • Validating the whole catalogue

    /products returns an array. This schema describes a single object, so validating the list fails at the root before any nested rule is checked.

In a real test suite

Nested schemas pay off on endpoints that embed related resources, such as orders containing products or users. A single schema assertion in a JUnit 5 test guards the whole tree, and when a backend change breaks one deep field, the pointer in the failure message saves a manual diff of two large JSON bodies.

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";

        // TODO: Fetch the product schema from /schema/product
        // TODO: Validate /products/1 against it
        // TODO: Print confirmation
    }
}