|
| 1 | +import * as yup from 'yup' |
| 2 | +import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types' |
| 3 | + |
| 4 | +export function AttributeInputSchema(): yup.SchemaOf<AttributeInput> { |
| 5 | + return yup.object({ |
| 6 | + key: yup.string(), |
| 7 | + val: yup.string() |
| 8 | + }) |
| 9 | +} |
| 10 | + |
| 11 | +export const ButtonComponentTypeSchema = yup.mixed().oneOf([ButtonComponentType.Button, ButtonComponentType.Submit]); |
| 12 | + |
| 13 | +export function ComponentInputSchema(): yup.SchemaOf<ComponentInput> { |
| 14 | + return yup.object({ |
| 15 | + child: yup.lazy(() => ComponentInputSchema()) as never, |
| 16 | + childrens: yup.array().of(yup.lazy(() => ComponentInputSchema()) as never).optional(), |
| 17 | + event: yup.lazy(() => EventInputSchema()) as never, |
| 18 | + name: yup.string().defined(), |
| 19 | + type: ButtonComponentTypeSchema.defined() |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +export function DropDownComponentInputSchema(): yup.SchemaOf<DropDownComponentInput> { |
| 24 | + return yup.object({ |
| 25 | + dropdownComponent: yup.lazy(() => ComponentInputSchema()) as never, |
| 26 | + getEvent: yup.lazy(() => EventInputSchema().defined()) as never |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +export function EventArgumentInputSchema(): yup.SchemaOf<EventArgumentInput> { |
| 31 | + return yup.object({ |
| 32 | + name: yup.string().defined().min(5), |
| 33 | + value: yup.string().defined().matches(/^foo/) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +export function EventInputSchema(): yup.SchemaOf<EventInput> { |
| 38 | + return yup.object({ |
| 39 | + arguments: yup.array().of(yup.lazy(() => EventArgumentInputSchema().defined()) as never).defined(), |
| 40 | + options: yup.array().of(EventOptionTypeSchema.defined()).optional() |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +export const EventOptionTypeSchema = yup.mixed().oneOf([EventOptionType.Reload, EventOptionType.Retry]); |
| 45 | + |
| 46 | +export function HttpInputSchema(): yup.SchemaOf<HttpInput> { |
| 47 | + return yup.object({ |
| 48 | + method: HttpMethodSchema, |
| 49 | + url: yup.mixed().defined() |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +export const HttpMethodSchema = yup.mixed().oneOf([HttpMethod.Get, HttpMethod.Post]); |
| 54 | + |
| 55 | +export function LayoutInputSchema(): yup.SchemaOf<LayoutInput> { |
| 56 | + return yup.object({ |
| 57 | + dropdown: yup.lazy(() => DropDownComponentInputSchema()) as never |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +export function PageInputSchema(): yup.SchemaOf<PageInput> { |
| 62 | + return yup.object({ |
| 63 | + attributes: yup.array().of(yup.lazy(() => AttributeInputSchema().defined()) as never).optional(), |
| 64 | + date: yup.mixed(), |
| 65 | + height: yup.number().defined(), |
| 66 | + id: yup.string().defined(), |
| 67 | + layout: yup.lazy(() => LayoutInputSchema().defined()) as never, |
| 68 | + pageType: PageTypeSchema.defined(), |
| 69 | + postIDs: yup.array().of(yup.string().defined()).optional(), |
| 70 | + show: yup.boolean().defined(), |
| 71 | + tags: yup.array().of(yup.string()).optional(), |
| 72 | + title: yup.string().defined(), |
| 73 | + width: yup.number().defined() |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +export const PageTypeSchema = yup.mixed().oneOf([PageType.BasicAuth, PageType.Lp, PageType.Restricted, PageType.Service]); |
0 commit comments