request: Use zod for type-safe validation.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-07-07 22:01:48 -07:00
parent cb7d1faa52
commit 0bc49bf723
4 changed files with 16 additions and 16 deletions

View File

@@ -13,11 +13,6 @@ export function invalidZulipServerError(domain: string): string {
https://zulip.readthedocs.io/en/stable/production/ssl-certificates.html`;
}
export function noOrgsError(domain: string): string {
return `${domain} does not have any organizations added.
Please contact your server administrator.`;
}
export function enterpriseOrgError(
length: number,
domains: string[],

View File

@@ -6,6 +6,7 @@ import stream from "stream";
import util from "util";
import getStream from "get-stream";
import * as z from "zod";
import Logger from "../common/logger-util";
import * as Messages from "../common/messages";
@@ -67,16 +68,14 @@ export const _getServerSettings = async (
throw new Error(Messages.invalidZulipServerError(domain));
}
const {realm_name, realm_uri, realm_icon} = JSON.parse(
await getStream(response),
);
if (
typeof realm_name !== "string" ||
typeof realm_uri !== "string" ||
typeof realm_icon !== "string"
) {
throw new TypeError(Messages.noOrgsError(domain));
}
const data: unknown = JSON.parse(await getStream(response));
const {realm_name, realm_uri, realm_icon} = z
.object({
realm_name: z.string(),
realm_uri: z.string(),
realm_icon: z.string(),
})
.parse(data);
return {
// Some Zulip Servers use absolute URL for server icon whereas others use relative URL

5
package-lock.json generated
View File

@@ -10671,6 +10671,11 @@
}
}
},
"zod": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.5.1.tgz",
"integrity": "sha512-Gg9GTai0iDHowuYM9VNhdFMmesgt44ufzqaE5CPHshpuK5fCzbibdqCnrWuYH6ZmOn/N+BlGmwZtVSijhKmhKw=="
},
"zwitch": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zwitch/-/zwitch-1.0.5.tgz",

View File

@@ -149,7 +149,8 @@
"i18n": "^0.13.2",
"iso-639-1": "^2.1.9",
"node-json-db": "^1.3.0",
"semver": "^7.3.5"
"semver": "^7.3.5",
"zod": "^3.5.1"
},
"devDependencies": {
"@types/adm-zip": "^0.4.34",