In order to validate requests and responses, you can define a schema file alongside a handler file. The schema file contains a (~json) schema against which the request received by the handler, and the response produced by it, are validated.
This can be useful to catch malformed requests produced by the frontend app calling the mock-server, and to ensure that the (usually randomly-generated) responses produced by the mock-server match the structure the frontend app actually expects.
The name of the file should match the format {method}.schema.json
.
Examples:
- handler file
mock-server/get.js
-> schema filemock-server/get.schema.json
- handler file
mock-server/users/post.js
-> schema filemock-server/users/post.schema.json
If the file is not present no validation occurs.
A schema file contains a json object with the following properties:
request
: object grouping the following propertiesquery
: json schema to validate the request queryparams
: json schema to validate the request paramsbody
: json schema to validate the json request body
response
: object grouping the following propertiesbody
: json schema to validate the json response body
Validation is not performed on the parts of the request/response for which there is no json schema defined.
Given the schema file mock-server/users/post.schema.json
:
{
"request": {
"body": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"response": {
"body": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
}
}
The following request would be ok:
POST /users
{ "name": "Alice" }
The following request would get a 400
error:
POST /users
{ "Name": "Alice" }
The response produced by the following handler file
(mock-server/users/post.js
) would go through:
module.exports = (req, res) => {
res.status(201).send({
name: req.body.name,
});
};
The response produced by the following handler file would be blocked, and a
500
error would be returned instead:
module.exports = (req, res) => {
res.status(201).send({
Name: req.body.name,
});
};
Given the schema file mock-server/users/get.schema.json
:
{
"request": {
"query": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"response": {
"body": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
}
}
}
The following request would be ok:
GET /users?name=Alice
The following request would get a 400
error:
POST /users?Name=Alice
The response produced by the following handler file (mock-server/users/get.js
)
would go through:
module.exports = (req, res) => {
res.status(200).send([
{
name: "Alice",
},
{
name: "Bob",
},
]);
};
The response produced by the following handler file would be blocked, and a
500
error would be returned instead:
module.exports = (req, res) => {
res.status(201).send([
{
Name: "Alice",
},
{
Name: "Bob",
},
]);
};