mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-24 01:29:19 +02:00
some progress
This commit is contained in:
parent
aea93a5527
commit
e3c15bd288
1388 changed files with 306946 additions and 68323 deletions
366
node_modules/mongoose/lib/schema/SubdocumentPath.js
generated
vendored
Normal file
366
node_modules/mongoose/lib/schema/SubdocumentPath.js
generated
vendored
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const CastError = require('../error/cast');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const ObjectExpectedError = require('../error/objectExpected');
|
||||
const SchemaSubdocumentOptions = require('../options/SchemaSubdocumentOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const applyDefaults = require('../helpers/document/applyDefaults');
|
||||
const $exists = require('./operators/exists');
|
||||
const castToNumber = require('./operators/helpers').castToNumber;
|
||||
const discriminator = require('../helpers/model/discriminator');
|
||||
const geospatial = require('./operators/geospatial');
|
||||
const getConstructor = require('../helpers/discriminator/getConstructor');
|
||||
const handleIdOption = require('../helpers/schema/handleIdOption');
|
||||
const internalToObjectOptions = require('../options').internalToObjectOptions;
|
||||
const utils = require('../utils');
|
||||
|
||||
let Subdocument;
|
||||
|
||||
module.exports = SubdocumentPath;
|
||||
|
||||
/**
|
||||
* Single nested subdocument SchemaType constructor.
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @param {String} path
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SubdocumentPath(schema, path, options) {
|
||||
const schemaTypeIdOption = SubdocumentPath.defaultOptions &&
|
||||
SubdocumentPath.defaultOptions._id;
|
||||
if (schemaTypeIdOption != null) {
|
||||
options = options || {};
|
||||
options._id = schemaTypeIdOption;
|
||||
}
|
||||
|
||||
schema = handleIdOption(schema, options);
|
||||
|
||||
this.caster = _createConstructor(schema);
|
||||
this.caster.path = path;
|
||||
this.caster.prototype.$basePath = path;
|
||||
this.schema = schema;
|
||||
this.$isSingleNested = true;
|
||||
this.base = schema.base;
|
||||
SchemaType.call(this, path, options, 'Embedded');
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype = Object.create(SchemaType.prototype);
|
||||
SubdocumentPath.prototype.constructor = SubdocumentPath;
|
||||
SubdocumentPath.prototype.OptionsConstructor = SchemaSubdocumentOptions;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _createConstructor(schema, baseClass) {
|
||||
// lazy load
|
||||
Subdocument || (Subdocument = require('../types/subdocument'));
|
||||
|
||||
const _embedded = function SingleNested(value, path, parent) {
|
||||
this.$__parent = parent;
|
||||
Subdocument.apply(this, arguments);
|
||||
|
||||
if (parent == null) {
|
||||
return;
|
||||
}
|
||||
this.$session(parent.$session());
|
||||
};
|
||||
|
||||
schema._preCompile();
|
||||
|
||||
const proto = baseClass != null ? baseClass.prototype : Subdocument.prototype;
|
||||
_embedded.prototype = Object.create(proto);
|
||||
_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://www.mongodb.com/docs/manual/reference/operator/query/geoWithin/
|
||||
*
|
||||
* @param {Object} val
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$geoWithin = function handle$geoWithin(val, context) {
|
||||
return { $geometry: this.castForQuery(null, val.$geometry, context) };
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$near =
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$nearSphere = geospatial.cast$near;
|
||||
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$within =
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$geoWithin = geospatial.cast$within;
|
||||
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$geoIntersects =
|
||||
geospatial.cast$geoIntersects;
|
||||
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$minDistance = castToNumber;
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$maxDistance = castToNumber;
|
||||
|
||||
SubdocumentPath.prototype.$conditionalHandlers.$exists = $exists;
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
*
|
||||
* @param {Object} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
|
||||
if (val && val.$isSingleNested && val.parent === doc) {
|
||||
return val;
|
||||
}
|
||||
|
||||
if (val != null && (typeof val !== 'object' || Array.isArray(val))) {
|
||||
throw new ObjectExpectedError(this.path, val);
|
||||
}
|
||||
|
||||
const Constructor = getConstructor(this.caster, val);
|
||||
|
||||
let subdoc;
|
||||
|
||||
// Only pull relevant selected paths and pull out the base path
|
||||
const parentSelected = doc && doc.$__ && doc.$__.selected || {};
|
||||
const path = this.path;
|
||||
const selected = Object.keys(parentSelected).reduce((obj, key) => {
|
||||
if (key.startsWith(path + '.')) {
|
||||
obj = obj || {};
|
||||
obj[key.substring(path.length + 1)] = parentSelected[key];
|
||||
}
|
||||
return obj;
|
||||
}, null);
|
||||
options = Object.assign({}, options, { priorDoc: priorVal });
|
||||
if (init) {
|
||||
subdoc = new Constructor(void 0, selected, doc, false, { defaults: false });
|
||||
delete subdoc.$__.defaults;
|
||||
subdoc.$init(val);
|
||||
applyDefaults(subdoc, selected);
|
||||
} else {
|
||||
if (Object.keys(val).length === 0) {
|
||||
return new Constructor({}, selected, doc, undefined, options);
|
||||
}
|
||||
|
||||
return new Constructor(val, selected, doc, undefined, options);
|
||||
}
|
||||
|
||||
return subdoc;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for query
|
||||
*
|
||||
* @param {string} [$conditional] optional query operator (like `$eq` or `$in`)
|
||||
* @param {any} value
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.castForQuery = function($conditional, val, context, options) {
|
||||
let handler;
|
||||
if ($conditional != null) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional);
|
||||
}
|
||||
return handler.call(this, val);
|
||||
}
|
||||
if (val == null) {
|
||||
return val;
|
||||
}
|
||||
|
||||
if (this.options.runSetters) {
|
||||
val = this._applySetters(val, context);
|
||||
}
|
||||
|
||||
const Constructor = getConstructor(this.caster, val);
|
||||
const overrideStrict = options != null && options.strict != null ?
|
||||
options.strict :
|
||||
void 0;
|
||||
|
||||
try {
|
||||
val = new Constructor(val, overrideStrict);
|
||||
} catch (error) {
|
||||
// Make sure we always wrap in a CastError (gh-6803)
|
||||
if (!(error instanceof CastError)) {
|
||||
throw new CastError('Embedded', val, this.path, error, this);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* Async validation on this single nested doc.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.doValidate = function(value, fn, scope, options) {
|
||||
const Constructor = getConstructor(this.caster, value);
|
||||
|
||||
if (value && !(value instanceof Constructor)) {
|
||||
value = new Constructor(value, null, (scope != null && scope.$__ != null) ? scope : null);
|
||||
}
|
||||
|
||||
if (options && options.skipSchemaValidators) {
|
||||
if (!value) {
|
||||
return fn(null);
|
||||
}
|
||||
return value.validate().then(() => fn(null), err => fn(err));
|
||||
}
|
||||
|
||||
SchemaType.prototype.doValidate.call(this, value, function(error) {
|
||||
if (error) {
|
||||
return fn(error);
|
||||
}
|
||||
if (!value) {
|
||||
return fn(null);
|
||||
}
|
||||
|
||||
value.validate().then(() => fn(null), err => fn(err));
|
||||
}, scope, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Synchronously validate this single nested doc
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SubdocumentPath.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 single nested subdocument.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
|
||||
* const schema = Schema({ shape: shapeSchema });
|
||||
*
|
||||
* const singleNestedPath = parentSchema.path('shape');
|
||||
* singleNestedPath.discriminator('Circle', Schema({ radius: Number }));
|
||||
*
|
||||
* @param {String} name
|
||||
* @param {Schema} schema fields to add to the schema for instances of this sub-class
|
||||
* @param {Object|string} [options] If string, same as `options.value`.
|
||||
* @param {String} [options.value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
|
||||
* @param {Boolean} [options.clone=true] By default, `discriminator()` clones the given `schema`. Set to `false` to skip cloning.
|
||||
* @return {Function} the constructor Mongoose will use for creating instances of this discriminator model
|
||||
* @see discriminators /docs/discriminators.html
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.discriminator = function(name, schema, options) {
|
||||
options = options || {};
|
||||
const value = utils.isPOJO(options) ? options.value : options;
|
||||
const clone = typeof options.clone === 'boolean'
|
||||
? options.clone
|
||||
: true;
|
||||
|
||||
if (schema.instanceOfSchema && clone) {
|
||||
schema = schema.clone();
|
||||
}
|
||||
|
||||
schema = discriminator(this.caster, name, schema, value);
|
||||
|
||||
this.caster.discriminators[name] = _createConstructor(schema, this.caster);
|
||||
|
||||
return this.caster.discriminators[name];
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SubdocumentPath.defaultOptions = {};
|
||||
|
||||
/**
|
||||
* Sets a default option for all SubdocumentPath instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all numbers have option `min` equal to 0.
|
||||
* mongoose.Schema.SubdocumentPath.set('required', true);
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {void}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SubdocumentPath.set = SchemaType.set;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.toJSON = function toJSON() {
|
||||
return { path: this.path, options: this.options };
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SubdocumentPath.prototype.clone = function() {
|
||||
const options = Object.assign({}, this.options);
|
||||
const schematype = new this.constructor(this.schema, this.path, options);
|
||||
schematype.validators = this.validators.slice();
|
||||
if (this.requiredValidator !== undefined) {
|
||||
schematype.requiredValidator = this.requiredValidator;
|
||||
}
|
||||
schematype.caster.discriminators = Object.assign({}, this.caster.discriminators);
|
||||
return schematype;
|
||||
};
|
||||
675
node_modules/mongoose/lib/schema/array.js
generated
vendored
Normal file
675
node_modules/mongoose/lib/schema/array.js
generated
vendored
Normal file
|
|
@ -0,0 +1,675 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const $exists = require('./operators/exists');
|
||||
const $type = require('./operators/type');
|
||||
const MongooseError = require('../error/mongooseError');
|
||||
const SchemaArrayOptions = require('../options/SchemaArrayOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const CastError = SchemaType.CastError;
|
||||
const Mixed = require('./mixed');
|
||||
const arrayDepth = require('../helpers/arrayDepth');
|
||||
const cast = require('../cast');
|
||||
const clone = require('../helpers/clone');
|
||||
const isOperator = require('../helpers/query/isOperator');
|
||||
const util = require('util');
|
||||
const utils = require('../utils');
|
||||
const castToNumber = require('./operators/helpers').castToNumber;
|
||||
const geospatial = require('./operators/geospatial');
|
||||
const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
|
||||
|
||||
let MongooseArray;
|
||||
let EmbeddedDoc;
|
||||
|
||||
const isNestedArraySymbol = Symbol('mongoose#isNestedArray');
|
||||
const emptyOpts = Object.freeze({});
|
||||
|
||||
/**
|
||||
* Array SchemaType constructor
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {SchemaType} cast
|
||||
* @param {Object} options
|
||||
* @param {Object} schemaOptions
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaArray(key, cast, options, schemaOptions) {
|
||||
// lazy load
|
||||
EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
|
||||
|
||||
let typeKey = 'type';
|
||||
if (schemaOptions && schemaOptions.typeKey) {
|
||||
typeKey = schemaOptions.typeKey;
|
||||
}
|
||||
this.schemaOptions = schemaOptions;
|
||||
|
||||
if (cast) {
|
||||
let castOptions = {};
|
||||
|
||||
if (utils.isPOJO(cast)) {
|
||||
if (cast[typeKey]) {
|
||||
// support { type: Woot }
|
||||
castOptions = clone(cast); // do not alter user arguments
|
||||
delete castOptions[typeKey];
|
||||
cast = cast[typeKey];
|
||||
} else {
|
||||
cast = Mixed;
|
||||
}
|
||||
}
|
||||
|
||||
if (options != null && options.ref != null && castOptions.ref == null) {
|
||||
castOptions.ref = options.ref;
|
||||
}
|
||||
|
||||
if (cast === Object) {
|
||||
cast = Mixed;
|
||||
}
|
||||
|
||||
// support { type: 'String' }
|
||||
const name = typeof cast === 'string'
|
||||
? cast
|
||||
: utils.getFunctionName(cast);
|
||||
|
||||
const Types = require('./index.js');
|
||||
const caster = Types.hasOwnProperty(name) ? Types[name] : cast;
|
||||
|
||||
this.casterConstructor = caster;
|
||||
|
||||
if (this.casterConstructor instanceof SchemaArray) {
|
||||
this.casterConstructor[isNestedArraySymbol] = true;
|
||||
}
|
||||
|
||||
if (typeof caster === 'function' &&
|
||||
!caster.$isArraySubdocument &&
|
||||
!caster.$isSchemaMap) {
|
||||
const path = this.caster instanceof EmbeddedDoc ? null : key;
|
||||
this.caster = new caster(path, castOptions);
|
||||
} else {
|
||||
this.caster = caster;
|
||||
if (!(this.caster instanceof EmbeddedDoc)) {
|
||||
this.caster.path = key;
|
||||
}
|
||||
}
|
||||
|
||||
this.$embeddedSchemaType = this.caster;
|
||||
}
|
||||
|
||||
this.$isMongooseArray = true;
|
||||
|
||||
SchemaType.call(this, key, options, 'Array');
|
||||
|
||||
let defaultArr;
|
||||
let fn;
|
||||
|
||||
if (this.defaultValue != null) {
|
||||
defaultArr = this.defaultValue;
|
||||
fn = typeof defaultArr === 'function';
|
||||
}
|
||||
|
||||
if (!('defaultValue' in this) || this.defaultValue !== void 0) {
|
||||
const defaultFn = function() {
|
||||
// Leave it up to `cast()` to convert the array
|
||||
return fn
|
||||
? defaultArr.call(this)
|
||||
: defaultArr != null
|
||||
? [].concat(defaultArr)
|
||||
: [];
|
||||
};
|
||||
defaultFn.$runBeforeSetters = !fn;
|
||||
this.default(defaultFn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaArray.schemaName = 'Array';
|
||||
|
||||
|
||||
/**
|
||||
* Options for all arrays.
|
||||
*
|
||||
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting.
|
||||
*
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaArray.options = { castNonArrays: true };
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaArray.defaultOptions = {};
|
||||
|
||||
/**
|
||||
* Sets a default option for all Array instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all Array instances have `required` of true by default.
|
||||
* mongoose.Schema.Array.set('required', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ test: Array }));
|
||||
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @api public
|
||||
*/
|
||||
SchemaArray.set = SchemaType.set;
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaArray.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaArray.prototype.constructor = SchemaArray;
|
||||
SchemaArray.prototype.OptionsConstructor = SchemaArrayOptions;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaArray._checkRequired = SchemaType.prototype.checkRequired;
|
||||
|
||||
/**
|
||||
* Override the function the required validator uses to check whether an array
|
||||
* passes the `required` check.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Require non-empty array to pass `required` check
|
||||
* mongoose.Schema.Types.Array.checkRequired(v => Array.isArray(v) && v.length);
|
||||
*
|
||||
* const M = mongoose.model({ arr: { type: Array, required: true } });
|
||||
* new M({ arr: [] }).validateSync(); // `null`, validation fails!
|
||||
*
|
||||
* @param {Function} fn
|
||||
* @return {Function}
|
||||
* @function checkRequired
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaArray.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies the `required` validator.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @param {Document} doc
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
|
||||
if (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
|
||||
return !!value;
|
||||
}
|
||||
|
||||
// `require('util').inherits()` does **not** copy static properties, and
|
||||
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
||||
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
||||
this.constructor.checkRequired() :
|
||||
SchemaArray.checkRequired();
|
||||
|
||||
return _checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds an enum validator if this is an array of strings or numbers. Equivalent to
|
||||
* `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
|
||||
*
|
||||
* @param {...String|Object} [args] enumeration values
|
||||
* @return {SchemaArray} this
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.enum = function() {
|
||||
let arr = this;
|
||||
while (true) {
|
||||
const instance = arr &&
|
||||
arr.caster &&
|
||||
arr.caster.instance;
|
||||
if (instance === 'Array') {
|
||||
arr = arr.caster;
|
||||
continue;
|
||||
}
|
||||
if (instance !== 'String' && instance !== 'Number') {
|
||||
throw new Error('`enum` can only be set on an array of strings or numbers ' +
|
||||
', not ' + instance);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
let enumArray = arguments;
|
||||
if (!Array.isArray(arguments) && utils.isObject(arguments)) {
|
||||
enumArray = utils.object.vals(enumArray);
|
||||
}
|
||||
|
||||
arr.caster.enum.apply(arr.caster, enumArray);
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Overrides the getters application for the population special-case
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Object} scope
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.applyGetters = function(value, scope) {
|
||||
if (scope != null && scope.$__ != null && scope.$populated(this.path)) {
|
||||
// means the object id was populated
|
||||
return value;
|
||||
}
|
||||
|
||||
const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
|
||||
if (Array.isArray(ret)) {
|
||||
const rawValue = utils.isMongooseArray(ret) ? ret.__array : ret;
|
||||
const len = rawValue.length;
|
||||
for (let i = 0; i < len; ++i) {
|
||||
rawValue[i] = this.caster.applyGetters(rawValue[i], scope);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
|
||||
if (this.casterConstructor.$isMongooseArray &&
|
||||
SchemaArray.options.castNonArrays &&
|
||||
!this[isNestedArraySymbol]) {
|
||||
// Check nesting levels and wrap in array if necessary
|
||||
let depth = 0;
|
||||
let arr = this;
|
||||
while (arr != null &&
|
||||
arr.$isMongooseArray &&
|
||||
!arr.$isMongooseDocumentArray) {
|
||||
++depth;
|
||||
arr = arr.casterConstructor;
|
||||
}
|
||||
|
||||
// No need to wrap empty arrays
|
||||
if (value != null && value.length !== 0) {
|
||||
const valueDepth = arrayDepth(value);
|
||||
if (valueDepth.min === valueDepth.max && valueDepth.max < depth && valueDepth.containsNonArrayItem) {
|
||||
for (let i = valueDepth.max; i < depth; ++i) {
|
||||
value = [value];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return SchemaType.prototype._applySetters.call(this, value, scope, init, priorVal);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts values for set().
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Document} doc document that triggers the casting
|
||||
* @param {Boolean} init whether this is an initialization cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
|
||||
// lazy load
|
||||
MongooseArray || (MongooseArray = require('../types').Array);
|
||||
|
||||
let i;
|
||||
let l;
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
const len = value.length;
|
||||
if (!len && doc) {
|
||||
const indexes = doc.schema.indexedPaths();
|
||||
|
||||
const arrayPath = this.path;
|
||||
for (i = 0, l = indexes.length; i < l; ++i) {
|
||||
const pathIndex = indexes[i][0][arrayPath];
|
||||
if (pathIndex === '2dsphere' || pathIndex === '2d') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Special case: if this index is on the parent of what looks like
|
||||
// GeoJSON, skip setting the default to empty array re: #1668, #3233
|
||||
const arrayGeojsonPath = this.path.endsWith('.coordinates') ?
|
||||
this.path.substring(0, this.path.lastIndexOf('.')) : null;
|
||||
if (arrayGeojsonPath != null) {
|
||||
for (i = 0, l = indexes.length; i < l; ++i) {
|
||||
const pathIndex = indexes[i][0][arrayGeojsonPath];
|
||||
if (pathIndex === '2dsphere') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
options = options || emptyOpts;
|
||||
|
||||
let rawValue = utils.isMongooseArray(value) ? value.__array : value;
|
||||
value = MongooseArray(rawValue, options.path || this._arrayPath || this.path, doc, this);
|
||||
rawValue = value.__array;
|
||||
|
||||
if (init && doc != null && doc.$__ != null && doc.$populated(this.path)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const caster = this.caster;
|
||||
const isMongooseArray = caster.$isMongooseArray;
|
||||
if (caster && this.casterConstructor !== Mixed) {
|
||||
try {
|
||||
const len = rawValue.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
const opts = {};
|
||||
// Perf: creating `arrayPath` is expensive for large arrays.
|
||||
// We only need `arrayPath` if this is a nested array, so
|
||||
// skip if possible.
|
||||
if (isMongooseArray) {
|
||||
if (options.arrayPath != null) {
|
||||
opts.arrayPathIndex = i;
|
||||
} else if (caster._arrayParentPath != null) {
|
||||
opts.arrayPathIndex = i;
|
||||
}
|
||||
}
|
||||
rawValue[i] = caster.applySetters(rawValue[i], doc, init, void 0, opts);
|
||||
}
|
||||
} catch (e) {
|
||||
// rethrow
|
||||
throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
const castNonArraysOption = this.options.castNonArrays != null ? this.options.castNonArrays : SchemaArray.options.castNonArrays;
|
||||
if (init || castNonArraysOption) {
|
||||
// gh-2442: if we're loading this from the db and its not an array, mark
|
||||
// the whole array as modified.
|
||||
if (!!doc && !!init) {
|
||||
doc.markModified(this.path);
|
||||
}
|
||||
return this.cast([value], doc, init);
|
||||
}
|
||||
|
||||
throw new CastError('Array', util.inspect(value), this.path, null, this);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaArray.prototype._castForPopulate = function _castForPopulate(value, doc) {
|
||||
// lazy load
|
||||
MongooseArray || (MongooseArray = require('../types').Array);
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
let i;
|
||||
const rawValue = value.__array ? value.__array : value;
|
||||
const len = rawValue.length;
|
||||
|
||||
const caster = this.caster;
|
||||
if (caster && this.casterConstructor !== Mixed) {
|
||||
try {
|
||||
for (i = 0; i < len; i++) {
|
||||
const opts = {};
|
||||
// Perf: creating `arrayPath` is expensive for large arrays.
|
||||
// We only need `arrayPath` if this is a nested array, so
|
||||
// skip if possible.
|
||||
if (caster.$isMongooseArray && caster._arrayParentPath != null) {
|
||||
opts.arrayPathIndex = i;
|
||||
}
|
||||
|
||||
rawValue[i] = caster.cast(rawValue[i], doc, false, void 0, opts);
|
||||
}
|
||||
} catch (e) {
|
||||
// rethrow
|
||||
throw new CastError('[' + e.kind + ']', util.inspect(value), this.path + '.' + i, e, this);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
throw new CastError('Array', util.inspect(value), this.path, null, this);
|
||||
};
|
||||
|
||||
SchemaArray.prototype.$toObject = SchemaArray.prototype.toObject;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.discriminator = function(name, schema) {
|
||||
let arr = this;
|
||||
while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
|
||||
arr = arr.casterConstructor;
|
||||
if (arr == null || typeof arr === 'function') {
|
||||
throw new MongooseError('You can only add an embedded discriminator on ' +
|
||||
'a document array, ' + this.path + ' is a plain array');
|
||||
}
|
||||
}
|
||||
return arr.discriminator(name, schema);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.clone = function() {
|
||||
const options = Object.assign({}, this.options);
|
||||
const schematype = new this.constructor(this.path, this.caster, options, this.schemaOptions);
|
||||
schematype.validators = this.validators.slice();
|
||||
if (this.requiredValidator !== undefined) {
|
||||
schematype.requiredValidator = this.requiredValidator;
|
||||
}
|
||||
return schematype;
|
||||
};
|
||||
|
||||
SchemaArray.prototype._castForQuery = function(val, context) {
|
||||
let Constructor = this.casterConstructor;
|
||||
|
||||
if (val &&
|
||||
Constructor.discriminators &&
|
||||
Constructor.schema &&
|
||||
Constructor.schema.options &&
|
||||
Constructor.schema.options.discriminatorKey) {
|
||||
if (typeof val[Constructor.schema.options.discriminatorKey] === 'string' &&
|
||||
Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]]) {
|
||||
Constructor = Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]];
|
||||
} else {
|
||||
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, val[Constructor.schema.options.discriminatorKey]);
|
||||
if (constructorByValue) {
|
||||
Constructor = constructorByValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const proto = this.casterConstructor.prototype;
|
||||
const protoCastForQuery = proto && proto.castForQuery;
|
||||
const protoCast = proto && proto.cast;
|
||||
const constructorCastForQuery = Constructor.castForQuery;
|
||||
const caster = this.caster;
|
||||
|
||||
if (Array.isArray(val)) {
|
||||
this.setters.reverse().forEach(setter => {
|
||||
val = setter.call(this, val, this);
|
||||
});
|
||||
val = val.map(function(v) {
|
||||
if (utils.isObject(v) && v.$elemMatch) {
|
||||
return v;
|
||||
}
|
||||
if (protoCastForQuery) {
|
||||
v = protoCastForQuery.call(caster, null, v, context);
|
||||
return v;
|
||||
} else if (protoCast) {
|
||||
v = protoCast.call(caster, v);
|
||||
return v;
|
||||
} else if (constructorCastForQuery) {
|
||||
v = constructorCastForQuery.call(caster, null, v, context);
|
||||
return v;
|
||||
}
|
||||
if (v != null) {
|
||||
v = new Constructor(v);
|
||||
return v;
|
||||
}
|
||||
return v;
|
||||
});
|
||||
} else if (protoCastForQuery) {
|
||||
val = protoCastForQuery.call(caster, null, val, context);
|
||||
} else if (protoCast) {
|
||||
val = protoCast.call(caster, val);
|
||||
} else if (constructorCastForQuery) {
|
||||
val = constructorCastForQuery.call(caster, null, val, context);
|
||||
} else if (val != null) {
|
||||
val = new Constructor(val);
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts values for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [value]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaArray.prototype.castForQuery = function($conditional, val, context) {
|
||||
let handler;
|
||||
|
||||
if ($conditional != null) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional + ' with Array.');
|
||||
}
|
||||
|
||||
return handler.call(this, val, context);
|
||||
} else {
|
||||
return this._castForQuery(val, context);
|
||||
}
|
||||
};
|
||||
|
||||
function cast$all(val, context) {
|
||||
if (!Array.isArray(val)) {
|
||||
val = [val];
|
||||
}
|
||||
|
||||
val = val.map((v) => {
|
||||
if (!utils.isObject(v)) {
|
||||
return v;
|
||||
}
|
||||
if (v.$elemMatch != null) {
|
||||
return { $elemMatch: cast(this.casterConstructor.schema, v.$elemMatch, null, this && this.$$context) };
|
||||
}
|
||||
|
||||
const o = {};
|
||||
o[this.path] = v;
|
||||
return cast(this.casterConstructor.schema, o, null, this && this.$$context)[this.path];
|
||||
}, this);
|
||||
|
||||
return this.castForQuery(null, val, context);
|
||||
}
|
||||
|
||||
function cast$elemMatch(val, context) {
|
||||
const keys = Object.keys(val);
|
||||
const numKeys = keys.length;
|
||||
for (let i = 0; i < numKeys; ++i) {
|
||||
const key = keys[i];
|
||||
const value = val[key];
|
||||
if (isOperator(key) && value != null) {
|
||||
val[key] = this.castForQuery(key, value, context);
|
||||
}
|
||||
}
|
||||
|
||||
// Is this an embedded discriminator and is the discriminator key set?
|
||||
// If so, use the discriminator schema. See gh-7449
|
||||
const discriminatorKey = this &&
|
||||
this.casterConstructor &&
|
||||
this.casterConstructor.schema &&
|
||||
this.casterConstructor.schema.options &&
|
||||
this.casterConstructor.schema.options.discriminatorKey;
|
||||
const discriminators = this &&
|
||||
this.casterConstructor &&
|
||||
this.casterConstructor.schema &&
|
||||
this.casterConstructor.schema.discriminators || {};
|
||||
if (discriminatorKey != null &&
|
||||
val[discriminatorKey] != null &&
|
||||
discriminators[val[discriminatorKey]] != null) {
|
||||
return cast(discriminators[val[discriminatorKey]], val, null, this && this.$$context);
|
||||
}
|
||||
|
||||
return cast(this.casterConstructor.schema, val, null, this && this.$$context);
|
||||
}
|
||||
|
||||
const handle = SchemaArray.prototype.$conditionalHandlers = {};
|
||||
|
||||
handle.$all = cast$all;
|
||||
handle.$options = String;
|
||||
handle.$elemMatch = cast$elemMatch;
|
||||
handle.$geoIntersects = geospatial.cast$geoIntersects;
|
||||
handle.$or = createLogicalQueryOperatorHandler('$or');
|
||||
handle.$and = createLogicalQueryOperatorHandler('$and');
|
||||
handle.$nor = createLogicalQueryOperatorHandler('$nor');
|
||||
|
||||
function createLogicalQueryOperatorHandler(op) {
|
||||
return function logicalQueryOperatorHandler(val) {
|
||||
if (!Array.isArray(val)) {
|
||||
throw new TypeError('conditional ' + op + ' requires an array');
|
||||
}
|
||||
|
||||
const ret = [];
|
||||
for (const obj of val) {
|
||||
ret.push(cast(this.casterConstructor.schema, obj, null, this && this.$$context));
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
}
|
||||
|
||||
handle.$near =
|
||||
handle.$nearSphere = geospatial.cast$near;
|
||||
|
||||
handle.$within =
|
||||
handle.$geoWithin = geospatial.cast$within;
|
||||
|
||||
handle.$size =
|
||||
handle.$minDistance =
|
||||
handle.$maxDistance = castToNumber;
|
||||
|
||||
handle.$exists = $exists;
|
||||
handle.$type = $type;
|
||||
|
||||
handle.$eq =
|
||||
handle.$gt =
|
||||
handle.$gte =
|
||||
handle.$lt =
|
||||
handle.$lte =
|
||||
handle.$not =
|
||||
handle.$regex =
|
||||
handle.$ne = SchemaArray.prototype._castForQuery;
|
||||
|
||||
// `$in` is special because you can also include an empty array in the query
|
||||
// like `$in: [1, []]`, see gh-5913
|
||||
handle.$nin = SchemaType.prototype.$conditionalHandlers.$nin;
|
||||
handle.$in = SchemaType.prototype.$conditionalHandlers.$in;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaArray;
|
||||
267
node_modules/mongoose/lib/schema/boolean.js
generated
vendored
Normal file
267
node_modules/mongoose/lib/schema/boolean.js
generated
vendored
Normal file
|
|
@ -0,0 +1,267 @@
|
|||
'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';
|
||||
|
||||
SchemaBoolean.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaBoolean.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaBoolean.prototype.constructor = SchemaBoolean;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaBoolean._cast = castBoolean;
|
||||
|
||||
/**
|
||||
* Sets a default option for all Boolean instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all booleans have `default` of false.
|
||||
* mongoose.Schema.Boolean.set('default', false);
|
||||
*
|
||||
* const Order = mongoose.model('Order', new Schema({ isPaid: Boolean }));
|
||||
* new Order({ }).isPaid; // false
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBoolean.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* 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 = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaBoolean._defaultCaster = v => {
|
||||
if (v != null && typeof v !== 'boolean') {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/*!
|
||||
* 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) {
|
||||
let castBoolean;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castBoolean = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castBoolean = this.constructor.cast();
|
||||
} else {
|
||||
castBoolean = SchemaBoolean.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castBoolean(value);
|
||||
} catch (error) {
|
||||
throw new CastError('Boolean', value, this.path, error, this);
|
||||
}
|
||||
};
|
||||
|
||||
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, context) {
|
||||
let handler;
|
||||
if ($conditional != null) {
|
||||
handler = SchemaBoolean.$conditionalHandlers[$conditional];
|
||||
|
||||
if (handler) {
|
||||
return handler.call(this, val);
|
||||
}
|
||||
|
||||
return this.applySetters(null, val, context);
|
||||
}
|
||||
|
||||
return this.applySetters(val, context);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaBoolean.prototype._castNullish = function _castNullish(v) {
|
||||
if (typeof v === 'undefined') {
|
||||
return v;
|
||||
}
|
||||
const castBoolean = typeof this.constructor.cast === 'function' ?
|
||||
this.constructor.cast() :
|
||||
SchemaBoolean.cast();
|
||||
if (castBoolean == null) {
|
||||
return v;
|
||||
}
|
||||
if (castBoolean.convertToFalse instanceof Set && castBoolean.convertToFalse.has(v)) {
|
||||
return false;
|
||||
}
|
||||
if (castBoolean.convertToTrue instanceof Set && castBoolean.convertToTrue.has(v)) {
|
||||
return true;
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaBoolean;
|
||||
268
node_modules/mongoose/lib/schema/buffer.js
generated
vendored
Normal file
268
node_modules/mongoose/lib/schema/buffer.js
generated
vendored
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseBuffer = require('../types/buffer');
|
||||
const SchemaBufferOptions = require('../options/SchemaBufferOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const handleBitwiseOperator = require('./operators/bitwise');
|
||||
const utils = require('../utils');
|
||||
|
||||
const Binary = MongooseBuffer.Binary;
|
||||
const CastError = SchemaType.CastError;
|
||||
|
||||
/**
|
||||
* 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';
|
||||
|
||||
SchemaBuffer.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaBuffer.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaBuffer.prototype.constructor = SchemaBuffer;
|
||||
SchemaBuffer.prototype.OptionsConstructor = SchemaBufferOptions;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaBuffer._checkRequired = v => !!(v && v.length);
|
||||
|
||||
/**
|
||||
* Sets a default option for all Buffer instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all buffers have `required` of true by default.
|
||||
* mongoose.Schema.Buffer.set('required', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ test: Buffer }));
|
||||
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaBuffer.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
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, null, this);
|
||||
}
|
||||
ret._subtype = value.sub_type;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (value == null || utils.isNonBuiltinObject(value)) {
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
}
|
||||
|
||||
// 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, null, this);
|
||||
}
|
||||
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, null, this);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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](https://api.mongodb.com/python/current/api/bson/binary.html).
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ uuid: { type: Buffer, subtype: 4 });
|
||||
* const M = db.model('M', s);
|
||||
* const 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, context) {
|
||||
return this.castForQuery(null, val, context);
|
||||
}
|
||||
|
||||
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, context) {
|
||||
let handler;
|
||||
if ($conditional != null) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional + ' with Buffer.');
|
||||
}
|
||||
return handler.call(this, val);
|
||||
}
|
||||
const casted = this.applySetters(val, context);
|
||||
return casted ? casted.toObject({ transform: false, virtuals: false }) : casted;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaBuffer;
|
||||
404
node_modules/mongoose/lib/schema/date.js
generated
vendored
Normal file
404
node_modules/mongoose/lib/schema/date.js
generated
vendored
Normal file
|
|
@ -0,0 +1,404 @@
|
|||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseError = require('../error/index');
|
||||
const SchemaDateOptions = require('../options/SchemaDateOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const castDate = require('../cast/date');
|
||||
const getConstructorName = require('../helpers/getConstructorName');
|
||||
const utils = require('../utils');
|
||||
|
||||
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';
|
||||
|
||||
SchemaDate.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaDate.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaDate.prototype.constructor = SchemaDate;
|
||||
SchemaDate.prototype.OptionsConstructor = SchemaDateOptions;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaDate._cast = castDate;
|
||||
|
||||
/**
|
||||
* Sets a default option for all Date instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all dates have `required` of true by default.
|
||||
* mongoose.Schema.Date.set('required', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ test: Date }));
|
||||
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaDate.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* 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 = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaDate._defaultCaster = v => {
|
||||
if (v != null && !(v instanceof Date)) {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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
|
||||
* const 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 (getConstructorName(this._index) !== '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 (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
|
||||
return value != null;
|
||||
}
|
||||
|
||||
// `require('util').inherits()` does **not** copy static properties, and
|
||||
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
||||
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
||||
this.constructor.checkRequired() :
|
||||
SchemaDate.checkRequired();
|
||||
return _checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum date validator.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ d: { type: Date, min: Date('1970-01-01') })
|
||||
* const M = db.model('M', s)
|
||||
* const 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
|
||||
* const min = [Date('1970-01-01'), 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
||||
* const schema = new Schema({ d: { type: Date, min: min })
|
||||
* const M = mongoose.model('M', schema);
|
||||
* const 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;
|
||||
if (typeof msg === 'string') {
|
||||
msg = msg.replace(/{MIN}/, (value === Date.now ? 'Date.now()' : value.toString()));
|
||||
}
|
||||
const _this = this;
|
||||
this.validators.push({
|
||||
validator: this.minValidator = function(val) {
|
||||
let _value = value;
|
||||
if (typeof value === 'function' && value !== Date.now) {
|
||||
_value = _value.call(this);
|
||||
}
|
||||
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:
|
||||
*
|
||||
* const s = new Schema({ d: { type: Date, max: Date('2014-01-01') })
|
||||
* const M = db.model('M', s)
|
||||
* const 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
|
||||
* const max = [Date('2014-01-01'), 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
||||
* const schema = new Schema({ d: { type: Date, max: max })
|
||||
* const M = mongoose.model('M', schema);
|
||||
* const 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;
|
||||
if (typeof msg === 'string') {
|
||||
msg = msg.replace(/{MAX}/, (value === Date.now ? 'Date.now()' : value.toString()));
|
||||
}
|
||||
const _this = this;
|
||||
this.validators.push({
|
||||
validator: this.maxValidator = function(val) {
|
||||
let _value = value;
|
||||
if (typeof _value === 'function' && _value !== Date.now) {
|
||||
_value = _value.call(this);
|
||||
}
|
||||
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) {
|
||||
let castDate;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castDate = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castDate = this.constructor.cast();
|
||||
} else {
|
||||
castDate = SchemaDate.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castDate(value);
|
||||
} catch (error) {
|
||||
throw new CastError('date', value, this.path, error, this);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Date Query casting.
|
||||
*
|
||||
* @param {Any} val
|
||||
* @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, context) {
|
||||
if ($conditional == null) {
|
||||
return this.applySetters(val, context);
|
||||
}
|
||||
|
||||
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;
|
||||
210
node_modules/mongoose/lib/schema/decimal128.js
generated
vendored
Normal file
210
node_modules/mongoose/lib/schema/decimal128.js
generated
vendored
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
const CastError = SchemaType.CastError;
|
||||
const castDecimal128 = require('../cast/decimal128');
|
||||
const utils = require('../utils');
|
||||
const isBsonType = require('../helpers/isBsonType');
|
||||
|
||||
/**
|
||||
* 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';
|
||||
|
||||
Decimal128.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
Decimal128.prototype = Object.create(SchemaType.prototype);
|
||||
Decimal128.prototype.constructor = Decimal128;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Decimal128._cast = castDecimal128;
|
||||
|
||||
/**
|
||||
* Sets a default option for all Decimal128 instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all decimal 128s have `required` of true by default.
|
||||
* mongoose.Schema.Decimal128.set('required', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ test: mongoose.Decimal128 }));
|
||||
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Decimal128.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* 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 = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Decimal128._defaultCaster = v => {
|
||||
if (v != null && !isBsonType(v, 'Decimal128')) {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
Decimal128._checkRequired = v => isBsonType(v, 'Decimal128');
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
// `require('util').inherits()` does **not** copy static properties, and
|
||||
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
||||
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
||||
this.constructor.checkRequired() :
|
||||
Decimal128.checkRequired();
|
||||
|
||||
return _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)) {
|
||||
if (isBsonType(value, 'Decimal128')) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
|
||||
let castDecimal128;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castDecimal128 = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castDecimal128 = this.constructor.cast();
|
||||
} else {
|
||||
castDecimal128 = Decimal128.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castDecimal128(value);
|
||||
} catch (error) {
|
||||
throw new CastError('Decimal128', value, this.path, error, this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* 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;
|
||||
619
node_modules/mongoose/lib/schema/documentarray.js
generated
vendored
Normal file
619
node_modules/mongoose/lib/schema/documentarray.js
generated
vendored
Normal file
|
|
@ -0,0 +1,619 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const ArrayType = require('./array');
|
||||
const CastError = require('../error/cast');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const SchemaDocumentArrayOptions =
|
||||
require('../options/SchemaDocumentArrayOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const SubdocumentPath = require('./SubdocumentPath');
|
||||
const discriminator = require('../helpers/model/discriminator');
|
||||
const handleIdOption = require('../helpers/schema/handleIdOption');
|
||||
const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
|
||||
const utils = require('../utils');
|
||||
const getConstructor = require('../helpers/discriminator/getConstructor');
|
||||
|
||||
const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
|
||||
const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
|
||||
const documentArrayParent = require('../helpers/symbols').documentArrayParent;
|
||||
|
||||
let MongooseDocumentArray;
|
||||
let Subdocument;
|
||||
|
||||
/**
|
||||
* SubdocsArray SchemaType constructor
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Schema} schema
|
||||
* @param {Object} options
|
||||
* @param {Object} schemaOptions
|
||||
* @inherits SchemaArray
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function DocumentArrayPath(key, schema, options, schemaOptions) {
|
||||
const schemaTypeIdOption = DocumentArrayPath.defaultOptions &&
|
||||
DocumentArrayPath.defaultOptions._id;
|
||||
if (schemaTypeIdOption != null) {
|
||||
schemaOptions = schemaOptions || {};
|
||||
schemaOptions._id = schemaTypeIdOption;
|
||||
}
|
||||
|
||||
if (schemaOptions != null && schemaOptions._id != null) {
|
||||
schema = handleIdOption(schema, schemaOptions);
|
||||
} else if (options != null && options._id != null) {
|
||||
schema = handleIdOption(schema, options);
|
||||
}
|
||||
|
||||
const EmbeddedDocument = _createConstructor(schema, options);
|
||||
EmbeddedDocument.prototype.$basePath = key;
|
||||
|
||||
ArrayType.call(this, key, EmbeddedDocument, options);
|
||||
|
||||
this.schema = schema;
|
||||
this.schemaOptions = schemaOptions || {};
|
||||
this.$isMongooseDocumentArray = true;
|
||||
this.Constructor = EmbeddedDocument;
|
||||
|
||||
EmbeddedDocument.base = schema.base;
|
||||
|
||||
const fn = this.defaultValue;
|
||||
|
||||
if (!('defaultValue' in this) || fn !== void 0) {
|
||||
this.default(function() {
|
||||
let arr = fn.call(this);
|
||||
if (arr != null && !Array.isArray(arr)) {
|
||||
arr = [arr];
|
||||
}
|
||||
// Leave it up to `cast()` to convert this to a documentarray
|
||||
return arr;
|
||||
});
|
||||
}
|
||||
|
||||
const parentSchemaType = this;
|
||||
this.$embeddedSchemaType = new SchemaType(key + '.$', {
|
||||
required: this &&
|
||||
this.schemaOptions &&
|
||||
this.schemaOptions.required || false
|
||||
});
|
||||
this.$embeddedSchemaType.cast = function(value, doc, init) {
|
||||
return parentSchemaType.cast(value, doc, init)[0];
|
||||
};
|
||||
this.$embeddedSchemaType.doValidate = function(value, fn, scope, options) {
|
||||
const Constructor = getConstructor(this.caster, value);
|
||||
|
||||
if (value && !(value instanceof Constructor)) {
|
||||
value = new Constructor(value, scope, null, null, options && options.index != null ? options.index : null);
|
||||
}
|
||||
|
||||
return SubdocumentPath.prototype.doValidate.call(this, value, fn, scope, options);
|
||||
};
|
||||
this.$embeddedSchemaType.$isMongooseDocumentArrayElement = true;
|
||||
this.$embeddedSchemaType.caster = this.Constructor;
|
||||
this.$embeddedSchemaType.schema = this.schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
DocumentArrayPath.schemaName = 'DocumentArray';
|
||||
|
||||
/**
|
||||
* Options for all document arrays.
|
||||
*
|
||||
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
DocumentArrayPath.options = { castNonArrays: true };
|
||||
|
||||
/*!
|
||||
* Inherits from ArrayType.
|
||||
*/
|
||||
DocumentArrayPath.prototype = Object.create(ArrayType.prototype);
|
||||
DocumentArrayPath.prototype.constructor = DocumentArrayPath;
|
||||
DocumentArrayPath.prototype.OptionsConstructor = SchemaDocumentArrayOptions;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function _createConstructor(schema, options, baseClass) {
|
||||
Subdocument || (Subdocument = require('../types/ArraySubdocument'));
|
||||
|
||||
// compile an embedded document for this schema
|
||||
function EmbeddedDocument() {
|
||||
Subdocument.apply(this, arguments);
|
||||
if (this.__parentArray == null || this.__parentArray.getArrayParent() == null) {
|
||||
return;
|
||||
}
|
||||
this.$session(this.__parentArray.getArrayParent().$session());
|
||||
}
|
||||
|
||||
schema._preCompile();
|
||||
|
||||
const proto = baseClass != null ? baseClass.prototype : Subdocument.prototype;
|
||||
EmbeddedDocument.prototype = Object.create(proto);
|
||||
EmbeddedDocument.prototype.$__setSchema(schema);
|
||||
EmbeddedDocument.schema = schema;
|
||||
EmbeddedDocument.prototype.constructor = EmbeddedDocument;
|
||||
EmbeddedDocument.$isArraySubdocument = true;
|
||||
EmbeddedDocument.events = new EventEmitter();
|
||||
EmbeddedDocument.base = schema.base;
|
||||
|
||||
// apply methods
|
||||
for (const i in schema.methods) {
|
||||
EmbeddedDocument.prototype[i] = schema.methods[i];
|
||||
}
|
||||
|
||||
// apply statics
|
||||
for (const i in schema.statics) {
|
||||
EmbeddedDocument[i] = schema.statics[i];
|
||||
}
|
||||
|
||||
for (const i in EventEmitter.prototype) {
|
||||
EmbeddedDocument[i] = EventEmitter.prototype[i];
|
||||
}
|
||||
|
||||
EmbeddedDocument.options = options;
|
||||
|
||||
return EmbeddedDocument;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a discriminator to this document array.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
|
||||
* const schema = Schema({ shapes: [shapeSchema] });
|
||||
*
|
||||
* const docArrayPath = parentSchema.path('shapes');
|
||||
* docArrayPath.discriminator('Circle', Schema({ radius: Number }));
|
||||
*
|
||||
* @param {String} name
|
||||
* @param {Schema} schema fields to add to the schema for instances of this sub-class
|
||||
* @param {Object|string} [options] If string, same as `options.value`.
|
||||
* @param {String} [options.value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
|
||||
* @param {Boolean} [options.clone=true] By default, `discriminator()` clones the given `schema`. Set to `false` to skip cloning.
|
||||
* @see discriminators /docs/discriminators.html
|
||||
* @return {Function} the constructor Mongoose will use for creating instances of this discriminator model
|
||||
* @api public
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.discriminator = function(name, schema, options) {
|
||||
if (typeof name === 'function') {
|
||||
name = utils.getFunctionName(name);
|
||||
}
|
||||
|
||||
options = options || {};
|
||||
const tiedValue = utils.isPOJO(options) ? options.value : options;
|
||||
const clone = typeof options.clone === 'boolean' ? options.clone : true;
|
||||
|
||||
if (schema.instanceOfSchema && clone) {
|
||||
schema = schema.clone();
|
||||
}
|
||||
|
||||
schema = discriminator(this.casterConstructor, name, schema, tiedValue);
|
||||
|
||||
const EmbeddedDocument = _createConstructor(schema, null, this.casterConstructor);
|
||||
EmbeddedDocument.baseCasterConstructor = this.casterConstructor;
|
||||
|
||||
try {
|
||||
Object.defineProperty(EmbeddedDocument, 'name', {
|
||||
value: name
|
||||
});
|
||||
} catch (error) {
|
||||
// Ignore error, only happens on old versions of node
|
||||
}
|
||||
|
||||
this.casterConstructor.discriminators[name] = EmbeddedDocument;
|
||||
|
||||
return this.casterConstructor.discriminators[name];
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs local validations first, then validations on each embedded doc
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.doValidate = function(array, fn, scope, options) {
|
||||
// lazy load
|
||||
MongooseDocumentArray || (MongooseDocumentArray = require('../types/DocumentArray'));
|
||||
|
||||
const _this = this;
|
||||
try {
|
||||
SchemaType.prototype.doValidate.call(this, array, cb, scope);
|
||||
} catch (err) {
|
||||
return fn(err);
|
||||
}
|
||||
|
||||
function cb(err) {
|
||||
if (err) {
|
||||
return fn(err);
|
||||
}
|
||||
|
||||
let count = array && array.length;
|
||||
let error;
|
||||
|
||||
if (!count) {
|
||||
return fn();
|
||||
}
|
||||
if (options && options.updateValidator) {
|
||||
return fn();
|
||||
}
|
||||
if (!utils.isMongooseDocumentArray(array)) {
|
||||
array = new MongooseDocumentArray(array, _this.path, scope);
|
||||
}
|
||||
|
||||
// handle sparse arrays, do not use array.forEach which does not
|
||||
// iterate over sparse elements yet reports array.length including
|
||||
// them :(
|
||||
|
||||
function callback(err) {
|
||||
if (err != null) {
|
||||
error = err;
|
||||
}
|
||||
--count || fn(error);
|
||||
}
|
||||
|
||||
for (let i = 0, len = count; i < len; ++i) {
|
||||
// sidestep sparse entries
|
||||
let doc = array[i];
|
||||
if (doc == null) {
|
||||
--count || fn(error);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If you set the array index directly, the doc might not yet be
|
||||
// a full fledged mongoose subdoc, so make it into one.
|
||||
if (!(doc instanceof Subdocument)) {
|
||||
const Constructor = getConstructor(_this.casterConstructor, array[i]);
|
||||
doc = array[i] = new Constructor(doc, array, undefined, undefined, i);
|
||||
}
|
||||
|
||||
if (options != null && options.validateModifiedOnly && !doc.$isModified()) {
|
||||
--count || fn(error);
|
||||
continue;
|
||||
}
|
||||
|
||||
doc.$__validate(callback);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Performs local validations first, then validations on each embedded doc.
|
||||
*
|
||||
* #### Note:
|
||||
*
|
||||
* This method ignores the asynchronous validators.
|
||||
*
|
||||
* @return {MongooseError|undefined}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.doValidateSync = function(array, scope, options) {
|
||||
const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, array, scope);
|
||||
if (schemaTypeError != null) {
|
||||
return schemaTypeError;
|
||||
}
|
||||
|
||||
const count = array && array.length;
|
||||
let resultError = null;
|
||||
|
||||
if (!count) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle sparse arrays, do not use array.forEach which does not
|
||||
// iterate over sparse elements yet reports array.length including
|
||||
// them :(
|
||||
|
||||
for (let i = 0, len = count; i < len; ++i) {
|
||||
// sidestep sparse entries
|
||||
let doc = array[i];
|
||||
if (!doc) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If you set the array index directly, the doc might not yet be
|
||||
// a full fledged mongoose subdoc, so make it into one.
|
||||
if (!(doc instanceof Subdocument)) {
|
||||
const Constructor = getConstructor(this.casterConstructor, array[i]);
|
||||
doc = array[i] = new Constructor(doc, array, undefined, undefined, i);
|
||||
}
|
||||
|
||||
if (options != null && options.validateModifiedOnly && !doc.$isModified()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const subdocValidateError = doc.validateSync();
|
||||
|
||||
if (subdocValidateError && resultError == null) {
|
||||
resultError = subdocValidateError;
|
||||
}
|
||||
}
|
||||
|
||||
return resultError;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.getDefault = function(scope, init, options) {
|
||||
let ret = typeof this.defaultValue === 'function'
|
||||
? this.defaultValue.call(scope)
|
||||
: this.defaultValue;
|
||||
|
||||
if (ret == null) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (options && options.skipCast) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// lazy load
|
||||
MongooseDocumentArray || (MongooseDocumentArray = require('../types/DocumentArray'));
|
||||
|
||||
if (!Array.isArray(ret)) {
|
||||
ret = [ret];
|
||||
}
|
||||
|
||||
ret = new MongooseDocumentArray(ret, this.path, scope);
|
||||
|
||||
for (let i = 0; i < ret.length; ++i) {
|
||||
const Constructor = getConstructor(this.casterConstructor, ret[i]);
|
||||
const _subdoc = new Constructor({}, ret, undefined,
|
||||
undefined, i);
|
||||
_subdoc.$init(ret[i]);
|
||||
_subdoc.isNew = true;
|
||||
|
||||
// Make sure all paths in the subdoc are set to `default` instead
|
||||
// of `init` since we used `init`.
|
||||
Object.assign(_subdoc.$__.activePaths.default, _subdoc.$__.activePaths.init);
|
||||
_subdoc.$__.activePaths.init = {};
|
||||
|
||||
ret[i] = _subdoc;
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
const _toObjectOptions = Object.freeze({ transform: false, virtuals: false });
|
||||
const initDocumentOptions = Object.freeze({ skipId: false, willInit: true });
|
||||
|
||||
/**
|
||||
* Casts contents
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Document} document that triggers the casting
|
||||
* @api private
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
|
||||
// lazy load
|
||||
MongooseDocumentArray || (MongooseDocumentArray = require('../types/DocumentArray'));
|
||||
|
||||
// Skip casting if `value` is the same as the previous value, no need to cast. See gh-9266
|
||||
if (value != null && value[arrayPathSymbol] != null && value === prev) {
|
||||
return value;
|
||||
}
|
||||
|
||||
let selected;
|
||||
let subdoc;
|
||||
|
||||
options = options || {};
|
||||
|
||||
const path = options.path || this.path;
|
||||
|
||||
if (!Array.isArray(value)) {
|
||||
if (!init && !DocumentArrayPath.options.castNonArrays) {
|
||||
throw new CastError('DocumentArray', value, this.path, null, this);
|
||||
}
|
||||
// gh-2442 mark whole array as modified if we're initializing a doc from
|
||||
// the db and the path isn't an array in the document
|
||||
if (!!doc && init) {
|
||||
doc.markModified(path);
|
||||
}
|
||||
return this.cast([value], doc, init, prev, options);
|
||||
}
|
||||
|
||||
// We need to create a new array, otherwise change tracking will
|
||||
// update the old doc (gh-4449)
|
||||
if (!options.skipDocumentArrayCast || utils.isMongooseDocumentArray(value)) {
|
||||
value = new MongooseDocumentArray(value, path, doc);
|
||||
}
|
||||
|
||||
if (prev != null) {
|
||||
value[arrayAtomicsSymbol] = prev[arrayAtomicsSymbol] || {};
|
||||
}
|
||||
|
||||
if (options.arrayPathIndex != null) {
|
||||
value[arrayPathSymbol] = path + '.' + options.arrayPathIndex;
|
||||
}
|
||||
|
||||
const rawArray = utils.isMongooseDocumentArray(value) ? value.__array : value;
|
||||
const len = rawArray.length;
|
||||
|
||||
for (let i = 0; i < len; ++i) {
|
||||
if (!rawArray[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const Constructor = getConstructor(this.casterConstructor, rawArray[i]);
|
||||
|
||||
// Check if the document has a different schema (re gh-3701)
|
||||
if (rawArray[i].$__ != null && !(rawArray[i] instanceof Constructor)) {
|
||||
const spreadDoc = handleSpreadDoc(rawArray[i], true);
|
||||
if (rawArray[i] !== spreadDoc) {
|
||||
rawArray[i] = spreadDoc;
|
||||
} else {
|
||||
rawArray[i] = rawArray[i].toObject({
|
||||
transform: false,
|
||||
// Special case: if different model, but same schema, apply virtuals
|
||||
// re: gh-7898
|
||||
virtuals: rawArray[i].schema === Constructor.schema
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (rawArray[i] instanceof Subdocument) {
|
||||
if (rawArray[i][documentArrayParent] !== doc) {
|
||||
if (init) {
|
||||
const subdoc = new Constructor(null, value, initDocumentOptions, selected, i);
|
||||
rawArray[i] = subdoc.$init(rawArray[i]);
|
||||
} else {
|
||||
const subdoc = new Constructor(rawArray[i], value, undefined, undefined, i);
|
||||
rawArray[i] = subdoc;
|
||||
}
|
||||
}
|
||||
// Might not have the correct index yet, so ensure it does.
|
||||
if (rawArray[i].__index == null) {
|
||||
rawArray[i].$setIndex(i);
|
||||
}
|
||||
} else if (rawArray[i] != null) {
|
||||
if (init) {
|
||||
if (doc) {
|
||||
selected || (selected = scopePaths(this, doc.$__.selected, init));
|
||||
} else {
|
||||
selected = true;
|
||||
}
|
||||
|
||||
subdoc = new Constructor(null, value, initDocumentOptions, selected, i);
|
||||
rawArray[i] = subdoc.$init(rawArray[i]);
|
||||
} else {
|
||||
if (prev && typeof prev.id === 'function') {
|
||||
subdoc = prev.id(rawArray[i]._id);
|
||||
}
|
||||
|
||||
if (prev && subdoc && utils.deepEqual(subdoc.toObject(_toObjectOptions), rawArray[i])) {
|
||||
// handle resetting doc with existing id and same data
|
||||
subdoc.set(rawArray[i]);
|
||||
// if set() is hooked it will have no return value
|
||||
// see gh-746
|
||||
rawArray[i] = subdoc;
|
||||
} else {
|
||||
try {
|
||||
subdoc = new Constructor(rawArray[i], value, undefined,
|
||||
undefined, i);
|
||||
// if set() is hooked it will have no return value
|
||||
// see gh-746
|
||||
rawArray[i] = subdoc;
|
||||
} catch (error) {
|
||||
throw new CastError('embedded', rawArray[i],
|
||||
value[arrayPathSymbol], error, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.clone = function() {
|
||||
const options = Object.assign({}, this.options);
|
||||
const schematype = new this.constructor(this.path, this.schema, options, this.schemaOptions);
|
||||
schematype.validators = this.validators.slice();
|
||||
if (this.requiredValidator !== undefined) {
|
||||
schematype.requiredValidator = this.requiredValidator;
|
||||
}
|
||||
schematype.Constructor.discriminators = Object.assign({},
|
||||
this.Constructor.discriminators);
|
||||
return schematype;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
DocumentArrayPath.prototype.applyGetters = function(value, scope) {
|
||||
return SchemaType.prototype.applyGetters.call(this, value, scope);
|
||||
};
|
||||
|
||||
/**
|
||||
* Scopes paths selected in a query to this array.
|
||||
* Necessary for proper default application of subdocument values.
|
||||
*
|
||||
* @param {DocumentArrayPath} array the array to scope `fields` paths
|
||||
* @param {Object|undefined} fields the root fields selected in the query
|
||||
* @param {Boolean|undefined} init if we are being created part of a query result
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function scopePaths(array, fields, init) {
|
||||
if (!(init && fields)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const path = array.path + '.';
|
||||
const keys = Object.keys(fields);
|
||||
let i = keys.length;
|
||||
const selected = {};
|
||||
let hasKeys;
|
||||
let key;
|
||||
let sub;
|
||||
|
||||
while (i--) {
|
||||
key = keys[i];
|
||||
if (key.startsWith(path)) {
|
||||
sub = key.substring(path.length);
|
||||
if (sub === '$') {
|
||||
continue;
|
||||
}
|
||||
if (sub.startsWith('$.')) {
|
||||
sub = sub.substring(2);
|
||||
}
|
||||
hasKeys || (hasKeys = true);
|
||||
selected[sub] = fields[key];
|
||||
}
|
||||
}
|
||||
|
||||
return hasKeys && selected || undefined;
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
DocumentArrayPath.defaultOptions = {};
|
||||
|
||||
/**
|
||||
* Sets a default option for all DocumentArray instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all numbers have option `min` equal to 0.
|
||||
* mongoose.Schema.DocumentArray.set('_id', false);
|
||||
*
|
||||
* @param {String} option The name of the option you'd like to set (e.g. trim, lowercase, etc...)
|
||||
* @param {Any} value The value of the option you'd like to set.
|
||||
* @return {void}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
DocumentArrayPath.set = SchemaType.set;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = DocumentArrayPath;
|
||||
39
node_modules/mongoose/lib/schema/index.js
generated
vendored
Normal file
39
node_modules/mongoose/lib/schema/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
exports.String = require('./string');
|
||||
|
||||
exports.Number = require('./number');
|
||||
|
||||
exports.Boolean = require('./boolean');
|
||||
|
||||
exports.DocumentArray = require('./documentarray');
|
||||
|
||||
exports.Subdocument = require('./SubdocumentPath');
|
||||
|
||||
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');
|
||||
|
||||
exports.UUID = require('./uuid');
|
||||
|
||||
// alias
|
||||
|
||||
exports.Oid = exports.ObjectId;
|
||||
exports.Object = exports.Mixed;
|
||||
exports.Bool = exports.Boolean;
|
||||
exports.ObjectID = exports.ObjectId;
|
||||
84
node_modules/mongoose/lib/schema/map.js
generated
vendored
Normal file
84
node_modules/mongoose/lib/schema/map.js
generated
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
const MongooseMap = require('../types/map');
|
||||
const SchemaMapOptions = require('../options/SchemaMapOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
class Map extends SchemaType {
|
||||
constructor(key, options) {
|
||||
super(key, options, 'Map');
|
||||
this.$isSchemaMap = true;
|
||||
}
|
||||
|
||||
set(option, value) {
|
||||
return SchemaType.set(option, value);
|
||||
}
|
||||
|
||||
cast(val, doc, init) {
|
||||
if (val instanceof MongooseMap) {
|
||||
return val;
|
||||
}
|
||||
|
||||
const path = this.path;
|
||||
|
||||
if (init) {
|
||||
const map = new MongooseMap({}, path, doc, this.$__schemaType);
|
||||
|
||||
if (val instanceof global.Map) {
|
||||
for (const key of val.keys()) {
|
||||
let _val = val.get(key);
|
||||
if (_val == null) {
|
||||
_val = map.$__schemaType._castNullish(_val);
|
||||
} else {
|
||||
_val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
|
||||
}
|
||||
map.$init(key, _val);
|
||||
}
|
||||
} else {
|
||||
for (const key of Object.keys(val)) {
|
||||
let _val = val[key];
|
||||
if (_val == null) {
|
||||
_val = map.$__schemaType._castNullish(_val);
|
||||
} else {
|
||||
_val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
|
||||
}
|
||||
map.$init(key, _val);
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
return new MongooseMap(val, path, doc, this.$__schemaType);
|
||||
}
|
||||
|
||||
clone() {
|
||||
const schematype = super.clone();
|
||||
|
||||
if (this.$__schemaType != null) {
|
||||
schematype.$__schemaType = this.$__schemaType.clone();
|
||||
}
|
||||
return schematype;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
Map.schemaName = 'Map';
|
||||
|
||||
Map.prototype.OptionsConstructor = SchemaMapOptions;
|
||||
|
||||
Map.defaultOptions = {};
|
||||
|
||||
module.exports = Map;
|
||||
129
node_modules/mongoose/lib/schema/mixed.js
generated
vendored
Normal file
129
node_modules/mongoose/lib/schema/mixed.js
generated
vendored
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
const symbols = require('./symbols');
|
||||
const isObject = require('../helpers/isObject');
|
||||
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 && 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';
|
||||
|
||||
Mixed.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Sets a default option for all Mixed instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all mixed instances have `required` of true by default.
|
||||
* mongoose.Schema.Mixed.set('required', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ test: mongoose.Mixed }));
|
||||
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Mixed.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* Casts `val` for Mixed.
|
||||
*
|
||||
* _this is a no-op_
|
||||
*
|
||||
* @param {Object} value to cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.cast = function(val) {
|
||||
if (val instanceof Error) {
|
||||
return utils.errorToPOJO(val);
|
||||
}
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $cond
|
||||
* @param {any} [val]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Mixed.prototype.castForQuery = function($cond, val) {
|
||||
return val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = Mixed;
|
||||
438
node_modules/mongoose/lib/schema/number.js
generated
vendored
Normal file
438
node_modules/mongoose/lib/schema/number.js
generated
vendored
Normal file
|
|
@ -0,0 +1,438 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module requirements.
|
||||
*/
|
||||
|
||||
const MongooseError = require('../error/index');
|
||||
const SchemaNumberOptions = require('../options/SchemaNumberOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const castNumber = require('../cast/number');
|
||||
const handleBitwiseOperator = require('./operators/bitwise');
|
||||
const utils = require('../utils');
|
||||
|
||||
const CastError = SchemaType.CastError;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Sets a default option for all Number instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all numbers have option `min` equal to 0.
|
||||
* mongoose.Schema.Number.set('min', 0);
|
||||
*
|
||||
* const Order = mongoose.model('Order', new Schema({ amount: Number }));
|
||||
* new Order({ amount: -10 }).validateSync().errors.amount.message; // Path `amount` must be larger than 0.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.set = SchemaType.set;
|
||||
|
||||
/*!
|
||||
* 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 = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaNumber._defaultCaster = v => {
|
||||
if (typeof v !== 'number') {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaNumber.schemaName = 'Number';
|
||||
|
||||
SchemaNumber.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaNumber.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaNumber.prototype.constructor = SchemaNumber;
|
||||
SchemaNumber.prototype.OptionsConstructor = SchemaNumberOptions;
|
||||
|
||||
/*!
|
||||
* 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 (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
|
||||
return value != null;
|
||||
}
|
||||
|
||||
// `require('util').inherits()` does **not** copy static properties, and
|
||||
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
||||
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
||||
this.constructor.checkRequired() :
|
||||
SchemaNumber.checkRequired();
|
||||
|
||||
return _checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum number validator.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ n: { type: Number, min: 10 })
|
||||
* const M = db.model('M', s)
|
||||
* const 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
|
||||
* const min = [10, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
|
||||
* const schema = new Schema({ n: { type: Number, min: min })
|
||||
* const M = mongoose.model('Measurement', schema);
|
||||
* const 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:
|
||||
*
|
||||
* const s = new Schema({ n: { type: Number, max: 10 })
|
||||
* const M = db.model('M', s)
|
||||
* const 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
|
||||
* const max = [10, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
|
||||
* const schema = new Schema({ n: { type: Number, max: max })
|
||||
* const M = mongoose.model('Measurement', schema);
|
||||
* const 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a enum validator
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ n: { type: Number, enum: [1, 2, 3] });
|
||||
* const M = db.model('M', s);
|
||||
*
|
||||
* const m = new M({ n: 4 });
|
||||
* await m.save(); // throws validation error
|
||||
*
|
||||
* m.n = 3;
|
||||
* await m.save(); // succeeds
|
||||
*
|
||||
* @param {Array} values allowed values
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaNumber.prototype.enum = function(values, message) {
|
||||
if (this.enumValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator !== this.enumValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
|
||||
if (!Array.isArray(values)) {
|
||||
const isObjectSyntax = utils.isPOJO(values) && values.values != null;
|
||||
if (isObjectSyntax) {
|
||||
message = values.message;
|
||||
values = values.values;
|
||||
} else if (typeof values === 'number') {
|
||||
values = Array.prototype.slice.call(arguments);
|
||||
message = null;
|
||||
}
|
||||
|
||||
if (utils.isPOJO(values)) {
|
||||
values = Object.values(values);
|
||||
}
|
||||
message = message || MongooseError.messages.Number.enum;
|
||||
}
|
||||
|
||||
message = message == null ? MongooseError.messages.Number.enum : message;
|
||||
|
||||
this.enumValidator = v => v == null || values.indexOf(v) !== -1;
|
||||
this.validators.push({
|
||||
validator: this.enumValidator,
|
||||
message: message,
|
||||
type: 'enum',
|
||||
enumValues: values
|
||||
});
|
||||
|
||||
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 (typeof value !== 'number' && SchemaType._isRef(this, value, doc, init)) {
|
||||
if (value == null || utils.isNonBuiltinObject(value)) {
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
}
|
||||
|
||||
const val = value && typeof value._id !== 'undefined' ?
|
||||
value._id : // documents
|
||||
value;
|
||||
|
||||
let castNumber;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castNumber = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castNumber = this.constructor.cast();
|
||||
} else {
|
||||
castNumber = SchemaNumber.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castNumber(val);
|
||||
} catch (err) {
|
||||
throw new CastError('Number', val, this.path, err, this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* 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, context) {
|
||||
let handler;
|
||||
if ($conditional != null) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new CastError('number', val, this.path, null, this);
|
||||
}
|
||||
return handler.call(this, val, context);
|
||||
}
|
||||
val = this.applySetters(val, context);
|
||||
return val;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaNumber;
|
||||
295
node_modules/mongoose/lib/schema/objectid.js
generated
vendored
Normal file
295
node_modules/mongoose/lib/schema/objectid.js
generated
vendored
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const SchemaObjectIdOptions = require('../options/SchemaObjectIdOptions');
|
||||
const SchemaType = require('../schematype');
|
||||
const castObjectId = require('../cast/objectid');
|
||||
const getConstructorName = require('../helpers/getConstructorName');
|
||||
const oid = require('../types/objectid');
|
||||
const isBsonType = require('../helpers/isBsonType');
|
||||
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) {
|
||||
utils.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.');
|
||||
}
|
||||
SchemaType.call(this, key, options, 'ObjectId');
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
ObjectId.schemaName = 'ObjectId';
|
||||
|
||||
ObjectId.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
ObjectId.prototype = Object.create(SchemaType.prototype);
|
||||
ObjectId.prototype.constructor = ObjectId;
|
||||
ObjectId.prototype.OptionsConstructor = SchemaObjectIdOptions;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Sets a default option for all ObjectId instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all object ids have option `required` equal to true.
|
||||
* mongoose.Schema.ObjectId.set('required', true);
|
||||
*
|
||||
* const Order = mongoose.model('Order', new Schema({ userId: ObjectId }));
|
||||
* new Order({ }).validateSync().errors.userId.message; // Path `userId` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
ObjectId.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* 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 => isBsonType(v, 'ObjectId');
|
||||
|
||||
/*!
|
||||
* 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 = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
ObjectId._defaultCaster = v => {
|
||||
if (!(isBsonType(v, 'ObjectId'))) {
|
||||
throw new Error(v + ' is not an instance of ObjectId');
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
// `require('util').inherits()` does **not** copy static properties, and
|
||||
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
||||
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
||||
this.constructor.checkRequired() :
|
||||
ObjectId.checkRequired();
|
||||
|
||||
return _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 (!(isBsonType(value, 'ObjectId')) && SchemaType._isRef(this, value, doc, init)) {
|
||||
// wait! we may need to cast this to a document
|
||||
if ((getConstructorName(value) || '').toLowerCase() === 'objectid') {
|
||||
return new oid(value.toHexString());
|
||||
}
|
||||
|
||||
if (value == null || utils.isNonBuiltinObject(value)) {
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
}
|
||||
|
||||
let castObjectId;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castObjectId = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castObjectId = this.constructor.cast();
|
||||
} else {
|
||||
castObjectId = ObjectId.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castObjectId(value);
|
||||
} catch (error) {
|
||||
throw new CastError('ObjectId', value, this.path, error, this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* 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();
|
||||
return _v;
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = ObjectId;
|
||||
38
node_modules/mongoose/lib/schema/operators/bitwise.js
generated
vendored
Normal file
38
node_modules/mongoose/lib/schema/operators/bitwise.js
generated
vendored
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*!
|
||||
* 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
node_modules/mongoose/lib/schema/operators/exists.js
generated
vendored
Normal file
12
node_modules/mongoose/lib/schema/operators/exists.js
generated
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
const castBoolean = require('../../cast/boolean');
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(val) {
|
||||
const path = this != null ? this.path : null;
|
||||
return castBoolean(val, path);
|
||||
};
|
||||
107
node_modules/mongoose/lib/schema/operators/geospatial.js
generated
vendored
Normal file
107
node_modules/mongoose/lib/schema/operators/geospatial.js
generated
vendored
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
/*!
|
||||
* 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);
|
||||
}
|
||||
|
||||
if (!Array.isArray(val)) {
|
||||
throw new TypeError('$near must be either an array or an object ' +
|
||||
'with a $geometry property');
|
||||
}
|
||||
|
||||
return SchemaArray.prototype.castForQuery.call(this, null, 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
node_modules/mongoose/lib/schema/operators/helpers.js
generated
vendored
Normal file
32
node_modules/mongoose/lib/schema/operators/helpers.js
generated
vendored
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
'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
node_modules/mongoose/lib/schema/operators/text.js
generated
vendored
Normal file
39
node_modules/mongoose/lib/schema/operators/text.js
generated
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
'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://www.mongodb.com/docs/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;
|
||||
};
|
||||
20
node_modules/mongoose/lib/schema/operators/type.js
generated
vendored
Normal file
20
node_modules/mongoose/lib/schema/operators/type.js
generated
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function(val) {
|
||||
if (Array.isArray(val)) {
|
||||
if (!val.every(v => typeof v === 'number' || typeof v === 'string')) {
|
||||
throw new Error('$type array values must be strings or numbers');
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
if (typeof val !== 'number' && typeof val !== 'string') {
|
||||
throw new Error('$type parameter must be number, string, or array of numbers and strings');
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
694
node_modules/mongoose/lib/schema/string.js
generated
vendored
Normal file
694
node_modules/mongoose/lib/schema/string.js
generated
vendored
Normal file
|
|
@ -0,0 +1,694 @@
|
|||
'use strict';
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const SchemaType = require('../schematype');
|
||||
const MongooseError = require('../error/index');
|
||||
const SchemaStringOptions = require('../options/SchemaStringOptions');
|
||||
const castString = require('../cast/string');
|
||||
const utils = require('../utils');
|
||||
const isBsonType = require('../helpers/isBsonType');
|
||||
|
||||
const CastError = SchemaType.CastError;
|
||||
|
||||
/**
|
||||
* 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';
|
||||
|
||||
SchemaString.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaString.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaString.prototype.constructor = SchemaString;
|
||||
Object.defineProperty(SchemaString.prototype, 'OptionsConstructor', {
|
||||
configurable: false,
|
||||
enumerable: false,
|
||||
writable: false,
|
||||
value: SchemaStringOptions
|
||||
});
|
||||
|
||||
/*!
|
||||
* 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 = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaString._defaultCaster = v => {
|
||||
if (v != null && typeof v !== 'string') {
|
||||
throw new Error();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Sets a default option for all String instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all strings have option `trim` equal to true.
|
||||
* mongoose.Schema.String.set('trim', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ name: String }));
|
||||
* new User({ name: ' John Doe ' }).name; // 'John Doe'
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.set = SchemaType.set;
|
||||
|
||||
/*!
|
||||
* 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:
|
||||
*
|
||||
* const states = ['opening', 'open', 'closing', 'closed']
|
||||
* const s = new Schema({ state: { type: String, enum: states }})
|
||||
* const M = db.model('M', s)
|
||||
* const 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
|
||||
* const enum = {
|
||||
* values: ['opening', 'open', 'closing', 'closed'],
|
||||
* message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
||||
* }
|
||||
* const s = new Schema({ state: { type: String, enum: enum })
|
||||
* const M = db.model('M', s)
|
||||
* const 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])) {
|
||||
if (Array.isArray(arguments[0].values)) {
|
||||
values = arguments[0].values;
|
||||
errorMessage = arguments[0].message;
|
||||
} else {
|
||||
values = utils.object.vals(arguments[0]);
|
||||
errorMessage = MongooseError.messages.String.enum;
|
||||
}
|
||||
} else {
|
||||
values = arguments;
|
||||
errorMessage = MongooseError.messages.String.enum;
|
||||
}
|
||||
|
||||
for (const value of values) {
|
||||
if (value !== undefined) {
|
||||
this.enumValues.push(this.cast(value));
|
||||
}
|
||||
}
|
||||
|
||||
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](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set).
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ email: { type: String, lowercase: true }})
|
||||
* const M = db.model('M', s);
|
||||
* const 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'
|
||||
*
|
||||
* Note that `lowercase` does **not** affect regular expression queries:
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Still queries for documents whose `email` matches the regular
|
||||
* // expression /SomeEmail/. Mongoose does **not** convert the RegExp
|
||||
* // to lowercase.
|
||||
* M.find({ email: /SomeEmail/ });
|
||||
*
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.lowercase = function(shouldApply) {
|
||||
if (arguments.length > 0 && !shouldApply) {
|
||||
return this;
|
||||
}
|
||||
return this.set(v => {
|
||||
if (typeof v !== 'string') {
|
||||
v = this.cast(v);
|
||||
}
|
||||
if (v) {
|
||||
return v.toLowerCase();
|
||||
}
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds an uppercase [setter](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set).
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ caps: { type: String, uppercase: true }})
|
||||
* const M = db.model('M', s);
|
||||
* const m = new M({ caps: 'an example' });
|
||||
* console.log(m.caps) // AN EXAMPLE
|
||||
* M.find({ caps: 'an example' }) // Matches documents where caps = 'AN EXAMPLE'
|
||||
*
|
||||
* Note that `uppercase` does **not** affect regular expression queries:
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Mongoose does **not** convert the RegExp to uppercase.
|
||||
* M.find({ email: /an example/ });
|
||||
*
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.uppercase = function(shouldApply) {
|
||||
if (arguments.length > 0 && !shouldApply) {
|
||||
return this;
|
||||
}
|
||||
return this.set(v => {
|
||||
if (typeof v !== 'string') {
|
||||
v = this.cast(v);
|
||||
}
|
||||
if (v) {
|
||||
return v.toUpperCase();
|
||||
}
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Adds a trim [setter](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set).
|
||||
*
|
||||
* The string value will be [trimmed](https://masteringjs.io/tutorials/fundamentals/trim-string) when set.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ name: { type: String, trim: true }});
|
||||
* const M = db.model('M', s);
|
||||
* const string = ' some name ';
|
||||
* console.log(string.length); // 11
|
||||
* const m = new M({ name: string });
|
||||
* console.log(m.name.length); // 9
|
||||
*
|
||||
* // Equivalent to `findOne({ name: string.trim() })`
|
||||
* M.findOne({ name: string });
|
||||
*
|
||||
* Note that `trim` does **not** affect regular expression queries:
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Mongoose does **not** trim whitespace from the RegExp.
|
||||
* M.find({ name: / some name / });
|
||||
*
|
||||
* @api public
|
||||
* @return {SchemaType} this
|
||||
*/
|
||||
|
||||
SchemaString.prototype.trim = function(shouldTrim) {
|
||||
if (arguments.length > 0 && !shouldTrim) {
|
||||
return this;
|
||||
}
|
||||
return this.set(v => {
|
||||
if (typeof v !== 'string') {
|
||||
v = this.cast(v);
|
||||
}
|
||||
if (v) {
|
||||
return v.trim();
|
||||
}
|
||||
return v;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum length validator.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const schema = new Schema({ postalCode: { type: String, minlength: 5 })
|
||||
* const Address = db.model('Address', schema)
|
||||
* const 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
|
||||
* const minlength = [5, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).'];
|
||||
* const schema = new Schema({ postalCode: { type: String, minlength: minlength })
|
||||
* const Address = mongoose.model('Address', schema);
|
||||
* const 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;
|
||||
};
|
||||
|
||||
SchemaString.prototype.minLength = SchemaString.prototype.minlength;
|
||||
|
||||
/**
|
||||
* Sets a maximum length validator.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const schema = new Schema({ postalCode: { type: String, maxlength: 9 })
|
||||
* const Address = db.model('Address', schema)
|
||||
* const 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
|
||||
* const maxlength = [9, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
|
||||
* const schema = new Schema({ postalCode: { type: String, maxlength: maxlength })
|
||||
* const Address = mongoose.model('Address', schema);
|
||||
* const 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;
|
||||
};
|
||||
|
||||
SchemaString.prototype.maxLength = SchemaString.prototype.maxlength;
|
||||
|
||||
/**
|
||||
* Sets a regexp validator.
|
||||
*
|
||||
* Any value that does not pass `regExp`.test(val) will fail validation.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* const s = new Schema({ name: { type: String, match: /^a/ }})
|
||||
* const M = db.model('M', s)
|
||||
* const 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
|
||||
* const match = [ /\.html$/, "That file doesn't end in .html ({VALUE})" ];
|
||||
* const s = new Schema({ file: { type: String, match: match }})
|
||||
* const M = db.model('M', s);
|
||||
* const 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.
|
||||
*
|
||||
* const 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;
|
||||
}
|
||||
|
||||
// In case RegExp happens to have `/g` flag set, we need to reset the
|
||||
// `lastIndex`, otherwise `match` will intermittently fail.
|
||||
regExp.lastIndex = 0;
|
||||
|
||||
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 (typeof value === 'object' && SchemaType._isRef(this, value, doc, true)) {
|
||||
return value != null;
|
||||
}
|
||||
|
||||
// `require('util').inherits()` does **not** copy static properties, and
|
||||
// plugins like mongoose-float use `inherits()` for pre-ES6.
|
||||
const _checkRequired = typeof this.constructor.checkRequired === 'function' ?
|
||||
this.constructor.checkRequired() :
|
||||
SchemaString.checkRequired();
|
||||
|
||||
return _checkRequired(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to String
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.cast = function(value, doc, init) {
|
||||
if (typeof value !== 'string' && SchemaType._isRef(this, value, doc, init)) {
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
|
||||
let castString;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castString = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castString = this.constructor.cast();
|
||||
} else {
|
||||
castString = SchemaString.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castString(value);
|
||||
} catch (error) {
|
||||
throw new CastError('string', value, this.path, null, this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val, context) {
|
||||
return this.castForQuery(null, val, context);
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleArray(val, context) {
|
||||
const _this = this;
|
||||
if (!Array.isArray(val)) {
|
||||
return [this.castForQuery(null, val, context)];
|
||||
}
|
||||
return val.map(function(m) {
|
||||
return _this.castForQuery(null, m, context);
|
||||
});
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingleNoSetters(val) {
|
||||
if (val == null) {
|
||||
return this._castNullish(val);
|
||||
}
|
||||
|
||||
return this.cast(val, this);
|
||||
}
|
||||
|
||||
const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$all: handleArray,
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle,
|
||||
$options: handleSingleNoSetters,
|
||||
$regex: function handle$regex(val) {
|
||||
if (Object.prototype.toString.call(val) === '[object RegExp]') {
|
||||
return val;
|
||||
}
|
||||
|
||||
return handleSingleNoSetters.call(this, val);
|
||||
},
|
||||
$not: handleSingle
|
||||
});
|
||||
|
||||
Object.defineProperty(SchemaString.prototype, '$conditionalHandlers', {
|
||||
configurable: false,
|
||||
enumerable: false,
|
||||
writable: false,
|
||||
value: Object.freeze($conditionalHandlers)
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} [val]
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaString.prototype.castForQuery = function($conditional, val, context) {
|
||||
let handler;
|
||||
if ($conditional != null) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler) {
|
||||
throw new Error('Can\'t use ' + $conditional + ' with String.');
|
||||
}
|
||||
return handler.call(this, val, context);
|
||||
}
|
||||
|
||||
if (Object.prototype.toString.call(val) === '[object RegExp]' || isBsonType(val, 'BSONRegExp')) {
|
||||
return val;
|
||||
}
|
||||
|
||||
return this.applySetters(val, context);
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaString;
|
||||
5
node_modules/mongoose/lib/schema/symbols.js
generated
vendored
Normal file
5
node_modules/mongoose/lib/schema/symbols.js
generated
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
'use strict';
|
||||
|
||||
exports.schemaMixedSymbol = Symbol.for('mongoose:schema_mixed');
|
||||
|
||||
exports.builtInMiddleware = Symbol.for('mongoose:built-in-middleware');
|
||||
332
node_modules/mongoose/lib/schema/uuid.js
generated
vendored
Normal file
332
node_modules/mongoose/lib/schema/uuid.js
generated
vendored
Normal file
|
|
@ -0,0 +1,332 @@
|
|||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const MongooseBuffer = require('../types/buffer');
|
||||
const SchemaType = require('../schematype');
|
||||
const CastError = SchemaType.CastError;
|
||||
const utils = require('../utils');
|
||||
const isBsonType = require('../helpers/isBsonType');
|
||||
const handleBitwiseOperator = require('./operators/bitwise');
|
||||
|
||||
const UUID_FORMAT = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
|
||||
const Binary = MongooseBuffer.Binary;
|
||||
|
||||
/**
|
||||
* Helper function to convert the input hex-string to a buffer
|
||||
* @param {String} hex The hex string to convert
|
||||
* @returns {Buffer} The hex as buffer
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function hex2buffer(hex) {
|
||||
// use buffer built-in function to convert from hex-string to buffer
|
||||
const buff = hex != null && Buffer.from(hex, 'hex');
|
||||
return buff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to convert the buffer input to a string
|
||||
* @param {Buffer} buf The buffer to convert to a hex-string
|
||||
* @returns {String} The buffer as a hex-string
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function binary2hex(buf) {
|
||||
// use buffer built-in function to convert from buffer to hex-string
|
||||
const hex = buf != null && buf.toString('hex');
|
||||
return hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a String to Binary
|
||||
* @param {String} uuidStr The value to process
|
||||
* @returns {MongooseBuffer} The binary to store
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function stringToBinary(uuidStr) {
|
||||
// Protect against undefined & throwing err
|
||||
if (typeof uuidStr !== 'string') uuidStr = '';
|
||||
const hex = uuidStr.replace(/[{}-]/g, ''); // remove extra characters
|
||||
const bytes = hex2buffer(hex);
|
||||
const buff = new MongooseBuffer(bytes);
|
||||
buff._subtype = 4;
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert binary to a uuid string
|
||||
* @param {Buffer|Binary|String} uuidBin The value to process
|
||||
* @returns {String} The completed uuid-string
|
||||
* @api private
|
||||
*/
|
||||
function binaryToString(uuidBin) {
|
||||
// i(hasezoey) dont quite know why, but "uuidBin" may sometimes also be the already processed string
|
||||
let hex;
|
||||
if (typeof uuidBin !== 'string' && uuidBin != null) {
|
||||
hex = binary2hex(uuidBin);
|
||||
const uuidStr = hex.substring(0, 8) + '-' + hex.substring(8, 8 + 4) + '-' + hex.substring(12, 12 + 4) + '-' + hex.substring(16, 16 + 4) + '-' + hex.substring(20, 20 + 12);
|
||||
return uuidStr;
|
||||
}
|
||||
return uuidBin;
|
||||
}
|
||||
|
||||
/**
|
||||
* UUIDv1 SchemaType constructor.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {Object} options
|
||||
* @inherits SchemaType
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function SchemaUUID(key, options) {
|
||||
SchemaType.call(this, key, options, 'UUID');
|
||||
this.getters.push(binaryToString);
|
||||
}
|
||||
|
||||
/**
|
||||
* This schema type's name, to defend against minifiers that mangle
|
||||
* function names.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
SchemaUUID.schemaName = 'UUID';
|
||||
|
||||
SchemaUUID.defaultOptions = {};
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
SchemaUUID.prototype = Object.create(SchemaType.prototype);
|
||||
SchemaUUID.prototype.constructor = SchemaUUID;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaUUID._cast = function(value) {
|
||||
if (value === null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function newBuffer(initbuff) {
|
||||
const buff = new MongooseBuffer(initbuff);
|
||||
buff._subtype = 4;
|
||||
return buff;
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
if (UUID_FORMAT.test(value)) {
|
||||
return stringToBinary(value);
|
||||
} else {
|
||||
throw new CastError(SchemaUUID.schemaName, value, this.path);
|
||||
}
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return newBuffer(value);
|
||||
}
|
||||
|
||||
if (value instanceof Binary) {
|
||||
return newBuffer(value.value(true));
|
||||
}
|
||||
|
||||
// Re: gh-647 and gh-3030, we're ok with casting using `toString()`
|
||||
// **unless** its the default Object.toString, because "[object Object]"
|
||||
// doesn't really qualify as useful data
|
||||
if (value.toString && value.toString !== Object.prototype.toString) {
|
||||
if (UUID_FORMAT.test(value.toString())) {
|
||||
return stringToBinary(value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
throw new CastError(SchemaUUID.schemaName, value, this.path);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a default option for all UUID instances.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make all UUIDs have `required` of true by default.
|
||||
* mongoose.Schema.UUID.set('required', true);
|
||||
*
|
||||
* const User = mongoose.model('User', new Schema({ test: mongoose.UUID }));
|
||||
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
|
||||
*
|
||||
* @param {String} option The option you'd like to set the value for
|
||||
* @param {Any} value value for option
|
||||
* @return {undefined}
|
||||
* @function set
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaUUID.set = SchemaType.set;
|
||||
|
||||
/**
|
||||
* Get/set the function used to cast arbitrary values to UUIDs.
|
||||
*
|
||||
* #### Example:
|
||||
*
|
||||
* // Make Mongoose refuse to cast UUIDs with 0 length
|
||||
* const original = mongoose.Schema.Types.UUID.cast();
|
||||
* mongoose.UUID.cast(v => {
|
||||
* assert.ok(typeof v === "string" && v.length > 0);
|
||||
* return original(v);
|
||||
* });
|
||||
*
|
||||
* // Or disable casting entirely
|
||||
* mongoose.UUID.cast(false);
|
||||
*
|
||||
* @param {Function} [caster]
|
||||
* @return {Function}
|
||||
* @function get
|
||||
* @static
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaUUID.cast = function cast(caster) {
|
||||
if (arguments.length === 0) {
|
||||
return this._cast;
|
||||
}
|
||||
if (caster === false) {
|
||||
caster = this._defaultCaster;
|
||||
}
|
||||
this._cast = caster;
|
||||
|
||||
return this._cast;
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
SchemaUUID._checkRequired = v => v != null;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
SchemaUUID.checkRequired = SchemaType.checkRequired;
|
||||
|
||||
/**
|
||||
* Check if the given value satisfies a required validator.
|
||||
*
|
||||
* @param {Any} value
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaUUID.prototype.checkRequired = function checkRequired(value) {
|
||||
if (Buffer.isBuffer(value)) {
|
||||
value = binaryToString(value);
|
||||
}
|
||||
return value != null && UUID_FORMAT.test(value);
|
||||
};
|
||||
|
||||
/**
|
||||
* Casts to UUID
|
||||
*
|
||||
* @param {Object} value
|
||||
* @param {Object} doc
|
||||
* @param {Boolean} init whether this is an initialization cast
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaUUID.prototype.cast = function(value, doc, init) {
|
||||
if (SchemaType._isRef(this, value, doc, init)) {
|
||||
if (isBsonType(value, 'UUID')) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return this._castRef(value, doc, init);
|
||||
}
|
||||
|
||||
let castFn;
|
||||
if (typeof this._castFunction === 'function') {
|
||||
castFn = this._castFunction;
|
||||
} else if (typeof this.constructor.cast === 'function') {
|
||||
castFn = this.constructor.cast();
|
||||
} else {
|
||||
castFn = SchemaUUID.cast();
|
||||
}
|
||||
|
||||
try {
|
||||
return castFn(value);
|
||||
} catch (error) {
|
||||
throw new CastError(SchemaUUID.schemaName, value, this.path, error, this);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleSingle(val) {
|
||||
return this.cast(val);
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleArray(val) {
|
||||
return val.map((m) => {
|
||||
return this.cast(m);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaUUID.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
$bitsAllClear: handleBitwiseOperator,
|
||||
$bitsAnyClear: handleBitwiseOperator,
|
||||
$bitsAllSet: handleBitwiseOperator,
|
||||
$bitsAnySet: handleBitwiseOperator,
|
||||
$all: handleArray,
|
||||
$gt: handleSingle,
|
||||
$gte: handleSingle,
|
||||
$in: handleArray,
|
||||
$lt: handleSingle,
|
||||
$lte: handleSingle,
|
||||
$ne: handleSingle,
|
||||
$nin: handleArray
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
*
|
||||
* @param {String} $conditional
|
||||
* @param {any} val
|
||||
* @api private
|
||||
*/
|
||||
|
||||
SchemaUUID.prototype.castForQuery = function($conditional, val, context) {
|
||||
let handler;
|
||||
if ($conditional != null) {
|
||||
handler = this.$conditionalHandlers[$conditional];
|
||||
if (!handler)
|
||||
throw new Error('Can\'t use ' + $conditional + ' with UUID.');
|
||||
return handler.call(this, val, context);
|
||||
} else {
|
||||
return this.cast(val);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = SchemaUUID;
|
||||
Loading…
Add table
Add a link
Reference in a new issue