Skip to content

Commit

Permalink
refactor: use options constructor for array, buffer, date schematypes…
Browse files Browse the repository at this point in the history
… re: #8012
  • Loading branch information
vkarpov15 committed Oct 7, 2019
1 parent 2d0955d commit d5e0c73
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 6 deletions.
31 changes: 31 additions & 0 deletions lib/options/SchemaArrayOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

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

class SchemaArrayOptions extends SchemaTypeOptions {}

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

/**
* If this is an array of strings, an array of allowed values for this path.
* Throws an error if this array isn't an array of strings.
*
* @api public
* @property enum
* @memberOf SchemaArrayOptions
* @type Array
* @instance
*/

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

/*!
* ignore
*/

module.exports = SchemaArrayOptions;
30 changes: 30 additions & 0 deletions lib/options/SchemaBufferOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

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

class SchemaBufferOptions extends SchemaTypeOptions {}

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

/**
* Set the default subtype for this buffer.
*
* @api public
* @property subtype
* @memberOf SchemaBufferOptions
* @type Number
* @instance
*/

Object.defineProperty(SchemaBufferOptions.prototype, 'subtype', opts);

/*!
* ignore
*/

module.exports = SchemaBufferOptions;
56 changes: 56 additions & 0 deletions lib/options/SchemaDateOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

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

class SchemaDateOptions extends SchemaTypeOptions {}

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

/**
* If set, Mongoose adds a validator that checks that this path is after the
* given `min`.
*
* @api public
* @property min
* @memberOf SchemaDateOptions
* @type Date
* @instance
*/

Object.defineProperty(SchemaDateOptions.prototype, 'min', opts);

/**
* If set, Mongoose adds a validator that checks that this path is before the
* given `max`.
*
* @api public
* @property max
* @memberOf SchemaDateOptions
* @type Date
* @instance
*/

Object.defineProperty(SchemaDateOptions.prototype, 'max', opts);

/**
* If set, Mongoose creates a TTL index on this path.
*
* @api public
* @property expires
* @memberOf SchemaDateOptions
* @type Date
* @instance
*/

Object.defineProperty(SchemaDateOptions.prototype, 'expires', opts);

/*!
* ignore
*/

module.exports = SchemaDateOptions;
2 changes: 1 addition & 1 deletion lib/options/SchemaNumberOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const opts = {
Object.defineProperty(SchemaNumberOptions.prototype, 'min', opts);

/**
* If set, Mongoose adds a validator that checks that this path is at least the
* If set, Mongoose adds a validator that checks that this path is less than the
* given `max`.
*
* @api public
Expand Down
2 changes: 2 additions & 0 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const $exists = require('./operators/exists');
const $type = require('./operators/type');
const MongooseError = require('../error/mongooseError');
const SchemaArrayOptions = require('../options/SchemaArrayOptions');
const SchemaType = require('../schematype');
const CastError = SchemaType.CastError;
const Mixed = require('./mixed');
Expand Down Expand Up @@ -134,6 +135,7 @@ SchemaArray.options = { castNonArrays: true };
*/
SchemaArray.prototype = Object.create(SchemaType.prototype);
SchemaArray.prototype.constructor = SchemaArray;
SchemaArray.prototype.OptionsConstructor = SchemaArrayOptions;

/*!
* ignore
Expand Down
7 changes: 4 additions & 3 deletions lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

'use strict';

const MongooseBuffer = require('../types/buffer');
const SchemaBufferOptions = require('../options/SchemaBufferOptions');
const SchemaType = require('../schematype');
const handleBitwiseOperator = require('./operators/bitwise');
const utils = require('../utils');

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

const MongooseBuffer = require('../types/buffer');
const SchemaType = require('../schematype');

const Binary = MongooseBuffer.Binary;
const CastError = SchemaType.CastError;
let Document;
Expand Down Expand Up @@ -42,6 +42,7 @@ SchemaBuffer.schemaName = 'Buffer';
*/
SchemaBuffer.prototype = Object.create(SchemaType.prototype);
SchemaBuffer.prototype.constructor = SchemaBuffer;
SchemaBuffer.prototype.OptionsConstructor = SchemaBufferOptions;

/*!
* ignore
Expand Down
5 changes: 3 additions & 2 deletions lib/schema/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
'use strict';

const MongooseError = require('../error/index');
const SchemaDateOptions = require('../options/SchemaDateOptions');
const SchemaType = require('../schematype');
const castDate = require('../cast/date');
const utils = require('../utils');

const SchemaType = require('../schematype');

const CastError = SchemaType.CastError;

/**
Expand Down Expand Up @@ -38,6 +38,7 @@ SchemaDate.schemaName = 'Date';
*/
SchemaDate.prototype = Object.create(SchemaType.prototype);
SchemaDate.prototype.constructor = SchemaDate;
SchemaDate.prototype.OptionsConstructor = SchemaDateOptions;

/*!
* ignore
Expand Down

0 comments on commit d5e0c73

Please sign in to comment.