Skip to content

Commit 84d4b91

Browse files
committed
Merge branch 'master' of github.com:Automattic/mongoose
2 parents 3b545e8 + 32366cc commit 84d4b91

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

lib/schema/number.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const CastError = SchemaType.CastError;
2626
*/
2727

2828
function SchemaNumber(key, options, _schemaOptions, parentSchema) {
29+
this.enumValues = [];
2930
SchemaType.call(this, key, options, 'Number', parentSchema);
3031
}
3132

@@ -316,8 +317,12 @@ SchemaNumber.prototype.enum = function(values, message) {
316317
this.validators = this.validators.filter(function(v) {
317318
return v.validator !== this.enumValidator;
318319
}, this);
320+
this.enumValidator = false;
319321
}
320322

323+
if (values === void 0 || values === false) {
324+
return this;
325+
}
321326

322327
if (!Array.isArray(values)) {
323328
const isObjectSyntax = utils.isPOJO(values) && values.values != null;
@@ -337,12 +342,19 @@ SchemaNumber.prototype.enum = function(values, message) {
337342

338343
message = message == null ? MongooseError.messages.Number.enum : message;
339344

340-
this.enumValidator = v => v == null || values.indexOf(v) !== -1;
345+
for (const value of values) {
346+
if (value !== undefined) {
347+
this.enumValues.push(this.cast(value));
348+
}
349+
}
350+
351+
const vals = this.enumValues;
352+
this.enumValidator = v => v == null || vals.indexOf(v) !== -1;
341353
this.validators.push({
342354
validator: this.enumValidator,
343355
message: message,
344356
type: 'enum',
345-
enumValues: values
357+
enumValues: vals
346358
});
347359

348360
return this;

lib/utils.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -875,15 +875,7 @@ exports.buffer.areEqual = function(a, b) {
875875
if (!Buffer.isBuffer(b)) {
876876
return false;
877877
}
878-
if (a.length !== b.length) {
879-
return false;
880-
}
881-
for (let i = 0, len = a.length; i < len; ++i) {
882-
if (a[i] !== b[i]) {
883-
return false;
884-
}
885-
}
886-
return true;
878+
return a.equals(b);
887879
};
888880

889881
exports.getFunctionName = getFunctionName;

test/schema.validation.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,56 @@ describe('schema', function() {
9797
await Test.path('state').doValidate('open');
9898
});
9999

100+
it('number enum', async function() {
101+
const Test = new Schema({
102+
status: { type: Number, enum: [1, 2, 3, null] },
103+
priority: { type: Number }
104+
});
105+
106+
assert.ok(Test.path('status') instanceof SchemaTypes.Number);
107+
assert.deepEqual(Test.path('status').enumValues, [1, 2, 3, null]);
108+
assert.equal(Test.path('status').validators.length, 1);
109+
110+
Test.path('status').enum(4, 5);
111+
112+
assert.deepEqual(Test.path('status').enumValues, [1, 2, 3, null, 4, 5]);
113+
114+
// with SchemaTypes validate method
115+
Test.path('priority').enum({
116+
values: [10, 20, 30],
117+
message: 'enum validator failed for path `{PATH}`: test'
118+
});
119+
120+
assert.equal(Test.path('priority').validators.length, 1);
121+
assert.deepEqual(Test.path('priority').enumValues, [10, 20, 30]);
122+
123+
await assert.rejects(Test.path('status').doValidate(6), ValidatorError);
124+
125+
// allow unsetting enums
126+
await Test.path('status').doValidate(undefined);
127+
128+
await Test.path('status').doValidate(null);
129+
130+
await assert.rejects(
131+
Test.path('status').doValidate(99),
132+
ValidatorError
133+
);
134+
135+
await assert.rejects(
136+
Test.path('priority').doValidate(40),
137+
err => {
138+
assert.ok(err instanceof ValidatorError);
139+
assert.equal(err.message,
140+
'enum validator failed for path `priority`: test');
141+
return true;
142+
}
143+
);
144+
145+
await Test.path('status').doValidate(1);
146+
147+
await Test.path('status').doValidate(2);
148+
});
149+
100150
it('string regexp', async function() {
101151
const Test = new Schema({
102152
simple: { type: String, match: /[a-z]/ }

0 commit comments

Comments
 (0)