validate icon indicating copy to clipboard operation
validate copied to clipboard

object required bug

Open ozt88 opened this issue 5 years ago • 2 comments
trafficstars

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

ozt88 avatar Feb 21 '20 02:02 ozt88

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 avatar Jul 15 '20 19:07 madslundt

@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.)

rofrankel avatar Aug 25 '22 06:08 rofrankel