master
Skye 2 years ago
parent 0621237195
commit 118b31bd2f
Signed by: me
GPG Key ID: 0104BC05F41B77B8

@ -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(),

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

Loading…
Cancel
Save