validate
validate copied to clipboard
object required bug
const schema = new Schema({
number: {
type: Number,
required: true,
},
parent: {
type: Object,
required: false,
properties: {
child: {
type: Number,
required: true,
}
}
}
});
const err = schema.validate({
number: 1
});
console.log(err);
/// parent.child is required
should.not.exist(err);
not required parent which have a child required
I expect this parent could be undefined but not
I was having the same issue. I've forked the project and do not validate children when object is null or undefined with required = false
I've not created a PR because this repository seems pretty dead (PRs are not merged into master from February 2019). https://github.com/madslundt/simple-schema-validation
@madslundt I think I found a special case of this bug in simple-schema-validation, when a schema has an array of optional objects that have a required child. https://codesandbox.io/s/node-playground-forked-7ogeel?file=/src/index.js
const Schema = require("simple-schema-validation");
const schema = new Schema({
items: [
{
itemName: { type: "string" },
child: {
required: false,
properties: { name: { type: "string", required: true } }
}
}
]
});
console.log(schema.validate({ items: [{}] }));
Gives this error: Error: items.0.child.name is required..
(Sorry to comment here; it looks like issues are disabled on the simple-schema-validation repo.)