Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow to define a type for custom SchemaType validators #1775

Merged
merged 3 commits into from

4 participants

@tjbutz

In previews releases the type of SchemaType validators used to equal the validator message. In the latest release the type is now "user defined". In the past I was able to distinguish all validation errors (build in and custom) by the type. Now I get for custom validation errors "user defined". This fix allows to define a type for custom SchemaType validators.

@mongoose-benchmarks

Benchmarks for 94f9996:
insert

Insert - Mongoose - Basic x 272 ops/sec ±53.57% (68 runs sampled)
Insert - Driver - Basic x 550 ops/sec ±12.92% (79 runs sampled)
Insert - Mongoose - Embedded Docs x 103 ops/sec ±8.73% (54 runs sampled)
Insert - Driver - Embedded Docs x 467 ops/sec ±11.22% (78 runs sampled)

delete

Delete - Mongoose x 579 ops/sec ±1.77% (87 runs sampled)
Delete - Driver x 627 ops/sec ±0.75% (88 runs sampled)

update

Update - Mongoose - Basic x 501 ops/sec ±5.36% (83 runs sampled)
Update - Driver - Basic x 587 ops/sec ±1.20% (86 runs sampled)
Update - Mongoose - Embedded Docs x 86.63 ops/sec ±7.83% (51 runs sampled)
Update - Driver - Embdedded Docs x 429 ops/sec ±2.04% (86 runs sampled)
Update - Mongoose - Multiple Documents x 295 ops/sec ±6.28% (77 runs sampled)
Update - Driver - Multiple Documents x 471 ops/sec ±9.43% (81 runs sampled)
Update - Mongoose - pop and push x 28.73 ops/sec ±10.13% (52 runs sampled)
Update - Mongoose - Array Manipulation, parallel ops x 264 ops/sec ±5.01% (71 runs sampled)
Update - Mongoose - findOneAndModify x 234 ops/sec ±9.73% (59 runs sampled)
Update - Mongoose - find and update, separate ops x 252 ops/sec ±8.94% (60 runs sampled)

read

Read - Mongoose - Basic x 586 ops/sec ±1.82% (84 runs sampled)
Read - Driver - Basic x 687 ops/sec ±0.87% (88 runs sampled)
Read - Mongoose - With lean x 613 ops/sec ±2.25% (86 runs sampled)
Read - Mongoose - Multiple Items x 169 ops/sec ±5.31% (60 runs sampled)
Read - Driver - Multiple Items x 484 ops/sec ±3.27% (83 runs sampled)
Read - Mongoose - Non-index x 204 ops/sec ±5.38% (62 runs sampled)
Read - Driver - Non-index x 456 ops/sec ±0.68% (88 runs sampled)
Read - Mongoose - Embedded Docs x 301 ops/sec ±7.89% (70 runs sampled)
Read - Driver - Embedded Docs x 667 ops/sec ±1.10% (87 runs sampled)

casting

Casting - Embedded Docs - 0 Docs x 2,959 ops/sec ±8.75% (49 runs sampled)
Casting - Embedded Docs - 10 Docs x 3,210 ops/sec ±8.27% (48 runs sampled)
Casting - Embedded Docs - 100 Docs x 2,868 ops/sec ±9.03% (46 runs sampled)
Casting - Embedded Docs - 1000 Docs x 1,680 ops/sec ±11.40% (39 runs sampled)
Casting - Embedded Docs - 10000 Docs x 439 ops/sec ±11.45% (36 runs sampled)

population

Populate - 1 value x 514 ops/sec ±4.56% (87 runs sampled)
Populate - 10 values x 330 ops/sec ±5.58% (74 runs sampled)
Populate - 100 values x 63.19 ops/sec ±6.43% (53 runs sampled)
Populate - 1000 values x 10.57 ops/sec ±7.57% (29 runs sampled)
Populate - 10000 values x 2.95 ops/sec ±5.21% (19 runs sampled)
Populate - 5 properties x 11.84 ops/sec ±6.32% (33 runs sampled)
Populate - 10 properties x 9.38 ops/sec ±6.65% (47 runs sampled)

multiop

