-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathrule.js
167 lines (147 loc) · 3.71 KB
/
rule.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var plugin = require('zephyr')
, format = require('format-util')
, Reason = require('./reason');
/**
* Represents a rule associated with a value to validate.
*/
function Rule(opts) {
if(!(this instanceof Rule)) {
return new Rule(opts);
}
for(var k in opts) {
this[k] = opts[k];
}
// reason constants
this.reasons = Reason.reasons;
}
/**
* @private
*
* Helper for creating validation errors.
*
* If a rule has a message property it takes precedence.
*
* @param reason A reason for the validation error.
* @param message A custom messaage for the error.
* @param ... Replacement parameters passed to format.
*/
function error(message) {
var msg
, err
, res
, args = Array.prototype.slice.call(arguments, 1)
, reason
, tmp;
// allow reason as first argument
if(message instanceof Reason) {
reason = message;
message = arguments[1];
args = args.slice(1);
}
if(typeof(this.rule.message) === 'function') {
// allow calls to error() so that message functions
// may call `this.error()` and not create a stack
// overflow, if the function returns an error it is used
tmp = this.rule.message;
this.rule.message = null;
res = tmp.call(this, message, args);
this.rule.message = tmp;
if(res instanceof Error) {
err = res;
}else{
msg = '' + res;
}
}else{
msg = typeof(this.rule.message) === 'string'
? this.rule.message
: message
|| format(require('../messages').default, this.field);
}
if(typeof this.rule.message === 'object'
&& reason
&& this.rule.message[reason]) {
msg = this.rule.message[reason];
}
if(typeof(msg) === 'string'
&& arguments.length > 1 && !this.rule.message && !this.literal) {
msg = format.apply(null, [msg].concat(args));
}
err = err || new Error(msg);
err.field = this.field;
err.value = this.value;
err.parent = this.parent;
err.names = this.names;
err.key = this.key || err.field;
//console.dir('raising error with names: ' + this.names);
if(!this.key && this.names && this.names.length) {
err.key = this.names.join('.');
}
//console.dir(err.key);
if(reason) {
err.reason = reason;
}
return err;
}
/**
* Get a new error reason.
*/
function reason(id, opts) {
return new Reason(id, opts);
}
/**
* Create an error and adds it to the list of errors to be reported.
*/
function raise(message) {
var parameters = [];
if(arguments.length > 1) {
parameters = Array.prototype.slice.call(arguments, 1);
}
var err = this.error.apply(this, [message].concat(parameters));
this.errors.push(err);
return err;
}
/**
* Determine if validation is required for a field.
*/
function validates() {
if(this.isRoot()) {
return true;
}else if(this.value === undefined && !this.rule.required) {
return false;
}
return this.rule.required
|| (!this.rule.required && this.source.hasOwnProperty(this.field));
}
/**
* Determine is additional fields are present.
*/
function diff(expected, received) {
var i
, results = received.slice(0);
for(i = 0;i < results.length;i++) {
if(~expected.indexOf(results[i])) {
results.splice(i, 1);
i--;
}
}
// no additional fields found
if(results.length === 0) {
return false;
}
// return diff array
return results;
}
/**
* Determine if we are validating the root source object.
*/
function isRoot() {
return this.source === this.value;
}
Rule.prototype.reason = reason;
Rule.prototype.error = error;
Rule.prototype.raise = raise;
Rule.prototype.format = format;
Rule.prototype.isRoot = isRoot;
Rule.prototype.validates = validates;
Rule.prototype.diff = diff;
module.exports = plugin({type: Rule, proto: Rule.prototype});