From 29c5f1a5be732dacee506c85d2e0cb3804eb432b Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 21 Oct 2019 16:11:40 -0400 Subject: [PATCH] fix(options): add missing minlength and maxlength to SchemaStringOptions Fix #8256 --- lib/options/SchemaStringOptions.js | 26 ++++++++++++++++++++++ lib/schema/string.js | 35 ++++++++++++++++++++---------- test/schema.test.js | 9 ++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/options/SchemaStringOptions.js b/lib/options/SchemaStringOptions.js index 11e8848a787..8c6bb0ac067 100644 --- a/lib/options/SchemaStringOptions.js +++ b/lib/options/SchemaStringOptions.js @@ -88,6 +88,32 @@ Object.defineProperty(SchemaStringOptions.prototype, 'trim', opts); Object.defineProperty(SchemaStringOptions.prototype, 'uppercase', opts); +/** + * If set, Mongoose will add a custom validator that ensures the given + * string's `length` is at least the given number. + * + * @api public + * @property minlength + * @memberOf SchemaStringOptions + * @type Number + * @instance + */ + +Object.defineProperty(SchemaStringOptions.prototype, 'minlength', opts); + +/** + * If set, Mongoose will add a custom validator that ensures the given + * string's `length` is at most the given number. + * + * @api public + * @property maxlength + * @memberOf SchemaStringOptions + * @type Number + * @instance + */ + +Object.defineProperty(SchemaStringOptions.prototype, 'maxlength', opts); + /*! * ignore */ diff --git a/lib/schema/string.js b/lib/schema/string.js index fe3e9568a07..645c4c3571e 100644 --- a/lib/schema/string.js +++ b/lib/schema/string.js @@ -43,7 +43,12 @@ SchemaString.schemaName = 'String'; */ SchemaString.prototype = Object.create(SchemaType.prototype); SchemaString.prototype.constructor = SchemaString; -SchemaString.prototype.OptionsConstructor = SchemaStringOptions; +Object.defineProperty(SchemaString.prototype, 'OptionsConstructor', { + configurable: false, + enumerable: false, + writable: false, + value: SchemaStringOptions +}); /*! * ignore @@ -574,17 +579,23 @@ function handleArray(val) { }); } -SchemaString.prototype.$conditionalHandlers = - utils.options(SchemaType.prototype.$conditionalHandlers, { - $all: handleArray, - $gt: handleSingle, - $gte: handleSingle, - $lt: handleSingle, - $lte: handleSingle, - $options: String, - $regex: handleSingle, - $not: handleSingle - }); +const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, { + $all: handleArray, + $gt: handleSingle, + $gte: handleSingle, + $lt: handleSingle, + $lte: handleSingle, + $options: String, + $regex: handleSingle, + $not: handleSingle +}); + +Object.defineProperty(SchemaString.prototype, '$conditionalHandlers', { + configurable: false, + enumerable: false, + writable: false, + value: Object.freeze($conditionalHandlers) +}); /** * Casts contents for queries. diff --git a/test/schema.test.js b/test/schema.test.js index 02c6c22baa9..7b4d6435c87 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -2135,4 +2135,13 @@ describe('schema', function() { assert.strictEqual(schema.path('name').isRequired, false); assert.strictEqual(schema.path('age').isRequired, false); }); + + it('SchemaStringOptions line up with schema/string (gh-8256)', function() { + const SchemaStringOptions = require('../lib/options/SchemaStringOptions'); + const keys = Object.keys(SchemaStringOptions.prototype). + filter(key => key !== 'constructor'); + const functions = Object.keys(Schema.Types.String.prototype). + filter(key => !['constructor', 'cast', 'castForQuery', 'checkRequired'].includes(key)); + assert.deepEqual(keys.sort(), functions.sort()); + }); });