Type safe data validation and sanitization for MongoDB update $set
operations
using validata.
See validata for more details on validation functionality.
npm i validata validata-mongo
Given interfaces
interface MyObject {
required: string;
optional?: string;
child: Child;
}
interface Child {
foo: number;
bar: number;
}
And validata
const myCheck = isObjectSet<MyObject>({
required: isString(),
optional: maybeString(),
child: isObjectSet({
foo: isNumber({ min: 1 }),
bar: asNumber({ coerceMax: 10 }),
}),
});
Then
const result = check(myCheck, () => ({
required: 'blue',
child: {
foo: 1,
bar: 2,
},
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","child":{"foo":1,"bar":2}}
const result = check(myCheck, () => ({
required: 'blue',
optional: 'red',
child: {
foo: 20,
bar: '20',
},
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","optional":"red","child":{"foo":20,"bar":10}}
// NOTE child.bar has been converted to a number and coerced to a max of 10
const result = check(myCheck, () => ({
required: 'blue',
'child.foo': 3,
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","child.foo":3}
const result = check(myCheck, () => ({
required: 'blue',
'child.bar': '73',
}));
console.log(JSON.stringify(result));
// -> SUCCESS -> {"required":"blue","child.bar":10}
// NOTE child.bar has been converted to a number and coerced to a max of 10
check(myCheck, () => ({
wow: 'blue',
}));
// -> ERROR -> {"issues":[{"path":["wow"],"value":"blue","reason":"unexpected-property"}],"name":"ValidationError"}
check(myCheck, () => ({
required: 'blue',
'child.unexpected': 17,
}));
// -> ERROR -> {"issues":[{"path":["child.unexpected"],"value":17,"reason":"unexpected-property"}],"name":"ValidationError"}