Skip to navigation

Zod-&-JSON Schema---Validation Examples

Zod & JSON Schema - Validation Examples.xlsx

Zod & JSON Schema

FeatureZODZOD sample resultJSON SchemaJSON Schema Sample Result
Validate File Size Limitconst fileSizeSchema = z.object({
maxSize: z.number().positive().max(1 * 1024 * 1024, { message: ‘File size must be less than 1MB’ }), // 1MB max size
});
{
“error”: “File size is too large”,
“issues”: [
{
“origin”: “number”,
“code”: “too_big”,
“maximum”: 1048576,
“inclusive”: true,
“path”: [
“maxSize”
],
“message”: “File size must be less than 1MB”
}
]
}
JSON Schema cannot validate file contents in multipart requests
Validate Guid Formatconst clientIdSchema = z.object({
clientId: z.uuid({ message: ‘clientId must be a valid UUID’ }),
});
{
“error”: “Invalid request body”,
“issues”: [
{
“origin”: “string”,
“code”: “invalid_format”,
“format”: “uuid”,
“pattern”: ”/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/”,
“path”: [
“clientId”
],
“message”: “clientId must be a valid UUID”
}
]
}
const clientIdSchema = {
type: ‘object’,
properties: {
clientId: {
type: ‘string’,
format: ‘uuid’,
description: ‘a sample description’,
},
},
};
{
“type”: “https://claconnect.com/platform/autoextract/problems/schema-validation”,
“title”: “Schema Validation Error”,
“status”: 400,
“detail”: “querystring/clientId must match format “uuid"",
“instance”: “/platform/autoextract”,
“validationErrors”: [
{
“instancePath”: “/clientId”,
“schemaPath”: ”#/properties/clientId/format”,
“keyword”: “format”,
“params”: {
“format”: “uuid”
},
“message”: “must match format “uuid""
}
]
}
Validate Request Payloadconst requestBodySchema = z.object({
clientId: z.uuid({ message: ‘clientId must be a valid UUID’ }),
});
{
“error”: “Invalid request body”,
“issues”: [
{
“expected”: “string”,
“code”: “invalid_type”,
“path”: [
“clientId”
],
“message”: “clientId must be a valid UUID”
}
]
}
const requestBodySchemaJSON = {
type: ‘object’, // Must be an object
properties: {
clientId: {
type: ‘string’, // UUID is a string
format: ‘uuid’, // JSON Schema supports ‘uuid’ format
description: ‘clientId must be a valid UUID’,
},
},
required: [‘clientId’], // clientId is required
};
{
“error”: “Invalid request body”,
“issues”: [
{
“origin”: “string”,
“code”: “invalid_format”,
“format”: “uuid”,
“pattern”: ”/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/”,
“path”: [
“clientId”
],
“message”: “clientId must be a valid UUID”
},
{
“expected”: “string”,
“code”: “invalid_type”,
“path”: [
“file”
],
“message”: “Invalid input: expected string, received undefined”
}
]
}
Validate Response Payloadconst zodResponseSchema = z.object({
message: z.string(),
clientId: z.uuid(),
filename: z.string(),
size: z.number(),
});
{
“error”: “Response validation failed”,
“issues”: [
{
“expected”: “string”,
“code”: “invalid_type”,
“path”: [
“filename”
],
“message”: “Invalid input: expected string, received undefined”
}
]
}
const responseBodySchemaJSON = {
type: ‘object’, // Must be an object
properties: {
message: { type: ‘string’ }, // message is a string
clientId: { type: ‘string’, format: ‘uuid’ }, // UUID string
filename: { type: ‘string’ }, // filename is a string
size: { type: ‘number’ }, // size is a number
},
required: [‘message’, ‘clientId’, ‘filename’, ‘size’], // all fields required
additionalProperties: false, // no extra fields allowed
};
{
“type”: “https://claconnect.com/platform/autoextract/problems/internal-server-error”,
“title”: “Internal Server Error”,
“status”: 500,
“detail”: ""filename” is required!”,
“instance”: “/platform/autoextract”
}