/
array.js
159 lines (136 loc) · 3.41 KB
/
array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Module dependencies.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, ArrayNumberSchema = function () {}
, Types = {
Boolean: require('./boolean')
, Date: require('./date')
, Number: ArrayNumberSchema
, String: require('./string')
, ObjectId: require('./objectid')
}
, MongooseArray = require('../types').Array
, Query = require('../query');
/**
* Array SchemaType constructor
*
* @param {String} key
* @param {SchemaType} cast
* @api private
*/
function SchemaArray (key, cast, options) {
SchemaType.call(this, key, options);
if (cast) {
this.caster = cast.name in Types ? Types[cast.name] : cast;
}
var self = this
, defaultArr
, fn;
if (this.defaultValue) {
defaultArr = this.defaultValue;
fn = 'function' == typeof defaultArr;
}
this.default(function(){
var arr = fn ? defaultArr() : defaultArr || [];
return new MongooseArray(arr, self.path, this);
});
};
/**
* Inherits from SchemaType.
*/
SchemaArray.prototype.__proto__ = SchemaType.prototype;
/**
* Check required
*
* @api private
*/
SchemaArray.prototype.checkRequired = function (value) {
return !!(value && value.length);
};
/**
* Casts contents
*
* @param {Object} value
* @param {Document} document that triggers the casting
* @api private
*/
SchemaArray.prototype.cast = function (value, doc) {
if (Array.isArray(value)){
if (!(value instanceof MongooseArray))
value = new MongooseArray(value, this.path, doc);
var caster = this.caster;
if (caster)
for (var i = 0, l = value.length; i < l; i++) {
try {
value[i] = caster.prototype.cast.call(null, value[i]);
} catch(e){
// rethrow
throw new CastError(e.type, value);
}
}
return value;
} else {
return this.cast([value], doc);
}
throw new CastError('array', value, caster);
};
SchemaArray.prototype.$conditionalHandlers = {
'$all': function (val) {
return this.cast(val);
}
// TODO Move elemMatch to documentarray
, '$elemMatch': function (val) {
var query = new Query(val);
query.cast(this.caster)
return query._conditions;
}
, '$size': function (val) {
return ArrayNumberSchema.prototype.cast.call(this, val);
}
, '$ne': function (val) {
return this.cast(val);
}
, '$in': function (val) {
return this.cast(val);
}
, '$nin': function (val) {
return this.cast(val);
}
};
SchemaArray.prototype.castForQuery = function ($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
if (!handler)
throw new Error("Can't use " + $conditional + " with Array.");
return handler.call(this, val);
} else {
val = $conditional;
if (Array.isArray(val)) {
return this.cast(val);
} else {
return this.caster.prototype.cast.call(null, val);
}
}
};
/**
* Number casting for arrays (equivalent, but without MongoseNumber)
*
* @see GH-176
* @param {Object} value
* @api private
*/
ArrayNumberSchema.prototype.cast = function (value) {
if (!isNaN(value)) {
if (value instanceof Number || typeof value == 'number' ||
(value.toString && value.toString() == Number(value)))
return Number(value);
}
throw new CastError('number', value);
};
/**
* Module exports.
*/
module.exports = SchemaArray;