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