zulip/web/src/schema.js
Anders Kaseorg c1675913a2 web: Move web app to ‘web’ directory.
Ever since we started bundling the app with webpack, there’s been less
and less overlap between our ‘static’ directory (files belonging to
the frontend app) and Django’s interpretation of the ‘static’
directory (files served directly to the web).

Split the app out to its own ‘web’ directory outside of ‘static’, and
remove all the custom collectstatic --ignore rules.  This makes it
much clearer what’s actually being served to the web, and what’s being
bundled by webpack.  It also shrinks the release tarball by 3%.

Signed-off-by: Anders Kaseorg <[email protected]>
2023-02-23 16:04:17 -08:00

53 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.
*/
export function check_string(var_name, val) {
if (typeof val !== "string") {
return var_name + " is not a string";
}
return undefined;
}
export function check_record(var_name, val, fields) {
if (typeof val !== "object") {
return var_name + " is not a record";
}
const field_results = Object.entries(fields).map(([field_name, f]) => {
if (val[field_name] === undefined) {
return field_name + " is missing";
}
return f(field_name, val[field_name]);
});
const msg = field_results.filter(Boolean).sort().join(", ");
if (msg) {
return "in " + var_name + " " + msg;
}
return undefined;
}
export function check_array(var_name, val, checker) {
if (!Array.isArray(val)) {
return var_name + " is not an array";
}
for (const item of val) {
const msg = checker("item", item);
if (msg) {
return "in " + var_name + " we found an item where " + msg;
}
}
return undefined;
}