-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdate.js
42 lines (37 loc) · 984 Bytes
/
date.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Rule for validating a date against a format.
*/
function validator() {
var moment = require('moment');
var mmt = this.rule.local ? moment : moment.utc;
var dt = !this.rule.format
? mmt(new Date(this.value)) : mmt(this.value, this.rule.format);
if(!dt.isValid()) {
if(this.rule.format) {
this.raise(
this.messages.date.format,
this.field, this.value, this.rule.format);
}else{
this.raise(
this.messages.date.invalid, this.field, this.value);
}
}
}
module.exports = function() {
/**
* Validates a date against the format property.
*
* @param cb The callback function.
*/
this.main.date = function date(cb) {
var validate = this.isRoot() || this.rule.required
|| (!this.rule.required && this.source.hasOwnProperty(this.field)
&& this.source[this.field]);
if(validate) {
this.required();
this.pattern();
validator.call(this);
}
cb();
}
}