Files
zulip/static/js/schema.js
Anders Kaseorg 28f3dfa284 js: Automatically convert var to let and const in most files.
This commit was originally automatically generated using `tools/lint
--only=eslint --fix`.  It was then modified by tabbott to contain only
changes to a set of files that are unlikely to result in significant
merge conflicts with any open pull request, excluding about 20 files.
His plan is to merge the remaining changes with more precise care,
potentially involving merging parts of conflicting pull requests
before running the `eslint --fix` operation.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-11-03 12:42:39 -08:00

57 lines
1.2 KiB
JavaScript

/*
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';
}
const field_results = _.map(fields, function (f, field_name) {
if (val[field_name] === undefined) {
return field_name + ' is missing';
}
return f(field_name, val[field_name]);
});
const msg = _.filter(field_results).sort().join(', ');
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';
}
let msg;
_.find(val, function (item) {
const res = checker('item', item);
if (res) {
msg = res;
return msg;
}
});
if (msg) {
return 'in ' + var_name + ' we found an item where ' + msg;
}
};
window.schema = exports;