Lightweight package for API testing to validate API responses against ZOD schemas. It can be used directly in any project.
npm install core-zod-schema-validatorvalidateSchemaZod() to report Zod validation errors in the response obtained from any network request.
sh
npm install -D core-zod-schema-validator
`
Compatibility
- TypeScript 4.5+
- You must enable strict mode in your tsconfig.json. This is a best practice for all TypeScript projects.
API Reference
$3
Function that validates the given JSON data against the provided Zod schema.
#### Parameters
- data (required)
- Type: any
- Description: The data to be validated.
- schema (required)
- Type: object
- Description: Zod schema to validate against.
See the Zod Schema documentation for more information.
- issuesStyles (optional)
- Type: object
- Description: An object with the icons used to flag the schema issues. If not provided, it will use the default icons defined in the plugin. Includes the following properties:
- iconPropertyError: The icon used to flag type errors in the data.
- iconPropertyMissing: The icon used to flag missing properties in the data.
#### Returns
An object containing the following properties:
- errors
- Type: array
- Description: An array of validation errors as provided by Zof, or null if the data is valid against the schema.
- dataMismatches
- Type: object
- Description: The original response data with all schema mismatches directly flagged.
- issuesStyles
- Type: object
- Description: Object issuesStyles used to flag schema issues in dataMismatches results.
#### Throws
- Error: If any of the required parameters are missing.
Example providing a ZOD schema:
`js
//...
const data = response.body
const { errors, dataMismatches } = validateSchemaZod(data, schema);
expect(errors).to.be.null; // Assertion to ensure no validation errors
//...
`
Usage Examples
$3
`js
///
import { z } from "zod";
import { validateSchemaZod } from 'core-zod-schema-validator'
// Definition for the "Category" object
const categorySchema = z.object({
id: z.number().int(),
name: z.number().optional(), // Optional as per schema
color: z.string()
});
// Definition for the "Tag" object
const tagSchema = z.object({
id: z.number().int(),
name: z.string(),
type: z.string()
});
// Definition for the "Pet" object
const petSchema = z.object({
id: z.string().optional(), // Optional as per schema
age: z.number().int(),
category: categorySchema.optional(), // Optional as per schema
name: z.number(), // Defined as integer in the schema
photoUrls: z.array(z.string()).min(2), // Minimum of 2 items
tags: z.array(tagSchema).min(1).optional(), // Optional as per schema, min 1 item.
status: z.enum(["available", "sold"]) // Specified as enum
});
const schema = z.array(petSchema);
describe('API Schema Validation Function', () => {
const customIssuesStyles = { iconPropertyError: '⛔', iconPropertyMissing: '❓' }
it('Test Zod Schema - Sample 2', () => {
const findByStatusReq = {
url: 'https://petstore.swagger.io/v2/pet/findByStatus?status=pending',
headers: { 'Content-Type': 'application/json' }
}
cy.request(findByStatusReq).then((response) => {
const result = validateSchemaZod(response.body, schema, customIssuesStyles)
expect(result.errors).to.be.null
});
})
});
``