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

Validation of subdocuments #31

Closed
jyrkih opened this issue Nov 1, 2013 · 4 comments
Closed

Validation of subdocuments #31

jyrkih opened this issue Nov 1, 2013 · 4 comments

Comments

@jyrkih
Copy link

jyrkih commented Nov 1, 2013

Hi, I have an issue which I think is a bug but it may also be my misunderstanding.

Here's my case. With schema as this:

Pet = new SimpleSchema({
    name: {
        type: String
    }
});

Person = new SimpleSchema({
    name: {
        type: String
    }
});

Family = new SimpleSchema({
    pets: {
        type: [Pet]
    },
    kids: {
        type: [Person]
    },
    parents: {
        type: [Person]
    },
    id: {
        type: String
    }
});

Validation seems to work for non-array properties just as one could imagine:

sc = Family.newContext();
sc.validate({$set:{id:"12"}}, {modifier:true});
sc.isValid(); // returns true

sc.validate({$addToSet:{pets:{name:"Felix"}}}, {modifier:true});
sc.isValid(); //false
sc.invalidKeys(); 

Now, the last statement returns

"[{"name":"kids.$.name","type":"required","message":"Name is required"},{"name":"parents.$.name","type":"required","message":"Name is required"}]"

which I interpret as it expects the object to be full schema compatible, even though I think it should validate only against "Pet" schema.

I encountered this with Collections2, where it's very hard to add an element into a subdocument array when schema is complex. Basically only using $set to replace the full array seems to work.

Or do I get it wrong?

Thanks,
Jyrki

@aldeed
Copy link
Collaborator

aldeed commented Nov 1, 2013

I can't test it out right now, but by looking at it, it seems like a bug with the requirement check. I don't think our tests cover complex combinations of required props, so I'll add some similar tests and then make sure everything works. It might not be until early next week, though. Feel free to do a PR before then if you can fix it, but the validation logic is pretty complex and confusing to the uninitiated. :)

@aldeed aldeed closed this as completed in cd51e8f Nov 4, 2013
@aldeed
Copy link
Collaborator

aldeed commented Nov 4, 2013

This is fixed in the latest release.

@chrisbutler
Copy link

Hey @aldeed, I think I'm having this issue with 0.2.14. I can $set an entire array just fine, but when I try to $push a single item, validation fails with all the schema keys listed in invalidKeys()

If it will help, I'd be happy to provide you with an example, but I thought it might make sense to ping you first before taking that step

@aldeed
Copy link
Collaborator

aldeed commented Nov 5, 2013

@chrisbutler, an example would be good. $push seems to work fine based on the tests.

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

No branches or pull requests

3 participants