From 2d0955dd60173cf2ee3683656976b0b8a617b8db Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 6 Oct 2019 18:20:12 -0400 Subject: [PATCH] refactor: use SchemaNumberOptions class for schematype number re: #8012 --- lib/options/SchemaNumberOptions.js | 44 ++++++++++++++++++++++++++++++ lib/schema/number.js | 2 ++ lib/schema/string.js | 2 +- lib/schematype.js | 6 ++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/options/SchemaNumberOptions.js diff --git a/lib/options/SchemaNumberOptions.js b/lib/options/SchemaNumberOptions.js new file mode 100644 index 00000000000..2186a83740b --- /dev/null +++ b/lib/options/SchemaNumberOptions.js @@ -0,0 +1,44 @@ +'use strict'; + +const SchemaTypeOptions = require('./SchemaTypeOptions'); + +class SchemaNumberOptions extends SchemaTypeOptions {} + +const opts = { + enumerable: true, + configurable: true, + writable: true, + value: null +}; + +/** + * If set, Mongoose adds a validator that checks that this path is at least the + * given `min`. + * + * @api public + * @property min + * @memberOf SchemaNumberOptions + * @type Number + * @instance + */ + +Object.defineProperty(SchemaNumberOptions.prototype, 'min', opts); + +/** + * If set, Mongoose adds a validator that checks that this path is at least the + * given `max`. + * + * @api public + * @property max + * @memberOf SchemaNumberOptions + * @type Number + * @instance + */ + +Object.defineProperty(SchemaNumberOptions.prototype, 'max', opts); + +/*! + * ignore + */ + +module.exports = SchemaNumberOptions; \ No newline at end of file diff --git a/lib/schema/number.js b/lib/schema/number.js index 8f9100d268c..62729eca174 100644 --- a/lib/schema/number.js +++ b/lib/schema/number.js @@ -5,6 +5,7 @@ */ const MongooseError = require('../error/index'); +const SchemaNumberOptions = require('../options/SchemaNumberOptions'); const SchemaType = require('../schematype'); const castNumber = require('../cast/number'); const handleBitwiseOperator = require('./operators/bitwise'); @@ -106,6 +107,7 @@ SchemaNumber.schemaName = 'Number'; */ SchemaNumber.prototype = Object.create(SchemaType.prototype); SchemaNumber.prototype.constructor = SchemaNumber; +SchemaNumber.prototype.OptionsConstructor = SchemaNumberOptions; /*! * ignore diff --git a/lib/schema/string.js b/lib/schema/string.js index c45d08f6dfb..fe3e9568a07 100644 --- a/lib/schema/string.js +++ b/lib/schema/string.js @@ -43,13 +43,13 @@ SchemaString.schemaName = 'String'; */ SchemaString.prototype = Object.create(SchemaType.prototype); SchemaString.prototype.constructor = SchemaString; +SchemaString.prototype.OptionsConstructor = SchemaStringOptions; /*! * ignore */ SchemaString._cast = castString; -SchemaString.OptionsConstructor = SchemaStringOptions; /** * Get/set the function used to cast arbitrary values to strings. diff --git a/lib/schematype.js b/lib/schematype.js index dea567766ca..2cd0caf0858 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -98,6 +98,12 @@ function SchemaType(path, options, instance) { }); } +/*! + * ignore + */ + +SchemaType.prototype.OptionsConstructor = SchemaTypeOptions; + /** * Get/set the function used to cast arbitrary values to this type. *