arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/mongoose/lib/schema/mixed.js
node_modules/mongoose/lib/schema/mixed.jsRaw
 1
 2/*!
 3 * Module dependencies.
 4 */
 5
 6var SchemaType = require('../schematype');
 7var utils = require('../utils');
 8
 9/**
10 * Mixed SchemaType constructor.
11 *
12 * @param {String} path
13 * @param {Object} options
14 * @inherits SchemaType
15 * @api private
16 */
17
18function Mixed (path, options) {
19  if (options && options.default) {
20    var def = options.default;
21    if (Array.isArray(def) && 0 === def.length) {
22      // make sure empty array defaults are handled
23      options.default = Array;
24    } else if (!options.shared &&
25               utils.isObject(def) &&
26               0 === Object.keys(def).length) {
27      // prevent odd "shared" objects between documents
28      options.default = function () {
29        return {}
30      }
31    }
32  }
33
34  SchemaType.call(this, path, options);
35};
36
37/*!
38 * Inherits from SchemaType.
39 */
40
41Mixed.prototype.__proto__ = SchemaType.prototype;
42
43/**
44 * Required validator
45 *
46 * @api private
47 */
48
49Mixed.prototype.checkRequired = function (val) {
50  return true;
51};
52
53/**
54 * Casts `val` for Mixed.
55 *
56 * _this is a no-op_
57 *
58 * @param {Object} value to cast
59 * @api private
60 */
61
62Mixed.prototype.cast = function (val) {
63  return val;
64};
65
66/**
67 * Casts contents for queries.
68 *
69 * @param {String} $cond
70 * @param {any} [val]
71 * @api private
72 */
73
74Mixed.prototype.castForQuery = function ($cond, val) {
75  if (arguments.length === 2) return val;
76  return $cond;
77};
78
79/*!
80 * Module exports.
81 */
82
83module.exports = Mixed;