mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 04:25:01 +00:00
refactor: replace io-ts with zod
This commit is contained in:
parent
fafaefa1fb
commit
28692962bc
161 changed files with 1450 additions and 2105 deletions
|
@ -1,17 +0,0 @@
|
|||
import * as t from "io-ts";
|
||||
|
||||
interface BoundedStringBrand {
|
||||
readonly BoundedString: unique symbol;
|
||||
}
|
||||
|
||||
export function asBoundedString(str: string) {
|
||||
return str as t.Branded<string, BoundedStringBrand>;
|
||||
}
|
||||
|
||||
export function tBoundedString(min: number, max: number) {
|
||||
return t.brand(
|
||||
t.string,
|
||||
(str): str is t.Branded<string, BoundedStringBrand> => (str.length >= min && str.length <= max),
|
||||
"BoundedString",
|
||||
);
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
import { either } from "fp-ts/lib/Either";
|
||||
import * as t from "io-ts";
|
||||
import { intToRgb } from "./intToRgb";
|
||||
import { parseColor } from "./parseColor";
|
||||
import { rgbToInt } from "./rgbToInt";
|
||||
|
||||
export const tColor = new t.Type<number, string>(
|
||||
"tColor",
|
||||
(s): s is number => typeof s === "number",
|
||||
(from, to) =>
|
||||
either.chain(t.string.validate(from, to), (input) => {
|
||||
const parsedColor = parseColor(input);
|
||||
return parsedColor == null ? t.failure(from, to, "Invalid color") : t.success(rgbToInt(parsedColor));
|
||||
}),
|
||||
(s) => intToRgb(s).join(","),
|
||||
);
|
|
@ -1,13 +0,0 @@
|
|||
import { either } from "fp-ts/lib/Either";
|
||||
import * as t from "io-ts";
|
||||
import { isValidTimezone } from "./isValidTimezone";
|
||||
|
||||
export const tValidTimezone = new t.Type<string, string>(
|
||||
"tValidTimezone",
|
||||
(s): s is string => typeof s === "string",
|
||||
(from, to) =>
|
||||
either.chain(t.string.validate(from, to), (input) => {
|
||||
return isValidTimezone(input) ? t.success(input) : t.failure(from, to, `Invalid timezone: ${input}`);
|
||||
}),
|
||||
(s) => s,
|
||||
);
|
|
@ -14,3 +14,11 @@ export type Awaitable<T = unknown> = T | Promise<T>;
|
|||
export type DeepMutable<T> = {
|
||||
-readonly [P in keyof T]: DeepMutable<T[P]>;
|
||||
};
|
||||
|
||||
// From https://stackoverflow.com/a/70262876/316944
|
||||
export declare abstract class As<Tag extends keyof never> {
|
||||
private static readonly $as$: unique symbol;
|
||||
private [As.$as$]: Record<Tag, true>;
|
||||
}
|
||||
|
||||
export type Brand<T, B extends keyof never> = T & As<B>;
|
||||
|
|
15
backend/src/utils/zColor.ts
Normal file
15
backend/src/utils/zColor.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import z from "zod";
|
||||
import { parseColor } from "./parseColor";
|
||||
import { rgbToInt } from "./rgbToInt";
|
||||
|
||||
export const zColor = z.string().transform((val, ctx) => {
|
||||
const parsedColor = parseColor(val);
|
||||
if (parsedColor == null) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Invalid color",
|
||||
});
|
||||
return z.NEVER;
|
||||
}
|
||||
return rgbToInt(parsedColor);
|
||||
});
|
8
backend/src/utils/zValidTimezone.ts
Normal file
8
backend/src/utils/zValidTimezone.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { ZodString } from "zod";
|
||||
import { isValidTimezone } from "./isValidTimezone";
|
||||
|
||||
export function zValidTimezone<Z extends ZodString>(z: Z) {
|
||||
return z.refine((val) => isValidTimezone(val), {
|
||||
message: "Invalid timezone",
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue