From 07fd15f6611dbd5357fdb1047f2d4def61fc64f0 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 6 Feb 2025 10:47:04 -0500 Subject: [PATCH] types(schema): allow calling Schema.prototype.number() with no message arg Fix #15236 --- test/schema.number.test.js | 23 +++++++++++++++++++++++ test/types/schema.test.ts | 8 ++++++++ types/schematypes.d.ts | 8 ++++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/test/schema.number.test.js b/test/schema.number.test.js index b8f051170b..99c69ca154 100644 --- a/test/schema.number.test.js +++ b/test/schema.number.test.js @@ -1,5 +1,6 @@ 'use strict'; +const assert = require('assert'); const start = require('./common'); const mongoose = start.mongoose; @@ -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!'); + }); }); diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 13408eaf29..2be451d4b3 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1725,3 +1725,11 @@ async function gh12959() { const leanDoc = await TestModel.findOne().lean().orFail(); expectType(leanDoc.__v); } + +async function gh15236() { + const schema = new Schema({ + myNum: { type: Number } + }); + + schema.path('myNum').min(0); +} diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index 5f364f0cea..03b42c181a 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -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; @@ -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;