ditto/src/schema.ts

58 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-08-12 16:28:16 +00:00
import { z } from '@/deps.ts';
2023-03-05 04:10:56 +00:00
2023-05-12 04:49:32 +00:00
/** Validates individual items in an array, dropping any that aren't valid. */
function filteredArray<T extends z.ZodTypeAny>(schema: T) {
return z.any().array().catch([])
.transform((arr) => (
arr.map((item) => {
const parsed = schema.safeParse(item);
return parsed.success ? parsed.data : undefined;
}).filter((item): item is z.infer<T> => Boolean(item))
));
}
const jsonSchema = z.string().transform((value, ctx) => {
try {
2023-07-08 23:41:11 +00:00
return JSON.parse(value) as unknown;
} catch (_e) {
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Invalid JSON' });
return z.NEVER;
}
});
2023-04-11 00:34:00 +00:00
/** Alias for `safeParse`, but instead of returning a success object it returns the value (or undefined on fail). */
function parseValue<T>(schema: z.ZodType<T>, value: unknown): T | undefined {
2023-04-08 02:37:16 +00:00
const result = schema.safeParse(value);
return result.success ? result.data : undefined;
}
2023-04-11 00:34:00 +00:00
const parseRelay = (relay: string | URL) => parseValue(relaySchema, relay);
2023-04-08 02:37:16 +00:00
const relaySchema = z.custom<URL>((relay) => {
if (typeof relay !== 'string') return false;
try {
const { protocol } = new URL(relay);
return protocol === 'wss:' || protocol === 'ws:';
} catch (_e) {
return false;
}
});
2023-05-12 04:49:32 +00:00
const emojiTagSchema = z.tuple([z.literal('emoji'), z.string(), z.string().url()]);
2023-07-08 23:41:11 +00:00
/** https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem */
const decode64Schema = z.string().transform((value, ctx) => {
try {
const binString = atob(value);
const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)!);
return new TextDecoder().decode(bytes);
} catch (_e) {
2023-07-09 02:01:49 +00:00
ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Invalid base64', fatal: true });
2023-07-08 23:41:11 +00:00
return z.NEVER;
}
});
const hashtagSchema = z.string().regex(/^\w{1,30}$/);
export { decode64Schema, emojiTagSchema, filteredArray, hashtagSchema, jsonSchema, parseRelay, relaySchema };