Skip to content

fixed yup schema to not allow null #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions example/yup/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export function ComponentInputSchema(): yup.SchemaOf<ComponentInput> {
childrens: yup.array().of(yup.lazy(() => ComponentInputSchema()) as never).optional(),
event: yup.lazy(() => EventInputSchema()) as never,
name: yup.string().defined(),
type: ButtonComponentTypeSchema.defined()
type: ButtonComponentTypeSchema.required()
})
}

export function DropDownComponentInputSchema(): yup.SchemaOf<DropDownComponentInput> {
return yup.object({
dropdownComponent: yup.lazy(() => ComponentInputSchema()) as never,
getEvent: yup.lazy(() => EventInputSchema().defined()) as never
getEvent: yup.lazy(() => EventInputSchema().required()) as never
})
}

Expand All @@ -36,8 +36,8 @@ export function EventArgumentInputSchema(): yup.SchemaOf<EventArgumentInput> {

export function EventInputSchema(): yup.SchemaOf<EventInput> {
return yup.object({
arguments: yup.array().of(yup.lazy(() => EventArgumentInputSchema().defined()) as never).defined(),
options: yup.array().of(EventOptionTypeSchema.defined()).optional()
arguments: yup.array().of(yup.lazy(() => EventArgumentInputSchema().required()) as never).required(),
options: yup.array().of(EventOptionTypeSchema.required()).optional()
})
}

Expand All @@ -46,7 +46,7 @@ export const EventOptionTypeSchema = yup.mixed().oneOf([EventOptionType.Reload,
export function HttpInputSchema(): yup.SchemaOf<HttpInput> {
return yup.object({
method: HttpMethodSchema,
url: yup.mixed().defined()
url: yup.mixed().required()
})
}

Expand All @@ -60,17 +60,17 @@ export function LayoutInputSchema(): yup.SchemaOf<LayoutInput> {

export function PageInputSchema(): yup.SchemaOf<PageInput> {
return yup.object({
attributes: yup.array().of(yup.lazy(() => AttributeInputSchema().defined()) as never).optional(),
attributes: yup.array().of(yup.lazy(() => AttributeInputSchema().required()) as never).optional(),
date: yup.mixed(),
height: yup.number().defined(),
height: yup.number().required(),
id: yup.string().defined(),
layout: yup.lazy(() => LayoutInputSchema().defined()) as never,
pageType: PageTypeSchema.defined(),
layout: yup.lazy(() => LayoutInputSchema().required()) as never,
pageType: PageTypeSchema.required(),
postIDs: yup.array().of(yup.string().defined()).optional(),
show: yup.boolean().defined(),
show: yup.boolean().required(),
tags: yup.array().of(yup.string()).optional(),
title: yup.string().defined(),
width: yup.number().defined()
width: yup.number().required()
})
}

Expand Down
16 changes: 8 additions & 8 deletions src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ const maybeNonEmptyString = (
schema: string,
childType: TypeNode
): string => {
if (config.notAllowEmptyString === true && isNamedType(childType)) {
const maybeScalarName = childType.name.value;
const tsType = tsVisitor.scalars[maybeScalarName];
if (tsType === 'string') {
return `${schema}.required()`;
}
if (!isNamedType(childType)) {
return `${schema}.required()`;
}
const maybeScalarName = childType.name.value;
const tsType = tsVisitor.scalars[maybeScalarName];
if (!config.notAllowEmptyString && tsType === 'string') {
return `${schema}.defined()`;
}
// fallback
return `${schema}.defined()`;
return `${schema}.required()`;
};

const yup4Scalar = (tsVisitor: TsVisitor, scalarName: string): string => {
Expand Down
30 changes: 15 additions & 15 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ describe('yup', () => {
'export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>',
'a: yup.string().defined()',
'b: yup.string().defined()',
'c: yup.boolean().defined()',
'd: yup.number().defined()',
'e: yup.number().defined()',
'c: yup.boolean().required()',
'd: yup.number().required()',
'e: yup.number().required()',
],
],
[
Expand Down Expand Up @@ -61,10 +61,10 @@ describe('yup', () => {
'export function ArrayInputSchema(): yup.SchemaOf<ArrayInput>',
'a: yup.array().of(yup.string()).optional(),',
'b: yup.array().of(yup.string().defined()).optional(),',
'c: yup.array().of(yup.string().defined()).defined(),',
'c: yup.array().of(yup.string().defined()).required(),',
'd: yup.array().of(yup.array().of(yup.string()).optional()).optional(),',
'e: yup.array().of(yup.array().of(yup.string()).defined()).optional(),',
'f: yup.array().of(yup.array().of(yup.string()).defined()).defined()',
'e: yup.array().of(yup.array().of(yup.string()).required()).optional(),',
'f: yup.array().of(yup.array().of(yup.string()).required()).required()',
],
],
[
Expand All @@ -82,11 +82,11 @@ describe('yup', () => {
`,
[
'export function AInputSchema(): yup.SchemaOf<AInput>',
'b: yup.lazy(() => BInputSchema().defined()) as never',
'b: yup.lazy(() => BInputSchema().required()) as never',
'export function BInputSchema(): yup.SchemaOf<BInput>',
'c: yup.lazy(() => CInputSchema().defined()) as never',
'c: yup.lazy(() => CInputSchema().required()) as never',
'export function CInputSchema(): yup.SchemaOf<CInput>',
'a: yup.lazy(() => AInputSchema().defined()) as never',
'a: yup.lazy(() => AInputSchema().required()) as never',
],
],
[
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('yup', () => {
[
'export const PageTypeSchema = yup.mixed().oneOf([PageType.Public, PageType.BasicAuth])',
'export function PageInputSchema(): yup.SchemaOf<PageInput>',
'pageType: PageTypeSchema.defined()',
'pageType: PageTypeSchema.required()',
],
],
[
Expand All @@ -139,7 +139,7 @@ describe('yup', () => {
'export function HttpInputSchema(): yup.SchemaOf<HttpInput>',
'export const HttpMethodSchema = yup.mixed().oneOf([HttpMethod.Get, HttpMethod.Post])',
'method: HttpMethodSchema',
'url: yup.mixed().defined()',
'url: yup.mixed().required()',
],
],
])('%s', async (_, textSchema, wantContains) => {
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('yup', () => {
{}
);
expect(result.content).toContain('phrase: yup.string().defined()');
expect(result.content).toContain('times: yup.number().defined()');
expect(result.content).toContain('times: yup.number().required()');
});

it('with importFrom', async () => {
Expand Down Expand Up @@ -235,9 +235,9 @@ describe('yup', () => {
'export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>',
'a: yup.string().required(),',
'b: yup.string().required(),',
'c: yup.boolean().defined(),',
'd: yup.number().defined(),',
'e: yup.number().defined()',
'c: yup.boolean().required(),',
'd: yup.number().required(),',
'e: yup.number().required()',
];
for (const wantContain of wantContains) {
expect(result.content).toContain(wantContain);
Expand Down