Skip to content

Commit f14a5b0

Browse files
Merge branch 'master' into add-bulkwrite-overwriteimmutable-types
2 parents 03855f4 + 84d4b91 commit f14a5b0

File tree

7 files changed

+76
-14
lines changed

7 files changed

+76
-14
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;

scripts/tsc-diagnostics-check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fs = require('fs');
44

55
const stdin = fs.readFileSync(0).toString('utf8');
6-
const maxInstantiations = isNaN(process.argv[2]) ? 375000 : parseInt(process.argv[2], 10);
6+
const maxInstantiations = isNaN(process.argv[2]) ? 390000 : parseInt(process.argv[2], 10);
77

88
console.log(stdin);
99

test/query.cursor.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ describe('QueryCursor', function() {
539539
setTimeout(() => {
540540
assert.equal(closeEventTriggeredCount, 1);
541541
done();
542-
}, 20);
542+
}, 200);
543543
});
544544

545545
it('closing query cursor emits `close` event only once with stream pause/resume (gh-10876)', function(done) {

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]/ }

test/types/schema.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,3 +2086,11 @@ function gh15751() {
20862086
const doc = new TestModel();
20872087
expectType<Types.ObjectId>(doc.myId);
20882088
}
2089+
2090+
function gh15798() {
2091+
const schema1 = new Schema({ name: String }, { statics: { testMe() { } }, versionKey: false });
2092+
model("M1", schema1).testMe();
2093+
2094+
const schema2 = new Schema({ name: String }, { statics: { testMe() { } }, timestamps: true });
2095+
model("M2", schema2).testMe();
2096+
}

types/inferschematype.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ declare module 'mongoose' {
114114

115115
type ResolveSchemaOptions<T> = MergeType<DefaultSchemaOptions, T>;
116116

117-
type ApplySchemaOptions<T, O = DefaultSchemaOptions> = ResolveTimestamps<T, O>;
117+
type ApplySchemaOptions<T, O = DefaultSchemaOptions> = Default__v<ResolveTimestamps<T, O>, O>;
118118

119119
type DefaultTimestampProps = {
120120
createdAt: NativeDate;

0 commit comments

Comments
 (0)