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

Missing scheme validation on update #25

Open
ijsf opened this issue Dec 28, 2015 · 2 comments
Open

Missing scheme validation on update #25

ijsf opened this issue Dec 28, 2015 · 2 comments
Labels

Comments

@ijsf
Copy link

ijsf commented Dec 28, 2015

I am currently having an issue with the model/scheme validation in case of a loadOneAndUpdate call.

Specifically, consider the normal create function, as in camo's examples, which obviously validates the input. I've added a quite strict model as well:

class Dog extends camo.Document {
    constructor() {
        super('dogs');
        this.name = { type: String; required: true; };
        this.breed = { type: String; choices: [ 'Collie', 'Bulldog' ] };
    }
}

var lassie = Dog.create({
    name: 'Lassie',
    breed: 'Collie'
});

lassie.save().then(function(l) {
    console.log(l.id);
});

However, let's say I want to update the breed of a specified Dog entry. In this case, I would use the loadOneAndUpdate function since I want the other values to remain the same, and the create function obviously wouldn't work because of the missing name:

Dog.loadOneAndUpdate({ id: ... }, { breed: 'totallywrongvalue' }).then(function(l) { ... });

Note that the breed does not adhere to the model here. Yet, the document is actually updated with this invalid value.

To solve this issue, I guess loadOneAndUpdate would have to do some kind of validation. Perhaps additionally, an explicit validation function validate could be used and exposed which validates any given data against the model.

@ijsf
Copy link
Author

ijsf commented Dec 29, 2015

Just noticed that a validate function does exist, though I guess a similar function that wouldn't check for missing keys would be more useful.

@scottwrobinson
Copy link
Owner

Hmm ya it looks like the internal validation isn't called when using loadOneAndUpdate. Nice catch. I'll include this fix in the upcoming update.

Thanks!

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

No branches or pull requests

2 participants