This commit is contained in:
Skye 2023-02-28 21:23:09 +09:00
parent 0621237195
commit 118b31bd2f
Signed by: me
GPG key ID: 0104BC05F41B77B8
2 changed files with 46 additions and 98 deletions

View file

@ -1,26 +1,11 @@
import { z } from 'zod'; 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<RevisionTag> = z.object({ export const revisionTagSchema = z.object({
type: z.union([z.literal('alpha'), z.literal('beta'), z.literal('rc'), z.literal('stable')]), type: z.enum(['alpha', 'beta', 'rc', 'stable']),
version: z.string() version: z.string()
}); });
export const fileInlineSchema: z.ZodType<FileInline> = z.object({ export const fileInlineSchema = z.object({
type: z.literal('file'), type: z.literal('file'),
inline: z.literal(true), inline: z.literal(true),
content: z.string(), content: z.string(),
@ -28,25 +13,26 @@ export const fileInlineSchema: z.ZodType<FileInline> = z.object({
sha512: z.string().regex(/^[a-fA-F0-9]{128}$/) sha512: z.string().regex(/^[a-fA-F0-9]{128}$/)
}); });
export const fileBlobSchema: z.ZodType<FileBlob> = z.object({ export const fileBlobSchema = z.object({
type: z.literal('file'), type: z.literal('file'),
inline: z.literal(false), inline: z.literal(false),
sha1: z.string().regex(/^[a-fA-F0-9]{40}$/), sha1: z.string().regex(/^[a-fA-F0-9]{40}$/),
sha512: z.string().regex(/^[a-fA-F0-9]{128}$/) sha512: z.string().regex(/^[a-fA-F0-9]{128}$/)
}); });
export const fileSchema: z.ZodType<File> = z.union([fileInlineSchema, fileBlobSchema]); export const fileSchema = z.union([fileInlineSchema, fileBlobSchema]);
export const fileTreeSchema: z.ZodType<FileTree> = z.lazy(() => export const fileTreeSchema = z.lazy(() => z.union([folderSchema, fileSchema]));
z.union([folderSchema, fileSchema])
);
export const folderSchema: z.ZodType<Folder> = z.object({ export const folderSchema: z.ZodType<{
type: 'folder';
children: Record<string, z.infer<typeof fileTreeSchema>>;
}> = z.object({
type: z.literal('folder'), type: z.literal('folder'),
children: z.record(z.string().regex(/^[^/]+$/), fileTreeSchema) children: z.record(z.string().regex(/^[^/]+$/), fileTreeSchema)
}); });
export const diffSetSchema: z.ZodType<DiffSet> = z.object({ export const diffSetSchema = z.object({
path: z path: z
.string() .string()
.regex(/^[^/]+$/) .regex(/^[^/]+$/)
@ -55,7 +41,7 @@ export const diffSetSchema: z.ZodType<DiffSet> = z.object({
value: fileTreeSchema value: fileTreeSchema
}); });
export const diffDeleteSchema: z.ZodType<DiffDelete> = z.object({ export const diffDeleteSchema = z.object({
path: z path: z
.string() .string()
.regex(/^[^/]+$/) .regex(/^[^/]+$/)
@ -63,28 +49,25 @@ export const diffDeleteSchema: z.ZodType<DiffDelete> = z.object({
type: z.literal('delete') type: z.literal('delete')
}); });
export const diffSchema: z.ZodType<Diff> = z.union([diffSetSchema, diffDeleteSchema]); export const diffSchema = z.union([diffSetSchema, diffDeleteSchema]);
export const revisionDiffSchema: z.ZodType<RevisionDiff> = z.object({ export const revisionDiffSchema = z.object({
id: z.string().regex(/^[A-Za-z0-9_-]{21}$/), id: z.string().regex(/^[A-Za-z0-9_-]{21}$/),
tag: revisionTagSchema.optional(), tag: revisionTagSchema.optional(),
type: z.literal('diff'), type: z.literal('diff'),
tree: diffSchema.array() tree: diffSchema.array()
}); });
export const revisionBaseSchema: z.ZodType<RevisionBase> = z.object({ export const revisionBaseSchema = z.object({
id: z.string().regex(/^[A-Za-z0-9_-]{21}$/), id: z.string().regex(/^[A-Za-z0-9_-]{21}$/),
tag: revisionTagSchema.optional(), tag: revisionTagSchema.optional(),
type: z.literal('base'), type: z.literal('base'),
tree: fileTreeSchema tree: fileTreeSchema
}); });
export const revisionSchema: z.ZodType<Revision> = z.union([ export const revisionSchema = z.union([revisionBaseSchema, revisionDiffSchema]);
revisionBaseSchema,
revisionDiffSchema
]);
export const modpackSchema: z.ZodType<Modpack> = z.object({ export const modpackSchema = z.object({
_id: z.string().regex(/^[A-Za-z0-9_-]{21}$/), _id: z.string().regex(/^[A-Za-z0-9_-]{21}$/),
type: z.literal('modpack'), type: z.literal('modpack'),
name: z.string(), name: z.string(),

View file

@ -1,65 +1,30 @@
export interface Modpack { import type { z } from 'zod';
_id: string; import type {
type: 'modpack'; diffDeleteSchema,
name: string; diffSchema,
author: string; diffSetSchema,
revisions: Revision[]; fileBlobSchema,
} fileInlineSchema,
fileSchema,
fileTreeSchema,
folderSchema,
modpackSchema,
revisionBaseSchema,
revisionDiffSchema,
revisionSchema,
revisionTagSchema
} from './types-zod';
export type Revision = RevisionBase | RevisionDiff; export type Modpack = z.infer<typeof modpackSchema>;
export type Revision = z.infer<typeof revisionSchema>;
export interface RevisionBase { export type RevisionBase = z.infer<typeof revisionBaseSchema>;
id: string; export type RevisionDiff = z.infer<typeof revisionDiffSchema>;
tag?: RevisionTag; export type RevisionTag = z.infer<typeof revisionTagSchema>;
type: 'base'; export type FileTree = z.infer<typeof fileTreeSchema>;
tree: FileTree; export type Folder = z.infer<typeof folderSchema>;
} export type File = z.infer<typeof fileSchema>;
export type FileInline = z.infer<typeof fileInlineSchema>;
export interface RevisionDiff { export type FileBlob = z.infer<typeof fileBlobSchema>;
id: string; export type Diff = z.infer<typeof diffSchema>;
tag?: RevisionTag; export type DiffSet = z.infer<typeof diffSetSchema>;
type: 'diff'; export type DiffDelete = z.infer<typeof diffDeleteSchema>;
tree: Diff[];
}
export interface RevisionTag {
type: 'alpha' | 'beta' | 'rc' | 'stable';
version: string;
}
export type FileTree = Folder | File;
export interface Folder {
type: 'folder';
children: Record<string, FileTree>;
}
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';
}