Generate MongoDB Validator schemas from your Zod schemas.
MongoDB validators use a version of JSONSchema based on draft 4 , it has some omitted properties and some extensions. Primarily the recommended use of bsonType instead of type. The purpose of this library is to convert zod schemas into MongoDB validators.
import { z } from "zod";
import { zodToMongoDBValidator } from "@thehogginator/zod-mongodb-validator";
const schema = z.object({
firstName: z.string().min(3).max(100),
lastName: z.string().min(3).max(100),
email: z.string().email().nullable()
}).describe('User');
assert.deepStrictEqual(
zodToMongoDBValidator(schema),
{
$jsonSchema: {
bsonType: 'object',
description: 'Users',
required: ['firstName', 'lastName', 'email'],
properties: {
firstName: {bsonType: 'string', minLength: 3, maxLength: 100},
lastName: {bsonType: 'string', minLength: 3, maxLength: 100},
email: {
oneOf: [
{bsonType: 'string', pattern: 'THE_EMAIL_REGEX'},
{bsonType: 'null'}
]
}
},
additionalProperties: false
}
}
)
You can pass the array of parsers to use as the 2nd argument to the zodToMongoDBValidator function, this allows you to omit parsers you don't want or add custom parsers for custom data types.
const zonedDateTime = z.custom<Temporal.ZonedDateTime>();
const schema = z.schema({
eventType: z.enum(['Created', 'Updated', 'Deleted']),
when: zonedDateTime
});
const generators = [...DEFAULT_GENERATORS, (type) => {
// Returning undefined indicates this generator doesn't handle the type
if(type !== zonedDateTime) { return undefined; }
return {
$jsonSchema: {
bsonType: 'string',
pattern: '^some-zoned-datetime-pattern$'
}
}
}]
The test run against an actual MongoDB instance to give more confidence around the output. To run the tests Spin up the project in the dev container to get an instance of MongoDB and then run the tests.