6 var SchemaType = require('../schematype')
7 , CastError = SchemaType.CastError;
10 * Date SchemaType constructor.
13 * @param {Object} options
17 function SchemaDate (key, options) {
18 SchemaType.call(this, key, options);
22 * Inherits from SchemaType.
25 SchemaDate.prototype.__proto__ = SchemaType.prototype;
28 * Required validator for date
33 SchemaDate.prototype.checkRequired = function (value) {
34 return value instanceof Date;
40 * @param {Object} value to cast
44 SchemaDate.prototype.cast = function (value) {
45 if (value === null || value === '')
48 if (value instanceof Date)
53 // support for timestamps
54 if (value instanceof Number || 'number' == typeof value
55 || String(value) == Number(value))
56 date = new Date(Number(value));
58 // support for date strings
59 else if (value.toString)
60 date = new Date(value.toString());
62 if (date.toString() != 'Invalid Date')
65 throw new CastError('date', value);
74 function handleSingle (val) {
75 return this.cast(val);
78 function handleArray (val) {
80 return val.map( function (m) {
85 SchemaDate.prototype.$conditionalHandlers = {
87 , '$lte': handleSingle
89 , '$gte': handleSingle
95 SchemaDate.prototype.castForQuery = function ($conditional, val) {
98 if (2 !== arguments.length) {
99 return this.cast($conditional);
102 handler = this.$conditionalHandlers[$conditional];
105 throw new Error("Can't use " + $conditional + " with Date.");
108 return handler.call(this, val);
115 module.exports = SchemaDate;