From c5b1ffbd37360f5c86ced9cb8c85e1f765c6c300 Mon Sep 17 00:00:00 2001 From: Tino Butz Date: Fri, 8 Nov 2013 22:59:37 +0100 Subject: [PATCH] Added test for "Allow to define a type for schema validators" --- test/schema.validation.test.js | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/schema.validation.test.js b/test/schema.validation.test.js index b413ea7c74e..f3013a19eb7 100644 --- a/test/schema.validation.test.js +++ b/test/schema.validation.test.js @@ -459,5 +459,46 @@ describe('schema', function(){ }) }) }) + + describe('types', function(){ + describe('are customizable', function(){ + it('for single custom validators', function(done){ + function validate () { + return false; + } + var validator = [validate, '{PATH} failed validation ({VALUE})', 'customType']; + + var schema = new Schema({ x: { type: [], validate: validator }}); + var M = mongoose.model('custom-validator-'+random(), schema); + + var m = new M({ x: [3,4,5,6] }); + + m.validate(function (err) { + assert.equal('x failed validation (3,4,5,6)', String(err.errors.x)); + assert.equal('customType', err.errors.x.type); + done(); + }) + }) + + it('for many custom validators', function(done){ + function validate () { + return false; + } + var validator = [ + { validator: validate, msg: '{PATH} failed validation ({VALUE})', 'customType'} + ] + var schema = new Schema({ x: { type: [], validate: validator }}); + var M = mongoose.model('custom-validator-'+random(), schema); + + var m = new M({ x: [3,4,5,6] }); + + m.validate(function (err) { + assert.equal('x failed validation (3,4,5,6)', String(err.errors.x)); + assert.equal('customType', err.errors.x.type); + done(); + }) + }) + }) + }) }); });