-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (140 loc) · 3.54 KB
/
index.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Module dependencies.
*/
var bind = require('bind'),
minstache = require('minstache'),
toFunction = require('to-function'),
SchemaValidate = require('schema-validate'),
// symbol imports
compile = minstache.compile;
/**
* Module exports.
*/
module.exports = FormValidate;
/**
* Initialize new form-validate.
*
* @param {Array} msgs the error messages
*/
function FormValidate(msgs) {
if (!(this instanceof FormValidate)) return new FormValidate(msgs);
var self = this;
this.msgs = msgs || {};
return function(Model){
// register schema validator
Model.use(new SchemaValidate());
// register own validation function
Model.validate(bind(self, self.validate, Model));
};
}
/**
* Performs the rewriting of the errors.
*
* @param {Function} Model the model class
* @param {Model} model the model instance which was validated
*/
FormValidate.prototype.validate = function(Model, model){
var msgs = this.msgs,
cache = {};
// map the errors to messages specified by the user
model.errors.forEach(function(error){
var attrFunc,
err = error.message,
attribute = err.attribute,
message;
// first check the cache if we already compiled a message for the
// attribute
if ((message = cache[attribute]) == null) {
// compile message if cache miss
message
= cache[attribute]
= new Message(msgs[attribute] || err.message);
}
// render the message
error.message = message.render(Model, model, error);
error.attr += err.uri
.split('#')[1]
.replace(/\//g, '.');
});
};
/**
* Initialize new message.
*/
function Message(msg) {
if (typeof msg === 'string') this.msg = msg;
else this.fn = msg;
}
/**
* Render the wrapped message;
*/
Message.prototype.render = function(Model, model, error){
var fn,
ctx;
ctx = this.buildContext(Model, model, error);
// generate function to access the attributes value
if (typeof this.compiled === 'function') {
// use compiled minstache template if we have one
return this.renderCompiled(ctx);
} else if (typeof this.msg === 'string') {
// we have a minstache template
return this.renderMinstache(ctx);
} else {
return this.renderFunction(ctx);
}
};
/**
* Render an already compiled minstache template.
*
* @api private
*/
Message.prototype.renderCompiled = function(ctx){
return this.compiled(ctx);
};
/**
* Render a bare minstache template.
*
* @api private
*/
Message.prototype.renderMinstache = function(ctx){
var compiled;
compiled = this.compiled = compile(this.msg, ctx);
return compiled(ctx);
};
/**
* Render a function.
*
* @api private
*/
Message.prototype.renderFunction = function(ctx){
return this.fn(ctx.property, ctx.propertyValue, ctx.attributeValue);
};
/**
* Build the context used for rendering.
*
* @api private
*/
Message.prototype.buildContext = function(Model, model, error){
var attrFunc,
valueFunc,
err = error.message,
ctx,
expr,
valueExpr;
expr = error.attr
+ err.schemaUri.split('#')[1].replace(/\//g, '.')
+ '.'
+ err.attribute;
valueExpr = error.attr
+ err.uri.split('#')[1].replace(/\//g, '.');
// escape array indices
expr = expr.replace(/\.([0-9])(\.?)/g, '[$1]$2');
valueExpr = valueExpr.replace(/\.([0-9])(\.?)/g, '[$1]$2');
attrFunc = toFunction(expr);
valueFunc = toFunction(valueExpr);
ctx = {
attributeValue: attrFunc(Model.attrs),
property: error.attr,
propertyValue: valueFunc(model.attrs)
};
return ctx;
};