Skip to content

Commit 2d0955d

Browse files
committed
refactor: use SchemaNumberOptions class for schematype number re: #8012
1 parent 166cd88 commit 2d0955d

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

lib/options/SchemaNumberOptions.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const SchemaTypeOptions = require('./SchemaTypeOptions');
4+
5+
class SchemaNumberOptions extends SchemaTypeOptions {}
6+
7+
const opts = {
8+
enumerable: true,
9+
configurable: true,
10+
writable: true,
11+
value: null
12+
};
13+
14+
/**
15+
* If set, Mongoose adds a validator that checks that this path is at least the
16+
* given `min`.
17+
*
18+
* @api public
19+
* @property min
20+
* @memberOf SchemaNumberOptions
21+
* @type Number
22+
* @instance
23+
*/
24+
25+
Object.defineProperty(SchemaNumberOptions.prototype, 'min', opts);
26+
27+
/**
28+
* If set, Mongoose adds a validator that checks that this path is at least the
29+
* given `max`.
30+
*
31+
* @api public
32+
* @property max
33+
* @memberOf SchemaNumberOptions
34+
* @type Number
35+
* @instance
36+
*/
37+
38+
Object.defineProperty(SchemaNumberOptions.prototype, 'max', opts);
39+
40+
/*!
41+
* ignore
42+
*/
43+
44+
module.exports = SchemaNumberOptions;

lib/schema/number.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
const MongooseError = require('../error/index');
8+
const SchemaNumberOptions = require('../options/SchemaNumberOptions');
89
const SchemaType = require('../schematype');
910
const castNumber = require('../cast/number');
1011
const handleBitwiseOperator = require('./operators/bitwise');
@@ -106,6 +107,7 @@ SchemaNumber.schemaName = 'Number';
106107
*/
107108
SchemaNumber.prototype = Object.create(SchemaType.prototype);
108109
SchemaNumber.prototype.constructor = SchemaNumber;
110+
SchemaNumber.prototype.OptionsConstructor = SchemaNumberOptions;
109111

110112
/*!
111113
* ignore

lib/schema/string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ SchemaString.schemaName = 'String';
4343
*/
4444
SchemaString.prototype = Object.create(SchemaType.prototype);
4545
SchemaString.prototype.constructor = SchemaString;
46+
SchemaString.prototype.OptionsConstructor = SchemaStringOptions;
4647

4748
/*!
4849
* ignore
4950
*/
5051

5152
SchemaString._cast = castString;
52-
SchemaString.OptionsConstructor = SchemaStringOptions;
5353

5454
/**
5555
* Get/set the function used to cast arbitrary values to strings.

lib/schematype.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ function SchemaType(path, options, instance) {
9898
});
9999
}
100100

101+
/*!
102+
* ignore
103+
*/
104+
105+
SchemaType.prototype.OptionsConstructor = SchemaTypeOptions;
106+
101107
/**
102108
* Get/set the function used to cast arbitrary values to this type.
103109
*

0 commit comments

Comments
 (0)