This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -10,10 +10,7 @@ const SchemaStringOptions = require('../options/SchemaStringOptions');
const castString = require('../cast/string');
const utils = require('../utils');
const populateModelSymbol = require('../helpers/symbols').populateModelSymbol;
const CastError = SchemaType.CastError;
let Document;
/**
* String SchemaType constructor.
@@ -86,18 +83,24 @@ SchemaString.cast = function cast(caster) {
return this._cast;
}
if (caster === false) {
caster = v => {
if (v != null && typeof v !== 'string') {
throw new Error();
}
return v;
};
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.
*
@@ -171,10 +174,10 @@ SchemaString.checkRequired = SchemaType.checkRequired;
*
* ####Example:
*
* var states = ['opening', 'open', 'closing', 'closed']
* var s = new Schema({ state: { type: String, enum: states }})
* var M = db.model('M', s)
* var m = new M({ state: 'invalid' })
* 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'
@@ -182,13 +185,13 @@ SchemaString.checkRequired = SchemaType.checkRequired;
* })
*
* // or with custom error messages
* var enum = {
* const enum = {
* values: ['opening', 'open', 'closing', 'closed'],
* message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
* }
* var s = new Schema({ state: { type: String, enum: enum })
* var M = db.model('M', s)
* var m = new M({ state: 'invalid' })
* 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'
@@ -217,8 +220,13 @@ SchemaString.prototype.enum = function() {
let errorMessage;
if (utils.isObject(arguments[0])) {
values = arguments[0].values;
errorMessage = arguments[0].message;
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;
@@ -249,9 +257,9 @@ SchemaString.prototype.enum = function() {
*
* ####Example:
*
* var s = new Schema({ email: { type: String, lowercase: true }})
* var M = db.model('M', s);
* var m = new M({ email: 'SomeEmail@example.COM' });
* 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'
*
@@ -287,9 +295,9 @@ SchemaString.prototype.lowercase = function(shouldApply) {
*
* ####Example:
*
* var s = new Schema({ caps: { type: String, uppercase: true }})
* var M = db.model('M', s);
* var m = new M({ caps: 'an 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'
*
@@ -325,11 +333,11 @@ SchemaString.prototype.uppercase = function(shouldApply) {
*
* ####Example:
*
* var s = new Schema({ name: { type: String, trim: true }});
* var M = db.model('M', s);
* var string = ' some name ';
* const s = new Schema({ name: { type: String, trim: true }});
* const M = db.model('M', s);
* const string = ' some name ';
* console.log(string.length); // 11
* var m = new M({ name: string });
* const m = new M({ name: string });
* console.log(m.name.length); // 9
*
* // Equivalent to `findOne({ name: string.trim() })`
@@ -365,9 +373,9 @@ SchemaString.prototype.trim = function(shouldTrim) {
*
* ####Example:
*
* var schema = new Schema({ postalCode: { type: String, minlength: 5 })
* var Address = db.model('Address', schema)
* var address = new Address({ postalCode: '9512' })
* 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';
@@ -376,10 +384,10 @@ SchemaString.prototype.trim = function(shouldTrim) {
*
* // 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' });
* 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).
* })
@@ -414,14 +422,16 @@ SchemaString.prototype.minlength = function(value, message) {
return this;
};
SchemaString.prototype.minLength = SchemaString.prototype.minlength;
/**
* 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' })
* 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';
@@ -430,10 +440,10 @@ SchemaString.prototype.minlength = function(value, message) {
*
* // 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' });
* 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).
* })
@@ -468,6 +478,8 @@ SchemaString.prototype.maxlength = function(value, message) {
return this;
};
SchemaString.prototype.maxLength = SchemaString.prototype.maxlength;
/**
* Sets a regexp validator.
*
@@ -475,9 +487,9 @@ SchemaString.prototype.maxlength = function(value, message) {
*
* ####Example:
*
* var s = new Schema({ name: { type: String, match: /^a/ }})
* var M = db.model('M', s)
* var m = new M({ name: 'I am invalid' })
* 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'
@@ -487,17 +499,17 @@ SchemaString.prototype.maxlength = function(value, message) {
* })
*
* // 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' });
* 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.
*
* var s = new Schema({ name: { type: String, match: /^a/, required: true }})
* 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
@@ -516,6 +528,10 @@ SchemaString.prototype.match = function match(regExp, message) {
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);
@@ -565,41 +581,22 @@ SchemaString.prototype.checkRequired = function checkRequired(value, doc) {
SchemaString.prototype.cast = function(value, doc, init) {
if (SchemaType._isRef(this, value, doc, init)) {
// wait! we may need to cast this to a document
if (value === null || value === undefined) {
return value;
}
// lazy load
Document || (Document = require('./../document'));
if (value instanceof Document) {
value.$__.wasPopulated = true;
return value;
}
// setting a populated path
if (typeof value === 'string') {
return value;
} else if (Buffer.isBuffer(value) || !utils.isObject(value)) {
throw new CastError('string', value, this.path, null, this);
}
// Handle the case where user directly sets a populated
// path to a plain object; cast to the Model used in
// the population query.
const path = doc.$__fullPath(this.path);
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
const pop = owner.populated(path, true);
const ret = new pop.options[populateModelSymbol](value);
ret.$__.wasPopulated = true;
return ret;
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();
}
const castString = typeof this.constructor.cast === 'function' ?
this.constructor.cast() :
SchemaString.cast();
try {
return castString(value);
} catch (error) {