GithubHelp home page GithubHelp logo

What do you think about adding another method to the model instance that actually does the validation on saving? about bookshelf-validate HOT 6 CLOSED

kripod avatar kripod commented on May 16, 2024
What do you think about adding another method to the model instance that actually does the validation on saving?

from bookshelf-validate.

Comments (6)

kripod avatar kripod commented on May 16, 2024

I decided to use a separate validationErrors method in order to get information about more errors at once. You could add an optional attribute which triggers validation automatically before saving the Model: I'd happily accept a pull request for that! 😊

Also, I think there should be a custom Error object made for throwing exceptions.

from bookshelf-validate.

anyong avatar anyong commented on May 16, 2024

Also, I think there should be a custom Error object made for throwing exceptions.

This is possible, but then you would probably also want to have your validations look like this:

validations: {
    email: [
        {
            method: 'isEmail',
            options: {/* validator isEmail options object */},
            error: 'E_INVALID_EMAIL'
        }
    ]
}

and the corresponding error messages:

{
    email: ['E_INVALID_EMAIL']
}

instead of the current one which returns just the called method name.

That way you can throw a custom error message for each validation even if it uses the same validation method in different models.

With that setup though it would reduce boilerplate to almost nothing, and just adding the validations object to a model would do pretty much everything you need.

from bookshelf-validate.

anyong avatar anyong commented on May 16, 2024

Also, not sure if you're interested but I refactored the plugin just to be a bit cleaner, get rid of the new Function eval, changed all of your let to const where possible, and got rid of the continue statements in favor of control flow logic.

If you want to use that, I can make a pull request with it and for the custom error and auto-validate on save stuff after I write that.

from bookshelf-validate.

kripod avatar kripod commented on May 16, 2024

That would be great, but please, submit the Error emission feature as a separate pull request.

I'd suggest to create a new ValidationError object with a data argument which shall return the values returned by the Model.validationErrors() function instead of adding an error property to Model.validations.[property]

from bookshelf-validate.

anyong avatar anyong commented on May 16, 2024

Ok this is working really well, I'm happy with the result. I also recommend not adding the special isRequired checks in the Plugin, but rather onto the validator object as a normal method.

I'll make a couple of PRs in a minute...

from bookshelf-validate.

kripod avatar kripod commented on May 16, 2024

Thank you very much for all the help you provided! I highly appreciate all of your efforts towards my project.

from bookshelf-validate.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.