I'm using an Atlas based mongodb instance and fastify for backend. I'm also using @sinclair/typebox to generate JSON Schemas for data validation. Normally, I use these schemas to validate input messages but I now want to reuse them as validators for MongoDB collections too.
I have something like this:
await database.command({
collMod: 'users',
validator: {
$jsonSchema: UserSchema,
},
});
And also, I have examples set for some of the fields. e.g.:
export const HandleSchema = Type.RegEx(/^[a-zA-Z0-9_-]{1,24}$/, {
examples: ['harry-potter', 'jane-doe-99'],
title: 'Handle',
// ...
});
export const UserSchema = Type.Object(
{
_id: HandleSchema,
// ...
},
{ additionalProperties: false },
);
However, I'm getting this error while trying to apply this validator:
Parsing of collection validator failed :: caused by :: Unknown $jsonSchema keyword: examples
Which means the examples keyword is not recognized by MongoDB; and unfortunately looks like this happens in server side. Looks like they dared to ignore the behavior suggested by the spec. I'm also seeing a flag that can be used to ignore unknown keywords, but unfortunately, setParameter doesn't work with atlas.
Now, at least I should be able to remove examples somehow from typebox or ajv. I tried Type.Strict(UserSchema) and ajv.customOptions.keywords set to ['examples'] (this probably is the inverse of what I want to achieve), but they didn't work.
How do I ignore examples field just for the mongodb schema? Or how do I fix this from MongoDB Atlas side (looks impossible)? Any other approach?
Thanks in advance.