mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-10-23 03:31:56 +00:00
request: Use zod for type-safe validation.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
@@ -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[],
|
||||
|
@@ -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
5
package-lock.json
generated
@@ -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",
|
||||
|
@@ -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",
|
||||
|
Reference in New Issue
Block a user