Loopback: Is there any way to validate model?

Created on 21 Oct 2014  路  4Comments  路  Source: strongloop/loopback

How can I validate model before creating it. I know "create" validates model automatically.

But what if I have to validate 2 models and then if both are valid - create them. Otherwise to throw an error. Something like:

    models.creditCard({
        name:'test'
    }).validate(function (err, data) {
        if (! err) {
            models.creditCard.create({
                name: 'test'
            }, function (err, data) {

            }l
        }   
    })

Thanks

doc

Most helpful comment

Found solution, thanks :)

    var creditcard = models.creditCard({
        name:'test'
    });

    creditcard.isValid(function(valid) {
        if (! valid) {
            console.log(creditcard.errors);
        }
    });

All 4 comments

Found solution, thanks :)

    var creditcard = models.creditCard({
        name:'test'
    });

    creditcard.isValid(function(valid) {
        if (! valid) {
            console.log(creditcard.errors);
        }
    });

@crandmck is this covered by our docs, either Confluence or API docs?

We have some doc on validation, but it really needs improvement. See #693.

You guys need to update one of the sample codes with validation... It is NOT clear where to add them.

Was this page helpful?
0 / 5 - 0 ratings