Skip to content

Commit

Permalink
refactor: use SchemaStringOptions class for string schematype options…
Browse files Browse the repository at this point in the history
… re: #8012
  • Loading branch information
vkarpov15 committed Oct 6, 2019
1 parent 523c181 commit 166cd88
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
82 changes: 82 additions & 0 deletions lib/options/SchemaStringOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict';

const SchemaTypeOptions = require('./SchemaTypeOptions');

class SchemaStringOptions extends SchemaTypeOptions {}

const opts = {
enumerable: true,
configurable: true,
writable: true,
value: null
};

/**
* Array of allowed values for this path
*
* @api public
* @property enum
* @memberOf SchemaStringOptions
* @type Array
* @instance
*/

Object.defineProperty(SchemaStringOptions.prototype, 'enum', opts);

/**
* Attach a validator that succeeds if the data string matches the given regular
* expression, and fails otherwise.
*
* @api public
* @property match
* @memberOf SchemaStringOptions
* @type RegExp
* @instance
*/

Object.defineProperty(SchemaStringOptions.prototype, 'match', opts);

/**
* If truthy, Mongoose will add a custom setter that lowercases this string
* using JavaScript's built-in `String#toLowerCase()`.
*
* @api public
* @property lowercase
* @memberOf SchemaStringOptions
* @type Boolean
* @instance
*/

Object.defineProperty(SchemaStringOptions.prototype, 'lowercase', opts);

/**
* If truthy, Mongoose will add a custom setter that removes leading and trailing
* whitespace using JavaScript's built-in `String#trim()`.
*
* @api public
* @property trim
* @memberOf SchemaStringOptions
* @type Boolean
* @instance
*/

Object.defineProperty(SchemaStringOptions.prototype, 'trim', opts);

/**
* If truthy, Mongoose will add a custom setter that uppercases this string
* using JavaScript's built-in `String#toUpperCase()`.
*
* @api public
* @property uppercase
* @memberOf SchemaStringOptions
* @type Boolean
* @instance
*/

Object.defineProperty(SchemaStringOptions.prototype, 'uppercase', opts);

/*!
* ignore
*/

module.exports = SchemaStringOptions;
12 changes: 12 additions & 0 deletions lib/options/SchemaTypeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ const opts = {
value: null
};

/**
* The type to cast this path to.
*
* @api public
* @property type
* @memberOf SchemaTypeOptions
* @type Function|String|Object
* @instance
*/

Object.defineProperty(SchemaTypeOptions.prototype, 'type', opts);

/**
* Function or object describing how to validate this schematype.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
*/

const SchemaType = require('../schematype');
const CastError = SchemaType.CastError;
const MongooseError = require('../error/index');
const SchemaStringOptions = require('../options/SchemaStringOptions');
const castString = require('../cast/string');
const utils = require('../utils');

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

const CastError = SchemaType.CastError;
let Document;

/**
Expand Down Expand Up @@ -48,6 +49,7 @@ SchemaString.prototype.constructor = SchemaString;
*/

SchemaString._cast = castString;
SchemaString.OptionsConstructor = SchemaStringOptions;

/**
* Get/set the function used to cast arbitrary values to strings.
Expand Down
4 changes: 3 additions & 1 deletion lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function SchemaType(path, options, instance) {
this.constructor.getters.slice() :
[];
this.setters = [];
this.options = new SchemaTypeOptions(options);

const Options = this.OptionsConstructor || SchemaTypeOptions;
this.options = new Options(options);
this._index = null;
this.selected;
if (utils.hasUserDefinedProperty(this.options, 'immutable')) {
Expand Down

0 comments on commit 166cd88

Please sign in to comment.