GithubHelp home page GithubHelp logo

bookshelf-validate's People

Contributors

anyong avatar belaarany avatar kripod avatar seiyria avatar stujo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bookshelf-validate's Issues

updating some not required fields triggers validator

I have a problem when I update a set of records

so for example if I have a student and this student has a list of books.

if I call

let books = await Books.where({student_id: id}).save({active: false}, {
                method: 'update',
                patch: true
            });

although I am only changing the active, bookshelf validator throughs name is required (where the name is a required field in books table)

Any idea?

Optional validation

How can one validate a field only if it's present?

Currently, If a value is null or undefined, validation always passes.

My use case is allowing null values for postcodes, but if one is provided then it must be valid.
So there either must be a valid one, or none at all.

I was hoping to achieve this with a custom validator:

postcode: [{
  method: 'isPostcode',
  error: 'Postcode is invalid',
  args: { optional: true }
}]

What do you think about adding another method to the model instance that actually does the validation on saving?

Rough example:

module.exports = function (bookshelf, validator) {
  'use strict';

  let Model = bookshelf.Model.extend({
    validationErrors: function () {
        // existing code...
    },

    initialize: function () {
        // attach event listener to do the validations on save
        if (typeof this.validateOnSave === 'function') {
            this.on('saving', this.validateSave);
        }
    },

    validateOnSave: Promise.method(function () {
        var errors = this.validationErrors();
        if (errors == null) {
            return null;
        }
        // Throw the errors or something so the model won't save.
    })
  });

  bookshelf.Model = Model;
};

If you want to override initialize in a model then you need to do:

initialize: function () {
    bookshelf.Model.prototype.initialize.apply(this);
    // more initialization here
}

I'm currently using your plugin like this and it works really nicely, saves me a ton of boilerplate. Thanks!

Must register plugin after models?

I received a strange error about this.collections being undefined when I attempted to register the bookshelf-validate plugin before registering my models. The error did not surface until calling into fetch / fetchAll.

Do you know why? It took me awhile to figure out that the order of registration seems to matter. Thanks!

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.