Skip to content

Commit f5b0357

Browse files
committed
fix: add SchemaMapOptions class for options to map schematype
Fix #8318
1 parent a821c30 commit f5b0357

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

lib/options/SchemaMapOptions.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const SchemaTypeOptions = require('./SchemaTypeOptions');
4+
5+
/**
6+
* The options defined on a Map schematype.
7+
*
8+
* ####Example:
9+
*
10+
* const schema = new Schema({ socialMediaHandles: { type: Map, of: String } });
11+
* schema.path('socialMediaHandles').options; // SchemaMapOptions instance
12+
*
13+
* @api public
14+
* @inherits SchemaTypeOptions
15+
* @constructor SchemaMapOptions
16+
*/
17+
18+
class SchemaMapOptions extends SchemaTypeOptions {}
19+
20+
const opts = require('./propertyOptions');
21+
22+
/**
23+
* If set, specifies the type of this map's values. Mongoose will cast
24+
* this map's values to the given type.
25+
*
26+
* If not set, Mongoose will not cast the map's values.
27+
*
28+
* ####Example:
29+
*
30+
* // Mongoose will cast `socialMediaHandles` values to strings
31+
* const schema = new Schema({ socialMediaHandles: { type: Map, of: String } });
32+
* schema.path('socialMediaHandles').options.of; // String
33+
*
34+
* @api public
35+
* @property of
36+
* @memberOf SchemaMapOptions
37+
* @type Function|string
38+
* @instance
39+
*/
40+
41+
Object.defineProperty(SchemaMapOptions.prototype, 'of', opts);
42+
43+
module.exports = SchemaMapOptions;

lib/schema/map.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
const MongooseMap = require('../types/map');
8+
const SchemaMapOptions = require('../options/SchemaMapOptions');
89
const SchemaType = require('../schematype');
910

1011
/*!
@@ -42,4 +43,6 @@ class Map extends SchemaType {
4243
}
4344
}
4445

46+
Map.prototype.OptionsConstructor = SchemaMapOptions;
47+
4548
module.exports = Map;

test/types.map.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

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

9+
const SchemaMapOptions = require('../lib/options/SchemaMapOptions');
910
const assert = require('assert');
1011
const co = require('co');
1112

@@ -47,6 +48,8 @@ describe('Map', function() {
4748
}
4849
});
4950

51+
assert.ok(TestSchema.path('v').options instanceof SchemaMapOptions);
52+
5053
const Test = db.model('MapTest', TestSchema);
5154

5255
return co(function*() {

0 commit comments

Comments
 (0)