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