Add the following schema types:
This should be used as a type to validate object Id's
We want to have a field with an objectId
var schema = yup
.objectId()
This isn't a great fit for the core library since it's server specific and requires bson. Luckily tho it is easy to add yourself for your application.
should be something like this:
const { ObjectId } = require('bson');
class ObjectIdSchema extends yup.mixed {
constructor() {
super({ type: 'objectId' });
this.withMutation(schema => {
schema.transform(function(value) {
if (this.isType(value)) return value;
return new ObjectId(value);
});
});
}
_typeCheck(value) {
return ObjectId.isValid(value);
}
}
yup.objectId = () => new ObjectIdSchema();
Most helpful comment
This isn't a great fit for the core library since it's server specific and requires
bson. Luckily tho it is easy to add yourself for your application.should be something like this: