question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Yo, I am coming over from Sequelize ORM and for the most part migration has been pretty easy. However, while I was working with the Bookshelf ORM I noticed a few things about validation that have me confused about how it should be implemented.

First there is this comment in the source for the save method:

// Sets and saves the hash of model attributes, triggering
// a "creating" or "updating" event on the model, as well as a "saving" event,
// to bind listeners for any necessary validation, logging, etc.
// If an error is thrown during these events, the model will not be saved.

Then there is this comment above the _validate method.

// Validation can be complicated, and is better handled
// on its own and not mixed in with database logic.

So I can’t tell if the plan is just to completely omit model validation from this ORM or not? Sequelize uses node-validator https://github.com/chriso/node-validator and I began to attempt implementing the missing validation methods that were omitted/stripped from the backbone inheritance, but stopped because I don’t know if it’s something Bookshelf is even interested in.

Can anyone tell me the plan for validation and Bookshelf or advise the best way to implement validation against my model if there is no intention to support it?

Thanks!

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
calmdevcommented, Jul 17, 2013

I believe this is exactly what I needed. Can you verify if the implementation is ok? I want to share it incase others are interested in this. Your example above demonstrates use for more complex cases, I think - which is pretty cool.

Model:

var User = Bookshelf.Model.extend({
    tableName: 'users',
    initialize: function() {
        this.on('saving', this.validate, this);
    },
    validations: {
        email: ['required', 'validEmail'],
        username: ['required', 'alphaNumeric'],
        age: ['isNumeric']
    },
    validate: function(model, attrs, options) {
        return CheckIt(this.toJSON()).run(this.validations);
    }
});

Usage:

User.forge({
    email: 'invalid@testcom',
    username: 'joe-invalid'
}).save().then(function(user) {
    console.log(user.toJSON());
}, function(errors) {
    console.log(errors.toJSON());
    // { email: 'The email must contain a valid email address', username: 'The username must only contain alpha-numeric characters' }
});
0reactions
bendruckercommented, Oct 5, 2014

Not at all @thetutlage. Please open a new issue though if you’re having trouble.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Model Validation and Why is it Important? - Datatron
Model validation is the process that is carried out after Model Training where the trained model is evaluated with a testing data set....
Read more >
Model Validation - an overview | ScienceDirect Topics
Model validation refers to the process of confirming that the model actually achieves its intended purpose. In most situations, this will involve confirmation ......
Read more >
Why is model validation so darn important and how is it ...
The purpose of model validation is to check the accuracy and performance of the model basis on the past data for which we...
Read more >
Statistical model validation - Wikipedia
In statistics, model validation is the task of evaluating whether a chosen statistical model is appropriate or not. Oftentimes in statistical inference, ...
Read more >
Model Validation | Kaggle
You'll want to evaluate almost every model you ever build. In most (though not all) applications, the relevant measure of model quality is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found