mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Updated mongoose
This commit is contained in:
320
node_modules/mongoose/lib/schema/string.js
generated
vendored
320
node_modules/mongoose/lib/schema/string.js
generated
vendored
@ -3,10 +3,11 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var SchemaType = require('../schematype')
|
||||
, CastError = SchemaType.CastError
|
||||
, utils = require('../utils')
|
||||
, Document
|
||||
var SchemaType = require('../schematype'),
|
||||
CastError = SchemaType.CastError,
|
||||
errorMessages = require('../error').messages,
|
||||
utils = require('../utils'),
|
||||
Document;
|
||||
|
||||
/**
|
||||
* String SchemaType constructor.
|
||||
@ -17,64 +18,100 @@ 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
|
||||
* function names.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
SchemaString.schemaName = 'String';
|
||||
|
||||
/*!
|
||||
* Inherits from SchemaType.
|
||||
*/
|
||||
|
||||
SchemaString.prototype.__proto__ = SchemaType.prototype;
|
||||
SchemaString.prototype = Object.create( SchemaType.prototype );
|
||||
SchemaString.prototype.constructor = SchemaString;
|
||||
|
||||
/**
|
||||
* Adds enumeration values and a coinciding validator.
|
||||
* Adds an enum validator
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var states = 'opening open closing closed'.split(' ')
|
||||
* var s = new Schema({ state: { type: String, enum: states })
|
||||
* var s = new Schema({ state: { type: String, enum: states }})
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ state: 'invalid' })
|
||||
* m.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* console.error(String(err)) // ValidationError: `invalid` is not a valid enum value for path `state`.
|
||||
* m.state = 'open'
|
||||
* m.save() // success
|
||||
* m.save(callback) // success
|
||||
* })
|
||||
*
|
||||
* @param {String} [args...] enumeration values
|
||||
* // or with custom error messages
|
||||
* var enu = {
|
||||
* values: 'opening open closing closed'.split(' '),
|
||||
* message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
||||
* }
|
||||
* var s = new Schema({ state: { type: String, enum: enu })
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ state: 'invalid' })
|
||||
* m.save(function (err) {
|
||||
* console.error(String(err)) // ValidationError: enum validator failed for path `state` with value `invalid`
|
||||
* m.state = 'open'
|
||||
* m.save(callback) // success
|
||||
* })
|
||||
*
|
||||
* @param {String|Object} [args...] enumeration values
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.enum = function () {
|
||||
var len = arguments.length;
|
||||
SchemaString.prototype.enum = function() {
|
||||
if (this.enumValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.enumValidator;
|
||||
}, this);
|
||||
this.enumValidator = false;
|
||||
}
|
||||
|
||||
if (!len || undefined === arguments[0] || false === arguments[0]) {
|
||||
if (this.enumValidator){
|
||||
this.enumValidator = false;
|
||||
this.validators = this.validators.filter(function(v){
|
||||
return v[1] != 'enum';
|
||||
});
|
||||
}
|
||||
if (undefined === arguments[0] || false === arguments[0]) {
|
||||
return this;
|
||||
}
|
||||
|
||||
for (var i = 0; i < len; i++) {
|
||||
if (undefined !== arguments[i]) {
|
||||
this.enumValues.push(this.cast(arguments[i]));
|
||||
var values;
|
||||
var errorMessage;
|
||||
|
||||
if (utils.isObject(arguments[0])) {
|
||||
values = arguments[0].values;
|
||||
errorMessage = arguments[0].message;
|
||||
} else {
|
||||
values = arguments;
|
||||
errorMessage = errorMessages.String.enum;
|
||||
}
|
||||
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
if (undefined !== values[i]) {
|
||||
this.enumValues.push(this.cast(values[i]));
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.enumValidator) {
|
||||
var values = this.enumValues;
|
||||
this.enumValidator = function(v){
|
||||
return undefined === v || ~values.indexOf(v);
|
||||
};
|
||||
this.validators.push([this.enumValidator, 'enum']);
|
||||
}
|
||||
var 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;
|
||||
};
|
||||
@ -93,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;
|
||||
});
|
||||
@ -115,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;
|
||||
});
|
||||
@ -141,14 +178,122 @@ 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;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a minimum length validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ postalCode: { type: String, minlength: 5 })
|
||||
* var Address = db.model('Address', schema)
|
||||
* var address = new Address({ postalCode: '9512' })
|
||||
* address.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* address.postalCode = '95125';
|
||||
* address.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MINLENGTH} token which will be replaced with the minimum allowed length
|
||||
* var minlength = [5, 'The value of path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).'];
|
||||
* var schema = new Schema({ postalCode: { type: String, minlength: minlength })
|
||||
* var Address = mongoose.model('Address', schema);
|
||||
* var address = new Address({ postalCode: '9512' });
|
||||
* address.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512`) is shorter than the minimum length (5).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value minimum string length
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.minlength = function(value, message) {
|
||||
if (this.minlengthValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.minlengthValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (null != value) {
|
||||
var msg = message || errorMessages.String.minlength;
|
||||
msg = msg.replace(/{MINLENGTH}/, value);
|
||||
this.validators.push({
|
||||
validator: this.minlengthValidator = function(v) {
|
||||
return v === null || v.length >= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'minlength',
|
||||
minlength: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a maximum length validator.
|
||||
*
|
||||
* ####Example:
|
||||
*
|
||||
* var schema = new Schema({ postalCode: { type: String, maxlength: 9 })
|
||||
* var Address = db.model('Address', schema)
|
||||
* var address = new Address({ postalCode: '9512512345' })
|
||||
* address.save(function (err) {
|
||||
* console.error(err) // validator error
|
||||
* address.postalCode = '95125';
|
||||
* address.save() // success
|
||||
* })
|
||||
*
|
||||
* // custom error messages
|
||||
* // We can also use the special {MAXLENGTH} token which will be replaced with the maximum allowed length
|
||||
* var maxlength = [9, 'The value of path `{PATH}` (`{VALUE}`) exceeds the maximum allowed length ({MAXLENGTH}).'];
|
||||
* var schema = new Schema({ postalCode: { type: String, maxlength: maxlength })
|
||||
* var Address = mongoose.model('Address', schema);
|
||||
* var address = new Address({ postalCode: '9512512345' });
|
||||
* address.validate(function (err) {
|
||||
* console.log(String(err)) // ValidationError: The value of path `postalCode` (`9512512345`) exceeds the maximum allowed length (9).
|
||||
* })
|
||||
*
|
||||
* @param {Number} value maximum string length
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.maxlength = function(value, message) {
|
||||
if (this.maxlengthValidator) {
|
||||
this.validators = this.validators.filter(function(v) {
|
||||
return v.validator != this.maxlengthValidator;
|
||||
}, this);
|
||||
}
|
||||
|
||||
if (null != value) {
|
||||
var msg = message || errorMessages.String.maxlength;
|
||||
msg = msg.replace(/{MAXLENGTH}/, value);
|
||||
this.validators.push({
|
||||
validator: this.maxlengthValidator = function(v) {
|
||||
return v === null || v.length <= value;
|
||||
},
|
||||
message: msg,
|
||||
type: 'maxlength',
|
||||
maxlength: value
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a regexp validator.
|
||||
*
|
||||
@ -158,27 +303,57 @@ SchemaString.prototype.trim = function () {
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, match: /^a/ }})
|
||||
* var M = db.model('M', s)
|
||||
* var m = new M({ name: 'invalid' })
|
||||
* var m = new M({ name: 'I am invalid' })
|
||||
* m.validate(function (err) {
|
||||
* console.error(err) // validation error
|
||||
* console.error(String(err)) // "ValidationError: Path `name` is invalid (I am invalid)."
|
||||
* m.name = 'apples'
|
||||
* m.validate(function (err) {
|
||||
* assert.ok(err) // success
|
||||
* })
|
||||
* })
|
||||
*
|
||||
* // using a custom error message
|
||||
* var match = [ /\.html$/, "That file doesn't end in .html ({VALUE})" ];
|
||||
* var s = new Schema({ file: { type: String, match: match }})
|
||||
* var M = db.model('M', s);
|
||||
* var m = new M({ file: 'invalid' });
|
||||
* m.validate(function (err) {
|
||||
* console.log(String(err)) // "ValidationError: That file doesn't end in .html (invalid)"
|
||||
* })
|
||||
*
|
||||
* Empty strings, `undefined`, and `null` values always pass the match validator. If you require these values, enable the `required` validator also.
|
||||
*
|
||||
* var s = new Schema({ name: { type: String, match: /^a/, required: true }})
|
||||
*
|
||||
* @param {RegExp} regExp regular expression to test against
|
||||
* @param {String} [message] optional custom error message
|
||||
* @return {SchemaType} this
|
||||
* @see Customized Error Messages #error_messages_MongooseError-messages
|
||||
* @api public
|
||||
*/
|
||||
|
||||
SchemaString.prototype.match = function match (regExp) {
|
||||
this.validators.push([function(v){
|
||||
return null != v && '' !== v
|
||||
? regExp.test(v)
|
||||
: true
|
||||
}, 'regexp']);
|
||||
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',
|
||||
regexp: regExp
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
@ -189,7 +364,7 @@ SchemaString.prototype.match = function match (regExp) {
|
||||
* @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 {
|
||||
@ -203,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
|
||||
|
||||
@ -237,7 +412,8 @@ SchemaString.prototype.cast = function (value, doc, init) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
// If null or undefined
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@ -246,12 +422,15 @@ SchemaString.prototype.cast = function (value, doc, init) {
|
||||
if (value._id && 'string' == typeof value._id) {
|
||||
return value._id;
|
||||
}
|
||||
if (value.toString) {
|
||||
|
||||
// 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) {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
throw new CastError('string', value, this.path);
|
||||
};
|
||||
|
||||
@ -259,29 +438,30 @@ 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);
|
||||
});
|
||||
}
|
||||
|
||||
SchemaString.prototype.$conditionalHandlers = {
|
||||
'$ne' : handleSingle
|
||||
, '$in' : handleArray
|
||||
, '$nin': handleArray
|
||||
, '$gt' : handleSingle
|
||||
, '$lt' : handleSingle
|
||||
, '$gte': handleSingle
|
||||
, '$lte': handleSingle
|
||||
, '$all': handleArray
|
||||
, '$regex': handleSingle
|
||||
, '$options': handleSingle
|
||||
};
|
||||
SchemaString.prototype.$conditionalHandlers =
|
||||
utils.options(SchemaType.prototype.$conditionalHandlers, {
|
||||
'$all': handleArray,
|
||||
'$gt' : handleSingle,
|
||||
'$gte': handleSingle,
|
||||
'$lt' : handleSingle,
|
||||
'$lte': handleSingle,
|
||||
'$options': handleSingle,
|
||||
'$regex': handleSingle
|
||||
});
|
||||
|
||||
/**
|
||||
* Casts contents for queries.
|
||||
@ -291,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];
|
||||
|
Reference in New Issue
Block a user