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
- Send a GET for
/schema/productand store the body as a String withasString(). - Send a GET for
/products/1, callthen(), and assert withbody()andmatchesJsonSchema()given the product schema text. - 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,priceandspecsand the missingage,cityandemail.Assuming nested objects are closed
Expecting an unknown key inside
specsto fail will surprise you here. AddadditionalPropertiesset to false inside each nested object when the contract really is closed.Validating the whole catalogue
/productsreturns 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.
Related lessons
- User Schema ValidationIntroduces
matchesJsonSchema()on the simpler, flat user schema. - Nested POJO DeserializationDeserialises the same nested product, category and specs, into Java classes.
- Validate Nested JSONChecks nested product fields one at a time with dot paths.
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";
// TODO: Fetch the product schema from /schema/product
// TODO: Validate /products/1 against it
// TODO: Print confirmation
}
}