-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
206 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module.exports = function(mongoose) { | ||
var SchemaNumber = mongoose.SchemaTypes.Number, | ||
errorMessages = mongoose.Error.messages; | ||
|
||
/** | ||
* Sets a minimum number validator not including the configurated value. | ||
* | ||
* ####Example: | ||
* | ||
* var s = new Schema({ n: { type: Number, exclusivemin: 10 }) | ||
* var M = db.model('M', s) | ||
* var m = new M({ n: 9 }) | ||
* m.save(function (err) { | ||
* console.error(err) // validator error | ||
* m.n = 10; | ||
* m.save() // success | ||
* }) | ||
* | ||
* // custom error messages | ||
* // We can also use the special {EXCLUSIVE_MIN} token which will be replaced with the invalid value | ||
* var min = [10, 'The value of path `{PATH}` ({VALUE}) should be greater than ({EXCLUSIVE_MIN}).']; | ||
* var schema = new Schema({ n: { type: Number, min: min }) | ||
* var M = mongoose.model('Measurement', schema); | ||
* var s= new M({ n: 4 }); | ||
* s.validate(function (err) { | ||
* console.log(String(err)) // ValidationError: The value of path `n` (4) should be greater than 10. | ||
* }) | ||
* | ||
* @param {Number} value minimum number | ||
* @param {String} [message] optional custom error message | ||
* @return {SchemaType} this | ||
* @see Customized Error Messages #error_messages_MongooseError-messages | ||
* @api public | ||
*/ | ||
|
||
SchemaNumber.prototype.exclusivemin = function (value, message) { | ||
if (this.exclusiveminValidator) { | ||
this.validators = this.validators.filter(function (v) { | ||
return v.validator !== this.exclusiveminValidator; | ||
}, this); | ||
} | ||
|
||
if (null !== value) { | ||
var msg = message || errorMessages.Number.exclusivemin; | ||
msg = msg.replace(/{EXCLUSIVE_MIN}/, value); | ||
this.validators.push([this.exclusiveminValidator = function (v) { | ||
return v === null || v === undefined || v > value; | ||
}, msg, 'exclusivemin']); | ||
} | ||
|
||
return this; | ||
}; | ||
|
||
errorMessages.Number.exclusivemin = 'Path `{PATH}` ({VALUE}) should be greater than {EXCLUSIVE_MIN}.'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
var db = require('db'), | ||
mongoose = db.mongoose, | ||
should = require('should'); | ||
|
||
describe('Number.exclusiveMin:', function() { | ||
var TestDoc; | ||
|
||
before('init db', db.init); | ||
before('load extensions', db.loadExtensions); | ||
before('load test model', function(done) { | ||
var TestDocSchema = new mongoose.Schema({ | ||
field01: Number, | ||
field02: { | ||
type: Number, | ||
exclusivemin: 5 | ||
}, | ||
field03: { | ||
type: Number, | ||
exclusivemin: [10, 'Invalid number value'] | ||
}, | ||
field04: { | ||
type: Number, | ||
exclusivemin: [20, 'Path {PATH} ({VALUE}) out of minimum limit {EXCLUSIVE_MIN}'] | ||
} | ||
}); | ||
TestDoc = mongoose.model('TestDoc', TestDocSchema); | ||
done(); | ||
}); | ||
|
||
it('should not impact normal Number types', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({field01: 1}); | ||
tst.save(function(err, tst) { | ||
if(err) { | ||
return done(err); | ||
} | ||
should(tst.field01).be.eql(1); | ||
done(); | ||
}); | ||
}); | ||
it('should not throw exclusiveMin error for bigger number', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({field02: 5.0009}); | ||
tst.save(function(err, tst) { | ||
if(err) { | ||
return done(err); | ||
} | ||
should(tst.field02).be.eql(5.0009); | ||
done(); | ||
}); | ||
}); | ||
it('should not throw exclusiveMin error for empty values', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({field01: 1}); | ||
tst.save(function(err, tst) { | ||
if(err) { | ||
return done(err); | ||
} | ||
should(tst.field01).be.eql(1); | ||
should(tst.field02).be.not.ok; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should throw exclusiveMin default error message', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({field02: 1}); | ||
tst.save(function(err) { | ||
should(err).be.ok; | ||
should(err.message).be.eql('Validation failed'); | ||
should(err.name).be.eql('ValidationError'); | ||
should(err.errors.field02).be.ok; | ||
should(err.errors.field02.message).be.eql( | ||
'Path `field02` (1) should be greater than 5.' | ||
); | ||
done(); | ||
}); | ||
}); | ||
it('should throw exclusiveMin error for exact value', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({field02: 5}); | ||
tst.save(function(err) { | ||
should(err).be.ok; | ||
should(err.message).be.eql('Validation failed'); | ||
should(err.name).be.eql('ValidationError'); | ||
should(err.errors.field02).be.ok; | ||
should(err.errors.field02.message).be.eql( | ||
'Path `field02` (5) should be greater than 5.' | ||
); | ||
done(); | ||
}); | ||
}); | ||
it('should throw exclusiveMin custom error message', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({ | ||
field02: 1, | ||
field03: 9 | ||
}); | ||
tst.save(function(err) { | ||
should(err).be.ok; | ||
should(err.message).be.eql('Validation failed'); | ||
should(err.name).be.eql('ValidationError'); | ||
should(err.errors.field02).be.ok; | ||
should(err.errors.field02.message).be.eql( | ||
'Path `field02` (1) should be greater than 5.' | ||
); | ||
should(err.errors.field03).be.ok; | ||
should(err.errors.field03.message).be.eql('Invalid number value'); | ||
done(); | ||
}); | ||
}); | ||
it('should throw exclusiveMin custom error with special tokens replaced', function(done) { | ||
this.timeout(5000); | ||
var tst = new TestDoc({field04: 15}); | ||
tst.save(function(err) { | ||
should(err).be.ok; | ||
should(err.message).be.eql('Validation failed'); | ||
should(err.name).be.eql('ValidationError'); | ||
should(err.errors.field04).be.ok; | ||
should(err.errors.field04.message).be.eql( | ||
'Path field04 (15) out of minimum limit 20' | ||
); | ||
done(); | ||
}); | ||
}); | ||
|
||
after('finish db', db.finish); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters