|
1 | 1 | import * as yup from 'yup' |
2 | | -import { PageType, HttpMethod, HttpInput, EventOptionType, EventArgumentInput, EventInput, ComponentInput, DropDownComponentInput, LayoutInput, ButtonComponentType, AttributeInput, PageInput, Guest, Admin, UserKind, User } from '../types' |
| 2 | +import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, PageInput, PageType, User, UserKind } from '../types' |
3 | 3 |
|
4 | | -export const PageTypeSchema = yup.string<PageType>().oneOf([PageType.BasicAuth, PageType.Lp, PageType.Restricted, PageType.Service]).defined(); |
5 | | - |
6 | | -export const HttpMethodSchema = yup.string<HttpMethod>().oneOf([HttpMethod.Get, HttpMethod.Post]).defined(); |
| 4 | +export const ButtonComponentTypeSchema = yup.string<ButtonComponentType>().oneOf([ButtonComponentType.Button, ButtonComponentType.Submit]).defined(); |
7 | 5 |
|
8 | 6 | export const EventOptionTypeSchema = yup.string<EventOptionType>().oneOf([EventOptionType.Reload, EventOptionType.Retry]).defined(); |
9 | 7 |
|
10 | | -export const ButtonComponentTypeSchema = yup.string<ButtonComponentType>().oneOf([ButtonComponentType.Button, ButtonComponentType.Submit]).defined(); |
| 8 | +export const HttpMethodSchema = yup.string<HttpMethod>().oneOf([HttpMethod.Get, HttpMethod.Post]).defined(); |
| 9 | + |
| 10 | +export const PageTypeSchema = yup.string<PageType>().oneOf([PageType.BasicAuth, PageType.Lp, PageType.Restricted, PageType.Service]).defined(); |
11 | 11 |
|
12 | 12 | function union<T extends {}>(...schemas: ReadonlyArray<yup.Schema<T>>): yup.MixedSchema<T> { |
13 | 13 | return yup.mixed<T>().test({ |
14 | 14 | test: (value) => schemas.some((schema) => schema.isValidSync(value)) |
15 | 15 | }).defined() |
16 | 16 | } |
17 | 17 |
|
18 | | -export const HttpInputSchema: yup.ObjectSchema<HttpInput> = yup.object({ |
19 | | - method: HttpMethodSchema.nullable().optional(), |
20 | | - url: yup.mixed().nonNullable() |
21 | | -}); |
| 18 | +export function AdminSchema(): yup.ObjectSchema<Admin> { |
| 19 | + return yup.object({ |
| 20 | + __typename: yup.string<'Admin'>().optional(), |
| 21 | + lastModifiedAt: yup.mixed().nullable().optional() |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +export function AttributeInputSchema(): yup.ObjectSchema<AttributeInput> { |
| 26 | + return yup.object({ |
| 27 | + key: yup.string().defined().nullable().optional(), |
| 28 | + val: yup.string().defined().nullable().optional() |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +export function ComponentInputSchema(): yup.ObjectSchema<ComponentInput> { |
| 33 | + return yup.object({ |
| 34 | + child: yup.lazy(() => ComponentInputSchema()).optional(), |
| 35 | + childrens: yup.array(yup.lazy(() => ComponentInputSchema())).defined().nullable().optional(), |
| 36 | + event: yup.lazy(() => EventInputSchema()).optional(), |
| 37 | + name: yup.string().defined().nonNullable(), |
| 38 | + type: ButtonComponentTypeSchema.nonNullable() |
| 39 | + }) |
| 40 | +} |
22 | 41 |
|
23 | | -export const EventArgumentInputSchema: yup.ObjectSchema<EventArgumentInput> = yup.object({ |
| 42 | +export function DropDownComponentInputSchema(): yup.ObjectSchema<DropDownComponentInput> { |
| 43 | + return yup.object({ |
| 44 | + dropdownComponent: yup.lazy(() => ComponentInputSchema()).optional(), |
| 45 | + getEvent: yup.lazy(() => EventInputSchema().nonNullable()) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +export function EventArgumentInputSchema(): yup.ObjectSchema<EventArgumentInput> { |
| 50 | + return yup.object({ |
24 | 51 | name: yup.string().defined().nonNullable().min(5), |
25 | 52 | value: yup.string().defined().nonNullable().matches(/^foo/) |
26 | | -}); |
| 53 | + }) |
| 54 | +} |
27 | 55 |
|
28 | | -export const EventInputSchema: yup.ObjectSchema<EventInput> = yup.object({ |
29 | | - arguments: yup.array(yup.lazy(() => EventArgumentInputSchema.nonNullable())).defined(), |
| 56 | +export function EventInputSchema(): yup.ObjectSchema<EventInput> { |
| 57 | + return yup.object({ |
| 58 | + arguments: yup.array(yup.lazy(() => EventArgumentInputSchema().nonNullable())).defined(), |
30 | 59 | options: yup.array(EventOptionTypeSchema.nonNullable()).defined().nullable().optional() |
31 | | -}); |
32 | | - |
33 | | -export const ComponentInputSchema: yup.ObjectSchema<ComponentInput> = yup.object({ |
34 | | - child: yup.lazy(() => ComponentInputSchema).optional(), |
35 | | - childrens: yup.array(yup.lazy(() => ComponentInputSchema)).defined().nullable().optional(), |
36 | | - event: yup.lazy(() => EventInputSchema).optional(), |
37 | | - name: yup.string().defined().nonNullable(), |
38 | | - type: ButtonComponentTypeSchema.nonNullable() |
39 | | -}); |
| 60 | + }) |
| 61 | +} |
40 | 62 |
|
41 | | -export const DropDownComponentInputSchema: yup.ObjectSchema<DropDownComponentInput> = yup.object({ |
42 | | - dropdownComponent: yup.lazy(() => ComponentInputSchema).optional(), |
43 | | - getEvent: yup.lazy(() => EventInputSchema.nonNullable()) |
44 | | -}); |
| 63 | +export function GuestSchema(): yup.ObjectSchema<Guest> { |
| 64 | + return yup.object({ |
| 65 | + __typename: yup.string<'Guest'>().optional(), |
| 66 | + lastLoggedIn: yup.mixed().nullable().optional() |
| 67 | + }) |
| 68 | +} |
45 | 69 |
|
46 | | -export const LayoutInputSchema: yup.ObjectSchema<LayoutInput> = yup.object({ |
47 | | - dropdown: yup.lazy(() => DropDownComponentInputSchema).optional() |
48 | | -}); |
| 70 | +export function HttpInputSchema(): yup.ObjectSchema<HttpInput> { |
| 71 | + return yup.object({ |
| 72 | + method: HttpMethodSchema.nullable().optional(), |
| 73 | + url: yup.mixed().nonNullable() |
| 74 | + }) |
| 75 | +} |
49 | 76 |
|
50 | | -export const AttributeInputSchema: yup.ObjectSchema<AttributeInput> = yup.object({ |
51 | | - key: yup.string().defined().nullable().optional(), |
52 | | - val: yup.string().defined().nullable().optional() |
53 | | -}); |
| 77 | +export function LayoutInputSchema(): yup.ObjectSchema<LayoutInput> { |
| 78 | + return yup.object({ |
| 79 | + dropdown: yup.lazy(() => DropDownComponentInputSchema()).optional() |
| 80 | + }) |
| 81 | +} |
54 | 82 |
|
55 | | -export const PageInputSchema: yup.ObjectSchema<PageInput> = yup.object({ |
56 | | - attributes: yup.array(yup.lazy(() => AttributeInputSchema.nonNullable())).defined().nullable().optional(), |
| 83 | +export function PageInputSchema(): yup.ObjectSchema<PageInput> { |
| 84 | + return yup.object({ |
| 85 | + attributes: yup.array(yup.lazy(() => AttributeInputSchema().nonNullable())).defined().nullable().optional(), |
57 | 86 | date: yup.mixed().nullable().optional(), |
58 | 87 | height: yup.number().defined().nonNullable(), |
59 | 88 | id: yup.string().defined().nonNullable(), |
60 | | - layout: yup.lazy(() => LayoutInputSchema.nonNullable()), |
| 89 | + layout: yup.lazy(() => LayoutInputSchema().nonNullable()), |
61 | 90 | pageType: PageTypeSchema.nonNullable(), |
62 | 91 | postIDs: yup.array(yup.string().defined().nonNullable()).defined().nullable().optional(), |
63 | 92 | show: yup.boolean().defined().nonNullable(), |
64 | 93 | tags: yup.array(yup.string().defined().nullable()).defined().nullable().optional(), |
65 | 94 | title: yup.string().defined().nonNullable(), |
66 | 95 | width: yup.number().defined().nonNullable() |
67 | | -}); |
68 | | - |
69 | | -export const GuestSchema: yup.ObjectSchema<Guest> = yup.object({ |
70 | | - __typename: yup.string<'Guest'>().optional(), |
71 | | - lastLoggedIn: yup.mixed().nullable().optional() |
72 | | -}); |
73 | | - |
74 | | -export const AdminSchema: yup.ObjectSchema<Admin> = yup.object({ |
75 | | - __typename: yup.string<'Admin'>().optional(), |
76 | | - lastModifiedAt: yup.mixed().nullable().optional() |
77 | | -}); |
78 | | - |
79 | | -export const UserKindSchema: yup.MixedSchema<UserKind> = union<UserKind>(AdminSchema, GuestSchema); |
| 96 | + }) |
| 97 | +} |
80 | 98 |
|
81 | | -export const UserSchema: yup.ObjectSchema<User> = yup.object({ |
| 99 | +export function UserSchema(): yup.ObjectSchema<User> { |
| 100 | + return yup.object({ |
82 | 101 | __typename: yup.string<'User'>().optional(), |
83 | 102 | createdAt: yup.mixed().nullable().optional(), |
84 | 103 | email: yup.string().defined().nullable().optional(), |
85 | 104 | id: yup.string().defined().nullable().optional(), |
86 | | - kind: UserKindSchema.nullable().optional(), |
| 105 | + kind: UserKindSchema().nullable().optional(), |
87 | 106 | name: yup.string().defined().nullable().optional(), |
88 | 107 | password: yup.string().defined().nullable().optional(), |
89 | 108 | updatedAt: yup.mixed().nullable().optional() |
90 | | -}); |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +export function UserKindSchema(): yup.MixedSchema<UserKind> { |
| 113 | + return union<UserKind>(AdminSchema(), GuestSchema()) |
| 114 | +} |
0 commit comments