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

@@ -8,6 +8,7 @@ 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;
@@ -97,18 +98,24 @@ SchemaDate.cast = function cast(caster) {
return this._cast;
}
if (caster === false) {
caster = v => {
if (v != null && !(v instanceof Date)) {
throw new Error();
}
return v;
};
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.
*
@@ -131,7 +138,7 @@ SchemaDate.cast = function cast(caster) {
* new Schema({ createdAt: { type: Date, expires: '1.5h' }});
*
* // expire in 7 days
* var schema = new Schema({ createdAt: Date });
* const schema = new Schema({ createdAt: Date });
* schema.path('createdAt').expires('7d');
*
* @param {Number|String} when
@@ -141,7 +148,7 @@ SchemaDate.cast = function cast(caster) {
*/
SchemaDate.prototype.expires = function(when) {
if (!this._index || this._index.constructor.name !== 'Object') {
if (getConstructorName(this._index) !== 'Object') {
this._index = {};
}
@@ -205,9 +212,9 @@ SchemaDate.prototype.checkRequired = function(value, doc) {
*
* ####Example:
*
* var s = new Schema({ d: { type: Date, min: Date('1970-01-01') })
* var M = db.model('M', s)
* var m = new M({ d: Date('1969-12-31') })
* 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');
@@ -216,10 +223,10 @@ SchemaDate.prototype.checkRequired = function(value, doc) {
*
* // custom error messages
* // We can also use the special {MIN} token which will be replaced with the invalid value
* var min = [Date('1970-01-01'), 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'];
* var schema = new Schema({ d: { type: Date, min: min })
* var M = mongoose.model('M', schema);
* var s= new M({ d: Date('1969-12-31') });
* 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).
* })
@@ -267,9 +274,9 @@ SchemaDate.prototype.min = function(value, message) {
*
* ####Example:
*
* var s = new Schema({ d: { type: Date, max: Date('2014-01-01') })
* var M = db.model('M', s)
* var m = new M({ d: Date('2014-12-08') })
* 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');
@@ -278,10 +285,10 @@ SchemaDate.prototype.min = function(value, message) {
*
* // custom error messages
* // We can also use the special {MAX} token which will be replaced with the invalid value
* var max = [Date('2014-01-01'), 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).'];
* var schema = new Schema({ d: { type: Date, max: max })
* var M = mongoose.model('M', schema);
* var s= new M({ d: Date('2014-12-08') });
* 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).
* })
@@ -332,9 +339,15 @@ SchemaDate.prototype.max = function(value, message) {
*/
SchemaDate.prototype.cast = function(value) {
const castDate = typeof this.constructor.cast === 'function' ?
this.constructor.cast() :
SchemaDate.cast();
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) {