Skip to content

Commit

Permalink
fix: add SchemaMapOptions class for options to map schematype
Browse files Browse the repository at this point in the history
Fix #8318
  • Loading branch information
vkarpov15 committed Nov 16, 2019
1 parent a821c30 commit f5b0357
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/options/SchemaMapOptions.js
@@ -0,0 +1,43 @@
'use strict';

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

/**
* The options defined on a Map schematype.
*
* ####Example:
*
* const schema = new Schema({ socialMediaHandles: { type: Map, of: String } });
* schema.path('socialMediaHandles').options; // SchemaMapOptions instance
*
* @api public
* @inherits SchemaTypeOptions
* @constructor SchemaMapOptions
*/

class SchemaMapOptions extends SchemaTypeOptions {}

const opts = require('./propertyOptions');

/**
* If set, specifies the type of this map's values. Mongoose will cast
* this map's values to the given type.
*
* If not set, Mongoose will not cast the map's values.
*
* ####Example:
*
* // Mongoose will cast `socialMediaHandles` values to strings
* const schema = new Schema({ socialMediaHandles: { type: Map, of: String } });
* schema.path('socialMediaHandles').options.of; // String
*
* @api public
* @property of
* @memberOf SchemaMapOptions
* @type Function|string
* @instance
*/

Object.defineProperty(SchemaMapOptions.prototype, 'of', opts);

module.exports = SchemaMapOptions;
3 changes: 3 additions & 0 deletions lib/schema/map.js
Expand Up @@ -5,6 +5,7 @@
*/

const MongooseMap = require('../types/map');
const SchemaMapOptions = require('../options/SchemaMapOptions');
const SchemaType = require('../schematype');

/*!
Expand Down Expand Up @@ -42,4 +43,6 @@ class Map extends SchemaType {
}
}

Map.prototype.OptionsConstructor = SchemaMapOptions;

module.exports = Map;
3 changes: 3 additions & 0 deletions test/types.map.test.js
Expand Up @@ -6,6 +6,7 @@

const start = require('./common');

const SchemaMapOptions = require('../lib/options/SchemaMapOptions');
const assert = require('assert');
const co = require('co');

Expand Down Expand Up @@ -47,6 +48,8 @@ describe('Map', function() {
}
});

assert.ok(TestSchema.path('v').options instanceof SchemaMapOptions);

const Test = db.model('MapTest', TestSchema);

return co(function*() {
Expand Down

0 comments on commit f5b0357

Please sign in to comment.