2018-05-22 21:50:24 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
These runtime schema validators are defensive and
|
|
|
|
should always succeed, so we don't necessarily want
|
|
|
|
to translate these. These are very similar to server
|
|
|
|
side validators in zerver/lib/validator.py.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.check_string = function (var_name, val) {
|
|
|
|
if (!_.isString(val)) {
|
|
|
|
return var_name + ' is not a string';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.check_record = function (var_name, val, fields) {
|
|
|
|
if (!_.isObject(val)) {
|
|
|
|
return var_name + ' is not a record';
|
|
|
|
}
|
|
|
|
|
2020-02-08 01:38:48 +01:00
|
|
|
const field_results = Object.entries(fields).map(([field_name, f]) => {
|
2018-05-22 21:50:24 +02:00
|
|
|
if (val[field_name] === undefined) {
|
|
|
|
return field_name + ' is missing';
|
|
|
|
}
|
|
|
|
return f(field_name, val[field_name]);
|
|
|
|
});
|
|
|
|
|
2020-02-08 03:33:46 +01:00
|
|
|
const msg = field_results.filter(Boolean).sort().join(', ');
|
2018-05-22 21:50:24 +02:00
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
return 'in ' + var_name + ' ' + msg;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.check_array = function (var_name, val, checker) {
|
|
|
|
if (!_.isArray(val)) {
|
|
|
|
return var_name + ' is not an array';
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:29:01 +01:00
|
|
|
for (const item of val) {
|
|
|
|
const msg = checker('item', item);
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-02-08 05:29:01 +01:00
|
|
|
if (msg) {
|
|
|
|
return 'in ' + var_name + ' we found an item where ' + msg;
|
2018-05-22 21:50:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.schema = exports;
|