Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var moment = require('moment'),
_ = require('lodash'),
SchemaType = require('warehouse').SchemaType;
var SchemaMoment = module.exports = function(options){
SchemaType.call(this, options);
};
SchemaMoment.__proto__ = SchemaType;
SchemaMoment.prototype.__proto__ = SchemaType.prototype;
SchemaMoment.prototype.checkRequired = function(value){
return moment.isMoment(value);
};
var cast = SchemaMoment.prototype.cast = function(value){
if (!value) return null;
if (moment.isMoment(value)) return value;
if (hexo.config.language && !_.isArray(hexo.config.language)){
return moment(value).locale(hexo.config.language.toLowerCase());
} else {
return moment(value);
}
};
SchemaTypeMoment.prototype.cast = function(value, data) {
value = SchemaType.prototype.cast.call(this, value, data);
if (value == null) return value;
const { options } = this;
value = toMoment(value);
if (options.language) value = value.locale(toMomentLocale(options.language));
if (options.timezone) value = value.tz(options.timezone);
return value;
};
SchemaTypeMoment.prototype.validate = function(value, data) {
value = SchemaType.prototype.validate.call(this, value, data);
if (value == null) return value;
value = toMoment(value);
if (!value.isValid()) {
throw new Error('`' + value + '` is not a valid date!');
}
return value;
};