Skip to content

Commit 07fd15f

Browse files
committed
types(schema): allow calling Schema.prototype.number() with no message arg
Fix #15236
1 parent 3501adf commit 07fd15f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

test/schema.number.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const assert = require('assert');
34
const start = require('./common');
45

56
const mongoose = start.mongoose;
@@ -18,4 +19,26 @@ describe('SchemaNumber', function() {
1819
});
1920
});
2021
});
22+
23+
it('allows calling `min()` with no message arg (gh-15236)', async function() {
24+
const schema = new Schema({ x: { type: Number } });
25+
schema.path('x').min(0);
26+
27+
const err = await new Promise((resolve) => {
28+
schema.path('x').doValidate(-1, err => {
29+
resolve(err);
30+
});
31+
});
32+
assert.ok(err);
33+
assert.equal(err.message, 'Path `x` (-1) is less than minimum allowed value (0).');
34+
35+
schema.path('x').min(0, 'Invalid value!');
36+
37+
const err2 = await new Promise((resolve) => {
38+
schema.path('x').doValidate(-1, err => {
39+
resolve(err);
40+
});
41+
});
42+
assert.equal(err2.message, 'Invalid value!');
43+
});
2144
});

test/types/schema.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1725,3 +1725,11 @@ async function gh12959() {
17251725
const leanDoc = await TestModel.findOne().lean().orFail();
17261726
expectType<number>(leanDoc.__v);
17271727
}
1728+
1729+
async function gh15236() {
1730+
const schema = new Schema({
1731+
myNum: { type: Number }
1732+
});
1733+
1734+
schema.path<Schema.Types.Number>('myNum').min(0);
1735+
}

types/schematypes.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,10 @@ declare module 'mongoose' {
389389
expires(when: number | string): this;
390390

391391
/** Sets a maximum date validator. */
392-
max(value: NativeDate, message: string): this;
392+
max(value: NativeDate, message?: string): this;
393393

394394
/** Sets a minimum date validator. */
395-
min(value: NativeDate, message: string): this;
395+
min(value: NativeDate, message?: string): this;
396396

397397
/** Default options for this SchemaType */
398398
defaultOptions: Record<string, any>;
@@ -457,10 +457,10 @@ declare module 'mongoose' {
457457
enum(vals: number[]): this;
458458

459459
/** Sets a maximum number validator. */
460-
max(value: number, message: string): this;
460+
max(value: number, message?: string): this;
461461

462462
/** Sets a minimum number validator. */
463-
min(value: number, message: string): this;
463+
min(value: number, message?: string): this;
464464

465465
/** Default options for this SchemaType */
466466
defaultOptions: Record<string, any>;

0 commit comments

Comments
 (0)