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