add buefy + vuetify libs; added script tags to index.html
This commit is contained in:
-205
@@ -1,205 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const CastError = require('../error/cast');
|
||||
const SchemaType = require('../schematype');
|
||||
const castBoolean = require('../cast/boolean');
|
||||
const utils = require('../utils');
|
||||
|
||||
/**
|
||||
* Boolean SchemaType constructor.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaBoolean(path, options) {
|
||||
SchemaType.call(this, path, options, 'Boolean');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaBoolean.schemaName = 'Boolean';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaBoolean.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaBoolean.prototype.constructor = SchemaBoolean;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaBoolean._cast = castBoolean;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to booleans.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Make Mongoose cast empty string '' to false.
|
||||
* const original = mongoose.Schema.Boolean.cast();
|
||||
* mongoose.Schema.Boolean.cast(v => {
|
||||
* if (v === '') {
|
||||
* return false;
|
||||
* }
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.Schema.Boolean.cast(false);
|
||||
*
|
||||
* @param {Function} caster
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBoolean.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (v != null && typeof v !== 'boolean') {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaBoolean._checkRequired = v => v === true || v === false;
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a boolean
|
||||
* passes the `required` check.
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBoolean.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator. For a boolean
|
||||
* to satisfy a required validator, it must be strictly equal to true or to
|
||||
* false.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype.checkRequired = function(value) {
|
||||
return this.constructor._checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Configure which values get casted to `true`.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const M = mongoose.model('Test', new Schema({ b: Boolean }));
|
||||
* new M({ b: 'affirmative' }).b; // undefined
|
||||
* mongoose.Schema.Boolean.convertToTrue.add('affirmative');
|
||||
* new M({ b: 'affirmative' }).b; // true
|
||||
*
|
||||
* @property convertToTrue
|
||||
* @type Set
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(SchemaBoolean, 'convertToTrue', {
|
||||
get: () => castBoolean.convertToTrue,
|
||||
set: v => { castBoolean.convertToTrue = v; }
|
||||
});
|
||||
|
||||
/**
|
||||
* Configure which values get casted to `false`.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* const M = mongoose.model('Test', new Schema({ b: Boolean }));
|
||||
* new M({ b: 'nay' }).b; // undefined
|
||||
* mongoose.Schema.Types.Boolean.convertToFalse.add('nay');
|
||||
* new M({ b: 'nay' }).b; // false
|
||||
*
|
||||
* @property convertToFalse
|
||||
* @type Set
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Object.defineProperty(SchemaBoolean, 'convertToFalse', {
|
||||
get: () => castBoolean.convertToFalse,
|
||||
set: v => { castBoolean.convertToFalse = v; }
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts to boolean
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Object} model - this value is optional
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype.cast = function(value) {
|
||||
try {
|
||||
return this.constructor.cast()(value);
|
||||
} catch (error) {
|
||||
throw new CastError('Boolean', value, this.path);
|
||||
}
|
||||
};
|
||||
|
||||
SchemaBoolean.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} val
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype.castForQuery = function($conditional, val) {
|
||||
let handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = SchemaBoolean.$conditionalHandlers[$conditional];
|
||||
|
||||
if (handler) {
|
||||
return handler.call(this, val);
|
||||
}
|
||||
|
||||
return this._castForQuery(val);
|
||||
}
|
||||
|
||||
return this._castForQuery($conditional);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaBoolean;
|
||||
-250
@@ -1,250 +0,0 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const handleBitwiseOperator = require('./operators/bitwise');
|
||||
const utils = require('../utils');
|
||||
|
||||
const MongooseBuffer = require('../types/buffer');
|
||||
const SchemaType = require('../schematype');
|
||||
|
||||
const Binary = MongooseBuffer.Binary;
|
||||
const CastError = SchemaType.CastError;
|
||||
let Document;
|
||||
|
||||
/**
|
||||
* Buffer SchemaType constructor
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaBuffer(key, options) {
|
||||
SchemaType.call(this, key, options, 'Buffer');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaBuffer.schemaName = 'Buffer';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaBuffer.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaBuffer.prototype.constructor = SchemaBuffer;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaBuffer._checkRequired = v => !!(v && v.length);
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Allow empty strings to pass `required` check
|
||||
* mongoose.Schema.Types.String.checkRequired(v => v != null);
|
||||
*
|
||||
* const M = mongoose.model({ buf: { type: Buffer, required: true } });
|
||||
* new M({ buf: Buffer.from('') }).validateSync(); // validation passes!
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBuffer.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator. To satisfy a
|
||||
* required validator, a buffer must not be null or undefined and have
|
||||
* non-zero length.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.checkRequired = function(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
return this.constructor._checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Document} doc document that triggers the casting
|
||||
* @param {Boolean} init
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.cast = function(value, doc, init) {
|
||||
let ret;
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (value instanceof Document) {
|
||||
value.$__.wasPopulated = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
// setting a populated path
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return value;
|
||||
} else if (!utils.isObject(value)) {
|
||||
throw new CastError('buffer', value, this.path);
|
||||
}
|
||||
|
||||
// Handle the case where user directly sets a populated
|
||||
// path to a plain object; cast to the Model used in
|
||||
// the population query.
|
||||
const path = doc.$__fullPath(this.path);
|
||||
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
const pop = owner.populated(path, true);
|
||||
ret = new pop.options.model(value);
|
||||
ret.$__.wasPopulated = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// documents
|
||||
if (value && value._id) {
|
||||
value = value._id;
|
||||
}
|
||||
|
||||
if (value && value.isMongooseBuffer) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(value)) {
|
||||
if (!value || !value.isMongooseBuffer) {
|
||||
value = new MongooseBuffer(value, [this.path, doc]);
|
||||
if (this.options.subtype != null) {
|
||||
value._subtype = this.options.subtype;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value instanceof Binary) {
|
||||
ret = new MongooseBuffer(value.value(true), [this.path, doc]);
|
||||
if (typeof value.sub_type !== 'number') {
|
||||
throw new CastError('buffer', value, this.path);
|
||||
}
|
||||
ret._subtype = value.sub_type;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
const type = typeof value;
|
||||
if (
|
||||
type === 'string' || type === 'number' || Array.isArray(value) ||
|
||||
(type === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) // gh-6863
|
||||
) {
|
||||
if (type === 'number') {
|
||||
value = [value];
|
||||
}
|
||||
ret = new MongooseBuffer(value, [this.path, doc]);
|
||||
if (this.options.subtype != null) {
|
||||
ret._subtype = this.options.subtype;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
throw new CastError('buffer', value, this.path);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
|
||||
* for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html).
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ uuid: { type: Buffer, subtype: 4 });
|
||||
* var M = db.model('M', s);
|
||||
* var m = new M({ uuid: 'test string' });
|
||||
* m.uuid._subtype; // 4
|
||||
*
|
||||
* @param {Number} subtype the default subtype
|
||||
* @return {SchemaType} this
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.subtype = function(subtype) {
|
||||
this.options.subtype = subtype;
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
function handleSingle(val) {
|
||||
return this.castForQuery(val);
|
||||
}
|
||||
|
||||
SchemaBuffer.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$bitsAllClear: handleBitwiseOperator,
|
||||
$bitsAnyClear: handleBitwiseOperator,
|
||||
$bitsAllSet: handleBitwiseOperator,
|
||||
$bitsAnySet: handleBitwiseOperator,
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [value]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBuffer.prototype.castForQuery = function($conditional, val) {
|
||||
let handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
|
||||
}
|
||||
return handler.call(this, val);
|
||||
}
|
||||
val = $conditional;
|
||||
const casted = this._castForQuery(val);
|
||||
return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaBuffer;
|
||||
-346
@@ -1,346 +0,0 @@
|
||||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('../error');
|
||||
const castDate = require('../cast/date');
|
||||
const utils = require('../utils');
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
|
||||
const CastError = SchemaType.CastError;
|
||||
|
||||
/**
|
||||
* Date SchemaType constructor.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaDate(key, options) {
|
||||
SchemaType.call(this, key, options, 'Date');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaDate.schemaName = 'Date';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaDate.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaDate.prototype.constructor = SchemaDate;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaDate._cast = castDate;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to dates.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Mongoose converts empty string '' into `null` for date types. You
|
||||
* // can create a custom caster to disable it.
|
||||
* const original = mongoose.Schema.Types.Date.cast();
|
||||
* mongoose.Schema.Types.Date.cast(v => {
|
||||
* assert.ok(v !== '');
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.Schema.Types.Date.cast(false);
|
||||
*
|
||||
* @param {Function} caster
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (v != null && !(v instanceof Date)) {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/**
|
||||
* Declares a TTL index (rounded to the nearest second) for _Date_ types only.
|
||||
*
|
||||
* This sets the `expireAfterSeconds` index option available in MongoDB >= 2.1.2.
|
||||
* This index type is only compatible with Date types.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // expire in 24 hours
|
||||
* new Schema({ createdAt: { type: Date, expires: 60*60*24 }});
|
||||
*
|
||||
* `expires` utilizes the `ms` module from [guille](https://github.com/guille/) allowing us to use a friendlier syntax:
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // expire in 24 hours
|
||||
* new Schema({ createdAt: { type: Date, expires: '24h' }});
|
||||
*
|
||||
* // expire in 1.5 hours
|
||||
* new Schema({ createdAt: { type: Date, expires: '1.5h' }});
|
||||
*
|
||||
* // expire in 7 days
|
||||
* var schema = new Schema({ createdAt: Date });
|
||||
* schema.path('createdAt').expires('7d');
|
||||
*
|
||||
* @param {Number|String} when
|
||||
* @added 3.0.0
|
||||
* @return {SchemaType} this
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.expires = function(when) {
|
||||
if (!this._index || this._index.constructor.name !== 'Object') {
|
||||
this._index = {};
|
||||
}
|
||||
|
||||
this._index.expires = when;
|
||||
utils.expires(this._index);
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaDate._checkRequired = v => v instanceof Date;
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Allow empty strings to pass `required` check
|
||||
* mongoose.Schema.Types.String.checkRequired(v => v != null);
|
||||
*
|
||||
* const M = mongoose.model({ str: { type: String, required: true } });
|
||||
* new M({ str: '' }).validateSync(); // `null`, validation passes!
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator. To satisfy
|
||||
* a required validator, the given value must be an instance of `Date`.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.checkRequired = function(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
return this.constructor._checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum date validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ d: { type: Date, min: Date('1970-01-01') })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ d: Date('1969-12-31') })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* m.d = Date('2014-12-08');
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MIN} token which will be replaced with the invalid value
|
||||
* var min = [Date('1970-01-01'), 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
||||
* var schema = new Schema({ d: { type: Date, min: min })
|
||||
* var M = mongoose.model('M', schema);
|
||||
* var s= new M({ d: Date('1969-12-31') });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `d` (1969-12-31) is before the limit (1970-01-01).
|
||||
* })
|
||||
*
|
||||
* @param {Date} value minimum date
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.min = function(value, message) {
|
||||
if (this.minValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.minValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value) {
|
||||
let msg = message || MongooseError.messages.Date.min;
|
||||
msg = msg.replace(/{MIN}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
|
||||
const _this = this;
|
||||
this.validators.push({
|
||||
validator: this.minValidator = function(val) {
|
||||
const min = (value === Date.now ? value() : _this.cast(value));
|
||||
return val === null || val.valueOf() >= min.valueOf();
|
||||
},
|
||||
message: msg,
|
||||
type: 'min',
|
||||
min: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a maximum date validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ d: { type: Date, max: Date('2014-01-01') })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ d: Date('2014-12-08') })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* m.d = Date('2013-12-31');
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAX} token which will be replaced with the invalid value
|
||||
* var max = [Date('2014-01-01'), 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
||||
* var schema = new Schema({ d: { type: Date, max: max })
|
||||
* var M = mongoose.model('M', schema);
|
||||
* var s= new M({ d: Date('2014-12-08') });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `d` (2014-12-08) exceeds the limit (2014-01-01).
|
||||
* })
|
||||
*
|
||||
* @param {Date} maximum date
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.max = function(value, message) {
|
||||
if (this.maxValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.maxValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value) {
|
||||
let msg = message || MongooseError.messages.Date.max;
|
||||
msg = msg.replace(/{MAX}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
|
||||
const _this = this;
|
||||
this.validators.push({
|
||||
validator: this.maxValidator = function(val) {
|
||||
const max = (value === Date.now ? value() : _this.cast(value));
|
||||
return val === null || val.valueOf() <= max.valueOf();
|
||||
},
|
||||
message: msg,
|
||||
type: 'max',
|
||||
max: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to date
|
||||
*
|
||||
* @param {Object} value to cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.cast = function(value) {
|
||||
const _castDate = this.constructor.cast();
|
||||
try {
|
||||
return _castDate(value);
|
||||
} catch (error) {
|
||||
throw new CastError('date', value, this.path);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Date Query casting.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
SchemaDate.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [value]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaDate.prototype.castForQuery = function($conditional, val) {
|
||||
if (arguments.length !== 2) {
|
||||
return this._castForQuery($conditional);
|
||||
}
|
||||
|
||||
const handler = this.$conditionalHandlers[$conditional];
|
||||
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional + ' with Date.');
|
||||
}
|
||||
|
||||
return handler.call(this, val);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaDate;
|
||||
-201
@@ -1,201 +0,0 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
const CastError = SchemaType.CastError;
|
||||
const Decimal128Type = require('../types/decimal128');
|
||||
const castDecimal128 = require('../cast/decimal128');
|
||||
const utils = require('../utils');
|
||||
|
||||
let Document;
|
||||
|
||||
/**
|
||||
* Decimal128 SchemaType constructor.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Decimal128(key, options) {
|
||||
SchemaType.call(this, key, options, 'Decimal128');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
Decimal128.schemaName = 'Decimal128';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
Decimal128.prototype = Object.create(SchemaType.prototype);
|
||||
Decimal128.prototype.constructor = Decimal128;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Decimal128._cast = castDecimal128;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to decimals.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Make Mongoose only refuse to cast numbers as decimal128
|
||||
* const original = mongoose.Schema.Types.Decimal128.cast();
|
||||
* mongoose.Decimal128.cast(v => {
|
||||
* assert.ok(typeof v !== 'number');
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.Decimal128.cast(false);
|
||||
*
|
||||
* @param {Function} [caster]
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Decimal128.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (v != null && !(v instanceof Decimal128Type)) {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Decimal128._checkRequired = v => v instanceof Decimal128Type;
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Decimal128.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Decimal128.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
return this.constructor._checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to Decimal128
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Object} doc
|
||||
* @param {Boolean} init whether this is an initialization cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Decimal128.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (value instanceof Document) {
|
||||
value.$__.wasPopulated = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
// setting a populated path
|
||||
if (value instanceof Decimal128Type) {
|
||||
return value;
|
||||
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
||||
throw new CastError('Decimal128', value, this.path);
|
||||
}
|
||||
|
||||
// Handle the case where user directly sets a populated
|
||||
// path to a plain object; cast to the Model used in
|
||||
// the population query.
|
||||
const path = doc.$__fullPath(this.path);
|
||||
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
const pop = owner.populated(path, true);
|
||||
let ret = value;
|
||||
if (!doc.$__.populated ||
|
||||
!doc.$__.populated[path] ||
|
||||
!doc.$__.populated[path].options ||
|
||||
!doc.$__.populated[path].options.options ||
|
||||
!doc.$__.populated[path].options.options.lean) {
|
||||
ret = new pop.options.model(value);
|
||||
ret.$__.wasPopulated = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const _castDecimal128 = this.constructor.cast();
|
||||
try {
|
||||
return _castDecimal128(value);
|
||||
} catch (error) {
|
||||
throw new CastError('Decimal128', value, this.path);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
Decimal128.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle
|
||||
});
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = Decimal128;
|
||||
-320
@@ -1,320 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const CastError = require('../error/cast');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const ObjectExpectedError = require('../error/objectExpected');
|
||||
const SchemaType = require('../schematype');
|
||||
const $exists = require('./operators/exists');
|
||||
const castToNumber = require('./operators/helpers').castToNumber;
|
||||
const discriminator = require('../helpers/model/discriminator');
|
||||
const geospatial = require('./operators/geospatial');
|
||||
const get = require('../helpers/get');
|
||||
const getDiscriminatorByValue = require('../queryhelpers').getDiscriminatorByValue;
|
||||
const internalToObjectOptions = require('../options').internalToObjectOptions;
|
||||
|
||||
let Subdocument;
|
||||
|
||||
module.exports = Embedded;
|
||||
|
||||
/**
|
||||
* Sub-schema schematype constructor
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Embedded(schema, path, options) {
|
||||
this.caster = _createConstructor(schema);
|
||||
this.caster.path = path;
|
||||
this.caster.prototype.$basePath = path;
|
||||
this.schema = schema;
|
||||
this.$isSingleNested = true;
|
||||
SchemaType.call(this, path, options, 'Embedded');
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Embedded.prototype = Object.create(SchemaType.prototype);
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _createConstructor(schema) {
|
||||
// lazy load
|
||||
Subdocument || (Subdocument = require('../types/subdocument'));
|
||||
|
||||
const _embedded = function SingleNested(value, path, parent) {
|
||||
const _this = this;
|
||||
|
||||
this.$parent = parent;
|
||||
Subdocument.apply(this, arguments);
|
||||
|
||||
this.$session(this.ownerDocument().$session());
|
||||
|
||||
if (parent) {
|
||||
parent.on('save', function() {
|
||||
_this.emit('save', _this);
|
||||
_this.constructor.emit('save', _this);
|
||||
});
|
||||
|
||||
parent.on('isNew', function(val) {
|
||||
_this.isNew = val;
|
||||
_this.emit('isNew', val);
|
||||
_this.constructor.emit('isNew', val);
|
||||
});
|
||||
}
|
||||
};
|
||||
_embedded.prototype = Object.create(Subdocument.prototype);
|
||||
_embedded.prototype.$__setSchema(schema);
|
||||
_embedded.prototype.constructor = _embedded;
|
||||
_embedded.schema = schema;
|
||||
_embedded.$isSingleNested = true;
|
||||
_embedded.events = new EventEmitter();
|
||||
_embedded.prototype.toBSON = function() {
|
||||
return this.toObject(internalToObjectOptions);
|
||||
};
|
||||
|
||||
// apply methods
|
||||
for (const i in schema.methods) {
|
||||
_embedded.prototype[i] = schema.methods[i];
|
||||
}
|
||||
|
||||
// apply statics
|
||||
for (const i in schema.statics) {
|
||||
_embedded[i] = schema.statics[i];
|
||||
}
|
||||
|
||||
for (const i in EventEmitter.prototype) {
|
||||
_embedded[i] = EventEmitter.prototype[i];
|
||||
}
|
||||
|
||||
return _embedded;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Special case for when users use a common location schema to represent
|
||||
* locations for use with $geoWithin.
|
||||
* https://docs.mongodb.org/manual/reference/operator/query/geoWithin/
|
||||
*
|
||||
* @param {Object} val
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.$conditionalHandlers.$geoWithin = function handle$geoWithin(val) {
|
||||
return { $geometry: this.castForQuery(val.$geometry) };
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Embedded.prototype.$conditionalHandlers.$near =
|
||||
Embedded.prototype.$conditionalHandlers.$nearSphere = geospatial.cast$near;
|
||||
|
||||
Embedded.prototype.$conditionalHandlers.$within =
|
||||
Embedded.prototype.$conditionalHandlers.$geoWithin = geospatial.cast$within;
|
||||
|
||||
Embedded.prototype.$conditionalHandlers.$geoIntersects =
|
||||
geospatial.cast$geoIntersects;
|
||||
|
||||
Embedded.prototype.$conditionalHandlers.$minDistance = castToNumber;
|
||||
Embedded.prototype.$conditionalHandlers.$maxDistance = castToNumber;
|
||||
|
||||
Embedded.prototype.$conditionalHandlers.$exists = $exists;
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
*
|
||||
* @param {Object} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.cast = function(val, doc, init, priorVal) {
|
||||
if (val && val.$isSingleNested) {
|
||||
return val;
|
||||
}
|
||||
|
||||
if (val != null && (typeof val !== 'object' || Array.isArray(val))) {
|
||||
throw new ObjectExpectedError(this.path, val);
|
||||
}
|
||||
|
||||
let Constructor = this.caster;
|
||||
const discriminatorKey = Constructor.schema.options.discriminatorKey;
|
||||
if (val != null &&
|
||||
Constructor.discriminators &&
|
||||
typeof val[discriminatorKey] === 'string') {
|
||||
if (Constructor.discriminators[val[discriminatorKey]]) {
|
||||
Constructor = Constructor.discriminators[val[discriminatorKey]];
|
||||
} else {
|
||||
const constructorByValue = getDiscriminatorByValue(Constructor, val[discriminatorKey]);
|
||||
if (constructorByValue) {
|
||||
Constructor = constructorByValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let subdoc;
|
||||
|
||||
// Only pull relevant selected paths and pull out the base path
|
||||
const parentSelected = get(doc, '$__.selected', {});
|
||||
const path = this.path;
|
||||
const selected = Object.keys(parentSelected).reduce((obj, key) => {
|
||||
if (key.startsWith(path + '.')) {
|
||||
obj[key.substr(path.length + 1)] = parentSelected[key];
|
||||
}
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
if (init) {
|
||||
subdoc = new Constructor(void 0, selected, doc);
|
||||
subdoc.init(val);
|
||||
} else {
|
||||
if (Object.keys(val).length === 0) {
|
||||
return new Constructor({}, selected, doc);
|
||||
}
|
||||
|
||||
return new Constructor(val, selected, doc, undefined, { priorDoc: priorVal });
|
||||
}
|
||||
|
||||
return subdoc;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for query
|
||||
*
|
||||
* @param {string} [$conditional] optional query operator (like `$eq` or `$in`)
|
||||
* @param {any} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.castForQuery = function($conditional, val) {
|
||||
let handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional);
|
||||
}
|
||||
return handler.call(this, val);
|
||||
}
|
||||
val = $conditional;
|
||||
if (val == null) {
|
||||
return val;
|
||||
}
|
||||
|
||||
if (this.options.runSetters) {
|
||||
val = this._applySetters(val);
|
||||
}
|
||||
|
||||
let Constructor = this.caster;
|
||||
const discriminatorKey = Constructor.schema.options.discriminatorKey;
|
||||
if (val != null &&
|
||||
Constructor.discriminators &&
|
||||
typeof val[discriminatorKey] === 'string') {
|
||||
if (Constructor.discriminators[val[discriminatorKey]]) {
|
||||
Constructor = Constructor.discriminators[val[discriminatorKey]];
|
||||
} else {
|
||||
const constructorByValue = getDiscriminatorByValue(Constructor, val[discriminatorKey]);
|
||||
if (constructorByValue) {
|
||||
Constructor = constructorByValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
val = new Constructor(val);
|
||||
} catch (error) {
|
||||
// Make sure we always wrap in a CastError (gh-6803)
|
||||
if (!(error instanceof CastError)) {
|
||||
throw new CastError('Embedded', val, this.path, error);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* Async validation on this single nested doc.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.doValidate = function(value, fn, scope, options) {
|
||||
let Constructor = this.caster;
|
||||
const discriminatorKey = Constructor.schema.options.discriminatorKey;
|
||||
if (value != null &&
|
||||
Constructor.discriminators &&
|
||||
typeof value[discriminatorKey] === 'string') {
|
||||
if (Constructor.discriminators[value[discriminatorKey]]) {
|
||||
Constructor = Constructor.discriminators[value[discriminatorKey]];
|
||||
} else {
|
||||
const constructorByValue = getDiscriminatorByValue(Constructor, value[discriminatorKey]);
|
||||
if (constructorByValue) {
|
||||
Constructor = constructorByValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options && options.skipSchemaValidators) {
|
||||
if (!(value instanceof Constructor)) {
|
||||
value = new Constructor(value, null, scope);
|
||||
}
|
||||
|
||||
return value.validate(fn);
|
||||
}
|
||||
|
||||
SchemaType.prototype.doValidate.call(this, value, function(error) {
|
||||
if (error) {
|
||||
return fn(error);
|
||||
}
|
||||
if (!value) {
|
||||
return fn(null);
|
||||
}
|
||||
|
||||
value.validate(fn);
|
||||
}, scope);
|
||||
};
|
||||
|
||||
/**
|
||||
* Synchronously validate this single nested doc
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Embedded.prototype.doValidateSync = function(value, scope, options) {
|
||||
if (!options || !options.skipSchemaValidators) {
|
||||
const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, value, scope);
|
||||
if (schemaTypeError) {
|
||||
return schemaTypeError;
|
||||
}
|
||||
}
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
return value.validateSync();
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a discriminator to this property
|
||||
*
|
||||
* @param {String} name
|
||||
* @param {Schema} schema fields to add to the schema for instances of this sub-class
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Embedded.prototype.discriminator = function(name, schema) {
|
||||
discriminator(this.caster, name, schema);
|
||||
|
||||
this.caster.discriminators[name] = _createConstructor(schema);
|
||||
|
||||
return this.caster.discriminators[name];
|
||||
};
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
exports.String = require('./string');
|
||||
|
||||
exports.Number = require('./number');
|
||||
|
||||
exports.Boolean = require('./boolean');
|
||||
|
||||
exports.DocumentArray = require('./documentarray');
|
||||
|
||||
exports.Embedded = require('./embedded');
|
||||
|
||||
exports.Array = require('./array');
|
||||
|
||||
exports.Buffer = require('./buffer');
|
||||
|
||||
exports.Date = require('./date');
|
||||
|
||||
exports.ObjectId = require('./objectid');
|
||||
|
||||
exports.Mixed = require('./mixed');
|
||||
|
||||
exports.Decimal128 = exports.Decimal = require('./decimal128');
|
||||
|
||||
exports.Map = require('./map');
|
||||
|
||||
// alias
|
||||
|
||||
exports.Oid = exports.ObjectId;
|
||||
exports.Object = exports.Mixed;
|
||||
exports.Bool = exports.Boolean;
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const MongooseMap = require('../types/map');
|
||||
const SchemaType = require('../schematype');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
class Map extends SchemaType {
|
||||
constructor(key, options) {
|
||||
super(key, options, 'Map');
|
||||
this.$isSchemaMap = true;
|
||||
}
|
||||
|
||||
cast(val, doc, init) {
|
||||
if (val instanceof MongooseMap) {
|
||||
return val;
|
||||
}
|
||||
|
||||
if (init) {
|
||||
const map = new MongooseMap({}, this.path, doc, this.$__schemaType);
|
||||
|
||||
for (const key of Object.keys(val)) {
|
||||
map.$init(key, this.$__schemaType.cast(val[key], doc, true));
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
return new MongooseMap(val, this.path, doc, this.$__schemaType);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Map;
|
||||
-105
@@ -1,105 +0,0 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
const symbols = require('./symbols');
|
||||
const utils = require('../utils');
|
||||
|
||||
/**
|
||||
* Mixed SchemaType constructor.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Mixed(path, options) {
|
||||
if (options && options.default) {
|
||||
const def = options.default;
|
||||
if (Array.isArray(def) && def.length === 0) {
|
||||
// make sure empty array defaults are handled
|
||||
options.default = Array;
|
||||
} else if (!options.shared && utils.isObject(def) && Object.keys(def).length === 0) {
|
||||
// prevent odd "shared" objects between documents
|
||||
options.default = function() {
|
||||
return {};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
SchemaType.call(this, path, options, 'Mixed');
|
||||
|
||||
this[symbols.schemaMixedSymbol] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
Mixed.schemaName = 'Mixed';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
Mixed.prototype = Object.create(SchemaType.prototype);
|
||||
Mixed.prototype.constructor = Mixed;
|
||||
|
||||
/**
|
||||
* Attaches a getter for all Mixed paths.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Hide the 'hidden' path
|
||||
* mongoose.Schema.Mixed.get(v => Object.assign({}, v, { hidden: null }));
|
||||
*
|
||||
* const Model = mongoose.model('Test', new Schema({ test: {} }));
|
||||
* new Model({ test: { hidden: 'Secret!' } }).test.hidden; // null
|
||||
*
|
||||
* @param {Function} getter
|
||||
* @return {this}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mixed.get = SchemaType.get;
|
||||
|
||||
/**
|
||||
* Casts `val` for Mixed.
|
||||
*
|
||||
* _this is a no-op_
|
||||
*
|
||||
* @param {Object} value to cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.cast = function(val) {
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $cond
|
||||
* @param {any} [val]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.castForQuery = function($cond, val) {
|
||||
if (arguments.length === 2) {
|
||||
return val;
|
||||
}
|
||||
return $cond;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = Mixed;
|
||||
-361
@@ -1,361 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
const MongooseError = require('../error');
|
||||
const SchemaType = require('../schematype');
|
||||
const castNumber = require('../cast/number');
|
||||
const handleBitwiseOperator = require('./operators/bitwise');
|
||||
const utils = require('../utils');
|
||||
|
||||
const CastError = SchemaType.CastError;
|
||||
let Document;
|
||||
|
||||
/**
|
||||
* Number SchemaType constructor.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaNumber(key, options) {
|
||||
SchemaType.call(this, key, options, 'Number');
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a getter for all Number instances.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Make all numbers round down
|
||||
* mongoose.Number.get(function(v) { return Math.floor(v); });
|
||||
*
|
||||
* const Model = mongoose.model('Test', new Schema({ test: Number }));
|
||||
* new Model({ test: 3.14 }).test; // 3
|
||||
*
|
||||
* @param {Function} getter
|
||||
* @return {this}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.get = SchemaType.get;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaNumber._cast = castNumber;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to numbers.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Make Mongoose cast empty strings '' to 0 for paths declared as numbers
|
||||
* const original = mongoose.Number.cast();
|
||||
* mongoose.Number.cast(v => {
|
||||
* if (v === '') { return 0; }
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.Number.cast(false);
|
||||
*
|
||||
* @param {Function} caster
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (typeof v !== 'number') {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaNumber.schemaName = 'Number';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaNumber.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaNumber.prototype.constructor = SchemaNumber;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaNumber._checkRequired = v => typeof v === 'number' || v instanceof Number;
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
return this.constructor._checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum number validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ n: { type: Number, min: 10 })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ n: 9 })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* m.n = 10;
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MIN} token which will be replaced with the invalid value
|
||||
* var min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
||||
* var schema = new Schema({ n: { type: Number, min: min })
|
||||
* var M = mongoose.model('Measurement', schema);
|
||||
* var s= new M({ n: 4 });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `n` (4) is beneath the limit (10).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value minimum number
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.min = function(value, message) {
|
||||
if (this.minValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.minValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value !== null && value !== undefined) {
|
||||
let msg = message || MongooseError.messages.Number.min;
|
||||
msg = msg.replace(/{MIN}/, value);
|
||||
this.validators.push({
|
||||
validator: this.minValidator = function(v) {
|
||||
return v == null || v >= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'min',
|
||||
min: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a maximum number validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ n: { type: Number, max: 10 })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ n: 11 })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* m.n = 10;
|
||||
* m.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAX} token which will be replaced with the invalid value
|
||||
* var max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
||||
* var schema = new Schema({ n: { type: Number, max: max })
|
||||
* var M = mongoose.model('Measurement', schema);
|
||||
* var s= new M({ n: 4 });
|
||||
* s.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `n` (4) exceeds the limit (10).
|
||||
* })
|
||||
*
|
||||
* @param {Number} maximum number
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.max = function(value, message) {
|
||||
if (this.maxValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.maxValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value !== null && value !== undefined) {
|
||||
let msg = message || MongooseError.messages.Number.max;
|
||||
msg = msg.replace(/{MAX}/, value);
|
||||
this.validators.push({
|
||||
validator: this.maxValidator = function(v) {
|
||||
return v == null || v <= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'max',
|
||||
max: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to number
|
||||
*
|
||||
* @param {Object} value value to cast
|
||||
* @param {Document} doc document that triggers the casting
|
||||
* @param {Boolean} init
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (value instanceof Document) {
|
||||
value.$__.wasPopulated = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
// setting a populated path
|
||||
if (typeof value === 'number') {
|
||||
return value;
|
||||
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
||||
throw new CastError('number', value, this.path);
|
||||
}
|
||||
|
||||
// Handle the case where user directly sets a populated
|
||||
// path to a plain object; cast to the Model used in
|
||||
// the population query.
|
||||
const path = doc.$__fullPath(this.path);
|
||||
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
const pop = owner.populated(path, true);
|
||||
const ret = new pop.options.model(value);
|
||||
ret.$__.wasPopulated = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
const val = value && typeof value._id !== 'undefined' ?
|
||||
value._id : // documents
|
||||
value;
|
||||
|
||||
try {
|
||||
return this.constructor.cast()(val);
|
||||
} catch (err) {
|
||||
throw new CastError('number', val, this.path);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
function handleArray(val) {
|
||||
const _this = this;
|
||||
if (!Array.isArray(val)) {
|
||||
return [this.cast(val)];
|
||||
}
|
||||
return val.map(function(m) {
|
||||
return _this.cast(m);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaNumber.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$bitsAllClear: handleBitwiseOperator,
|
||||
$bitsAnyClear: handleBitwiseOperator,
|
||||
$bitsAllSet: handleBitwiseOperator,
|
||||
$bitsAnySet: handleBitwiseOperator,
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle,
|
||||
$mod: handleArray
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [value]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.castForQuery = function($conditional, val) {
|
||||
let handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new CastError('Can\'t use ' + $conditional + ' with Number.');
|
||||
}
|
||||
return handler.call(this, val);
|
||||
}
|
||||
val = this._castForQuery($conditional);
|
||||
return val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaNumber;
|
||||
-282
@@ -1,282 +0,0 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const castObjectId = require('../cast/objectid');
|
||||
const SchemaType = require('../schematype');
|
||||
const oid = require('../types/objectid');
|
||||
const utils = require('../utils');
|
||||
|
||||
const CastError = SchemaType.CastError;
|
||||
let Document;
|
||||
|
||||
/**
|
||||
* ObjectId SchemaType constructor.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function ObjectId(key, options) {
|
||||
const isKeyHexStr = typeof key === 'string' && key.length === 24 && /^[a-f0-9]+$/i.test(key);
|
||||
const suppressWarning = options && options.suppressWarning;
|
||||
if ((isKeyHexStr || typeof key === 'undefined') && !suppressWarning) {
|
||||
console.warn('mongoose: To create a new ObjectId please try ' +
|
||||
'`Mongoose.Types.ObjectId` instead of using ' +
|
||||
'`Mongoose.Schema.ObjectId`. Set the `suppressWarning` option if ' +
|
||||
'you\'re trying to create a hex char path in your schema.');
|
||||
console.trace();
|
||||
}
|
||||
SchemaType.call(this, key, options, 'ObjectID');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
ObjectId.schemaName = 'ObjectId';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
ObjectId.prototype = Object.create(SchemaType.prototype);
|
||||
ObjectId.prototype.constructor = ObjectId;
|
||||
|
||||
/**
|
||||
* Attaches a getter for all ObjectId instances
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Always convert to string when getting an ObjectId
|
||||
* mongoose.ObjectId.get(v => v.toString());
|
||||
*
|
||||
* const Model = mongoose.model('Test', new Schema({}));
|
||||
* typeof (new Model({})._id); // 'string'
|
||||
*
|
||||
* @param {Function} getter
|
||||
* @return {this}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ObjectId.get = SchemaType.get;
|
||||
|
||||
/**
|
||||
* Adds an auto-generated ObjectId default if turnOn is true.
|
||||
* @param {Boolean} turnOn auto generated ObjectId defaults
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
ObjectId.prototype.auto = function(turnOn) {
|
||||
if (turnOn) {
|
||||
this.default(defaultId);
|
||||
this.set(resetId);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
ObjectId._checkRequired = v => v instanceof oid;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
ObjectId._cast = castObjectId;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to objectids.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Make Mongoose only try to cast length 24 strings. By default, any 12
|
||||
* // char string is a valid ObjectId.
|
||||
* const original = mongoose.ObjectId.cast();
|
||||
* mongoose.ObjectId.cast(v => {
|
||||
* assert.ok(typeof v !== 'string' || v.length === 24);
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.ObjectId.cast(false);
|
||||
*
|
||||
* @param {Function} caster
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ObjectId.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (!(v instanceof oid)) {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Allow empty strings to pass `required` check
|
||||
* mongoose.Schema.Types.String.checkRequired(v => v != null);
|
||||
*
|
||||
* const M = mongoose.model({ str: { type: String, required: true } });
|
||||
* new M({ str: '' }).validateSync(); // `null`, validation passes!
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ObjectId.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
return this.constructor._checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to ObjectId
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Object} doc
|
||||
* @param {Boolean} init whether this is an initialization cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
ObjectId.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (value instanceof Document) {
|
||||
value.$__.wasPopulated = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
// setting a populated path
|
||||
if (value instanceof oid) {
|
||||
return value;
|
||||
} else if ((value.constructor.name || '').toLowerCase() === 'objectid') {
|
||||
return new oid(value.toHexString());
|
||||
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
||||
throw new CastError('ObjectId', value, this.path);
|
||||
}
|
||||
|
||||
// Handle the case where user directly sets a populated
|
||||
// path to a plain object; cast to the Model used in
|
||||
// the population query.
|
||||
const path = doc.$__fullPath(this.path);
|
||||
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
const pop = owner.populated(path, true);
|
||||
let ret = value;
|
||||
if (!doc.$__.populated ||
|
||||
!doc.$__.populated[path] ||
|
||||
!doc.$__.populated[path].options ||
|
||||
!doc.$__.populated[path].options.options ||
|
||||
!doc.$__.populated[path].options.options.lean) {
|
||||
ret = new pop.options.model(value);
|
||||
ret.$__.wasPopulated = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
try {
|
||||
return this.constructor.cast()(value);
|
||||
} catch (error) {
|
||||
throw new CastError('ObjectId', value, this.path);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
ObjectId.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle
|
||||
});
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function defaultId() {
|
||||
return new oid();
|
||||
}
|
||||
|
||||
defaultId.$runBeforeSetters = true;
|
||||
|
||||
function resetId(v) {
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (this instanceof Document) {
|
||||
if (v === void 0) {
|
||||
const _v = new oid;
|
||||
this.$__._id = _v;
|
||||
return _v;
|
||||
}
|
||||
|
||||
this.$__._id = v;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = ObjectId;
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const CastError = require('../../error/cast');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleBitwiseOperator(val) {
|
||||
const _this = this;
|
||||
if (Array.isArray(val)) {
|
||||
return val.map(function(v) {
|
||||
return _castNumber(_this.path, v);
|
||||
});
|
||||
} else if (Buffer.isBuffer(val)) {
|
||||
return val;
|
||||
}
|
||||
// Assume trying to cast to number
|
||||
return _castNumber(_this.path, val);
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _castNumber(path, num) {
|
||||
const v = Number(num);
|
||||
if (isNaN(v)) {
|
||||
throw new CastError('number', num, path);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
module.exports = handleBitwiseOperator;
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const castBoolean = require('../../cast/boolean');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(val) {
|
||||
const path = this != null ? this.path : null;
|
||||
return castBoolean(val, path);
|
||||
};
|
||||
-102
@@ -1,102 +0,0 @@
|
||||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const castArraysOfNumbers = require('./helpers').castArraysOfNumbers;
|
||||
const castToNumber = require('./helpers').castToNumber;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
exports.cast$geoIntersects = cast$geoIntersects;
|
||||
exports.cast$near = cast$near;
|
||||
exports.cast$within = cast$within;
|
||||
|
||||
function cast$near(val) {
|
||||
const SchemaArray = require('../array');
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
castArraysOfNumbers(val, this);
|
||||
return val;
|
||||
}
|
||||
|
||||
_castMinMaxDistance(this, val);
|
||||
|
||||
if (val && val.$geometry) {
|
||||
return cast$geometry(val, this);
|
||||
}
|
||||
|
||||
return SchemaArray.prototype.castForQuery.call(this, val);
|
||||
}
|
||||
|
||||
function cast$geometry(val, self) {
|
||||
switch (val.$geometry.type) {
|
||||
case 'Polygon':
|
||||
case 'LineString':
|
||||
case 'Point':
|
||||
castArraysOfNumbers(val.$geometry.coordinates, self);
|
||||
break;
|
||||
default:
|
||||
// ignore unknowns
|
||||
break;
|
||||
}
|
||||
|
||||
_castMinMaxDistance(self, val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
function cast$within(val) {
|
||||
_castMinMaxDistance(this, val);
|
||||
|
||||
if (val.$box || val.$polygon) {
|
||||
const type = val.$box ? '$box' : '$polygon';
|
||||
val[type].forEach(arr => {
|
||||
if (!Array.isArray(arr)) {
|
||||
const msg = 'Invalid $within $box argument. '
|
||||
+ 'Expected an array, received ' + arr;
|
||||
throw new TypeError(msg);
|
||||
}
|
||||
arr.forEach((v, i) => {
|
||||
arr[i] = castToNumber.call(this, v);
|
||||
});
|
||||
});
|
||||
} else if (val.$center || val.$centerSphere) {
|
||||
const type = val.$center ? '$center' : '$centerSphere';
|
||||
val[type].forEach((item, i) => {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach((v, j) => {
|
||||
item[j] = castToNumber.call(this, v);
|
||||
});
|
||||
} else {
|
||||
val[type][i] = castToNumber.call(this, item);
|
||||
}
|
||||
});
|
||||
} else if (val.$geometry) {
|
||||
cast$geometry(val, this);
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
function cast$geoIntersects(val) {
|
||||
const geo = val.$geometry;
|
||||
if (!geo) {
|
||||
return;
|
||||
}
|
||||
|
||||
cast$geometry(val, this);
|
||||
return val;
|
||||
}
|
||||
|
||||
function _castMinMaxDistance(self, val) {
|
||||
if (val.$maxDistance) {
|
||||
val.$maxDistance = castToNumber.call(self, val.$maxDistance);
|
||||
}
|
||||
if (val.$minDistance) {
|
||||
val.$minDistance = castToNumber.call(self, val.$minDistance);
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
const SchemaNumber = require('../number');
|
||||
|
||||
/*!
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
exports.castToNumber = castToNumber;
|
||||
exports.castArraysOfNumbers = castArraysOfNumbers;
|
||||
|
||||
/*!
|
||||
* @ignore
|
||||
*/
|
||||
|
||||
function castToNumber(val) {
|
||||
return SchemaNumber.cast()(val);
|
||||
}
|
||||
|
||||
function castArraysOfNumbers(arr, self) {
|
||||
arr.forEach(function(v, i) {
|
||||
if (Array.isArray(v)) {
|
||||
castArraysOfNumbers(v, self);
|
||||
} else {
|
||||
arr[i] = castToNumber.call(self, v);
|
||||
}
|
||||
});
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const CastError = require('../../error/cast');
|
||||
const castBoolean = require('../../cast/boolean');
|
||||
const castString = require('../../cast/string');
|
||||
|
||||
/*!
|
||||
* Casts val to an object suitable for `$text`. Throws an error if the object
|
||||
* can't be casted.
|
||||
*
|
||||
* @param {Any} val value to cast
|
||||
* @param {String} [path] path to associate with any errors that occured
|
||||
* @return {Object} casted object
|
||||
* @see https://docs.mongodb.com/manual/reference/operator/query/text/
|
||||
* @api private
|
||||
*/
|
||||
|
||||
module.exports = function(val, path) {
|
||||
if (val == null || typeof val !== 'object') {
|
||||
throw new CastError('$text', val, path);
|
||||
}
|
||||
|
||||
if (val.$search != null) {
|
||||
val.$search = castString(val.$search, path + '.$search');
|
||||
}
|
||||
if (val.$language != null) {
|
||||
val.$language = castString(val.$language, path + '.$language');
|
||||
}
|
||||
if (val.$caseSensitive != null) {
|
||||
val.$caseSensitive = castBoolean(val.$caseSensitive,
|
||||
path + '.$castSensitive');
|
||||
}
|
||||
if (val.$diacriticSensitive != null) {
|
||||
val.$diacriticSensitive = castBoolean(val.$diacriticSensitive,
|
||||
path + '.$diacriticSensitive');
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(val) {
|
||||
if (typeof val !== 'number' && typeof val !== 'string') {
|
||||
throw new Error('$type parameter must be number or string');
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
-604
@@ -1,604 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
const CastError = SchemaType.CastError;
|
||||
const MongooseError = require('../error');
|
||||
const castString = require('../cast/string');
|
||||
const utils = require('../utils');
|
||||
|
||||
let Document;
|
||||
|
||||
/**
|
||||
* String SchemaType constructor.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaString(key, options) {
|
||||
this.enumValues = [];
|
||||
this.regExp = null;
|
||||
SchemaType.call(this, key, options, 'String');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaString.schemaName = 'String';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaString.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaString.prototype.constructor = SchemaString;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaString._cast = castString;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to strings.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Throw an error if you pass in an object. Normally, Mongoose allows
|
||||
* // objects with custom `toString()` functions.
|
||||
* const original = mongoose.Schema.Types.String.cast();
|
||||
* mongoose.Schema.Types.String.cast(v => {
|
||||
* assert.ok(v == null || typeof v !== 'object');
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.Schema.Types.String.cast(false);
|
||||
*
|
||||
* @param {Function} caster
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = v => {
|
||||
if (v != null && typeof v !== 'string') {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/**
|
||||
* Attaches a getter for all String instances.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Make all numbers round down
|
||||
* mongoose.Schema.String.get(v => v.toLowerCase());
|
||||
*
|
||||
* const Model = mongoose.model('Test', new Schema({ test: String }));
|
||||
* new Model({ test: 'FOO' }).test; // 'foo'
|
||||
*
|
||||
* @param {Function} getter
|
||||
* @return {this}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.get = SchemaType.get;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaString._checkRequired = v => (v instanceof String || typeof v === 'string') && v.length;
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether a string
|
||||
* passes the `required` check.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* // Allow empty strings to pass `required` check
|
||||
* mongoose.Schema.Types.String.checkRequired(v => v != null);
|
||||
*
|
||||
* const M = mongoose.model({ str: { type: String, required: true } });
|
||||
* new M({ str: '' }).validateSync(); // `null`, validation passes!
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Adds an enum validator
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var states = ['opening', 'open', 'closing', 'closed']
|
||||
* var s = new Schema({ state: { type: String, enum: states }})
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ state: 'invalid' })
|
||||
* m.save(function (err) {
|
||||
* console.error(String(err)) // ValidationError: `invalid` is not a valid enum value for path `state`.
|
||||
* m.state = 'open'
|
||||
* m.save(callback) // success
|
||||
* })
|
||||
*
|
||||
* // or with custom error messages
|
||||
* var enum = {
|
||||
* values: ['opening', 'open', 'closing', 'closed'],
|
||||
* message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
||||
* }
|
||||
* var s = new Schema({ state: { type: String, enum: enum })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ state: 'invalid' })
|
||||
* m.save(function (err) {
|
||||
* console.error(String(err)) // ValidationError: enum validator failed for path `state` with value `invalid`
|
||||
* m.state = 'open'
|
||||
* m.save(callback) // success
|
||||
* })
|
||||
*
|
||||
* @param {String|Object} [args...] enumeration values
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.enum = function() {
|
||||
if (this.enumValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.enumValidator;
|
||||
}, this);
|
||||
this.enumValidator = false;
|
||||
}
|
||||
|
||||
if (arguments[0] === void 0 || arguments[0] === false) {
|
||||
return this;
|
||||
}
|
||||
|
||||
let values;
|
||||
let errorMessage;
|
||||
|
||||
if (utils.isObject(arguments[0])) {
|
||||
values = arguments[0].values;
|
||||
errorMessage = arguments[0].message;
|
||||
} else {
|
||||
values = arguments;
|
||||
errorMessage = MongooseError.messages.String.enum;
|
||||
}
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
if (undefined !== values[i]) {
|
||||
this.enumValues.push(this.cast(values[i]));
|
||||
}
|
||||
}
|
||||
|
||||
const vals = this.enumValues;
|
||||
this.enumValidator = function(v) {
|
||||
return undefined === v || ~vals.indexOf(v);
|
||||
};
|
||||
this.validators.push({
|
||||
validator: this.enumValidator,
|
||||
message: errorMessage,
|
||||
type: 'enum',
|
||||
enumValues: vals
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a lowercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ email: { type: String, lowercase: true }})
|
||||
* var M = db.model('M', s);
|
||||
* var m = new M({ email: 'SomeEmail@example.COM' });
|
||||
* console.log(m.email) // someemail@example.com
|
||||
* M.find({ email: 'SomeEmail@example.com' }); // Queries by 'someemail@example.com'
|
||||
*
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.lowercase = function(shouldApply) {
|
||||
if (arguments.length > 0 && !shouldApply) {
|
||||
return this;
|
||||
}
|
||||
return this.set(function(v, self) {
|
||||
if (typeof v !== 'string') {
|
||||
v = self.cast(v);
|
||||
}
|
||||
if (v) {
|
||||
return v.toLowerCase();
|
||||
}
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds an uppercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ caps: { type: String, uppercase: true }})
|
||||
* var M = db.model('M', s);
|
||||
* var m = new M({ caps: 'an example' });
|
||||
* console.log(m.caps) // AN EXAMPLE
|
||||
* M.find({ caps: 'an example' }) // Matches documents where caps = 'AN EXAMPLE'
|
||||
*
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.uppercase = function(shouldApply) {
|
||||
if (arguments.length > 0 && !shouldApply) {
|
||||
return this;
|
||||
}
|
||||
return this.set(function(v, self) {
|
||||
if (typeof v !== 'string') {
|
||||
v = self.cast(v);
|
||||
}
|
||||
if (v) {
|
||||
return v.toUpperCase();
|
||||
}
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a trim [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set).
|
||||
*
|
||||
* The string value will be trimmed when set.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, trim: true }})
|
||||
* var M = db.model('M', s)
|
||||
* var string = ' some name '
|
||||
* console.log(string.length) // 11
|
||||
* var m = new M({ name: string })
|
||||
* console.log(m.name.length) // 9
|
||||
*
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.trim = function(shouldTrim) {
|
||||
if (arguments.length > 0 && !shouldTrim) {
|
||||
return this;
|
||||
}
|
||||
return this.set(function(v, self) {
|
||||
if (typeof v !== 'string') {
|
||||
v = self.cast(v);
|
||||
}
|
||||
if (v) {
|
||||
return v.trim();
|
||||
}
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum length validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ postalCode: { type: String, minlength: 5 })
|
||||
* var Address = db.model('Address', schema)
|
||||
* var address = new Address({ postalCode: '9512' })
|
||||
* address.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* address.postalCode = '95125';
|
||||
* address.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MINLENGTH} token which will be replaced with the minimum allowed length
|
||||
* var minlength = [5, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).'];
|
||||
* var schema = new Schema({ postalCode: { type: String, minlength: minlength })
|
||||
* var Address = mongoose.model('Address', schema);
|
||||
* var address = new Address({ postalCode: '9512' });
|
||||
* address.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512`) is shorter than the minimum length (5).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value minimum string length
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.minlength = function(value, message) {
|
||||
if (this.minlengthValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.minlengthValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value !== null && value !== undefined) {
|
||||
let msg = message || MongooseError.messages.String.minlength;
|
||||
msg = msg.replace(/{MINLENGTH}/, value);
|
||||
this.validators.push({
|
||||
validator: this.minlengthValidator = function(v) {
|
||||
return v === null || v.length >= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'minlength',
|
||||
minlength: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a maximum length validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ postalCode: { type: String, maxlength: 9 })
|
||||
* var Address = db.model('Address', schema)
|
||||
* var address = new Address({ postalCode: '9512512345' })
|
||||
* address.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* address.postalCode = '95125';
|
||||
* address.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAXLENGTH} token which will be replaced with the maximum allowed length
|
||||
* var maxlength = [9, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
|
||||
* var schema = new Schema({ postalCode: { type: String, maxlength: maxlength })
|
||||
* var Address = mongoose.model('Address', schema);
|
||||
* var address = new Address({ postalCode: '9512512345' });
|
||||
* address.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512512345`) exceeds the maximum allowed length (9).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value maximum string length
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.maxlength = function(value, message) {
|
||||
if (this.maxlengthValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.maxlengthValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (value !== null && value !== undefined) {
|
||||
let msg = message || MongooseError.messages.String.maxlength;
|
||||
msg = msg.replace(/{MAXLENGTH}/, value);
|
||||
this.validators.push({
|
||||
validator: this.maxlengthValidator = function(v) {
|
||||
return v === null || v.length <= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'maxlength',
|
||||
maxlength: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a regexp validator.
|
||||
*
|
||||
* Any value that does not pass `regExp`.test(val) will fail validation.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, match: /^a/ }})
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ name: 'I am invalid' })
|
||||
* m.validate(function (err) {
|
||||
* console.error(String(err)) // "ValidationError: Path `name` is invalid (I am invalid)."
|
||||
* m.name = 'apples'
|
||||
* m.validate(function (err) {
|
||||
* assert.ok(err) // success
|
||||
* })
|
||||
* })
|
||||
*
|
||||
* // using a custom error message
|
||||
* var match = [ /\.html$/, "That file doesn't end in .html ({VALUE})" ];
|
||||
* var s = new Schema({ file: { type: String, match: match }})
|
||||
* var M = db.model('M', s);
|
||||
* var m = new M({ file: 'invalid' });
|
||||
* m.validate(function (err) {
|
||||
* console.log(String(err)) // "ValidationError: That file doesn't end in .html (invalid)"
|
||||
* })
|
||||
*
|
||||
* Empty strings, `undefined`, and `null` values always pass the match validator. If you require these values, enable the `required` validator also.
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, match: /^a/, required: true }})
|
||||
*
|
||||
* @param {RegExp} regExp regular expression to test against
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.match = function match(regExp, message) {
|
||||
// yes, we allow multiple match validators
|
||||
|
||||
const msg = message || MongooseError.messages.String.match;
|
||||
|
||||
const matchValidator = function(v) {
|
||||
if (!regExp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const ret = ((v != null && v !== '')
|
||||
? regExp.test(v)
|
||||
: true);
|
||||
return ret;
|
||||
};
|
||||
|
||||
this.validators.push({
|
||||
validator: matchValidator,
|
||||
message: msg,
|
||||
type: 'regexp',
|
||||
regexp: regExp
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies the `required` validator. The value is
|
||||
* considered valid if it is a string (that is, not `null` or `undefined`) and
|
||||
* has positive length. The `required` validator **will** fail for empty
|
||||
* strings.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
return this.constructor._checkRequired(value, doc);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to String
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
Document || (Document = require('./../document'));
|
||||
|
||||
if (value instanceof Document) {
|
||||
value.$__.wasPopulated = true;
|
||||
return value;
|
||||
}
|
||||
|
||||
// setting a populated path
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
|
||||
throw new CastError('string', value, this.path);
|
||||
}
|
||||
|
||||
// Handle the case where user directly sets a populated
|
||||
// path to a plain object; cast to the Model used in
|
||||
// the population query.
|
||||
const path = doc.$__fullPath(this.path);
|
||||
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
|
||||
const pop = owner.populated(path, true);
|
||||
const ret = new pop.options.model(value);
|
||||
ret.$__.wasPopulated = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
try {
|
||||
return this.constructor.cast()(value);
|
||||
} catch (error) {
|
||||
throw new CastError('string', value, this.path);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.castForQuery(val);
|
||||
}
|
||||
|
||||
function handleArray(val) {
|
||||
const _this = this;
|
||||
if (!Array.isArray(val)) {
|
||||
return [this.castForQuery(val)];
|
||||
}
|
||||
return val.map(function(m) {
|
||||
return _this.castForQuery(m);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaString.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$all: handleArray,
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle,
|
||||
$options: handleSingle,
|
||||
$regex: handleSingle,
|
||||
$not: handleSingle
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [val]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.castForQuery = function($conditional, val) {
|
||||
let handler;
|
||||
if (arguments.length === 2) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional + ' with String.');
|
||||
}
|
||||
return handler.call(this, val);
|
||||
}
|
||||
val = $conditional;
|
||||
if (Object.prototype.toString.call(val) === '[object RegExp]') {
|
||||
return val;
|
||||
}
|
||||
|
||||
return this._castForQuery(val);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaString;
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
exports.schemaMixedSymbol = Symbol.for('mongoose:schema_mixed');
|
||||
|
||||
exports.builtInMiddleware = Symbol.for('mongoose:built-in-middleware');
|
||||
Reference in New Issue
Block a user