Skip to content

Commit

Permalink
fix(options): add missing minlength and maxlength to SchemaStringOptions
Browse files Browse the repository at this point in the history
Fix #8256
  • Loading branch information
vkarpov15 committed Oct 21, 2019
1 parent a81211d commit 29c5f1a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
26 changes: 26 additions & 0 deletions lib/options/SchemaStringOptions.js
Expand Up @@ -88,6 +88,32 @@ Object.defineProperty(SchemaStringOptions.prototype, 'trim', opts);

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

/**
* If set, Mongoose will add a custom validator that ensures the given
* string's `length` is at least the given number.
*
* @api public
* @property minlength
* @memberOf SchemaStringOptions
* @type Number
* @instance
*/

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

/**
* If set, Mongoose will add a custom validator that ensures the given
* string's `length` is at most the given number.
*
* @api public
* @property maxlength
* @memberOf SchemaStringOptions
* @type Number
* @instance
*/

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

/*!
* ignore
*/
Expand Down
35 changes: 23 additions & 12 deletions lib/schema/string.js
Expand Up @@ -43,7 +43,12 @@ SchemaString.schemaName = 'String';
*/
SchemaString.prototype = Object.create(SchemaType.prototype);
SchemaString.prototype.constructor = SchemaString;
SchemaString.prototype.OptionsConstructor = SchemaStringOptions;
Object.defineProperty(SchemaString.prototype, 'OptionsConstructor', {
configurable: false,
enumerable: false,
writable: false,
value: SchemaStringOptions
});

/*!
* ignore
Expand Down Expand Up @@ -574,17 +579,23 @@ function handleArray(val) {
});
}

SchemaString.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
$all: handleArray,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle,
$options: String,
$regex: handleSingle,
$not: handleSingle
});
const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
$all: handleArray,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle,
$options: String,
$regex: handleSingle,
$not: handleSingle
});

Object.defineProperty(SchemaString.prototype, '$conditionalHandlers', {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze($conditionalHandlers)
});

/**
* Casts contents for queries.
Expand Down
9 changes: 9 additions & 0 deletions test/schema.test.js
Expand Up @@ -2135,4 +2135,13 @@ describe('schema', function() {
assert.strictEqual(schema.path('name').isRequired, false);
assert.strictEqual(schema.path('age').isRequired, false);
});

it('SchemaStringOptions line up with schema/string (gh-8256)', function() {
const SchemaStringOptions = require('../lib/options/SchemaStringOptions');
const keys = Object.keys(SchemaStringOptions.prototype).
filter(key => key !== 'constructor');
const functions = Object.keys(Schema.Types.String.prototype).
filter(key => !['constructor', 'cast', 'castForQuery', 'checkRequired'].includes(key));
assert.deepEqual(keys.sort(), functions.sort());
});
});

0 comments on commit 29c5f1a

Please sign in to comment.