Zodify
This commit is contained in:
parent
0621237195
commit
118b31bd2f
2 changed files with 46 additions and 98 deletions
|
@ -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<RevisionTag> = 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<FileInline> = 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<FileInline> = z.object({
|
|||
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'),
|
||||
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<File> = z.union([fileInlineSchema, fileBlobSchema]);
|
||||
export const fileSchema = z.union([fileInlineSchema, fileBlobSchema]);
|
||||
|
||||
export const fileTreeSchema: z.ZodType<FileTree> = z.lazy(() =>
|
||||
z.union([folderSchema, fileSchema])
|
||||
);
|
||||
export const fileTreeSchema = z.lazy(() => 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'),
|
||||
children: z.record(z.string().regex(/^[^/]+$/), fileTreeSchema)
|
||||
});
|
||||
|
||||
export const diffSetSchema: z.ZodType<DiffSet> = z.object({
|
||||
export const diffSetSchema = z.object({
|
||||
path: z
|
||||
.string()
|
||||
.regex(/^[^/]+$/)
|
||||
|
@ -55,7 +41,7 @@ export const diffSetSchema: z.ZodType<DiffSet> = z.object({
|
|||
value: fileTreeSchema
|
||||
});
|
||||
|
||||
export const diffDeleteSchema: z.ZodType<DiffDelete> = z.object({
|
||||
export const diffDeleteSchema = z.object({
|
||||
path: z
|
||||
.string()
|
||||
.regex(/^[^/]+$/)
|
||||
|
@ -63,28 +49,25 @@ export const diffDeleteSchema: z.ZodType<DiffDelete> = z.object({
|
|||
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}$/),
|
||||
tag: revisionTagSchema.optional(),
|
||||
type: z.literal('diff'),
|
||||
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}$/),
|
||||
tag: revisionTagSchema.optional(),
|
||||
type: z.literal('base'),
|
||||
tree: fileTreeSchema
|
||||
});
|
||||
|
||||
export const revisionSchema: z.ZodType<Revision> = z.union([
|
||||
revisionBaseSchema,
|
||||
revisionDiffSchema
|
||||
]);
|
||||
export const revisionSchema = z.union([revisionBaseSchema, revisionDiffSchema]);
|
||||
|
||||
export const modpackSchema: z.ZodType<Modpack> = z.object({
|
||||
export const modpackSchema = z.object({
|
||||
_id: z.string().regex(/^[A-Za-z0-9_-]{21}$/),
|
||||
type: z.literal('modpack'),
|
||||
name: z.string(),
|
||||
|
|
|
@ -1,65 +1,30 @@
|
|||
export interface Modpack {
|
||||
_id: string;
|
||||
type: 'modpack';
|
||||
name: string;
|
||||
author: string;
|
||||
revisions: Revision[];
|
||||
}
|
||||
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 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<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';
|
||||
}
|
||||
export type Modpack = z.infer<typeof modpackSchema>;
|
||||
export type Revision = z.infer<typeof revisionSchema>;
|
||||
export type RevisionBase = z.infer<typeof revisionBaseSchema>;
|
||||
export type RevisionDiff = z.infer<typeof revisionDiffSchema>;
|
||||
export type RevisionTag = z.infer<typeof revisionTagSchema>;
|
||||
export type FileTree = z.infer<typeof fileTreeSchema>;
|
||||
export type Folder = z.infer<typeof folderSchema>;
|
||||
export type File = z.infer<typeof fileSchema>;
|
||||
export type FileInline = z.infer<typeof fileInlineSchema>;
|
||||
export type FileBlob = z.infer<typeof fileBlobSchema>;
|
||||
export type Diff = z.infer<typeof diffSchema>;
|
||||
export type DiffSet = z.infer<typeof diffSetSchema>;
|
||||
export type DiffDelete = z.infer<typeof diffDeleteSchema>;
|
||||
|
|
Loading…
Reference in a new issue