Skip to content

Commit

Permalink
types(schema): allow calling Schema.prototype.number() with no messag…
Browse files Browse the repository at this point in the history
…e arg

Fix #15236
  • Loading branch information
vkarpov15 committed Feb 6, 2025
1 parent 3501adf commit 07fd15f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
23 changes: 23 additions & 0 deletions test/schema.number.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const assert = require('assert');
const start = require('./common');

const mongoose = start.mongoose;
Expand All @@ -18,4 +19,26 @@ describe('SchemaNumber', function() {
});
});
});

it('allows calling `min()` with no message arg (gh-15236)', async function() {
const schema = new Schema({ x: { type: Number } });
schema.path('x').min(0);

const err = await new Promise((resolve) => {
schema.path('x').doValidate(-1, err => {
resolve(err);
});
});
assert.ok(err);
assert.equal(err.message, 'Path `x` (-1) is less than minimum allowed value (0).');

schema.path('x').min(0, 'Invalid value!');

const err2 = await new Promise((resolve) => {
schema.path('x').doValidate(-1, err => {
resolve(err);
});
});
assert.equal(err2.message, 'Invalid value!');
});
});
8 changes: 8 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1725,3 +1725,11 @@ async function gh12959() {
const leanDoc = await TestModel.findOne().lean().orFail();
expectType<number>(leanDoc.__v);
}

async function gh15236() {
const schema = new Schema({
myNum: { type: Number }
});

schema.path<Schema.Types.Number>('myNum').min(0);
}
8 changes: 4 additions & 4 deletions types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ declare module 'mongoose' {
expires(when: number | string): this;

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

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

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

/** Sets a maximum number validator. */
max(value: number, message: string): this;
max(value: number, message?: string): this;

/** Sets a minimum number validator. */
min(value: number, message: string): this;
min(value: number, message?: string): this;

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

0 comments on commit 07fd15f

Please sign in to comment.