Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zform validation: Migrate validation logic to zod.
xoldyckk8b3f8d77b0a6
diff --git a/web/src/schema.js b/web/src/schema.jsdeleted file mode 100644index 9d779b87e36..00000000000--- a/web/src/schema.js+++ /dev/null@@ -1,52 +0,0 @@-/*--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;-}diff --git a/web/src/zform.js b/web/src/zform.jsindex 35cdaa77a63..f1fee2acf1e 100644--- a/web/src/zform.js+++ b/web/src/zform.js@@ -1,59 +1,34 @@import $ from "jquery";+import {z} from "zod";import render_widgets_zform_choices from "../templates/widgets/zform_choices.hbs";import * as blueslip from "./blueslip";-import * as schema from "./schema";import * as transmit from "./transmit";-export function validate_extra_data(data) {- function check(data) {- function check_choice_data(data) {- function check_choice_item(field_name, val) {- return schema.check_record(field_name, val, {- short_name: schema.check_string,- long_name: schema.check_string,- reply: schema.check_string,- });- }-- function check_choices(field_name, val) {- return schema.check_array(field_name, val, check_choice_item);- }-- return schema.check_record("zform data", data, {- heading: schema.check_string,- choices: check_choices,- });- }-- if (data.type === "choices") {- return check_choice_data(data);- }-- return "unknown zform type: " + data.type;- }-- const msg = check(data);-- if (msg) {- blueslip.warn(msg);- return false;- }-- return true;-}+const zform_widget_extra_data_schema = z.object({+ choices: z.array(+ z.object({+ long_name: z.string(),+ reply: z.string(),+ short_name: z.string(),+ }),+ ),+ heading: z.string(),+ type: z.literal("choices"),+});export function activate(opts) {const self = {};const $outer_elem = opts.$elem;- const data = opts.extra_data;+ const parse_result = zform_widget_extra_data_schema.safeParse(opts.extra_data);- if (!validate_extra_data(data)) {- // callee will log reason we fail+ if (!parse_result.success) {+ blueslip.warn("invalid zform extra data", parse_result.error.issues);return undefined;}+ const {data} = parse_result;function make_choices(data) {// Assign idx values to each of our choices so thatdiff --git a/web/tests/schema.test.js b/web/tests/schema.test.jsdeleted file mode 100644index 439a044a531..00000000000--- a/web/tests/schema.test.js+++ /dev/null@@ -1,38 +0,0 @@-"use strict";--const {strict: assert} = require("assert");--const {zrequire} = require("./lib/namespace");-const {run_test} = require("./lib/test");--const schema = zrequire("schema");--run_test("basics", () => {- assert.equal(schema.check_string("x", "fred"), undefined);- assert.equal(schema.check_string("x", [1, 2]), "x is not a string");-- const fields = {- foo: schema.check_string,- bar: schema.check_string,- };-- const check_rec = (val) => schema.check_record("my_rec", val, fields);-- assert.equal(check_rec({foo: "apple", bar: "banana"}), undefined);-- assert.equal(check_rec("bogus"), "my_rec is not a record");-- assert.equal(check_rec({foo: "apple"}), "in my_rec bar is missing");-- assert.equal(check_rec({}), "in my_rec bar is missing, foo is missing");-- assert.equal(check_rec({foo: "apple", bar: 42}), "in my_rec bar is not a string");-- const check_array = (val) => schema.check_array("lst", val, schema.check_string);-- assert.equal(check_array(["foo", "bar"]), undefined);-- assert.equal(check_array("foo"), "lst is not an array");-- assert.equal(check_array(["foo", 3]), "in lst we found an item where item is not a string");-});