The below returns success, even though obj contains an element that is not in the schema.
Question
Is it possible to have validate fail, when it sees elements that are not specified in the schema?
Jest have this expect(obj1).toEqual(obj2).
If Validate can't do it, what options do I then have to detect unwanted elements?
const Schema = require("validate");
const obj = {good: "1", bad: "2"};
const user = new Schema({
good: {
type: String,
required: true,
}
});
const r = user.validate(obj);
console.log(r);
CodePudding user response:
Yes, there is a strict option on the Schema, from the documentation:
Schema
A Schema defines the structure that objects should be validated against.
Parameters
objschema definitionoptsoptions
opts.typecast
opts.strip
opts.strictValidation fails when object contains properties not defined in the schema (optional, default false)
So you'll need something like:
const options = { strict: true };
const user = new Schema({
good: {
type: String,
required: true,
}
}, options);
