|
| 1 | +import { z } from 'zod' |
| 2 | +import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types' |
| 3 | + |
| 4 | +type definedNonNullAny = {}; |
| 5 | + |
| 6 | +export const isDefinedNonNullAny = (v: any): v is definedNonNullAny => v !== undefined && v !== null; |
| 7 | + |
| 8 | +export const definedNonNullAnySchema: z.ZodSchema<definedNonNullAny> = z.any().refine((v) => isDefinedNonNullAny(v)); |
| 9 | + |
| 10 | +export function AttributeInputSchema(): z.ZodSchema<AttributeInput> { |
| 11 | + return z.object({ |
| 12 | + key: z.string().nullish(), |
| 13 | + val: z.string().nullish() |
| 14 | + }) |
| 15 | +} |
| 16 | + |
| 17 | +export const ButtonComponentTypeSchema = z.nativeEnum(ButtonComponentType); |
| 18 | + |
| 19 | +export function ComponentInputSchema(): z.ZodSchema<ComponentInput> { |
| 20 | + return z.object({ |
| 21 | + child: z.lazy(() => ComponentInputSchema().nullish()), |
| 22 | + childrens: z.array(z.lazy(() => ComponentInputSchema().nullable())).nullish(), |
| 23 | + event: z.lazy(() => EventInputSchema().nullish()), |
| 24 | + name: z.string(), |
| 25 | + type: ButtonComponentTypeSchema |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +export function DropDownComponentInputSchema(): z.ZodSchema<DropDownComponentInput> { |
| 30 | + return z.object({ |
| 31 | + dropdownComponent: z.lazy(() => ComponentInputSchema().nullish()), |
| 32 | + getEvent: z.lazy(() => EventInputSchema()) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +export function EventArgumentInputSchema(): z.ZodSchema<EventArgumentInput> { |
| 37 | + return z.object({ |
| 38 | + name: z.string().min(5), |
| 39 | + value: z.string().regex(/^foo/) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +export function EventInputSchema(): z.ZodSchema<EventInput> { |
| 44 | + return z.object({ |
| 45 | + arguments: z.array(z.lazy(() => EventArgumentInputSchema())), |
| 46 | + options: z.array(EventOptionTypeSchema).nullish() |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +export const EventOptionTypeSchema = z.nativeEnum(EventOptionType); |
| 51 | + |
| 52 | +export function HttpInputSchema(): z.ZodSchema<HttpInput> { |
| 53 | + return z.object({ |
| 54 | + method: HttpMethodSchema.nullish(), |
| 55 | + url: definedNonNullAnySchema |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +export const HttpMethodSchema = z.nativeEnum(HttpMethod); |
| 60 | + |
| 61 | +export function LayoutInputSchema(): z.ZodSchema<LayoutInput> { |
| 62 | + return z.object({ |
| 63 | + dropdown: z.lazy(() => DropDownComponentInputSchema().nullish()) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +export function PageInputSchema(): z.ZodSchema<PageInput> { |
| 68 | + return z.object({ |
| 69 | + attributes: z.array(z.lazy(() => AttributeInputSchema())).nullish(), |
| 70 | + date: definedNonNullAnySchema.nullish(), |
| 71 | + height: z.number(), |
| 72 | + id: z.string(), |
| 73 | + layout: z.lazy(() => LayoutInputSchema()), |
| 74 | + pageType: PageTypeSchema, |
| 75 | + postIDs: z.array(z.string()).nullish(), |
| 76 | + show: z.boolean(), |
| 77 | + tags: z.array(z.string().nullable()).nullish(), |
| 78 | + title: z.string(), |
| 79 | + width: z.number() |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +export const PageTypeSchema = z.nativeEnum(PageType); |
0 commit comments