1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-12 02:42:48 +00:00

updated bunch of file paths and changed the way posts are loaded

This commit is contained in:
2016-01-05 12:28:04 -06:00
parent 4bb8cae81e
commit 6ab45fe935
13249 changed files with 317868 additions and 2101398 deletions

View File

@@ -2,23 +2,22 @@
* Module dependencies.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, NumberSchema = require('./number')
, Types = {
Boolean: require('./boolean')
, Date: require('./date')
, Number: require('./number')
, String: require('./string')
, ObjectId: require('./objectid')
, Buffer: require('./buffer')
}
, MongooseArray = require('../types').Array
, EmbeddedDoc = require('../types').Embedded
, Mixed = require('./mixed')
, cast = require('../cast')
, utils = require('../utils')
, isMongooseObject = utils.isMongooseObject
var SchemaType = require('../schematype'),
CastError = SchemaType.CastError,
Types = {
Boolean: require('./boolean'),
Date: require('./date'),
Number: require('./number'),
String: require('./string'),
ObjectId: require('./objectid'),
Buffer: require('./buffer')
},
MongooseArray = require('../types').Array,
EmbeddedDoc = require('../types').Embedded,
Mixed = require('./mixed'),
cast = require('../cast'),
utils = require('../utils'),
isMongooseObject = utils.isMongooseObject;
/**
* Array SchemaType constructor
@@ -30,7 +29,7 @@ var SchemaType = require('../schematype')
* @api private
*/
function SchemaArray (key, cast, options) {
function SchemaArray(key, cast, options) {
if (cast) {
var castOptions = {};
@@ -63,16 +62,16 @@ function SchemaArray (key, cast, options) {
SchemaType.call(this, key, options, 'Array');
var self = this
, defaultArr
, fn;
var self = this,
defaultArr,
fn;
if (this.defaultValue) {
defaultArr = this.defaultValue;
fn = 'function' == typeof defaultArr;
}
this.default(function(){
this.default(function() {
var arr = fn ? defaultArr() : defaultArr || [];
return new MongooseArray(arr, self.path, this);
});
@@ -99,7 +98,7 @@ SchemaArray.prototype.constructor = SchemaArray;
* @api private
*/
SchemaArray.prototype.checkRequired = function (value) {
SchemaArray.prototype.checkRequired = function(value) {
return !!(value && value.length);
};
@@ -111,7 +110,7 @@ SchemaArray.prototype.checkRequired = function (value) {
* @api private
*/
SchemaArray.prototype.applyGetters = function (value, scope) {
SchemaArray.prototype.applyGetters = function(value, scope) {
if (this.caster.options && this.caster.options.ref) {
// means the object id was populated
return value;
@@ -129,7 +128,7 @@ SchemaArray.prototype.applyGetters = function (value, scope) {
* @api private
*/
SchemaArray.prototype.cast = function (value, doc, init) {
SchemaArray.prototype.cast = function(value, doc, init) {
if (Array.isArray(value)) {
if (!value.length && doc) {
@@ -149,7 +148,7 @@ SchemaArray.prototype.cast = function (value, doc, init) {
if (this.caster) {
try {
for (var i = 0, l = value.length; i < l; i++) {
for (i = 0, l = value.length; i < l; i++) {
value[i] = this.caster.cast(value[i], doc, init);
}
} catch (e) {
@@ -177,9 +176,9 @@ SchemaArray.prototype.cast = function (value, doc, init) {
* @api private
*/
SchemaArray.prototype.castForQuery = function ($conditional, value) {
var handler
, val;
SchemaArray.prototype.castForQuery = function($conditional, value) {
var handler,
val;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
@@ -198,7 +197,10 @@ SchemaArray.prototype.castForQuery = function ($conditional, value) {
var caster = this.caster;
if (Array.isArray(val)) {
val = val.map(function (v) {
val = val.map(function(v) {
if (utils.isObject(v) && v.$elemMatch) {
return v;
}
if (method) v = method.call(caster, v);
return isMongooseObject(v) ?
v.toObject({ virtuals: false }) :
@@ -221,14 +223,14 @@ SchemaArray.prototype.castForQuery = function ($conditional, value) {
* $atomic cast helpers
*/
function castToNumber (val) {
function castToNumber(val) {
return Types.Number.prototype.cast.call(this, val);
}
function castArraysOfNumbers (arr, self) {
function castArraysOfNumbers(arr, self) {
self || (self = this);
arr.forEach(function (v, i) {
arr.forEach(function(v, i) {
if (Array.isArray(v)) {
castArraysOfNumbers(v, self);
} else {
@@ -237,7 +239,7 @@ function castArraysOfNumbers (arr, self) {
});
}
function cast$near (val) {
function cast$near(val) {
if (Array.isArray(val)) {
castArraysOfNumbers(val, this);
return val;
@@ -250,7 +252,7 @@ function cast$near (val) {
return SchemaArray.prototype.castForQuery.call(this, val);
}
function cast$geometry (val, self) {
function cast$geometry(val, self) {
switch (val.$geometry.type) {
case 'Polygon':
case 'LineString':
@@ -269,7 +271,7 @@ function cast$geometry (val, self) {
return val;
}
function cast$within (val) {
function cast$within(val) {
var self = this;
if (val.$maxDistance) {
@@ -278,27 +280,27 @@ function cast$within (val) {
if (val.$box || val.$polygon) {
var type = val.$box ? '$box' : '$polygon';
val[type].forEach(function (arr) {
val[type].forEach(function(arr) {
if (!Array.isArray(arr)) {
var msg = 'Invalid $within $box argument. '
+ 'Expected an array, received ' + arr;
throw new TypeError(msg);
}
arr.forEach(function (v, i) {
arr.forEach(function(v, i) {
arr[i] = castToNumber.call(this, v);
});
})
});
} else if (val.$center || val.$centerSphere) {
var type = val.$center ? '$center' : '$centerSphere';
val[type].forEach(function (item, i) {
type = val.$center ? '$center' : '$centerSphere';
val[type].forEach(function(item, i) {
if (Array.isArray(item)) {
item.forEach(function (v, j) {
item.forEach(function(v, j) {
item[j] = castToNumber.call(this, v);
});
} else {
val[type][i] = castToNumber.call(this, item);
}
})
});
} else if (val.$geometry) {
cast$geometry(val, this);
}
@@ -306,12 +308,12 @@ function cast$within (val) {
return val;
}
function cast$all (val) {
function cast$all(val) {
if (!Array.isArray(val)) {
val = [val];
}
val = val.map(function (v) {
val = val.map(function(v) {
if (utils.isObject(v)) {
var o = {};
o[this.path] = v;
@@ -323,28 +325,23 @@ function cast$all (val) {
return this.castForQuery(val);
}
function cast$elemMatch (val) {
var hasDollarKey = false;
function cast$elemMatch(val) {
var keys = Object.keys(val);
var numKeys = keys.length;
var key;
var value;
for (var i = 0; i < numKeys; ++i) {
var key = keys[i];
var value = val[key];
key = keys[i];
value = val[key];
if (key.indexOf('$') === 0 && value) {
val[key] = this.castForQuery(key, value);
hasDollarKey = true;
}
}
if (hasDollarKey) {
return val;
}
return cast(this.casterConstructor.schema, val);
}
function cast$geoIntersects (val) {
function cast$geoIntersects(val) {
var geo = val.$geometry;
if (!geo) return;

View File

@@ -5,7 +5,6 @@
var utils = require('../utils');
var SchemaType = require('../schematype');
var utils = require('../utils');
/**
* Boolean SchemaType constructor.
@@ -16,7 +15,7 @@ var utils = require('../utils');
* @api private
*/
function SchemaBoolean (path, options) {
function SchemaBoolean(path, options) {
SchemaType.call(this, path, options, 'Boolean');
}
@@ -40,7 +39,7 @@ SchemaBoolean.prototype.constructor = SchemaBoolean;
* @api private
*/
SchemaBoolean.prototype.checkRequired = function (value) {
SchemaBoolean.prototype.checkRequired = function(value) {
return value === true || value === false;
};
@@ -51,29 +50,16 @@ SchemaBoolean.prototype.checkRequired = function (value) {
* @api private
*/
SchemaBoolean.prototype.cast = function (value) {
SchemaBoolean.prototype.cast = function(value) {
if (null === value) return value;
if ('0' === value) return false;
if ('true' === value) return true;
if ('false' === value) return false;
return !! value;
}
/*!
* ignore
*/
function handleArray (val) {
var self = this;
return val.map(function (m) {
return self.cast(m);
});
}
return !!value;
};
SchemaBoolean.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
'$in': handleArray
});
utils.options(SchemaType.prototype.$conditionalHandlers, {});
/**
* Casts contents for queries.
@@ -83,7 +69,7 @@ SchemaBoolean.$conditionalHandlers =
* @api private
*/
SchemaBoolean.prototype.castForQuery = function ($conditional, val) {
SchemaBoolean.prototype.castForQuery = function($conditional, val) {
var handler;
if (2 === arguments.length) {
handler = SchemaBoolean.$conditionalHandlers[$conditional];

View File

@@ -2,6 +2,7 @@
* Module dependencies.
*/
var handleBitwiseOperator = require('./operators/bitwise');
var utils = require('../utils');
var MongooseBuffer = require('../types').Buffer;
@@ -20,7 +21,7 @@ var Document;
* @api private
*/
function SchemaBuffer (key, options) {
function SchemaBuffer(key, options) {
SchemaType.call(this, key, options, 'Buffer');
}
@@ -44,7 +45,7 @@ SchemaBuffer.prototype.constructor = SchemaBuffer;
* @api private
*/
SchemaBuffer.prototype.checkRequired = function (value, doc) {
SchemaBuffer.prototype.checkRequired = function(value, doc) {
if (SchemaType._isRef(this, value, doc, true)) {
return null != value;
} else {
@@ -61,7 +62,8 @@ SchemaBuffer.prototype.checkRequired = function (value, doc) {
* @api private
*/
SchemaBuffer.prototype.cast = function (value, doc, init) {
SchemaBuffer.prototype.cast = function(value, doc, init) {
var ret;
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
@@ -90,7 +92,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
var path = doc.$__fullPath(this.path);
var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
var pop = owner.populated(path, true);
var ret = new pop.options.model(value);
ret = new pop.options.model(value);
ret.$__.wasPopulated = true;
return ret;
}
@@ -111,7 +113,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
return value;
} else if (value instanceof Binary) {
var ret = new MongooseBuffer(value.value(true), [this.path, doc]);
ret = new MongooseBuffer(value.value(true), [this.path, doc]);
if (typeof value.sub_type !== 'number') {
throw new CastError('buffer', value, this.path);
}
@@ -123,7 +125,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
var type = typeof value;
if ('string' == type || 'number' == type || Array.isArray(value)) {
var ret = new MongooseBuffer(value, [this.path, doc]);
ret = new MongooseBuffer(value, [this.path, doc]);
return ret;
}
@@ -133,26 +135,20 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
/*!
* ignore
*/
function handleSingle (val) {
function handleSingle(val) {
return this.castForQuery(val);
}
function handleArray (val) {
var self = this;
return val.map( function (m) {
return self.castForQuery(m);
});
}
SchemaBuffer.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
'$bitsAllClear': handleBitwiseOperator,
'$bitsAnyClear': handleBitwiseOperator,
'$bitsAllSet': handleBitwiseOperator,
'$bitsAnySet': handleBitwiseOperator,
'$gt' : handleSingle,
'$gte': handleSingle,
'$in' : handleArray,
'$lt' : handleSingle,
'$lte': handleSingle,
'$ne' : handleSingle,
'$nin': handleArray
'$lte': handleSingle
});
/**
@@ -163,7 +159,7 @@ SchemaBuffer.prototype.$conditionalHandlers =
* @api private
*/
SchemaBuffer.prototype.castForQuery = function ($conditional, val) {
SchemaBuffer.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];

View File

@@ -2,7 +2,7 @@
* Module requirements.
*/
var errorMessages = require('../error').messages
var errorMessages = require('../error').messages;
var utils = require('../utils');
var SchemaType = require('../schematype');
@@ -18,7 +18,7 @@ var CastError = SchemaType.CastError;
* @api private
*/
function SchemaDate (key, options) {
function SchemaDate(key, options) {
SchemaType.call(this, key, options, 'Date');
}
@@ -67,7 +67,7 @@ SchemaDate.prototype.constructor = SchemaDate;
* @api public
*/
SchemaDate.prototype.expires = function (when) {
SchemaDate.prototype.expires = function(when) {
if (!this._index || 'Object' !== this._index.constructor.name) {
this._index = {};
}
@@ -83,7 +83,7 @@ SchemaDate.prototype.expires = function (when) {
* @api private
*/
SchemaDate.prototype.checkRequired = function (value) {
SchemaDate.prototype.checkRequired = function(value) {
return value instanceof Date;
};
@@ -118,9 +118,9 @@ SchemaDate.prototype.checkRequired = function (value) {
* @api public
*/
SchemaDate.prototype.min = function (value, message) {
SchemaDate.prototype.min = function(value, message) {
if (this.minValidator) {
this.validators = this.validators.filter(function (v) {
this.validators = this.validators.filter(function(v) {
return v.validator != this.minValidator;
}, this);
}
@@ -130,12 +130,13 @@ SchemaDate.prototype.min = function (value, message) {
msg = msg.replace(/{MIN}/, (value === Date.now ? 'Date.now()' : this.cast(value).toString()));
var self = this;
this.validators.push({
validator: this.minValidator = function (val) {
validator: this.minValidator = function(val) {
var min = (value === Date.now ? value() : self.cast(value));
return val === null || val.valueOf() >= min.valueOf();
},
message: msg,
type: 'min'
type: 'min',
min: value
});
}
@@ -173,9 +174,9 @@ SchemaDate.prototype.min = function (value, message) {
* @api public
*/
SchemaDate.prototype.max = function (value, message) {
SchemaDate.prototype.max = function(value, message) {
if (this.maxValidator) {
this.validators = this.validators.filter(function(v){
this.validators = this.validators.filter(function(v) {
return v.validator != this.maxValidator;
}, this);
}
@@ -190,7 +191,8 @@ SchemaDate.prototype.max = function (value, message) {
return val === null || val.valueOf() <= max.valueOf();
},
message: msg,
type: 'max'
type: 'max',
max: value
});
}
@@ -204,10 +206,10 @@ SchemaDate.prototype.max = function (value, message) {
* @api private
*/
SchemaDate.prototype.cast = function (value) {
SchemaDate.prototype.cast = function(value) {
// If null or undefined
if (value == null || value === '')
return value;
return null;
if (value instanceof Date)
return value;
@@ -238,27 +240,16 @@ SchemaDate.prototype.cast = function (value) {
* @api private
*/
function handleSingle (val) {
function handleSingle(val) {
return this.cast(val);
}
function handleArray (val) {
var self = this;
return val.map( function (m) {
return self.cast(m);
});
}
SchemaDate.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
'$all': handleArray,
'$gt': handleSingle,
'$gte': handleSingle,
'$in': handleArray,
'$lt': handleSingle,
'$lte': handleSingle,
'$ne': handleSingle,
'$nin': handleArray
'$lte': handleSingle
});
@@ -270,7 +261,7 @@ SchemaDate.prototype.$conditionalHandlers =
* @api private
*/
SchemaDate.prototype.castForQuery = function ($conditional, val) {
SchemaDate.prototype.castForQuery = function($conditional, val) {
var handler;
if (2 !== arguments.length) {

View File

@@ -1,10 +1,11 @@
/* eslint no-empty: 1 */
/*!
* Module dependencies.
*/
var ArrayType = require('./array');
var Document = require('../document');
var CastError = require('../error/cast');
var MongooseDocumentArray = require('../types/documentarray');
var SchemaType = require('../schematype');
var Subdocument = require('../types/embedded');
@@ -19,10 +20,9 @@ var Subdocument = require('../types/embedded');
* @api private
*/
function DocumentArray (key, schema, options) {
function DocumentArray(key, schema, options) {
// compile an embedded document for this schema
function EmbeddedDocument () {
function EmbeddedDocument() {
Subdocument.apply(this, arguments);
}
@@ -35,7 +35,7 @@ function DocumentArray (key, schema, options) {
EmbeddedDocument.prototype[i] = schema.methods[i];
// apply statics
for (var i in schema.statics)
for (i in schema.statics)
EmbeddedDocument[i] = schema.statics[i];
EmbeddedDocument.options = options;
@@ -47,7 +47,7 @@ function DocumentArray (key, schema, options) {
var path = this.path;
var fn = this.defaultValue;
this.default(function(){
this.default(function() {
var arr = fn.call(this);
if (!Array.isArray(arr)) arr = [arr];
return new MongooseDocumentArray(arr, path, this);
@@ -74,8 +74,8 @@ DocumentArray.prototype.constructor = DocumentArray;
* @api private
*/
DocumentArray.prototype.doValidate = function (array, fn, scope) {
SchemaType.prototype.doValidate.call(this, array, function (err) {
DocumentArray.prototype.doValidate = function(array, fn, scope) {
SchemaType.prototype.doValidate.call(this, array, function(err) {
if (err) {
return fn(err);
}
@@ -97,7 +97,7 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
continue;
}
doc.validate(function (err) {
doc.validate({ __noPromise: true }, function(err) {
if (err) {
error = err;
}
@@ -118,12 +118,12 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
* @api private
*/
DocumentArray.prototype.doValidateSync = function (array, scope) {
DocumentArray.prototype.doValidateSync = function(array, scope) {
var schemaTypeError = SchemaType.prototype.doValidateSync.call(this, array, scope);
if (schemaTypeError) return schemaTypeError;
var count = array && array.length
, resultError = null;
var count = array && array.length,
resultError = null;
if (!count) return;
@@ -156,10 +156,10 @@ DocumentArray.prototype.doValidateSync = function (array, scope) {
* @api private
*/
DocumentArray.prototype.cast = function (value, doc, init, prev) {
var selected
, subdoc
, i
DocumentArray.prototype.cast = function(value, doc, init, prev) {
var selected,
subdoc,
i;
if (!Array.isArray(value)) {
// gh-2442 mark whole array as modified if we're initializing a doc from
@@ -190,7 +190,7 @@ DocumentArray.prototype.cast = function (value, doc, init, prev) {
} else {
try {
subdoc = prev.id(value[i]._id);
} catch(e) {}
} catch (e) {}
if (prev && subdoc) {
// handle resetting doc with existing id but differing data
@@ -200,17 +200,22 @@ DocumentArray.prototype.cast = function (value, doc, init, prev) {
// see gh-746
value[i] = subdoc;
} else {
subdoc = new this.casterConstructor(value[i], value, undefined, undefined, i);
// if set() is hooked it will have no return value
// see gh-746
value[i] = subdoc;
try {
subdoc = new this.casterConstructor(value[i], value, undefined,
undefined, i);
// if set() is hooked it will have no return value
// see gh-746
value[i] = subdoc;
} catch (error) {
throw new CastError('embedded', value[i], value._path);
}
}
}
}
}
return value;
}
};
/*!
* Scopes paths selected in a query to this array.
@@ -221,15 +226,15 @@ DocumentArray.prototype.cast = function (value, doc, init, prev) {
* @param {Boolean|undefined} init - if we are being created part of a query result
*/
function scopePaths (array, fields, init) {
function scopePaths(array, fields, init) {
if (!(init && fields)) return undefined;
var path = array.path + '.'
, keys = Object.keys(fields)
, i = keys.length
, selected = {}
, hasKeys
, key
var path = array.path + '.',
keys = Object.keys(fields),
i = keys.length,
selected = {},
hasKeys,
key;
while (i--) {
key = keys[i];

137
node_modules/mongoose/lib/schema/embedded.js generated vendored Normal file
View File

@@ -0,0 +1,137 @@
var SchemaType = require('../schematype');
var Subdocument = require('../types/subdocument');
module.exports = Embedded;
/**
* Sub-schema schematype constructor
*
* @param {Schema} schema
* @param {String} key
* @param {Object} options
* @inherits SchemaType
* @api private
*/
function Embedded(schema, path, options) {
var _embedded = function(value, path, parent) {
var _this = this;
Subdocument.apply(this, arguments);
this.$parent = parent;
if (parent) {
parent.on('save', function() {
_this.emit('save', _this);
});
}
};
_embedded.prototype = Object.create(Subdocument.prototype);
_embedded.prototype.$__setSchema(schema);
_embedded.schema = schema;
_embedded.$isSingleNested = true;
_embedded.prototype.$basePath = path;
// apply methods
for (var i in schema.methods) {
_embedded.prototype[i] = schema.methods[i];
}
// apply statics
for (i in schema.statics) {
_embedded[i] = schema.statics[i];
}
this.caster = _embedded;
this.schema = schema;
this.$isSingleNested = true;
SchemaType.call(this, path, options, 'Embedded');
}
Embedded.prototype = Object.create(SchemaType.prototype);
/**
* Casts contents
*
* @param {Object} value
* @api private
*/
Embedded.prototype.cast = function(val, doc, init) {
if (val && val.$isSingleNested) {
return val;
}
var subdoc = new this.caster(undefined, undefined, doc);
if (init) {
subdoc.init(val);
} else {
subdoc.set(val, undefined, true);
}
return subdoc;
};
/**
* Casts contents for query
*
* @param {string} [$conditional] optional query operator (like `$eq` or `$in`)
* @param {any} value
* @api private
*/
Embedded.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new Error('Can\'t use ' + $conditional);
}
return handler.call(this, val);
} else {
val = $conditional;
return new this.caster(val).
toObject({ virtuals: false });
}
};
/**
* Async validation on this single nested doc.
*
* @api private
*/
Embedded.prototype.doValidate = function(value, fn) {
SchemaType.prototype.doValidate.call(this, value, function(error) {
if (error) {
return fn(error);
}
if (!value) {
return fn(null);
}
value.validate(fn, { __noPromise: true });
});
};
/**
* Synchronously validate this single nested doc
*
* @api private
*/
Embedded.prototype.doValidateSync = function(value) {
var schemaTypeError = SchemaType.prototype.doValidateSync.call(this, value);
if (schemaTypeError) {
return schemaTypeError;
}
if (!value) {
return;
}
return value.validateSync();
};
/**
* Required validator for single nested docs
*
* @api private
*/
Embedded.prototype.checkRequired = function(value) {
return !!value && value.$isSingleNested;
};

View File

@@ -11,6 +11,8 @@ exports.Boolean = require('./boolean');
exports.DocumentArray = require('./documentarray');
exports.Embedded = require('./embedded');
exports.Array = require('./array');
exports.Buffer = require('./buffer');

View File

@@ -15,7 +15,7 @@ var utils = require('../utils');
* @api private
*/
function Mixed (path, options) {
function Mixed(path, options) {
if (options && options.default) {
var def = options.default;
if (Array.isArray(def) && 0 === def.length) {
@@ -25,9 +25,9 @@ function Mixed (path, options) {
utils.isObject(def) &&
0 === Object.keys(def).length) {
// prevent odd "shared" objects between documents
options.default = function () {
return {}
}
options.default = function() {
return {};
};
}
}
@@ -54,7 +54,7 @@ Mixed.prototype.constructor = Mixed;
* @api private
*/
Mixed.prototype.checkRequired = function (val) {
Mixed.prototype.checkRequired = function(val) {
return (val !== undefined) && (val !== null);
};
@@ -67,7 +67,7 @@ Mixed.prototype.checkRequired = function (val) {
* @api private
*/
Mixed.prototype.cast = function (val) {
Mixed.prototype.cast = function(val) {
return val;
};
@@ -79,7 +79,7 @@ Mixed.prototype.cast = function (val) {
* @api private
*/
Mixed.prototype.castForQuery = function ($cond, val) {
Mixed.prototype.castForQuery = function($cond, val) {
if (arguments.length === 2) return val;
return $cond;
};

View File

@@ -2,11 +2,12 @@
* Module requirements.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, errorMessages = require('../error').messages
, utils = require('../utils')
, Document
var SchemaType = require('../schematype');
var CastError = SchemaType.CastError;
var handleBitwiseOperator = require('./operators/bitwise');
var errorMessages = require('../error').messages;
var utils = require('../utils');
var Document;
/**
* Number SchemaType constructor.
@@ -17,7 +18,7 @@ var SchemaType = require('../schematype')
* @api private
*/
function SchemaNumber (key, options) {
function SchemaNumber(key, options) {
SchemaType.call(this, key, options, 'Number');
}
@@ -41,7 +42,7 @@ SchemaNumber.prototype.constructor = SchemaNumber;
* @api private
*/
SchemaNumber.prototype.checkRequired = function checkRequired (value, doc) {
SchemaNumber.prototype.checkRequired = function checkRequired(value, doc) {
if (SchemaType._isRef(this, value, doc, true)) {
return null != value;
} else {
@@ -80,9 +81,9 @@ SchemaNumber.prototype.checkRequired = function checkRequired (value, doc) {
* @api public
*/
SchemaNumber.prototype.min = function (value, message) {
SchemaNumber.prototype.min = function(value, message) {
if (this.minValidator) {
this.validators = this.validators.filter(function (v) {
this.validators = this.validators.filter(function(v) {
return v.validator != this.minValidator;
}, this);
}
@@ -91,11 +92,12 @@ SchemaNumber.prototype.min = function (value, message) {
var msg = message || errorMessages.Number.min;
msg = msg.replace(/{MIN}/, value);
this.validators.push({
validator: this.minValidator = function (v) {
return v === null || v >= value;
validator: this.minValidator = function(v) {
return v == null || v >= value;
},
message: msg,
type: 'min'
type: 'min',
min: value
});
}
@@ -133,9 +135,9 @@ SchemaNumber.prototype.min = function (value, message) {
* @api public
*/
SchemaNumber.prototype.max = function (value, message) {
SchemaNumber.prototype.max = function(value, message) {
if (this.maxValidator) {
this.validators = this.validators.filter(function(v){
this.validators = this.validators.filter(function(v) {
return v.validator != this.maxValidator;
}, this);
}
@@ -145,10 +147,11 @@ SchemaNumber.prototype.max = function (value, message) {
msg = msg.replace(/{MAX}/, value);
this.validators.push({
validator: this.maxValidator = function(v) {
return v === null || v <= value;
return v == null || v <= value;
},
message: msg,
type: 'max'
type: 'max',
max: value
});
}
@@ -164,7 +167,7 @@ SchemaNumber.prototype.max = function (value, message) {
* @api private
*/
SchemaNumber.prototype.cast = function (value, doc, init) {
SchemaNumber.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
@@ -202,15 +205,17 @@ SchemaNumber.prototype.cast = function (value, doc, init) {
? value._id // documents
: value;
if (!isNaN(val)){
if (!isNaN(val)) {
if (null === val) return val;
if ('' === val) return null;
if ('string' == typeof val) val = Number(val);
if (val instanceof Number) return val
if (typeof val === 'string' || typeof val === 'boolean') {
val = Number(val);
}
if (val instanceof Number) return val;
if ('number' == typeof val) return val;
if (val.toString && !Array.isArray(val) &&
val.toString() == Number(val)) {
return new Number(val)
return new Number(val);
}
}
@@ -221,28 +226,31 @@ SchemaNumber.prototype.cast = function (value, doc, init) {
* ignore
*/
function handleSingle (val) {
return this.cast(val)
function handleSingle(val) {
return this.cast(val);
}
function handleArray (val) {
function handleArray(val) {
var self = this;
return val.map(function (m) {
return self.cast(m)
if (!Array.isArray(val)) {
return [this.cast(val)];
}
return val.map(function(m) {
return self.cast(m);
});
}
SchemaNumber.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
'$all': handleArray,
'$bitsAllClear': handleBitwiseOperator,
'$bitsAnyClear': handleBitwiseOperator,
'$bitsAllSet': handleBitwiseOperator,
'$bitsAnySet': handleBitwiseOperator,
'$gt' : handleSingle,
'$gte': handleSingle,
'$in' : handleArray,
'$lt' : handleSingle,
'$lte': handleSingle,
'$ne' : handleSingle,
'$mod': handleArray,
'$nin': handleArray
'$mod': handleArray
});
/**
@@ -253,7 +261,7 @@ SchemaNumber.prototype.$conditionalHandlers =
* @api private
*/
SchemaNumber.prototype.castForQuery = function ($conditional, val) {
SchemaNumber.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
@@ -262,7 +270,7 @@ SchemaNumber.prototype.castForQuery = function ($conditional, val) {
return handler.call(this, val);
} else {
val = this.cast($conditional);
return val == null ? val : val
return val == null ? val : val;
}
};

View File

@@ -1,12 +1,14 @@
/* eslint no-empty: 1 */
/*!
* Module dependencies.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, oid = require('../types/objectid')
, utils = require('../utils')
, Document
var SchemaType = require('../schematype'),
CastError = SchemaType.CastError,
oid = require('../types/objectid'),
utils = require('../utils'),
Document;
/**
* ObjectId SchemaType constructor.
@@ -17,7 +19,7 @@ var SchemaType = require('../schematype')
* @api private
*/
function ObjectId (key, options) {
function ObjectId(key, options) {
SchemaType.call(this, key, options, 'ObjectID');
}
@@ -42,10 +44,10 @@ ObjectId.prototype.constructor = ObjectId;
* @return {SchemaType} this
*/
ObjectId.prototype.auto = function (turnOn) {
ObjectId.prototype.auto = function(turnOn) {
if (turnOn) {
this.default(defaultId);
this.set(resetId)
this.set(resetId);
}
return this;
@@ -57,7 +59,7 @@ ObjectId.prototype.auto = function (turnOn) {
* @api private
*/
ObjectId.prototype.checkRequired = function checkRequired (value, doc) {
ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
if (SchemaType._isRef(this, value, doc, true)) {
return null != value;
} else {
@@ -74,7 +76,7 @@ ObjectId.prototype.checkRequired = function checkRequired (value, doc) {
* @api private
*/
ObjectId.prototype.cast = function (value, doc, init) {
ObjectId.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
@@ -121,7 +123,7 @@ ObjectId.prototype.cast = function (value, doc, init) {
if (value._id.toString instanceof Function) {
try {
return oid.createFromHexString(value._id.toString());
} catch(e) {}
} catch (e) {}
}
}
@@ -140,27 +142,16 @@ ObjectId.prototype.cast = function (value, doc, init) {
* ignore
*/
function handleSingle (val) {
function handleSingle(val) {
return this.cast(val);
}
function handleArray (val) {
var self = this;
return val.map(function (m) {
return self.cast(m);
});
}
ObjectId.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
'$all': handleArray,
'$gt': handleSingle,
'$gte': handleSingle,
'$in': handleArray,
'$lt': handleSingle,
'$lte': handleSingle,
'$ne': handleSingle,
'$nin': handleArray
'$lte': handleSingle
});
/**
@@ -171,7 +162,7 @@ ObjectId.prototype.$conditionalHandlers =
* @api private
*/
ObjectId.prototype.castForQuery = function ($conditional, val) {
ObjectId.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
@@ -187,11 +178,11 @@ ObjectId.prototype.castForQuery = function ($conditional, val) {
* ignore
*/
function defaultId () {
function defaultId() {
return new oid();
};
}
function resetId (v) {
function resetId(v) {
this.$__._id = null;
return v;
}

37
node_modules/mongoose/lib/schema/operators/bitwise.js generated vendored Normal file
View File

@@ -0,0 +1,37 @@
/*!
* Module requirements.
*/
var CastError = require('../../error/cast');
/*!
* ignore
*/
function handleBitwiseOperator(val) {
var _this = this;
if (Array.isArray(val)) {
return val.map(function(v) {
return _castNumber(_this, v);
});
} else if (Buffer.isBuffer(val)) {
return val;
} else {
// Assume trying to cast to number
return _castNumber(_this, val);
}
}
/*!
* ignore
*/
function _castNumber(_this, num) {
var v = Number(num);
if (isNaN(v)) {
throw new CastError('number', num, _this.path);
}
return v;
}
module.exports = handleBitwiseOperator;

View File

@@ -3,11 +3,11 @@
* Module dependencies.
*/
var SchemaType = require('../schematype')
, CastError = SchemaType.CastError
, errorMessages = require('../error').messages
, utils = require('../utils')
, Document
var SchemaType = require('../schematype');
var CastError = SchemaType.CastError;
var errorMessages = require('../error').messages;
var utils = require('../utils');
var Document;
/**
* String SchemaType constructor.
@@ -18,11 +18,11 @@ var SchemaType = require('../schematype')
* @api private
*/
function SchemaString (key, options) {
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
@@ -73,7 +73,7 @@ SchemaString.prototype.constructor = SchemaString;
* @api public
*/
SchemaString.prototype.enum = function () {
SchemaString.prototype.enum = function() {
if (this.enumValidator) {
this.validators = this.validators.filter(function(v) {
return v.validator != this.enumValidator;
@@ -103,13 +103,14 @@ SchemaString.prototype.enum = function () {
}
var vals = this.enumValues;
this.enumValidator = function (v) {
this.enumValidator = function(v) {
return undefined === v || ~vals.indexOf(v);
};
this.validators.push({
validator: this.enumValidator,
message: errorMessage,
type: 'enum'
type: 'enum',
enumValues: vals
});
return this;
@@ -129,9 +130,9 @@ SchemaString.prototype.enum = function () {
* @return {SchemaType} this
*/
SchemaString.prototype.lowercase = function () {
return this.set(function (v, self) {
if ('string' != typeof v) v = self.cast(v)
SchemaString.prototype.lowercase = function() {
return this.set(function(v, self) {
if ('string' != typeof v) v = self.cast(v);
if (v) return v.toLowerCase();
return v;
});
@@ -151,9 +152,9 @@ SchemaString.prototype.lowercase = function () {
* @return {SchemaType} this
*/
SchemaString.prototype.uppercase = function () {
return this.set(function (v, self) {
if ('string' != typeof v) v = self.cast(v)
SchemaString.prototype.uppercase = function() {
return this.set(function(v, self) {
if ('string' != typeof v) v = self.cast(v);
if (v) return v.toUpperCase();
return v;
});
@@ -177,9 +178,9 @@ SchemaString.prototype.uppercase = function () {
* @return {SchemaType} this
*/
SchemaString.prototype.trim = function () {
return this.set(function (v, self) {
if ('string' != typeof v) v = self.cast(v)
SchemaString.prototype.trim = function() {
return this.set(function(v, self) {
if ('string' != typeof v) v = self.cast(v);
if (v) return v.trim();
return v;
});
@@ -200,12 +201,12 @@ SchemaString.prototype.trim = function () {
* })
*
* // custom error messages
* // We can also use the special {MINLENGTH} token which will be replaced with the invalid value
* var minlength = [10, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum length ({MINLENGTH}).'];
* // We can also use the special {MINLENGTH} token which will be replaced with the minimum allowed length
* var minlength = [5, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).'];
* var schema = new Schema({ postalCode: { type: String, minlength: minlength })
* var Address = mongoose.model('Address', schema);
* var address = new Address({ postalCode: '9512' });
* s.validate(function (err) {
* address.validate(function (err) {
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512`) is shorter than the minimum length (5).
* })
*
@@ -216,9 +217,9 @@ SchemaString.prototype.trim = function () {
* @api public
*/
SchemaString.prototype.minlength = function (value, message) {
SchemaString.prototype.minlength = function(value, message) {
if (this.minlengthValidator) {
this.validators = this.validators.filter(function (v) {
this.validators = this.validators.filter(function(v) {
return v.validator != this.minlengthValidator;
}, this);
}
@@ -227,11 +228,12 @@ SchemaString.prototype.minlength = function (value, message) {
var msg = message || errorMessages.String.minlength;
msg = msg.replace(/{MINLENGTH}/, value);
this.validators.push({
validator: this.minlengthValidator = function (v) {
validator: this.minlengthValidator = function(v) {
return v === null || v.length >= value;
},
message: msg,
type: 'minlength'
type: 'minlength',
minlength: value
});
}
@@ -253,13 +255,13 @@ SchemaString.prototype.minlength = function (value, message) {
* })
*
* // custom error messages
* // We can also use the special {MAXLENGTH} token which will be replaced with the invalid value
* var maxlength = [10, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
* // We can also use the special {MAXLENGTH} token which will be replaced with the maximum allowed length
* var maxlength = [9, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
* var schema = new Schema({ postalCode: { type: String, maxlength: maxlength })
* var Address = mongoose.model('Address', schema);
* var address = new Address({ postalCode: '9512512345' });
* address.validate(function (err) {
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512512345`) exceeds the maximum allowed length (10).
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512512345`) exceeds the maximum allowed length (9).
* })
*
* @param {Number} value maximum string length
@@ -269,9 +271,9 @@ SchemaString.prototype.minlength = function (value, message) {
* @api public
*/
SchemaString.prototype.maxlength = function (value, message) {
SchemaString.prototype.maxlength = function(value, message) {
if (this.maxlengthValidator) {
this.validators = this.validators.filter(function(v){
this.validators = this.validators.filter(function(v) {
return v.validator != this.maxlengthValidator;
}, this);
}
@@ -284,7 +286,8 @@ SchemaString.prototype.maxlength = function (value, message) {
return v === null || v.length <= value;
},
message: msg,
type: 'maxlength'
type: 'maxlength',
maxlength: value
});
}
@@ -329,19 +332,28 @@ SchemaString.prototype.maxlength = function (value, message) {
* @api public
*/
SchemaString.prototype.match = function match (regExp, message) {
SchemaString.prototype.match = function match(regExp, message) {
// yes, we allow multiple match validators
var msg = message || errorMessages.String.match;
var matchValidator = function(v) {
if (!regExp) {
return false;
}
var ret = ((null != v && '' !== v)
? regExp.test(v)
: true);
return ret;
};
this.validators.push({ validator: matchValidator, message: msg, type: 'regexp' });
this.validators.push({
validator: matchValidator,
message: msg,
type: 'regexp',
regexp: regExp
});
return this;
};
@@ -352,7 +364,7 @@ SchemaString.prototype.match = function match (regExp, message) {
* @api private
*/
SchemaString.prototype.checkRequired = function checkRequired (value, doc) {
SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
if (SchemaType._isRef(this, value, doc, true)) {
return null != value;
} else {
@@ -366,7 +378,7 @@ SchemaString.prototype.checkRequired = function checkRequired (value, doc) {
* @api private
*/
SchemaString.prototype.cast = function (value, doc, init) {
SchemaString.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
@@ -426,13 +438,16 @@ SchemaString.prototype.cast = function (value, doc, init) {
* ignore
*/
function handleSingle (val) {
function handleSingle(val) {
return this.castForQuery(val);
}
function handleArray (val) {
function handleArray(val) {
var self = this;
return val.map(function (m) {
if (!Array.isArray(val)) {
return [this.castForQuery(val)];
}
return val.map(function(m) {
return self.castForQuery(m);
});
}
@@ -442,11 +457,8 @@ SchemaString.prototype.$conditionalHandlers =
'$all': handleArray,
'$gt' : handleSingle,
'$gte': handleSingle,
'$in' : handleArray,
'$lt' : handleSingle,
'$lte': handleSingle,
'$ne' : handleSingle,
'$nin': handleArray,
'$options': handleSingle,
'$regex': handleSingle
});
@@ -459,7 +471,7 @@ SchemaString.prototype.$conditionalHandlers =
* @api private
*/
SchemaString.prototype.castForQuery = function ($conditional, val) {
SchemaString.prototype.castForQuery = function($conditional, val) {
var handler;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
@@ -468,7 +480,9 @@ SchemaString.prototype.castForQuery = function ($conditional, val) {
return handler.call(this, val);
} else {
val = $conditional;
if (val instanceof RegExp) return val;
if (Object.prototype.toString.call(val) === '[object RegExp]') {
return val;
}
return this.cast(val);
}
};