|
| 1 | +import * as v from 'valibot' |
| 2 | +import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types' |
| 3 | + |
| 4 | +export const ButtonComponentTypeSchema = v.enum_(ButtonComponentType); |
| 5 | + |
| 6 | +export const EventOptionTypeSchema = v.enum_(EventOptionType); |
| 7 | + |
| 8 | +export const HttpMethodSchema = v.enum_(HttpMethod); |
| 9 | + |
| 10 | +export const PageTypeSchema = v.enum_(PageType); |
| 11 | + |
| 12 | +export function AttributeInputSchema() { |
| 13 | + return v.object({ |
| 14 | + key: v.nullish(v.string()), |
| 15 | + val: v.nullish(v.string()) |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +export function ComponentInputSchema(): v.GenericSchema<ComponentInput> { |
| 20 | + return v.object({ |
| 21 | + child: v.lazy(() => v.nullish(ComponentInputSchema())), |
| 22 | + childrens: v.nullish(v.array(v.lazy(() => v.nullable(ComponentInputSchema())))), |
| 23 | + event: v.lazy(() => v.nullish(EventInputSchema())), |
| 24 | + name: v.string(), |
| 25 | + type: ButtonComponentTypeSchema |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +export function DropDownComponentInputSchema() { |
| 30 | + return v.object({ |
| 31 | + dropdownComponent: v.lazy(() => v.nullish(ComponentInputSchema())), |
| 32 | + getEvent: v.lazy(() => EventInputSchema()) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +export function EventArgumentInputSchema(): v.GenericSchema<EventArgumentInput> { |
| 37 | + return v.object({ |
| 38 | + name: v.pipe(v.string(), v.minLength(5)), |
| 39 | + value: v.pipe(v.string(), v.regex(/^foo/, "message")) |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +type Infer1 = v.InferOutput<ReturnType<typeof EventArgumentInputSchema>>; |
| 44 | + |
| 45 | +const ESchema = v.object({ |
| 46 | + name: v.pipe(v.string(), v.minLength(5)), |
| 47 | + value: v.pipe(v.string(), v.regex(/^foo/, "message")) |
| 48 | +}); |
| 49 | + |
| 50 | +type Infer2 = v.InferOutput<typeof ESchema>; |
| 51 | + |
| 52 | +// const obj = v.object({ value: v.pipe(v.string(), v.minLength(5)) }) |
| 53 | +// // const obj = v.pipe(v.string(), v.minLength(5)); |
| 54 | +// type Infer1 = v.InferOutput<typeof obj>; |
| 55 | + |
| 56 | +// // const str2: v.ObjectSchema<Readonly<{ value: v.SchemaWithPipe<[v.StringSchema<undefined>, v.MinLengthAction<string, 5, undefined>]> }>, undefined> = v.object({ value: v.pipe(v.string(), v.minLength(5)) }) |
| 57 | +// // const str2: v.ObjectSchema<Readonly<{ value: Omit<v.GenericSchema<string>, '_types' | '_run'> }>, undefined> = v.object({ value: v.pipe(v.string(), v.minLength(5)) }) |
| 58 | +// // const str2: v.GenericSchema<string> = v.pipe(v.string(), v.minLength(5)); |
| 59 | +// const obj2: v.GenericSchema<{ value: string }> = v.object({ value: v.pipe(v.string(), v.minLength(5)) }) |
| 60 | +// type Infer2 = v.InferOutput<typeof obj2>; |
| 61 | + |
| 62 | +// 型が一致しているか確認するためのチェック |
| 63 | +export const typeCheck: Infer1 extends Infer2 |
| 64 | + ? Infer2 extends Infer1 |
| 65 | + ? true |
| 66 | + : false |
| 67 | + : false = true; |
| 68 | + |
| 69 | + |
| 70 | +export function EventInputSchema() { |
| 71 | + return v.object({ |
| 72 | + arguments: v.array(v.lazy(() => EventArgumentInputSchema())), |
| 73 | + options: v.nullish(v.array(EventOptionTypeSchema)) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +export function HttpInputSchema() { |
| 78 | + return v.object({ |
| 79 | + method: v.nullish(HttpMethodSchema), |
| 80 | + url: v.any() |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +export function LayoutInputSchema() { |
| 85 | + return v.object({ |
| 86 | + dropdown: v.lazy(() => v.nullish(DropDownComponentInputSchema())) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +export function PageInputSchema() { |
| 91 | + return v.object({ |
| 92 | + attributes: v.nullish(v.array(v.lazy(() => AttributeInputSchema()))), |
| 93 | + date: v.nullish(v.any()), |
| 94 | + height: v.number(), |
| 95 | + id: v.string(), |
| 96 | + layout: v.lazy(() => LayoutInputSchema()), |
| 97 | + pageType: PageTypeSchema, |
| 98 | + postIDs: v.nullish(v.array(v.string())), |
| 99 | + show: v.boolean(), |
| 100 | + tags: v.nullish(v.array(v.nullable(v.string()))), |
| 101 | + title: v.string(), |
| 102 | + width: v.number() |
| 103 | + }) |
| 104 | +} |
0 commit comments