This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
/
validation.js
262 lines (251 loc) · 9.19 KB
/
validation.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
var _ = require('underscore'),
async = require('async'),
moment = require('moment'),
pupil = require('./pupil/src/pupil'),
messages = require('./messages'),
utils = require('./utils'),
logger = require('./logger'),
db = require('../db');
var _parseDuration = function(duration) {
var parts = duration.split(' ');
return moment.duration(parseInt(parts[0]), parts[1]);
};
var _exists = function(doc, query, callback) {
db.fti(
'data_records',
{ q: query, include_docs: true },
function(err, result) {
if (err) {
return callback(err);
}
var found = _.some(result.rows, function(row) {
return row.id !== doc._id &&
row.doc &&
row.doc.errors &&
row.doc.errors.length === 0;
});
return callback(null, found);
}
);
};
var _formatParam = function(name, value) {
name = name.replace(/"/g, '');
if (typeof value === 'string') {
value = value.replace(/"/g, '\\"');
return name + ':"' + value + '"';
}
if (typeof value === 'number') {
return name + '<int>:' + value;
}
return name + ':' + value;
};
module.exports = {
_formatParam: _formatParam,
extractErrors: function(result, messages, ignores) {
// wrap single item in array; defaults to empty array
ignores = ignores || [];
if (!_.isArray(ignores)) {
ignores = [ ignores ];
}
return _.reduce(result, function(memo, valid, key) {
if (!valid && !_.contains(ignores, key)) {
memo.push({
code: 'invalid_' + key,
message: messages[key]
});
}
return memo;
}, []);
},
getMessages: function(validations, locale) {
return _.reduce(validations, function(memo, validation) {
if (validation.property &&
(validation.message || validation.translation_key)) {
memo[validation.property] = messages.getMessage(validation, locale);
}
return memo;
}, {});
},
getRules: function(validations) {
return _.reduce(validations, function(memo, validation) {
if (validation.property && validation.rule) {
memo[validation.property] = validation.rule;
}
return memo;
}, {});
},
/*
* Custom validations in addition to pupil but follows Pupil API
* */
extra_validations: {
/*
* Check if fields on a doc are unique in the db, return true if unique
* false otherwise.
*/
unique: function(doc, validation, callback) {
var conjunctions = _.map(validation.funcArgs, function(field) {
return _formatParam(field, doc[field]);
});
_exists(doc, conjunctions.join(' AND '), function(err, result) {
callback(err, !result);
});
},
uniqueWithin: function(doc, validation, callback) {
var fields = _.clone(validation.funcArgs);
var duration = _parseDuration(fields.pop());
var conjunctions = _.map(fields, function(field) {
return _formatParam(field, doc[field]);
});
// lucene date range query bug
// fails: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
// works: "yyyy-MM-dd'T'HH:mm:ss.SSS"
var start = moment().subtract(duration).toISOString().replace(/Z$/,'');
var endOfTime = '3000-01-01T00:00:00';
conjunctions.push(
'reported_date<date>:[' + start + ' TO ' + endOfTime + ']'
);
_exists(doc, conjunctions.join(' AND '), function(err, result) {
callback(err, !result);
});
},
exists: function(doc, validation, callback) {
var formName = validation.funcArgs[0];
var fieldName = validation.funcArgs[1];
var fieldValue = doc[validation.field];
var conjunctions = [
_formatParam('form', formName),
_formatParam(fieldName, fieldValue)
];
_exists(doc, conjunctions.join(' AND '), callback);
}
},
/**
* Validation setings may consist of Pupil.js rules and custom rules.
* These cannot be combined as part of the same rule.
*
* Not OK:
* rule: "regex(\d{5}) && unique('patient_id')"
*
* OK:
* rule: "regex(\d{5}) && max(11111)"
*
* If for example you want to validate that patient_id is 5 numbers and it
* is unique (or some other custom validation) you need to define two
* validation configs/separate rules in your settings. Example validation
* settings:
*
* [
* {
* property: "patient_id",
* rule: "regex(\d{5})",
* message: [{
* content: "Patient ID must be 5 numbers.",
* locale: "en"
* }]
* },
* {
* property: "patient_id",
* rule: "unique('patient_id')",
* message: [{
* content: "Patient ID must be unique.",
* locale: "en"
* }]
* }
* ]
*
* validate() modifies the property value of the second item to
* `patient_id_unique` so that pupil.validate() still returns a valid
* result. Then we process the result once more to extract the custom
* validation results and error messages.
*
* @param ignores -
* Optional array keys of doc that is always considered valid
*
* @callback Array of errors if validation failed, empty array otherwise.
* */
validate: function(doc, validations, ignores, callback) {
var self = module.exports,
result = {},
errors = [];
callback = callback || ignores;
validations = validations || [];
/*
* Modify validation objects that are calling a custom validation
* function. Add function name and args and append the function name to
* the property value so pupil.validate() will still work and error
* messages can be generated.
*
**/
var names = Object.keys(self.extra_validations);
_.each(validations, function(config, idx) {
var entities;
try {
logger.debug('validation rule %s', config.rule);
entities = pupil.parser.parse(pupil.lexer.tokenize(config.rule));
} catch(e) {
logger.error('error parsing validation: %s', e);
return errors.push('Error on pupil validations: ' + JSON.stringify(e));
}
_.each(entities, function(entity) {
logger.debug('validation rule entity %s', entity);
if (entity.sub && entity.sub.length > 0) {
_.each(entity.sub, function(e) {
logger.debug('validation rule entity sub %s', e.funcName);
if (names.indexOf(e.funcName) >= 0) {
var v = validations[idx];
// only update the first time through
if (v.property.indexOf('_' + e.funcName) === -1) {
v.funcName = e.funcName;
v.funcArgs = e.funcArgs;
v.field = config.property;
v.property += '_' + e.funcName;
}
}
});
}
});
});
// trouble parsing pupil rules
if (errors.length > 0) {
return callback(errors);
}
var attributes = _.extend({}, doc, doc.fields);
try {
result = pupil.validate(self.getRules(validations), attributes);
} catch(e) {
errors.push('Error on pupil validations: ' + JSON.stringify(e));
return callback(errors);
}
/*
* Run async/extra validations in series and collect results.
*/
async.eachSeries(validations, function(v, cb) {
if (!v.funcName) {
return cb(); // continue series
}
self.extra_validations[v.funcName].call(this, attributes, v,
function(err, res) {
/*
* Be careful to not to make an invalid pupil result valid,
* only assign false values. If async result is true then do
* nothing since default is already true. Fields are valid
* unless proven otherwise.
*/
if (res === false) {
result.results[v.property] = res;
}
cb(err); // continue series
}
);
}, function() {
errors = errors.concat(
self.extractErrors(
result.fields(),
self.getMessages(validations, utils.getLocale(doc)),
ignores
)
);
callback(errors);
});
}
};