Skip to content

Commit 166cd88

Browse files
committed
refactor: use SchemaStringOptions class for string schematype options re: #8012
1 parent 523c181 commit 166cd88

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

lib/options/SchemaStringOptions.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
const SchemaTypeOptions = require('./SchemaTypeOptions');
4+
5+
class SchemaStringOptions extends SchemaTypeOptions {}
6+
7+
const opts = {
8+
enumerable: true,
9+
configurable: true,
10+
writable: true,
11+
value: null
12+
};
13+
14+
/**
15+
* Array of allowed values for this path
16+
*
17+
* @api public
18+
* @property enum
19+
* @memberOf SchemaStringOptions
20+
* @type Array
21+
* @instance
22+
*/
23+
24+
Object.defineProperty(SchemaStringOptions.prototype, 'enum', opts);
25+
26+
/**
27+
* Attach a validator that succeeds if the data string matches the given regular
28+
* expression, and fails otherwise.
29+
*
30+
* @api public
31+
* @property match
32+
* @memberOf SchemaStringOptions
33+
* @type RegExp
34+
* @instance
35+
*/
36+
37+
Object.defineProperty(SchemaStringOptions.prototype, 'match', opts);
38+
39+
/**
40+
* If truthy, Mongoose will add a custom setter that lowercases this string
41+
* using JavaScript's built-in `String#toLowerCase()`.
42+
*
43+
* @api public
44+
* @property lowercase
45+
* @memberOf SchemaStringOptions
46+
* @type Boolean
47+
* @instance
48+
*/
49+
50+
Object.defineProperty(SchemaStringOptions.prototype, 'lowercase', opts);
51+
52+
/**
53+
* If truthy, Mongoose will add a custom setter that removes leading and trailing
54+
* whitespace using JavaScript's built-in `String#trim()`.
55+
*
56+
* @api public
57+
* @property trim
58+
* @memberOf SchemaStringOptions
59+
* @type Boolean
60+
* @instance
61+
*/
62+
63+
Object.defineProperty(SchemaStringOptions.prototype, 'trim', opts);
64+
65+
/**
66+
* If truthy, Mongoose will add a custom setter that uppercases this string
67+
* using JavaScript's built-in `String#toUpperCase()`.
68+
*
69+
* @api public
70+
* @property uppercase
71+
* @memberOf SchemaStringOptions
72+
* @type Boolean
73+
* @instance
74+
*/
75+
76+
Object.defineProperty(SchemaStringOptions.prototype, 'uppercase', opts);
77+
78+
/*!
79+
* ignore
80+
*/
81+
82+
module.exports = SchemaStringOptions;

lib/options/SchemaTypeOptions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ const opts = {
1818
value: null
1919
};
2020

21+
/**
22+
* The type to cast this path to.
23+
*
24+
* @api public
25+
* @property type
26+
* @memberOf SchemaTypeOptions
27+
* @type Function|String|Object
28+
* @instance
29+
*/
30+
31+
Object.defineProperty(SchemaTypeOptions.prototype, 'type', opts);
32+
2133
/**
2234
* Function or object describing how to validate this schematype.
2335
*

lib/schema/string.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
*/
66

77
const SchemaType = require('../schematype');
8-
const CastError = SchemaType.CastError;
98
const MongooseError = require('../error/index');
9+
const SchemaStringOptions = require('../options/SchemaStringOptions');
1010
const castString = require('../cast/string');
1111
const utils = require('../utils');
1212

1313
const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
1414

15+
const CastError = SchemaType.CastError;
1516
let Document;
1617

1718
/**
@@ -48,6 +49,7 @@ SchemaString.prototype.constructor = SchemaString;
4849
*/
4950

5051
SchemaString._cast = castString;
52+
SchemaString.OptionsConstructor = SchemaStringOptions;
5153

5254
/**
5355
* Get/set the function used to cast arbitrary values to strings.

lib/schematype.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function SchemaType(path, options, instance) {
4343
this.constructor.getters.slice() :
4444
[];
4545
this.setters = [];
46-
this.options = new SchemaTypeOptions(options);
46+
47+
const Options = this.OptionsConstructor || SchemaTypeOptions;
48+
this.options = new Options(options);
4749
this._index = null;
4850
this.selected;
4951
if (utils.hasUserDefinedProperty(this.options, 'immutable')) {

0 commit comments

Comments
 (0)