From 118b31bd2fcb30f819059551b8ca20e09b30fa00 Mon Sep 17 00:00:00 2001 From: Skye Date: Tue, 28 Feb 2023 21:23:09 +0900 Subject: [PATCH] Zodify --- src/lib/types-zod.ts | 51 ++++++++---------------- src/lib/types.ts | 95 ++++++++++++++------------------------------ 2 files changed, 47 insertions(+), 99 deletions(-) diff --git a/src/lib/types-zod.ts b/src/lib/types-zod.ts index fa4a354..f708894 100644 --- a/src/lib/types-zod.ts +++ b/src/lib/types-zod.ts @@ -1,26 +1,11 @@ import { z } from 'zod'; -import type { - FileTree, - File, - Folder, - Modpack, - Revision, - RevisionBase, - RevisionDiff, - RevisionTag, - FileInline, - FileBlob, - Diff, - DiffSet, - DiffDelete -} from './types'; -export const revisionTagSchema: z.ZodType = z.object({ - type: z.union([z.literal('alpha'), z.literal('beta'), z.literal('rc'), z.literal('stable')]), +export const revisionTagSchema = z.object({ + type: z.enum(['alpha', 'beta', 'rc', 'stable']), version: z.string() }); -export const fileInlineSchema: z.ZodType = z.object({ +export const fileInlineSchema = z.object({ type: z.literal('file'), inline: z.literal(true), content: z.string(), @@ -28,25 +13,26 @@ export const fileInlineSchema: z.ZodType = z.object({ sha512: z.string().regex(/^[a-fA-F0-9]{128}$/) }); -export const fileBlobSchema: z.ZodType = z.object({ +export const fileBlobSchema = z.object({ type: z.literal('file'), inline: z.literal(false), sha1: z.string().regex(/^[a-fA-F0-9]{40}$/), sha512: z.string().regex(/^[a-fA-F0-9]{128}$/) }); -export const fileSchema: z.ZodType = z.union([fileInlineSchema, fileBlobSchema]); +export const fileSchema = z.union([fileInlineSchema, fileBlobSchema]); -export const fileTreeSchema: z.ZodType = z.lazy(() => - z.union([folderSchema, fileSchema]) -); +export const fileTreeSchema = z.lazy(() => z.union([folderSchema, fileSchema])); -export const folderSchema: z.ZodType = z.object({ +export const folderSchema: z.ZodType<{ + type: 'folder'; + children: Record>; +}> = z.object({ type: z.literal('folder'), children: z.record(z.string().regex(/^[^/]+$/), fileTreeSchema) }); -export const diffSetSchema: z.ZodType = z.object({ +export const diffSetSchema = z.object({ path: z .string() .regex(/^[^/]+$/) @@ -55,7 +41,7 @@ export const diffSetSchema: z.ZodType = z.object({ value: fileTreeSchema }); -export const diffDeleteSchema: z.ZodType = z.object({ +export const diffDeleteSchema = z.object({ path: z .string() .regex(/^[^/]+$/) @@ -63,28 +49,25 @@ export const diffDeleteSchema: z.ZodType = z.object({ type: z.literal('delete') }); -export const diffSchema: z.ZodType = z.union([diffSetSchema, diffDeleteSchema]); +export const diffSchema = z.union([diffSetSchema, diffDeleteSchema]); -export const revisionDiffSchema: z.ZodType = z.object({ +export const revisionDiffSchema = z.object({ id: z.string().regex(/^[A-Za-z0-9_-]{21}$/), tag: revisionTagSchema.optional(), type: z.literal('diff'), tree: diffSchema.array() }); -export const revisionBaseSchema: z.ZodType = z.object({ +export const revisionBaseSchema = z.object({ id: z.string().regex(/^[A-Za-z0-9_-]{21}$/), tag: revisionTagSchema.optional(), type: z.literal('base'), tree: fileTreeSchema }); -export const revisionSchema: z.ZodType = z.union([ - revisionBaseSchema, - revisionDiffSchema -]); +export const revisionSchema = z.union([revisionBaseSchema, revisionDiffSchema]); -export const modpackSchema: z.ZodType = z.object({ +export const modpackSchema = z.object({ _id: z.string().regex(/^[A-Za-z0-9_-]{21}$/), type: z.literal('modpack'), name: z.string(), diff --git a/src/lib/types.ts b/src/lib/types.ts index e0446d5..827d082 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,65 +1,30 @@ -export interface Modpack { - _id: string; - type: 'modpack'; - name: string; - author: string; - revisions: Revision[]; -} - -export type Revision = RevisionBase | RevisionDiff; - -export interface RevisionBase { - id: string; - tag?: RevisionTag; - type: 'base'; - tree: FileTree; -} - -export interface RevisionDiff { - id: string; - tag?: RevisionTag; - type: 'diff'; - tree: Diff[]; -} - -export interface RevisionTag { - type: 'alpha' | 'beta' | 'rc' | 'stable'; - version: string; -} - -export type FileTree = Folder | File; - -export interface Folder { - type: 'folder'; - children: Record; -} - -export type File = FileInline | FileBlob; - -export interface FileInline { - type: 'file'; - inline: true; - content: string; - sha1: string; - sha512: string; -} - -export interface FileBlob { - type: 'file'; - inline: false; - sha1: string; - sha512: string; -} - -export type Diff = DiffSet | DiffDelete; - -export interface DiffSet { - path: string[]; - type: 'set'; - value: FileTree; -} - -export interface DiffDelete { - path: string[]; - type: 'delete'; -} +import type { z } from 'zod'; +import type { + diffDeleteSchema, + diffSchema, + diffSetSchema, + fileBlobSchema, + fileInlineSchema, + fileSchema, + fileTreeSchema, + folderSchema, + modpackSchema, + revisionBaseSchema, + revisionDiffSchema, + revisionSchema, + revisionTagSchema +} from './types-zod'; + +export type Modpack = z.infer; +export type Revision = z.infer; +export type RevisionBase = z.infer; +export type RevisionDiff = z.infer; +export type RevisionTag = z.infer; +export type FileTree = z.infer; +export type Folder = z.infer; +export type File = z.infer; +export type FileInline = z.infer; +export type FileBlob = z.infer; +export type Diff = z.infer; +export type DiffSet = z.infer; +export type DiffDelete = z.infer;