2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2016-12-05 14:14:09 +00:00
|
|
|
|
|
|
|
/* global getSchema:false */
|
2018-12-07 14:54:29 +00:00
|
|
|
const should = require('./init.js');
|
|
|
|
const async = require('async');
|
2013-04-06 10:57:12 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let j = require('../'), db, User;
|
|
|
|
const ValidationError = j.ValidationError;
|
2013-03-24 22:21:18 +00:00
|
|
|
|
|
|
|
function getValidAttributes() {
|
2014-01-24 17:09:53 +00:00
|
|
|
return {
|
|
|
|
name: 'Anatoliy',
|
|
|
|
email: 'email@example.com',
|
|
|
|
state: '',
|
|
|
|
age: 26,
|
|
|
|
gender: 'male',
|
|
|
|
createdByAdmin: false,
|
2016-04-01 11:48:17 +00:00
|
|
|
createdByScript: true,
|
2014-01-24 17:09:53 +00:00
|
|
|
};
|
2013-03-24 22:21:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('validations', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let User, Entry, Employee;
|
2015-07-13 10:56:25 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
before(function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
db = getSchema();
|
|
|
|
User = db.define('User', {
|
|
|
|
email: String,
|
|
|
|
name: String,
|
|
|
|
password: String,
|
|
|
|
state: String,
|
|
|
|
age: Number,
|
|
|
|
gender: String,
|
|
|
|
domain: String,
|
|
|
|
pendingPeriod: Number,
|
|
|
|
createdByAdmin: Boolean,
|
|
|
|
createdByScript: Boolean,
|
2016-04-01 11:48:17 +00:00
|
|
|
updatedAt: Date,
|
2013-03-29 07:43:04 +00:00
|
|
|
});
|
2015-07-13 10:56:25 +00:00
|
|
|
Entry = db.define('Entry', {
|
2016-08-19 17:46:59 +00:00
|
|
|
id: {type: 'string', id: true, generated: false},
|
|
|
|
name: {type: 'string'},
|
2015-07-13 10:56:25 +00:00
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee = db.define('Employee', {
|
|
|
|
id: {type: Number, id: true, generated: false},
|
|
|
|
name: {type: String},
|
|
|
|
age: {type: Number},
|
2017-08-03 11:27:11 +00:00
|
|
|
}, {
|
|
|
|
validateUpdate: true,
|
2017-07-31 17:53:43 +00:00
|
|
|
});
|
2015-07-13 10:56:25 +00:00
|
|
|
Entry.validatesUniquenessOf('id');
|
2017-07-31 17:53:43 +00:00
|
|
|
db.automigrate(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, done);
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-29 07:43:04 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
User.destroyAll(function() {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
2013-03-26 00:41:00 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 00:41:00 +00:00
|
|
|
|
2017-07-31 17:53:43 +00:00
|
|
|
after(function(done) {
|
|
|
|
Employee.destroyAll(done);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-29 07:43:04 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('commons', function() {
|
|
|
|
describe('skipping', function() {
|
|
|
|
it('should NOT skip when `if` is fulfilled', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-01-24 17:09:53 +00:00
|
|
|
user.createdByAdmin = true;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.false();
|
2014-01-24 17:09:53 +00:00
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
2014-11-19 13:50:08 +00:00
|
|
|
user.pendingPeriod = 1;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-01 16:08:22 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should skip when `if` is NOT fulfilled', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = false;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
|
|
|
user.errors.should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
user.pendingPeriod = 1;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
2014-11-19 13:50:08 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT skip when `unless` is fulfilled', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesPresenceOf('pendingPeriod', {unless: 'createdByAdmin'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = false;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
|
|
|
user.pendingPeriod = 1;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
2014-11-19 13:50:08 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should skip when `unless` is NOT fulfilled', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesPresenceOf('pendingPeriod', {unless: 'createdByAdmin'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = true;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
|
|
|
user.errors.should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
user.pendingPeriod = 1;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
2014-11-19 13:50:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('skipping in async validation', function() {
|
|
|
|
it('should skip when `if` is NOT fulfilled', function(done) {
|
|
|
|
User.validateAsync('pendingPeriod', function(err, done) {
|
2014-11-19 13:50:08 +00:00
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {if: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = false;
|
2016-04-01 11:48:17 +00:00
|
|
|
user.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
|
|
|
user.errors.should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT skip when `if` is fulfilled', function(done) {
|
|
|
|
User.validateAsync('pendingPeriod', function(err, done) {
|
2014-11-19 13:50:08 +00:00
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {if: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = true;
|
2016-04-01 11:48:17 +00:00
|
|
|
user.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should skip when `unless` is NOT fulfilled', function(done) {
|
|
|
|
User.validateAsync('pendingPeriod', function(err, done) {
|
2014-11-19 13:50:08 +00:00
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {unless: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = true;
|
2016-04-01 11:48:17 +00:00
|
|
|
user.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
|
|
|
user.errors.should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should NOT skip when `unless` is fulfilled', function(done) {
|
|
|
|
User.validateAsync('pendingPeriod', function(err, done) {
|
2014-11-19 13:50:08 +00:00
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {unless: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User;
|
2014-11-19 13:50:08 +00:00
|
|
|
user.createdByAdmin = false;
|
2016-04-01 11:48:17 +00:00
|
|
|
user.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.false();
|
2014-11-19 13:50:08 +00:00
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-01 16:08:22 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('lifecycle', function() {
|
|
|
|
it('should work on create', function(done) {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesPresenceOf('name');
|
2016-04-01 11:48:17 +00:00
|
|
|
User.create(function(e, u) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(e);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'Valid'}, function(e, d) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should work on update', function(done) {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesPresenceOf('name');
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'Valid'}, function(e, d) {
|
2016-04-01 11:48:17 +00:00
|
|
|
d.updateAttribute('name', null, function(e) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(e);
|
|
|
|
e.should.be.instanceOf(Error);
|
|
|
|
e.should.be.instanceOf(ValidationError);
|
2016-04-01 11:48:17 +00:00
|
|
|
d.updateAttribute('name', 'Vasiliy', function(e) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
2013-04-04 15:31:07 +00:00
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-05 06:24:08 +00:00
|
|
|
it('should ignore errors on upsert by default', function(done) {
|
2015-04-16 07:03:56 +00:00
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
2017-08-22 20:09:45 +00:00
|
|
|
// It's important to pass an existing id value to updateOrCreate,
|
|
|
|
// otherwise DAO falls back to regular create()
|
|
|
|
User.create({name: 'a-name'}, (err, u) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
User.updateOrCreate({id: u.id}, done);
|
|
|
|
});
|
2015-04-16 07:03:56 +00:00
|
|
|
});
|
|
|
|
|
2015-05-05 06:24:08 +00:00
|
|
|
it('should be skipped by upsert when disabled via settings', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const Customer = User.extend('Customer');
|
2015-05-05 06:24:08 +00:00
|
|
|
Customer.attachTo(db);
|
|
|
|
db.autoupdate(function(err) {
|
|
|
|
if (err) return done(err);
|
2017-08-22 20:09:45 +00:00
|
|
|
// It's important to pass an existing id value,
|
|
|
|
// otherwise DAO falls back to regular create()
|
|
|
|
Customer.create({name: 'a-name'}, (err, u) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
Customer.prototype.isValid = function() {
|
|
|
|
throw new Error('isValid() should not be called at all');
|
|
|
|
};
|
|
|
|
Customer.settings.validateUpsert = false;
|
|
|
|
|
|
|
|
Customer.updateOrCreate({id: u.id, name: ''}, done);
|
|
|
|
});
|
2015-05-05 06:24:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-16 07:03:56 +00:00
|
|
|
it('should work on upsert when enabled via settings', function(done) {
|
|
|
|
User.validatesPresenceOf('name');
|
|
|
|
User.settings.validateUpsert = true;
|
2017-08-22 20:09:45 +00:00
|
|
|
// It's important to pass an existing id value,
|
|
|
|
// otherwise DAO falls back to regular create()
|
|
|
|
User.create({name: 'a-name'}, (err, u) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
User.upsert({id: u.id, name: ''}, function(err, u) {
|
|
|
|
if (!err) return done(new Error('Validation should have failed.'));
|
|
|
|
err.should.be.instanceOf(ValidationError);
|
|
|
|
done();
|
|
|
|
});
|
2015-04-16 07:03:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should return error code', function(done) {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesPresenceOf('name');
|
2016-04-01 11:48:17 +00:00
|
|
|
User.create(function(e, u) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(e);
|
|
|
|
e.details.codes.name.should.eql(['presence']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-04-04 15:31:07 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should allow to modify error after validation', function(done) {
|
|
|
|
User.afterValidate = function(next) {
|
2014-01-24 17:09:53 +00:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
done();
|
|
|
|
});
|
2013-04-30 18:47:35 +00:00
|
|
|
|
2014-05-20 08:47:44 +00:00
|
|
|
it('should include validation messages in err.message', function(done) {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-05-20 08:47:44 +00:00
|
|
|
User.validatesPresenceOf('name');
|
2016-04-01 11:48:17 +00:00
|
|
|
User.create(function(e, u) {
|
2014-05-20 08:47:44 +00:00
|
|
|
should.exist(e);
|
|
|
|
e.message.should.match(/`name` can't be blank/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-01-12 13:52:54 +00:00
|
|
|
it('should include property value in err.message', function(done) {
|
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
2016-04-01 11:48:17 +00:00
|
|
|
User.create(function(e, u) {
|
2015-01-12 13:52:54 +00:00
|
|
|
should.exist(e);
|
|
|
|
e.message.should.match(/`name` can't be blank \(value: undefined\)/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-20 08:47:44 +00:00
|
|
|
it('should include model name in err.message', function(done) {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-05-20 08:47:44 +00:00
|
|
|
User.validatesPresenceOf('name');
|
2016-04-01 11:48:17 +00:00
|
|
|
User.create(function(e, u) {
|
2014-05-20 08:47:44 +00:00
|
|
|
should.exist(e);
|
|
|
|
e.message.should.match(/`User` instance/i);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-08-19 13:41:55 +00:00
|
|
|
it('should return validation metadata', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const expected = {name: [{validation: 'presence', options: {}}]};
|
2014-08-26 15:51:01 +00:00
|
|
|
delete User.validations;
|
2014-08-19 13:41:55 +00:00
|
|
|
User.validatesPresenceOf('name');
|
2018-12-07 14:54:29 +00:00
|
|
|
const validations = User.validations;
|
2014-08-19 13:41:55 +00:00
|
|
|
validations.should.eql(expected);
|
|
|
|
});
|
2013-03-29 07:43:04 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-29 07:43:04 +00:00
|
|
|
|
2016-06-24 15:07:25 +00:00
|
|
|
describe('validation with or without options', function() {
|
|
|
|
it('should work on update with options', function(done) {
|
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'Valid'}, function(e, d) {
|
|
|
|
d.updateAttribute('name', null, {options: 'options'}, function(e) {
|
2016-06-24 15:07:25 +00:00
|
|
|
should.exist(e);
|
|
|
|
e.should.be.instanceOf(Error);
|
|
|
|
e.should.be.instanceOf(ValidationError);
|
2017-10-11 19:53:13 +00:00
|
|
|
d.updateAttribute('name', 'Vasiliy', {options: 'options'}, err => {
|
|
|
|
if (err) return done(err);
|
|
|
|
// test passed
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes options to custom sync validator', done => {
|
|
|
|
delete User.validations;
|
|
|
|
User.validate('name', function(err, options) {
|
|
|
|
if (options.testFlag !== 'someValue') err();
|
|
|
|
});
|
|
|
|
User.create({name: 'Valid'}, {testFlag: 'someValue'}, function(e, d) {
|
|
|
|
d.updateAttribute('name', null, {testFlag: 'otherValue'}, function(e) {
|
|
|
|
should.exist(e);
|
|
|
|
e.should.be.instanceOf(ValidationError);
|
|
|
|
d.updateAttribute('name', 'Vasiliy', {testFlag: 'someValue'}, err => {
|
|
|
|
if (err) return done(err);
|
|
|
|
// test passed
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes options to async validator', done => {
|
|
|
|
delete User.validations;
|
|
|
|
User.validateAsync('name', function(err, options, done) {
|
|
|
|
if (options.testFlag !== 'someValue') {
|
|
|
|
console.error(
|
|
|
|
'Unexpected validation options: %j Expected %j',
|
2018-07-16 06:46:25 +00:00
|
|
|
options, {testFlag: 'someValue'}
|
|
|
|
);
|
2017-10-11 19:53:13 +00:00
|
|
|
err();
|
|
|
|
}
|
|
|
|
process.nextTick(function() { done(); });
|
|
|
|
});
|
|
|
|
User.create({name: 'Valid'}, {testFlag: 'someValue'}, function(e, d) {
|
|
|
|
if (e) return done(e);
|
|
|
|
d.updateAttribute('name', null, {testFlag: 'otherValue'}, function(e) {
|
|
|
|
should.exist(e);
|
|
|
|
e.should.be.instanceOf(ValidationError);
|
|
|
|
d.updateAttribute('name', 'Vasiliy', {testFlag: 'someValue'}, err => {
|
|
|
|
if (err) return done(err);
|
|
|
|
// test passed
|
2016-06-24 15:07:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work on update without options', function(done) {
|
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'Valid'}, function(e, d) {
|
2016-06-24 15:07:25 +00:00
|
|
|
d.updateAttribute('name', null, function(e) {
|
|
|
|
should.exist(e);
|
|
|
|
e.should.be.instanceOf(Error);
|
|
|
|
e.should.be.instanceOf(ValidationError);
|
|
|
|
d.updateAttribute('name', 'Vasiliy', function(e) {
|
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work on create with options', function(done) {
|
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
|
|
|
User.create(function(e, u) {
|
|
|
|
should.exist(e);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'Valid'}, {options: 'options'}, function(e, d) {
|
2016-06-24 15:07:25 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work on create without options', function(done) {
|
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
|
|
|
User.create(function(e, u) {
|
|
|
|
should.exist(e);
|
2016-08-19 17:46:59 +00:00
|
|
|
User.create({name: 'Valid'}, function(e, d) {
|
2016-06-24 15:07:25 +00:00
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('presence', function() {
|
|
|
|
it('should validate presence', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesPresenceOf('name', 'email');
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const validations = User.validations;
|
2016-08-19 17:46:59 +00:00
|
|
|
validations.name.should.eql([{validation: 'presence', options: {}}]);
|
|
|
|
validations.email.should.eql([{validation: 'presence', options: {}}]);
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User;
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.not.be.true();
|
2014-01-24 17:09:53 +00:00
|
|
|
u.name = 1;
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.not.be.true();
|
2014-01-24 17:09:53 +00:00
|
|
|
u.email = 2;
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2015-04-22 17:57:48 +00:00
|
|
|
it('should reject NaN value as a number', function() {
|
|
|
|
User.validatesPresenceOf('age');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User();
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.false();
|
2015-04-22 17:57:48 +00:00
|
|
|
u.age = NaN;
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.false();
|
2015-04-22 17:57:48 +00:00
|
|
|
u.age = 1;
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2015-04-22 17:57:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow "NaN" value as a string', function() {
|
|
|
|
User.validatesPresenceOf('name');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User();
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.false();
|
2015-04-22 17:57:48 +00:00
|
|
|
u.name = 'NaN';
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2015-04-22 17:57:48 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should skip validation by property (if/unless)', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesPresenceOf('domain', {unless: 'createdByScript'});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User(getValidAttributes());
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
user.createdByScript = false;
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.false();
|
2014-01-24 17:09:53 +00:00
|
|
|
user.errors.domain.should.eql(['can\'t be blank']);
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
user.domain = 'domain';
|
2017-07-26 07:22:11 +00:00
|
|
|
user.isValid().should.be.true();
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate presence on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesPresenceOf('name', 'age');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo-new', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new'},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.age[0], 'can\'t be blank');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('absence', function() {
|
|
|
|
it('should validate absence', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesAbsenceOf('reserved', {if: 'locked'});
|
2018-12-07 14:54:29 +00:00
|
|
|
let u = new User({reserved: 'foo', locked: true});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.not.be.true();
|
2014-07-11 20:56:02 +00:00
|
|
|
u.reserved = null;
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2017-01-31 15:14:53 +00:00
|
|
|
u = new User({reserved: 'foo', locked: false});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate absence on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesAbsenceOf('name');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
data.name = 'Foo';
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new', age: 5},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.name[0], 'can\'t be set');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('uniqueness', function() {
|
|
|
|
it('should validate uniqueness', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesUniquenessOf('email');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hey'});
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2016-04-01 11:48:17 +00:00
|
|
|
u.save(function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'hey'});
|
2016-04-01 11:48:17 +00:00
|
|
|
u2.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.false();
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
2017-07-26 07:22:11 +00:00
|
|
|
})).should.be.false();
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should handle same object modification', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesUniquenessOf('email');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hey'});
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2016-04-01 11:48:17 +00:00
|
|
|
u.save(function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
u.name = 'Goghi';
|
2016-04-01 11:48:17 +00:00
|
|
|
u.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2014-01-24 17:09:53 +00:00
|
|
|
u.save(done);
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
// async validations always falsy when called as sync
|
|
|
|
})).should.not.be.ok;
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
|
|
|
|
2014-05-14 18:38:40 +00:00
|
|
|
it('should support multi-key constraint', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const EMAIL = 'user@xample.com';
|
|
|
|
const SiteUser = db.define('SiteUser', {
|
2014-05-14 18:38:40 +00:00
|
|
|
siteId: String,
|
2016-04-01 11:48:17 +00:00
|
|
|
email: String,
|
2014-05-14 18:38:40 +00:00
|
|
|
});
|
2016-08-19 17:46:59 +00:00
|
|
|
SiteUser.validatesUniquenessOf('email', {scopedTo: ['siteId']});
|
2014-05-14 18:38:40 +00:00
|
|
|
async.waterfall([
|
|
|
|
function automigrate(next) {
|
|
|
|
db.automigrate(next);
|
|
|
|
},
|
|
|
|
function createSite1User(next) {
|
|
|
|
SiteUser.create(
|
2016-08-19 17:46:59 +00:00
|
|
|
{siteId: 1, email: EMAIL},
|
2018-07-16 06:46:25 +00:00
|
|
|
next
|
|
|
|
);
|
2014-05-14 18:38:40 +00:00
|
|
|
},
|
|
|
|
function createSite2User(user1, next) {
|
|
|
|
SiteUser.create(
|
2016-08-19 17:46:59 +00:00
|
|
|
{siteId: 2, email: EMAIL},
|
2018-07-16 06:46:25 +00:00
|
|
|
next
|
|
|
|
);
|
2014-05-14 18:38:40 +00:00
|
|
|
},
|
|
|
|
function validateDuplicateUser(user2, next) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const user3 = new SiteUser({siteId: 1, email: EMAIL});
|
2014-05-14 18:38:40 +00:00
|
|
|
user3.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.false();
|
2014-05-14 18:38:40 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
},
|
2014-05-14 18:38:40 +00:00
|
|
|
], function(err) {
|
|
|
|
if (err && err.name == 'ValidationError') {
|
|
|
|
console.error('ValidationError:', err.details.messages);
|
|
|
|
}
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should skip blank values', function(done) {
|
2014-07-11 20:07:57 +00:00
|
|
|
User.validatesUniquenessOf('email');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: ' '});
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2016-04-01 11:48:17 +00:00
|
|
|
u.save(function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: null});
|
2016-04-01 11:48:17 +00:00
|
|
|
u2.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2014-07-11 20:07:57 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-07-26 07:22:11 +00:00
|
|
|
})).should.be.false();
|
2014-07-11 20:07:57 +00:00
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should work with if/unless', function(done) {
|
2015-01-12 15:50:51 +00:00
|
|
|
User.validatesUniquenessOf('email', {
|
2014-07-11 20:56:02 +00:00
|
|
|
if: function() { return true; },
|
2016-04-01 11:48:17 +00:00
|
|
|
unless: function() { return false; },
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hello'});
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2014-07-11 20:56:02 +00:00
|
|
|
done();
|
2017-07-26 07:22:11 +00:00
|
|
|
})).should.be.false();
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2015-07-13 10:56:25 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should work with id property on create', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Entry.create({id: 'entry'}, function(err, entry) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const e = new Entry({id: 'entry'});
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(e.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.false();
|
2015-07-13 10:56:25 +00:00
|
|
|
done();
|
2017-07-26 07:22:11 +00:00
|
|
|
})).should.be.false();
|
2015-07-13 10:56:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should work with id property after create', function(done) {
|
2015-07-13 10:56:25 +00:00
|
|
|
Entry.findById('entry', function(err, e) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(e.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2015-07-13 10:56:25 +00:00
|
|
|
done();
|
2017-07-26 07:22:11 +00:00
|
|
|
})).should.be.false();
|
2015-07-13 10:56:25 +00:00
|
|
|
});
|
|
|
|
});
|
2015-12-21 12:38:11 +00:00
|
|
|
|
|
|
|
it('passes case insensitive validation', function(done) {
|
|
|
|
User.validatesUniquenessOf('email', {ignoreCase: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hey'});
|
2015-12-21 12:38:11 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
u.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'HEY'});
|
2015-12-21 12:38:11 +00:00
|
|
|
u2.isValid(function(valid) {
|
|
|
|
valid.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passed case sensitive validation', function(done) {
|
|
|
|
User.validatesUniquenessOf('email', {ignoreCase: false});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hey'});
|
2015-12-21 12:38:11 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
u.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'HEY'});
|
2015-12-21 12:38:11 +00:00
|
|
|
u2.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes case insensitive validation with string that needs escaping', function(done) {
|
|
|
|
User.validatesUniquenessOf('email', {ignoreCase: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'me+me@my.com'});
|
2015-12-21 12:38:11 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
u.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'ME+ME@MY.COM'});
|
2015-12-21 12:38:11 +00:00
|
|
|
u2.isValid(function(valid) {
|
|
|
|
valid.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passed case sensitive validation with string that needs escaping', function(done) {
|
|
|
|
User.validatesUniquenessOf('email', {ignoreCase: false});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'me+me@my.com'});
|
2015-12-21 12:38:11 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
u.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'ME+ME@MY.COM'});
|
2015-12-21 12:38:11 +00:00
|
|
|
u2.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes partial case insensitive validation with string that needs escaping', function(done) {
|
|
|
|
User.validatesUniquenessOf('email', {ignoreCase: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'also+me@my.com'});
|
2015-12-21 12:38:11 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
u.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'Me@My.com'});
|
2015-12-21 12:38:11 +00:00
|
|
|
u2.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes partial case sensitive validation with string that needs escaping', function(done) {
|
|
|
|
User.validatesUniquenessOf('email', {ignoreCase: false});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'also+me@my.com'});
|
2015-12-21 12:38:11 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
u.save(function(err) {
|
|
|
|
if (err) return done(err);
|
2018-12-07 14:54:29 +00:00
|
|
|
const u2 = new User({email: 'Me@My.com'});
|
2015-12-21 12:38:11 +00:00
|
|
|
u2.isValid(function(valid) {
|
|
|
|
valid.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false();
|
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate uniqueness on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesUniquenessOf('name');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo-new', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: 'Bar', age: 5},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.name[0], 'is not unique');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('format', function() {
|
2017-07-26 07:13:29 +00:00
|
|
|
it('should validate the format of valid strings', function() {
|
|
|
|
User.validatesFormatOf('name', {with: /[a-z][A-Z]*$/});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({name: 'valid name'});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.true();
|
|
|
|
});
|
2015-01-27 09:02:07 +00:00
|
|
|
|
2017-07-26 07:13:29 +00:00
|
|
|
it('should validate the format of invalid strings', function() {
|
|
|
|
User.validatesFormatOf('name', {with: /[a-z][A-Z]*$/});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({name: 'invalid name!'});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate the format of valid numbers', function() {
|
|
|
|
User.validatesFormatOf('age', {with: /^\d+$/});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({age: 30});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate the format of invalid numbers', function() {
|
|
|
|
User.validatesFormatOf('age', {with: /^\d+$/});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({age: 'thirty'});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.false();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should overwrite default blank message with custom format message', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const CUSTOM_MESSAGE = 'custom validation message';
|
2017-07-26 07:13:29 +00:00
|
|
|
User.validatesFormatOf('name', {with: /[a-z][A-Z]*$/, message: CUSTOM_MESSAGE});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({name: 'invalid name string 123'});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.false();
|
2018-05-24 21:16:15 +00:00
|
|
|
u.errors.should.containEql({
|
2017-07-26 07:13:29 +00:00
|
|
|
name: [CUSTOM_MESSAGE],
|
|
|
|
codes: {
|
|
|
|
name: ['format'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip missing values when allowing blank', function() {
|
|
|
|
User.validatesFormatOf('email', {with: /^\S+@\S+\.\S+$/, allowBlank: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.true();
|
2015-01-27 09:02:07 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should skip null values when allowing null', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesFormatOf('email', {with: /^\S+@\S+\.\S+$/, allowNull: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: null});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.true();
|
2015-01-27 09:02:07 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not skip missing values', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesFormatOf('email', {with: /^\S+@\S+\.\S+$/});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.false();
|
2015-01-27 09:02:07 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should not skip null values', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
User.validatesFormatOf('email', {with: /^\S+@\S+\.\S+$/});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: null});
|
2017-07-26 07:13:29 +00:00
|
|
|
u.isValid().should.be.false();
|
2015-01-27 09:02:07 +00:00
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
2017-09-01 16:48:29 +00:00
|
|
|
describe('validate format correctly on bulk creation with global flag enabled in RegExp', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesFormatOf('name', {with: /^[a-z]+$/g, allowNull: false});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met for all items', function(done) {
|
|
|
|
Employee.create([
|
|
|
|
{name: 'test'},
|
|
|
|
{name: 'test'},
|
|
|
|
{name: 'test'},
|
|
|
|
{name: 'test'},
|
|
|
|
{name: 'test'},
|
|
|
|
{name: 'test'},
|
|
|
|
], (err, instances) => {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(instances);
|
|
|
|
instances.should.have.lengthOf(6);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-31 17:53:43 +00:00
|
|
|
describe('validate format on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesFormatOf('name', {with: /^\w+\s\w+$/, allowNull: false});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo Mo', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: '45foo', age: 5},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.name[0], 'is invalid');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('numericality', function() {
|
2016-09-23 09:26:29 +00:00
|
|
|
it('passes when given numeric values', function() {
|
|
|
|
User.validatesNumericalityOf('age');
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: 10});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when given non-numeric values', function() {
|
|
|
|
User.validatesNumericalityOf('age');
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: 'notanumber'});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.false();
|
2018-05-24 21:16:15 +00:00
|
|
|
user.errors.should.containEql({age: ['is not a number']});
|
2016-09-23 09:26:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when given undefined values', function() {
|
|
|
|
User.validatesNumericalityOf('age');
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({});
|
2016-10-20 20:36:37 +00:00
|
|
|
user.isValid().should.be.false();
|
2018-05-24 21:16:15 +00:00
|
|
|
user.errors.should.containEql({age: ['is blank']});
|
2016-09-23 09:26:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('skips undefined values when allowBlank option is true', function() {
|
|
|
|
User.validatesNumericalityOf('age', {allowBlank: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when given non-numeric values when allowBlank option is true', function() {
|
|
|
|
User.validatesNumericalityOf('age', {allowBlank: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: 'test'});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.false();
|
2018-05-24 21:16:15 +00:00
|
|
|
user.errors.should.containEql({age: ['is not a number']});
|
2016-09-23 09:26:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when given null values', function() {
|
|
|
|
User.validatesNumericalityOf('age');
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: null});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.false();
|
2018-05-24 21:16:15 +00:00
|
|
|
user.errors.should.containEql({age: ['is null']});
|
2016-09-23 09:26:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when given null values when allowNull option is true', function() {
|
|
|
|
User.validatesNumericalityOf('age', {allowNull: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: null});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when given float values', function() {
|
|
|
|
User.validatesNumericalityOf('age');
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: 13.37});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails when given non-integer values when int option is true', function() {
|
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
2018-12-07 14:54:29 +00:00
|
|
|
const user = new User({age: 13.37});
|
2016-09-23 09:26:29 +00:00
|
|
|
user.isValid().should.be.false();
|
2017-03-08 15:24:18 +00:00
|
|
|
user.errors.should.match({age: /is not an integer/});
|
2016-09-23 09:26:29 +00:00
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate numericality on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesNumericalityOf('age');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo-new', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {age: {someAge: 5}},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.age[0], 'is not a number');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('inclusion', function() {
|
2017-03-18 10:50:10 +00:00
|
|
|
it('fails when included value is not used for property', function(done) {
|
|
|
|
User.validatesInclusionOf('name', {in: ['bob', 'john']});
|
|
|
|
User.create({name: 'bobby'}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({name: /is not included in the list/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when included value is used for property', function(done) {
|
|
|
|
User.validatesInclusionOf('name', {in: ['bob', 'john']});
|
|
|
|
User.create({name: 'bob'}, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
user.name.should.eql('bob');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a custom error message', function(done) {
|
|
|
|
User.validatesInclusionOf('name', {in: ['bob', 'john'], message: 'not used'});
|
|
|
|
User.create({name: 'dude'}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({name: /not used/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a null value when allowNull is false', function(done) {
|
|
|
|
User.validatesInclusionOf('name', {in: ['bob'], allowNull: false});
|
|
|
|
User.create({name: null}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({name: /is null/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes with a null value when allowNull is true', function(done) {
|
|
|
|
User.validatesInclusionOf('name', {in: ['bob'], allowNull: true});
|
|
|
|
User.create({name: null}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if value is used for integer property', function(done) {
|
|
|
|
User.validatesInclusionOf('age', {in: [123, 456]});
|
|
|
|
User.create({age: 789}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({age: /is not included in the list/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-07-19 23:26:13 +00:00
|
|
|
|
|
|
|
it('passes with an empty value when allowBlank option is true', function(done) {
|
|
|
|
User.validatesInclusionOf('gender', {in: ['male', 'female'], allowBlank: true});
|
|
|
|
User.create({gender: ''}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with an empty value when allowBlank option is false', function(done) {
|
|
|
|
User.validatesInclusionOf('gender', {in: ['male', 'female'], allowBlank: false});
|
|
|
|
User.create({gender: ''}, function(err) {
|
|
|
|
err.should.be.instanceOf(ValidationError);
|
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`gender` is blank (value: "").');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function getErrorDetails(err) {
|
|
|
|
return err.message.replace(/^.*Details: /, '');
|
|
|
|
}
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate inclusion on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesInclusionOf('name', {in: ['Foo-new']});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo-new', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: 'Foo-new2', age: 5},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.name[0], 'is not included in ' +
|
|
|
|
'the list');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('exclusion', function() {
|
2017-03-08 15:24:18 +00:00
|
|
|
it('fails when excluded value is used for property', function(done) {
|
|
|
|
User.validatesExclusionOf('name', {in: ['bob']});
|
|
|
|
User.create({name: 'bob'}, function(err, user) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({name: /is reserved/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when excluded value not found for property', function(done) {
|
|
|
|
User.validatesExclusionOf('name', {in: ['dude']});
|
|
|
|
User.create({name: 'bob'}, function(err, user) {
|
|
|
|
if (err) return done(err);
|
|
|
|
user.name.should.eql('bob');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a custom error message', function(done) {
|
|
|
|
User.validatesExclusionOf('name', {in: ['bob'], message: 'cannot use this'});
|
|
|
|
User.create({name: 'bob'}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({name: /cannot use this/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a null value when allowNull is false', function(done) {
|
|
|
|
User.validatesExclusionOf('name', {in: ['bob'], allowNull: false});
|
|
|
|
User.create({name: null}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({name: /is null/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes with a null value when allowNull is true', function(done) {
|
|
|
|
User.validatesExclusionOf('name', {in: ['bob'], allowNull: true});
|
|
|
|
User.create({name: null}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if value is used for integer property', function(done) {
|
|
|
|
User.validatesExclusionOf('age', {in: [123, 456]});
|
|
|
|
User.create({age: 123}, function(err) {
|
|
|
|
err.should.be.instanceof(Error);
|
|
|
|
err.details.messages.should.match({age: /is reserved/});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate exclusion on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesExclusionOf('name', {in: ['Bob']});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo-new', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: 'Bob', age: 5},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.name[0], 'is reserved');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('length', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should validate length');
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
describe('validate length on update', function() {
|
|
|
|
before(function(done) {
|
|
|
|
Employee.destroyAll(function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
delete Employee.validations;
|
|
|
|
db.automigrate('Employee', function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Employee.create(empData, function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(inst);
|
|
|
|
Employee.validatesLengthOf('name', {min: 5});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeeds when validate condition is met', function(done) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const data = {name: 'Foo-new', age: 5};
|
2017-07-31 17:53:43 +00:00
|
|
|
Employee.updateAll({id: 1}, data,
|
|
|
|
function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
should.equal(emp.count, 1);
|
|
|
|
Employee.find({where: {id: 1}}, function(err, emp) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(emp);
|
|
|
|
data.id = 1;
|
|
|
|
should.deepEqual(data, emp[0].toObject());
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws err when validate condition is not met', function(done) {
|
|
|
|
Employee.updateAll({where: {id: 1}}, {name: 'Bob', age: 5},
|
|
|
|
function(err, emp) {
|
|
|
|
should.exist(err);
|
2017-08-02 18:55:47 +00:00
|
|
|
should.not.exist(emp);
|
2017-07-31 17:53:43 +00:00
|
|
|
should.equal(err.statusCode, 422);
|
|
|
|
should.equal(err.details.messages.name[0], 'too short');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('custom', function() {
|
2014-07-11 20:56:02 +00:00
|
|
|
it('should validate using custom sync validation', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
User.validate('email', function(err) {
|
2014-07-11 20:56:02 +00:00
|
|
|
if (this.email === 'hello') err();
|
2016-08-19 17:46:59 +00:00
|
|
|
}, {code: 'invalid-email'});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hello'});
|
2017-07-26 07:22:11 +00:00
|
|
|
Boolean(u.isValid()).should.be.false();
|
2016-08-19 17:46:59 +00:00
|
|
|
u.errors.codes.should.eql({email: ['invalid-email']});
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-07-11 20:56:02 +00:00
|
|
|
it('should validate and return detailed error messages', function() {
|
2016-04-01 11:48:17 +00:00
|
|
|
User.validate('global', function(err) {
|
2014-07-11 20:56:02 +00:00
|
|
|
if (this.email === 'hello' || this.email === 'hey') {
|
2014-07-11 21:03:07 +00:00
|
|
|
this.errors.add('email', 'Cannot be `' + this.email + '`', 'invalid-email');
|
2014-07-11 20:56:02 +00:00
|
|
|
err(false); // false: prevent global error message
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hello'});
|
2017-07-26 07:22:11 +00:00
|
|
|
Boolean(u.isValid()).should.be.false();
|
2018-05-24 21:16:15 +00:00
|
|
|
u.errors.should.containEql({email: ['Cannot be `hello`']});
|
2016-08-19 17:46:59 +00:00
|
|
|
u.errors.codes.should.eql({email: ['invalid-email']});
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-07-11 20:56:02 +00:00
|
|
|
it('should validate using custom async validation', function(done) {
|
2016-04-01 11:48:17 +00:00
|
|
|
User.validateAsync('email', function(err, next) {
|
2014-07-11 20:56:02 +00:00
|
|
|
process.nextTick(next);
|
2015-01-12 15:50:51 +00:00
|
|
|
}, {
|
2014-07-11 20:56:02 +00:00
|
|
|
if: function() { return true; },
|
2016-04-01 11:48:17 +00:00
|
|
|
unless: function() { return false; },
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({email: 'hello'});
|
2016-04-01 11:48:17 +00:00
|
|
|
Boolean(u.isValid(function(valid) {
|
2017-07-26 07:22:11 +00:00
|
|
|
valid.should.be.true();
|
2014-07-11 20:56:02 +00:00
|
|
|
done();
|
2017-07-26 07:22:11 +00:00
|
|
|
})).should.be.false();
|
2014-07-11 20:56:02 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-01-12 13:52:54 +00:00
|
|
|
|
|
|
|
describe('invalid value formatting', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let origMaxLen;
|
2015-01-12 13:52:54 +00:00
|
|
|
beforeEach(function saveAndSetMaxLen() {
|
|
|
|
origMaxLen = ValidationError.maxPropertyStringLength;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function restoreMaxLen() {
|
|
|
|
ValidationError.maxPropertyStringLength = origMaxLen;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate long strings', function() {
|
|
|
|
ValidationError.maxPropertyStringLength = 9;
|
2018-12-07 14:54:29 +00:00
|
|
|
const err = givenValidationError('prop', '1234567890abc', 'is invalid');
|
2015-01-12 13:52:54 +00:00
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: "12...abc").');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate long objects', function() {
|
|
|
|
ValidationError.maxPropertyStringLength = 12;
|
2018-12-07 14:54:29 +00:00
|
|
|
const err = givenValidationError('prop', {foo: 'bar'}, 'is invalid');
|
2015-01-12 13:52:54 +00:00
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: { foo:... }).');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate long arrays', function() {
|
|
|
|
ValidationError.maxPropertyStringLength = 12;
|
2018-12-07 14:54:29 +00:00
|
|
|
const err = givenValidationError('prop', [{a: 1, b: 2}], 'is invalid');
|
2015-01-12 13:52:54 +00:00
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: [ { a...} ]).');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should print only top-level object properties', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const err = givenValidationError('prop', {a: {b: 'c'}}, 'is invalid');
|
2015-01-12 13:52:54 +00:00
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: { a: [Object] }).');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should print only top-level props of objects in array', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const err = givenValidationError('prop', [{a: {b: 'c'}}], 'is invalid');
|
2015-01-12 13:52:54 +00:00
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: [ { a: [Object] } ]).');
|
|
|
|
});
|
|
|
|
|
2015-01-12 15:50:51 +00:00
|
|
|
it('should exclude colors from Model values', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const obj = new User();
|
2015-01-12 15:50:51 +00:00
|
|
|
obj.email = 'test@example.com';
|
2018-12-07 14:54:29 +00:00
|
|
|
const err = givenValidationError('user', obj, 'is invalid');
|
2015-01-12 15:50:51 +00:00
|
|
|
getErrorDetails(err).should.equal(
|
2018-07-16 06:46:25 +00:00
|
|
|
'`user` is invalid (value: { email: \'test@example.com\' }).'
|
|
|
|
);
|
2015-01-12 15:50:51 +00:00
|
|
|
});
|
|
|
|
|
2015-01-12 13:52:54 +00:00
|
|
|
function givenValidationError(propertyName, propertyValue, errorMessage) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const jsonVal = {};
|
2015-01-12 13:52:54 +00:00
|
|
|
jsonVal[propertyName] = propertyValue;
|
2018-12-07 14:54:29 +00:00
|
|
|
const errorVal = {};
|
2015-01-12 13:52:54 +00:00
|
|
|
errorVal[propertyName] = [errorMessage];
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const obj = {
|
2015-01-12 13:52:54 +00:00
|
|
|
errors: errorVal,
|
2016-04-01 11:48:17 +00:00
|
|
|
toJSON: function() { return jsonVal; },
|
2015-01-12 13:52:54 +00:00
|
|
|
};
|
|
|
|
return new ValidationError(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getErrorDetails(err) {
|
|
|
|
return err.message.replace(/^.*Details: /, '');
|
|
|
|
}
|
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
2017-04-21 18:12:04 +00:00
|
|
|
describe('date', function() {
|
|
|
|
it('should validate a date object', function() {
|
|
|
|
User.validatesDateOf('updatedAt');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({updatedAt: new Date()});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2017-04-21 18:12:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate a date string', function() {
|
|
|
|
User.validatesDateOf('updatedAt');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({updatedAt: '2000-01-01'});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2017-04-21 18:12:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate a null date', function() {
|
|
|
|
User.validatesDateOf('updatedAt');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({updatedAt: null});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2017-04-21 18:12:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate an undefined date', function() {
|
|
|
|
User.validatesDateOf('updatedAt');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({updatedAt: undefined});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.be.true();
|
2017-04-21 18:12:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate an invalid date string', function() {
|
|
|
|
User.validatesDateOf('updatedAt');
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({updatedAt: 'invalid date string'});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.not.be.true();
|
2018-05-24 21:16:15 +00:00
|
|
|
u.errors.should.containEql({
|
2017-04-21 18:12:04 +00:00
|
|
|
updatedAt: ['is not a valid date'],
|
|
|
|
codes: {
|
|
|
|
updatedAt: ['date'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should attach validation by default to all date properties', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const AnotherUser = db.define('User', {
|
2017-04-21 18:12:04 +00:00
|
|
|
email: String,
|
|
|
|
name: String,
|
|
|
|
password: String,
|
|
|
|
state: String,
|
|
|
|
age: Number,
|
|
|
|
gender: String,
|
|
|
|
domain: String,
|
|
|
|
pendingPeriod: Number,
|
|
|
|
createdByAdmin: Boolean,
|
|
|
|
createdByScript: Boolean,
|
|
|
|
updatedAt: Date,
|
|
|
|
});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new AnotherUser({updatedAt: 'invalid date string'});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.not.be.true();
|
2018-05-24 21:16:15 +00:00
|
|
|
u.errors.should.containEql({
|
2017-04-21 18:12:04 +00:00
|
|
|
updatedAt: ['is not a valid date'],
|
|
|
|
codes: {
|
|
|
|
updatedAt: ['date'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should overwrite default blank message with custom format message', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const CUSTOM_MESSAGE = 'custom validation message';
|
2017-04-21 18:12:04 +00:00
|
|
|
User.validatesDateOf('updatedAt', {message: CUSTOM_MESSAGE});
|
2018-12-07 14:54:29 +00:00
|
|
|
const u = new User({updatedAt: 'invalid date string'});
|
2017-07-26 07:22:11 +00:00
|
|
|
u.isValid().should.not.be.true();
|
2018-05-24 21:16:15 +00:00
|
|
|
u.errors.should.containEql({
|
2017-04-21 18:12:04 +00:00
|
|
|
updatedAt: [CUSTOM_MESSAGE],
|
|
|
|
codes: {
|
|
|
|
updatedAt: ['date'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
2017-07-31 17:53:43 +00:00
|
|
|
|
|
|
|
var empData = [{
|
|
|
|
id: 1,
|
|
|
|
name: 'Foo',
|
|
|
|
age: 1,
|
|
|
|
}, {
|
|
|
|
id: 2,
|
|
|
|
name: 'Bar',
|
|
|
|
age: 2,
|
|
|
|
}, {
|
|
|
|
id: 3,
|
|
|
|
name: 'Baz',
|
|
|
|
age: 3,
|
|
|
|
}];
|