Multi-Op - Mongoose - Heavy Read, low write x 7.26 ops/sec ±6.83% (39 runs sampled)
Multi-Op - Driver - Heavy Read, low write x 12.65 ops/sec ±5.71% (37 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Heavy Read, low write x 1.68 ops/sec ±8.72% (13 runs sampled)
Multi-Op - Driver - Embedded Docs - Heavy Read, low write x 9.66 ops/sec ±3.47% (50 runs sampled)
Multi-Op - Mongoose - Heavy Write, low read x 4.05 ops/sec ±6.71% (24 runs sampled)
Multi-Op - Driver - Heavy Write, low read x 8.36 ops/sec ±2.88% (43 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Heavy Write, low read x 0.56 ops/sec ±10.09% (7 runs sampled)
Multi-Op - Driver - Embedded Docs - Heavy Write, low read x 5.23 ops/sec ±3.86% (29 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Read-write-update x 1.34 ops/sec ±6.27% (11 runs sampled)
Multi-Op - Driver - Embedded Docs - Read-write-update x 7.96 ops/sec ±3.97% (42 runs sampled)
@mongoose-benchmarks

Benchmarks for c5b1ffb:
insert

Insert - Mongoose - Basic x 263 ops/sec ±52.92% (66 runs sampled)
Insert - Driver - Basic x 523 ops/sec ±11.33% (80 runs sampled)
Insert - Mongoose - Embedded Docs x 80.67 ops/sec ±21.81% (49 runs sampled)
Insert - Driver - Embedded Docs x 481 ops/sec ±7.79% (80 runs sampled)

delete

Delete - Mongoose x 540 ops/sec ±3.89% (83 runs sampled)
Delete - Driver x 606 ops/sec ±1.05% (85 runs sampled)

update

Update - Mongoose - Basic x 499 ops/sec ±3.65% (85 runs sampled)
Update - Driver - Basic x 582 ops/sec ±2.04% (84 runs sampled)
Update - Mongoose - Embedded Docs x 86.50 ops/sec ±8.38% (51 runs sampled)
Update - Driver - Embdedded Docs x 439 ops/sec ±1.54% (87 runs sampled)
Update - Mongoose - Multiple Documents x 300 ops/sec ±6.02% (74 runs sampled)
Update - Driver - Multiple Documents x 474 ops/sec ±5.46% (81 runs sampled)
Update - Mongoose - pop and push x 24.78 ops/sec ±17.26% (49 runs sampled)
Update - Mongoose - Array Manipulation, parallel ops x 227 ops/sec ±6.52% (66 runs sampled)
Update - Mongoose - findOneAndModify x 251 ops/sec ±9.48% (63 runs sampled)
Update - Mongoose - find and update, separate ops x 241 ops/sec ±9.39% (55 runs sampled)

read

Read - Mongoose - Basic x 567 ops/sec ±1.86% (85 runs sampled)
Read - Driver - Basic x 657 ops/sec ±1.12% (85 runs sampled)
Read - Mongoose - With lean x 593 ops/sec ±0.92% (86 runs sampled)
Read - Mongoose - Multiple Items x 138 ops/sec ±7.56% (55 runs sampled)
Read - Driver - Multiple Items x 482 ops/sec ±1.79% (85 runs sampled)
Read - Mongoose - Non-index x 200 ops/sec ±6.39% (62 runs sampled)
Read - Driver - Non-index x 436 ops/sec ±2.36% (83 runs sampled)
Read - Mongoose - Embedded Docs x 273 ops/sec ±7.75% (66 runs sampled)
Read - Driver - Embedded Docs x 649 ops/sec ±1.90% (84 runs sampled)

casting

Casting - Embedded Docs - 0 Docs x 2,911 ops/sec ±9.18% (47 runs sampled)
Casting - Embedded Docs - 10 Docs x 2,970 ops/sec ±9.69% (48 runs sampled)
Casting - Embedded Docs - 100 Docs x 2,942 ops/sec ±9.24% (51 runs sampled)
Casting - Embedded Docs - 1000 Docs x 2,070 ops/sec ±11.61% (45 runs sampled)
Casting - Embedded Docs - 10000 Docs x 451 ops/sec ±10.77% (35 runs sampled)

population

Populate - 1 value x 495 ops/sec ±4.12% (82 runs sampled)
Populate - 10 values x 296 ops/sec ±7.05% (71 runs sampled)
Populate - 100 values x 59.30 ops/sec ±6.33% (50 runs sampled)
Populate - 1000 values x 10.64 ops/sec ±7.69% (33 runs sampled)
Populate - 10000 values x 2.98 ops/sec ±5.59% (19 runs sampled)
Populate - 5 properties x 11.71 ops/sec ±6.96% (39 runs sampled)
Populate - 10 properties x 8.82 ops/sec ±7.35% (46 runs sampled)

multiop

Multi-Op - Mongoose - Heavy Read, low write x 6.91 ops/sec ±5.47% (38 runs sampled)
Multi-Op - Driver - Heavy Read, low write x 12.00 ops/sec ±5.79% (43 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Heavy Read, low write x 1.68 ops/sec ±7.70% (13 runs sampled)
Multi-Op - Driver - Embedded Docs - Heavy Read, low write x 10.08 ops/sec ±4.12% (52 runs sampled)
Multi-Op - Mongoose - Heavy Write, low read x 4.00 ops/sec ±6.00% (24 runs sampled)
Multi-Op - Driver - Heavy Write, low read x 8.35 ops/sec ±3.46% (43 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Heavy Write, low read x 0.56 ops/sec ±9.33% (7 runs sampled)
Multi-Op - Driver - Embedded Docs - Heavy Write, low read x 5.32 ops/sec ±4.17% (30 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Read-write-update x 1.34 ops/sec ±8.13% (11 runs sampled)
Multi-Op - Driver - Embedded Docs - Read-write-update x 7.38 ops/sec ±7.81% (39 runs sampled)
@mongoose-benchmarks

Benchmarks for 3a0ec5f:
insert

Insert - Mongoose - Basic x 346 ops/sec ±13.87% (67 runs sampled)
Insert - Driver - Basic x 521 ops/sec ±15.12% (79 runs sampled)
Insert - Mongoose - Embedded Docs x 95.20 ops/sec ±9.22% (51 runs sampled)
Insert - Driver - Embedded Docs x 444 ops/sec ±18.60% (76 runs sampled)

delete

Delete - Mongoose x 567 ops/sec ±3.54% (85 runs sampled)
Delete - Driver x 618 ops/sec ±0.97% (86 runs sampled)

update

Update - Mongoose - Basic x 514 ops/sec ±3.05% (86 runs sampled)
Update - Driver - Basic x 582 ops/sec ±1.53% (88 runs sampled)
Update - Mongoose - Embedded Docs x 80.48 ops/sec ±14.71% (49 runs sampled)
Update - Driver - Embdedded Docs x 437 ops/sec ±1.06% (87 runs sampled)
Update - Mongoose - Multiple Documents x 315 ops/sec ±6.16% (78 runs sampled)
Update - Driver - Multiple Documents x 471 ops/sec ±11.47% (81 runs sampled)
Update - Mongoose - pop and push x 28.26 ops/sec ±15.23% (53 runs sampled)
Update - Mongoose - Array Manipulation, parallel ops x 252 ops/sec ±6.39% (69 runs sampled)
Update - Mongoose - findOneAndModify x 249 ops/sec ±9.53% (62 runs sampled)
Update - Mongoose - find and update, separate ops x 246 ops/sec ±9.17% (61 runs sampled)

read

Read - Mongoose - Basic x 581 ops/sec ±1.81% (82 runs sampled)
Read - Driver - Basic x 674 ops/sec ±0.99% (86 runs sampled)
Read - Mongoose - With lean x 595 ops/sec ±2.48% (84 runs sampled)
Read - Mongoose - Multiple Items x 156 ops/sec ±7.02% (57 runs sampled)
Read - Driver - Multiple Items x 478 ops/sec ±3.09% (82 runs sampled)
Read - Mongoose - Non-index x 191 ops/sec ±6.45% (61 runs sampled)
Read - Driver - Non-index x 447 ops/sec ±1.54% (85 runs sampled)
Read - Mongoose - Embedded Docs x 277 ops/sec ±8.40% (63 runs sampled)
Read - Driver - Embedded Docs x 642 ops/sec ±1.81% (79 runs sampled)

casting

Casting - Embedded Docs - 0 Docs x 3,076 ops/sec ±8.96% (51 runs sampled)
Casting - Embedded Docs - 10 Docs x 3,114 ops/sec ±10.69% (47 runs sampled)
Casting - Embedded Docs - 100 Docs x 3,065 ops/sec ±8.78% (50 runs sampled)
Casting - Embedded Docs - 1000 Docs x 1,931 ops/sec ±10.72% (51 runs sampled)
Casting - Embedded Docs - 10000 Docs x 461 ops/sec ±11.93% (38 runs sampled)

population

Populate - 1 value x 516 ops/sec ±3.58% (85 runs sampled)
Populate - 10 values x 332 ops/sec ±5.54% (75 runs sampled)
Populate - 100 values x 63.31 ops/sec ±6.26% (53 runs sampled)
Populate - 1000 values x 11.58 ops/sec ±6.92% (52 runs sampled)
Populate - 10000 values x 3.12 ops/sec ±4.46% (20 runs sampled)
Populate - 5 properties x 12.23 ops/sec ±6.24% (39 runs sampled)
Populate - 10 properties x 9.46 ops/sec ±6.84% (49 runs sampled)

multiop

Multi-Op - Mongoose - Heavy Read, low write x 7.43 ops/sec ±5.63% (40 runs sampled)
Multi-Op - Driver - Heavy Read, low write x 13.44 ops/sec ±5.60% (51 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Heavy Read, low write x 1.74 ops/sec ±9.22% (14 runs sampled)
Multi-Op - Driver - Embedded Docs - Heavy Read, low write x 9.70 ops/sec ±4.35% (51 runs sampled)
Multi-Op - Mongoose - Heavy Write, low read x 4.09 ops/sec ±7.22% (24 runs sampled)
Multi-Op - Driver - Heavy Write, low read x 8.17 ops/sec ±3.68% (43 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Heavy Write, low read x 0.54 ops/sec ±8.86% (7 runs sampled)
Multi-Op - Driver - Embedded Docs - Heavy Write, low read x 5.11 ops/sec ±4.20% (29 runs sampled)
Multi-Op - Mongoose - Embedded Docs - Read-write-update x 1.33 ops/sec ±5.28% (11 runs sampled)
Multi-Op - Driver - Embedded Docs - Read-write-update x 8.35 ops/sec ±2.90% (44 runs sampled)
@aheckmann aheckmann merged commit c99ced0 into Automattic:master

1 check passed

Details default The Travis CI build passed
@aheckmann
Owner

thanks!

@ragulka

Awesome! I've been waiting for this for a looong time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 3 deletions.
  1. +5 −3 lib/schematype.js
  2. +41 −0 test/schema.validation.test.js
View
8 lib/schematype.js
@@ -392,14 +392,16 @@ SchemaType.prototype.get = function (fn) {
*
* @param {RegExp|Function|Object} obj validator
* @param {String} [errorMsg] optional error message
+ * @param {String} [type] optional validator type
* @return {SchemaType} this
* @api public
*/
-SchemaType.prototype.validate = function (obj, message) {
+SchemaType.prototype.validate = function (obj, message, type) {
if ('function' == typeof obj || obj && 'RegExp' === obj.constructor.name) {
if (!message) message = errorMessages.general.default;
- this.validators.push([obj, message, 'user defined']);
+ if (!type) type = 'user defined';
+ this.validators.push([obj, message, type]);
return this;
}
@@ -415,7 +417,7 @@ SchemaType.prototype.validate = function (obj, message) {
throw new Error(msg);
}
- this.validate(arg.validator, arg.msg);
+ this.validate(arg.validator, arg.msg, arg.type);
}
return this;
View
41 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})', type: '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();
+ })
+ })
+ })
+ })
});
});
Something went wrong with that request. Please try again.