Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object required bug #110

Open
ozt88 opened this issue Feb 21, 2020 · 2 comments
Open

object required bug #110

ozt88 opened this issue Feb 21, 2020 · 2 comments

Comments

@ozt88
Copy link

ozt88 commented Feb 21, 2020

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

@madslundt
Copy link

madslundt commented Jul 15, 2020

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

@rofrankel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants