2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
|
|
|
var should = require('./init.js');
|
2014-05-14 18:38:40 +00:00
|
|
|
var async = require('async');
|
2013-04-06 10:57:12 +00:00
|
|
|
|
2013-03-24 22:21:18 +00:00
|
|
|
var j = require('../'), db, User;
|
2013-11-25 16:20:05 +00:00
|
|
|
var 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,
|
|
|
|
createdByScript: true
|
|
|
|
};
|
2013-03-24 22:21:18 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('validations', function () {
|
|
|
|
|
2015-07-13 10:56:25 +00:00
|
|
|
var User, Entry;
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
before(function (done) {
|
|
|
|
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,
|
|
|
|
updatedAt: Date
|
2013-03-29 07:43:04 +00:00
|
|
|
});
|
2015-07-13 10:56:25 +00:00
|
|
|
Entry = db.define('Entry', {
|
|
|
|
id: { type: 'string', id: true, generated: false },
|
|
|
|
name: { type: 'string' }
|
|
|
|
});
|
|
|
|
Entry.validatesUniquenessOf('id');
|
2014-01-24 17:09:53 +00:00
|
|
|
db.automigrate(done);
|
|
|
|
});
|
2013-03-29 07:43:04 +00:00
|
|
|
|
2014-01-24 17:09:53 +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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
after(function () {
|
2014-03-27 20:56:24 +00:00
|
|
|
// db.disconnect();
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-29 07:43:04 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('commons', function () {
|
2013-04-01 16:08:22 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('skipping', function () {
|
2013-04-01 16:08:22 +00:00
|
|
|
|
2014-11-19 13:50:08 +00:00
|
|
|
it('should NOT skip when `if` is fulfilled', function () {
|
2014-01-24 17:09:53 +00:00
|
|
|
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = true;
|
|
|
|
user.isValid().should.be.false;
|
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
2014-11-19 13:50:08 +00:00
|
|
|
user.pendingPeriod = 1;
|
2014-01-24 17:09:53 +00:00
|
|
|
user.isValid().should.be.true;
|
|
|
|
});
|
2013-04-01 16:08:22 +00:00
|
|
|
|
2014-11-19 13:50:08 +00:00
|
|
|
it('should skip when `if` is NOT fulfilled', function () {
|
|
|
|
User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = false;
|
|
|
|
user.isValid().should.be.true;
|
|
|
|
user.errors.should.be.false;
|
|
|
|
user.pendingPeriod = 1;
|
|
|
|
user.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT skip when `unless` is fulfilled', function () {
|
|
|
|
User.validatesPresenceOf('pendingPeriod', {unless: 'createdByAdmin'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = false;
|
|
|
|
user.isValid().should.be.false;
|
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
|
|
|
user.pendingPeriod = 1;
|
|
|
|
user.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip when `unless` is NOT fulfilled', function () {
|
|
|
|
User.validatesPresenceOf('pendingPeriod', {unless: 'createdByAdmin'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = true;
|
|
|
|
user.isValid().should.be.true;
|
|
|
|
user.errors.should.be.false;
|
|
|
|
user.pendingPeriod = 1;
|
|
|
|
user.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('skipping in async validation', function () {
|
|
|
|
|
|
|
|
it('should skip when `if` is NOT fulfilled', function (done) {
|
|
|
|
User.validateAsync('pendingPeriod', function (err, done) {
|
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
|
|
|
}, {if: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = false;
|
|
|
|
user.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
user.errors.should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT skip when `if` is fulfilled', function (done) {
|
|
|
|
User.validateAsync('pendingPeriod', function (err, done) {
|
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
|
|
|
}, {if: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = true;
|
|
|
|
user.isValid(function (valid) {
|
|
|
|
valid.should.be.false;
|
|
|
|
user.errors.pendingPeriod.should.eql(['can\'t be blank']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip when `unless` is NOT fulfilled', function (done) {
|
|
|
|
User.validateAsync('pendingPeriod', function (err, done) {
|
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
|
|
|
}, {unless: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = true;
|
|
|
|
user.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
user.errors.should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should NOT skip when `unless` is fulfilled', function (done) {
|
|
|
|
User.validateAsync('pendingPeriod', function (err, done) {
|
|
|
|
if (!this.pendingPeriod) err();
|
|
|
|
done();
|
|
|
|
}, {unless: 'createdByAdmin', code: 'presence', message: 'can\'t be blank'});
|
|
|
|
var user = new User;
|
|
|
|
user.createdByAdmin = false;
|
|
|
|
user.isValid(function (valid) {
|
|
|
|
valid.should.be.false;
|
|
|
|
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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('lifecycle', function () {
|
2013-04-01 16:10:46 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
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');
|
|
|
|
User.create(function (e, u) {
|
|
|
|
should.exist(e);
|
|
|
|
User.create({name: 'Valid'}, function (e, d) {
|
|
|
|
should.not.exist(e);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
User.create({name: 'Valid'}, function (e, d) {
|
|
|
|
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();
|
2013-04-04 15:31:07 +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');
|
|
|
|
// It's important to pass an id value, otherwise DAO falls back
|
|
|
|
// to regular create()
|
|
|
|
User.updateOrCreate({ id: 999 }, done);
|
|
|
|
});
|
|
|
|
|
2015-05-05 06:24:08 +00:00
|
|
|
it('should be skipped by upsert when disabled via settings', function(done) {
|
|
|
|
var Customer = User.extend('Customer');
|
|
|
|
Customer.attachTo(db);
|
|
|
|
db.autoupdate(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
Customer.prototype.isValid = function() {
|
|
|
|
throw new Error('isValid() should not be called at all');
|
|
|
|
};
|
|
|
|
Customer.settings.validateUpsert = false;
|
|
|
|
// It's important to pass an id value, otherwise DAO falls back
|
|
|
|
// to regular create()
|
|
|
|
Customer.updateOrCreate({ id: 999 }, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-16 07:03:56 +00:00
|
|
|
it('should work on upsert when enabled via settings', function(done) {
|
|
|
|
delete User.validations;
|
|
|
|
User.validatesPresenceOf('name');
|
|
|
|
User.settings.validateUpsert = true;
|
|
|
|
// It's important to pass an id value, otherwise DAO falls back
|
|
|
|
// to regular create()
|
|
|
|
User.upsert({ id: 999 }, function(err, u) {
|
|
|
|
if (!err) return done(new Error('Validation should have failed.'));
|
|
|
|
err.should.be.instanceOf(ValidationError);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +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');
|
|
|
|
User.create(function (e, u) {
|
|
|
|
should.exist(e);
|
|
|
|
e.details.codes.name.should.eql(['presence']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-04-04 15:31:07 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should allow to modify error after validation', function (done) {
|
|
|
|
User.afterValidate = function (next) {
|
|
|
|
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');
|
|
|
|
User.create(function (e, u) {
|
|
|
|
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');
|
|
|
|
User.create(function (e, u) {
|
|
|
|
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');
|
|
|
|
User.create(function (e, u) {
|
|
|
|
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() {
|
|
|
|
var 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');
|
2014-08-26 15:51:01 +00:00
|
|
|
var 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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('presence', function () {
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should validate presence', function () {
|
|
|
|
User.validatesPresenceOf('name', 'email');
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-09-05 16:00:11 +00:00
|
|
|
var validations = User.validations;
|
|
|
|
validations.name.should.eql([{validation: 'presence', options: {}}]);
|
|
|
|
validations.email.should.eql([{validation: 'presence', options: {}}]);
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var u = new User;
|
|
|
|
u.isValid().should.not.be.true;
|
|
|
|
u.name = 1;
|
2014-09-05 16:00:11 +00:00
|
|
|
u.isValid().should.not.be.true;
|
2014-01-24 17:09:53 +00:00
|
|
|
u.email = 2;
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
});
|
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');
|
|
|
|
var u = new User();
|
|
|
|
u.isValid().should.be.false;
|
|
|
|
u.age = NaN;
|
|
|
|
u.isValid().should.be.false;
|
|
|
|
u.age = 1;
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow "NaN" value as a string', function() {
|
|
|
|
User.validatesPresenceOf('name');
|
|
|
|
var u = new User();
|
|
|
|
u.isValid().should.be.false;
|
|
|
|
u.name = 'NaN';
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should skip validation by property (if/unless)', function () {
|
|
|
|
User.validatesPresenceOf('domain', {unless: 'createdByScript'});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
var user = new User(getValidAttributes())
|
|
|
|
user.isValid().should.be.true;
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
user.createdByScript = false;
|
|
|
|
user.isValid().should.be.false;
|
|
|
|
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';
|
|
|
|
user.isValid().should.be.true;
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-07-11 20:56:02 +00:00
|
|
|
describe('absence', function () {
|
|
|
|
|
|
|
|
it('should validate absence', function () {
|
|
|
|
User.validatesAbsenceOf('reserved', { if: 'locked' });
|
|
|
|
var u = new User({reserved: 'foo', locked: true});
|
|
|
|
u.isValid().should.not.be.true;
|
|
|
|
u.reserved = null;
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
var u = new User({reserved: 'foo', locked: false});
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
describe('uniqueness', function () {
|
|
|
|
it('should validate uniqueness', function (done) {
|
|
|
|
User.validatesUniquenessOf('email');
|
|
|
|
var u = new User({email: 'hey'});
|
|
|
|
Boolean(u.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
u.save(function () {
|
|
|
|
var u2 = new User({email: 'hey'});
|
|
|
|
u2.isValid(function (valid) {
|
|
|
|
valid.should.be.false;
|
|
|
|
done();
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
})).should.be.false;
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should handle same object modification', function (done) {
|
|
|
|
User.validatesUniquenessOf('email');
|
|
|
|
var u = new User({email: 'hey'});
|
|
|
|
Boolean(u.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
u.save(function () {
|
|
|
|
u.name = 'Goghi';
|
|
|
|
u.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
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) {
|
|
|
|
var EMAIL = 'user@xample.com';
|
|
|
|
var SiteUser = db.define('SiteUser', {
|
|
|
|
siteId: String,
|
|
|
|
email: String
|
|
|
|
});
|
|
|
|
SiteUser.validatesUniquenessOf('email', { scopedTo: ['siteId'] });
|
|
|
|
async.waterfall([
|
|
|
|
function automigrate(next) {
|
|
|
|
db.automigrate(next);
|
|
|
|
},
|
|
|
|
function createSite1User(next) {
|
|
|
|
SiteUser.create(
|
|
|
|
{ siteId: 1, email: EMAIL },
|
|
|
|
next);
|
|
|
|
},
|
|
|
|
function createSite2User(user1, next) {
|
|
|
|
SiteUser.create(
|
|
|
|
{ siteId: 2, email: EMAIL },
|
|
|
|
next);
|
|
|
|
},
|
|
|
|
function validateDuplicateUser(user2, next) {
|
|
|
|
var user3 = new SiteUser({ siteId: 1, email: EMAIL });
|
|
|
|
user3.isValid(function(valid) {
|
|
|
|
valid.should.be.false;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function(err) {
|
|
|
|
if (err && err.name == 'ValidationError') {
|
|
|
|
console.error('ValidationError:', err.details.messages);
|
|
|
|
}
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-07-11 20:07:57 +00:00
|
|
|
it('should skip blank values', function (done) {
|
|
|
|
User.validatesUniquenessOf('email');
|
|
|
|
var u = new User({email: ' '});
|
|
|
|
Boolean(u.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
u.save(function () {
|
|
|
|
var u2 = new User({email: null});
|
|
|
|
u2.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})).should.be.false;
|
|
|
|
});
|
2015-01-12 15:50:51 +00:00
|
|
|
|
2014-07-11 20:56:02 +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; },
|
|
|
|
unless: function() { return false; }
|
|
|
|
});
|
|
|
|
var u = new User({email: 'hello'});
|
|
|
|
Boolean(u.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
done();
|
|
|
|
})).should.be.false;
|
|
|
|
});
|
2015-07-13 10:56:25 +00:00
|
|
|
|
|
|
|
it('should work with id property on create', function (done) {
|
|
|
|
Entry.create({ id: 'entry' }, function(err, entry) {
|
|
|
|
var e = new Entry({ id: 'entry' });
|
|
|
|
Boolean(e.isValid(function (valid) {
|
|
|
|
valid.should.be.false;
|
|
|
|
done();
|
|
|
|
})).should.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work with id property after create', function (done) {
|
|
|
|
Entry.findById('entry', function(err, e) {
|
|
|
|
Boolean(e.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
done();
|
|
|
|
})).should.be.false;
|
|
|
|
});
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('format', function () {
|
|
|
|
it('should validate format');
|
|
|
|
it('should overwrite default blank message with custom format message');
|
2015-01-27 09:02:07 +00:00
|
|
|
|
|
|
|
it('should skip missing values when allowing null', function () {
|
|
|
|
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/, allowNull: true });
|
|
|
|
var u = new User({});
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should skip null values when allowing null', function () {
|
|
|
|
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/, allowNull: true });
|
|
|
|
var u = new User({ email: null });
|
|
|
|
u.isValid().should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not skip missing values', function () {
|
|
|
|
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/ });
|
|
|
|
var u = new User({});
|
|
|
|
u.isValid().should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not skip null values', function () {
|
|
|
|
User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/ });
|
|
|
|
var u = new User({ email: null });
|
|
|
|
u.isValid().should.be.false;
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('numericality', function () {
|
|
|
|
it('should validate numericality');
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('inclusion', function () {
|
|
|
|
it('should validate inclusion');
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('exclusion', function () {
|
|
|
|
it('should validate exclusion');
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('length', function () {
|
|
|
|
it('should validate length');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('custom', function () {
|
2014-07-11 20:56:02 +00:00
|
|
|
it('should validate using custom sync validation', function() {
|
|
|
|
User.validate('email', function (err) {
|
|
|
|
if (this.email === 'hello') err();
|
2014-07-11 21:03:07 +00:00
|
|
|
}, { code: 'invalid-email' });
|
2014-07-11 20:56:02 +00:00
|
|
|
var u = new User({email: 'hello'});
|
|
|
|
Boolean(u.isValid()).should.be.false;
|
2014-07-11 21:03:07 +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() {
|
|
|
|
User.validate('global', function (err) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var u = new User({email: 'hello'});
|
|
|
|
Boolean(u.isValid()).should.be.false;
|
2014-07-11 21:03:07 +00:00
|
|
|
u.errors.should.eql({ email: ['Cannot be `hello`'] });
|
|
|
|
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) {
|
|
|
|
User.validateAsync('email', function (err, next) {
|
|
|
|
process.nextTick(next);
|
2015-01-12 15:50:51 +00:00
|
|
|
}, {
|
2014-07-11 20:56:02 +00:00
|
|
|
if: function() { return true; },
|
|
|
|
unless: function() { return false; }
|
|
|
|
});
|
|
|
|
var u = new User({email: 'hello'});
|
|
|
|
Boolean(u.isValid(function (valid) {
|
|
|
|
valid.should.be.true;
|
|
|
|
done();
|
|
|
|
})).should.be.false;
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-01-12 13:52:54 +00:00
|
|
|
|
|
|
|
describe('invalid value formatting', function() {
|
|
|
|
var origMaxLen;
|
|
|
|
beforeEach(function saveAndSetMaxLen() {
|
|
|
|
origMaxLen = ValidationError.maxPropertyStringLength;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function restoreMaxLen() {
|
|
|
|
ValidationError.maxPropertyStringLength = origMaxLen;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate long strings', function() {
|
|
|
|
ValidationError.maxPropertyStringLength = 9;
|
|
|
|
var err = givenValidationError('prop', '1234567890abc', 'is invalid');
|
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: "12...abc").');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate long objects', function() {
|
|
|
|
ValidationError.maxPropertyStringLength = 12;
|
|
|
|
var err = givenValidationError('prop', { foo: 'bar' }, 'is invalid');
|
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: { foo:... }).');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should truncate long arrays', function() {
|
|
|
|
ValidationError.maxPropertyStringLength = 12;
|
|
|
|
var err = givenValidationError('prop', [{ a: 1, b: 2}], 'is invalid');
|
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: [ { a...} ]).');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should print only top-level object properties', function() {
|
|
|
|
var err = givenValidationError('prop', { a: { b: 'c' }}, 'is invalid');
|
|
|
|
getErrorDetails(err)
|
|
|
|
.should.equal('`prop` is invalid (value: { a: [Object] }).');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should print only top-level props of objects in array', function() {
|
|
|
|
var err = givenValidationError('prop', [{ a: { b: 'c' }}], 'is invalid');
|
|
|
|
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() {
|
|
|
|
var obj = new User();
|
|
|
|
obj.email = 'test@example.com';
|
|
|
|
var err = givenValidationError('user', obj, 'is invalid');
|
|
|
|
getErrorDetails(err).should.equal(
|
|
|
|
'`user` is invalid (value: { email: \'test@example.com\' }).');
|
|
|
|
});
|
|
|
|
|
2015-01-12 13:52:54 +00:00
|
|
|
function givenValidationError(propertyName, propertyValue, errorMessage) {
|
|
|
|
var jsonVal = {};
|
|
|
|
jsonVal[propertyName] = propertyValue;
|
|
|
|
var errorVal = {};
|
|
|
|
errorVal[propertyName] = [errorMessage];
|
|
|
|
|
|
|
|
var obj = {
|
|
|
|
errors: errorVal,
|
|
|
|
toJSON: function() { return jsonVal; }
|
|
|
|
};
|
|
|
|
return new ValidationError(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getErrorDetails(err) {
|
|
|
|
return err.message.replace(/^.*Details: /, '');
|
|
|
|
}
|
|
|
|
});
|
2013-03-24 22:21:18 +00:00
|
|
|
});
|