Coder Social home page Coder Social logo

bookshelf-validator's Introduction

bookshelf-validator

This is forked from Fluxxu's bookshelf-validator (https://github.com/thanhn08/bookshelf-validator). Much credit goes to him.

Full feature validation plugin for Bookshelf.

Install

npm install bookshelf-validator

Quick Example

var Validator = require('bookshelf-validator'),
    ValidationError = Validator.ValidationError;

var DbContext = Bookshelf.initialize(config);

DbContext.plugin(Validator.plugin);

var Obj = DbContext.Model.extend({
    tableName: 'obj',
    validation: {
        'name': [
            { validator: 'notEmpty', message: 'name is required' },
            { validator: 'matches', args: [/^[a-z0-9 ]+$/i], message: 'name format incorrect'},
            function (value, context) {
                return DbContext.knex('obj').where('name', value).count('* AS count')
                    .then(function (results) {
                        if (results[0].count > 0)
                            context.addError('name already exists');
                    });
            }
        ],
        'quantity': [
            { validator: 'isInt', message: 'quantity must be integer' }
        ]
    }
});

Obj.forge({}).save()
    .then(function () {
        throw new Error('obj should not be saved');
    })
    .catch(ValidationError, function (e) {
        expect(e.errors).to.have.keys(['name', 'quantity']);
        expect(e.errors.name).to.have.length(2);
        expect(e.errors.name).to.deep.equal(['name is required', 'name format incorrect']);
        expect(e.errors.quantity).to.have.length(1);
        expect(e.errors.quantity[0]).to.equal('quantity must be integer');
    });


Obj.forge({ name: 'Name', quantity: 1 }).save()
    .then(function () {
        return Obj.forge({ name: 'Name', quantity: 1}).save()
            .then(function () {
                throw new Error('obj should not be saved');
            })
            .catch(ValidationError, function (e) {
                expect(e.errors).to.have.keys(['name']);
                expect(e.errors.name).to.have.length(1);
                expect(e.errors.name[0]).to.equal('name already exists');
            });
    });

bookshelf-validator's People

Contributors

fluxxu avatar thanhn08 avatar

Stargazers

Tuan avatar

Watchers

 avatar

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.