diff --git a/example/yup/schemas.ts b/example/yup/schemas.ts index f574b5e3..5051a100 100644 --- a/example/yup/schemas.ts +++ b/example/yup/schemas.ts @@ -1,114 +1,114 @@ import * as yup from 'yup' import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, PageInput, PageType, User, UserKind } from '../types' -function union(...schemas: ReadonlyArray>): yup.BaseSchema { - return yup.mixed().test({ +function union(...schemas: ReadonlyArray>): yup.MixedSchema { + return yup.mixed().test({ test: (value) => schemas.some((schema) => schema.isValidSync(value)) - }) + }).defined() } -export function AdminSchema(): yup.SchemaOf { +export function AdminSchema(): yup.ObjectSchema { return yup.object({ - __typename: yup.mixed().oneOf(['Admin', undefined]), - lastModifiedAt: yup.mixed() + __typename: yup.string<'Admin'>().optional(), + lastModifiedAt: yup.mixed().nullable().optional() }) } -export function AttributeInputSchema(): yup.SchemaOf { +export function AttributeInputSchema(): yup.ObjectSchema { return yup.object({ - key: yup.string(), - val: yup.string() + key: yup.string().defined().nullable().optional(), + val: yup.string().defined().nullable().optional() }) } -export const ButtonComponentTypeSchema = yup.mixed().oneOf([ButtonComponentType.Button, ButtonComponentType.Submit]); +export const ButtonComponentTypeSchema = yup.string().oneOf([ButtonComponentType.Button, ButtonComponentType.Submit]).defined(); -export function ComponentInputSchema(): yup.SchemaOf { +export function ComponentInputSchema(): yup.ObjectSchema { return yup.object({ - child: yup.lazy(() => ComponentInputSchema()) as never, - childrens: yup.array().of(yup.lazy(() => ComponentInputSchema()) as never).optional(), - event: yup.lazy(() => EventInputSchema()) as never, - name: yup.string().defined(), - type: ButtonComponentTypeSchema.defined() + child: yup.lazy(() => ComponentInputSchema()).optional(), + childrens: yup.array(yup.lazy(() => ComponentInputSchema())).defined().nullable().optional(), + event: yup.lazy(() => EventInputSchema()).optional(), + name: yup.string().defined().nonNullable(), + type: ButtonComponentTypeSchema.nonNullable() }) } -export function DropDownComponentInputSchema(): yup.SchemaOf { +export function DropDownComponentInputSchema(): yup.ObjectSchema { return yup.object({ - dropdownComponent: yup.lazy(() => ComponentInputSchema()) as never, - getEvent: yup.lazy(() => EventInputSchema().defined()) as never + dropdownComponent: yup.lazy(() => ComponentInputSchema()).optional(), + getEvent: yup.lazy(() => EventInputSchema().nonNullable()) }) } -export function EventArgumentInputSchema(): yup.SchemaOf { +export function EventArgumentInputSchema(): yup.ObjectSchema { return yup.object({ - name: yup.string().defined().min(5), - value: yup.string().defined().matches(/^foo/) + name: yup.string().defined().nonNullable().min(5), + value: yup.string().defined().nonNullable().matches(/^foo/) }) } -export function EventInputSchema(): yup.SchemaOf { +export function EventInputSchema(): yup.ObjectSchema { return yup.object({ - arguments: yup.array().of(yup.lazy(() => EventArgumentInputSchema().defined()) as never).defined(), - options: yup.array().of(EventOptionTypeSchema.defined()).optional() + arguments: yup.array(yup.lazy(() => EventArgumentInputSchema().nonNullable())).defined(), + options: yup.array(EventOptionTypeSchema.nonNullable()).defined().nullable().optional() }) } -export const EventOptionTypeSchema = yup.mixed().oneOf([EventOptionType.Reload, EventOptionType.Retry]); +export const EventOptionTypeSchema = yup.string().oneOf([EventOptionType.Reload, EventOptionType.Retry]).defined(); -export function GuestSchema(): yup.SchemaOf { +export function GuestSchema(): yup.ObjectSchema { return yup.object({ - __typename: yup.mixed().oneOf(['Guest', undefined]), - lastLoggedIn: yup.mixed() + __typename: yup.string<'Guest'>().optional(), + lastLoggedIn: yup.mixed().nullable().optional() }) } -export function HttpInputSchema(): yup.SchemaOf { +export function HttpInputSchema(): yup.ObjectSchema { return yup.object({ - method: HttpMethodSchema, - url: yup.mixed().defined() + method: HttpMethodSchema.nullable().optional(), + url: yup.mixed().nonNullable() }) } -export const HttpMethodSchema = yup.mixed().oneOf([HttpMethod.Get, HttpMethod.Post]); +export const HttpMethodSchema = yup.string().oneOf([HttpMethod.Get, HttpMethod.Post]).defined(); -export function LayoutInputSchema(): yup.SchemaOf { +export function LayoutInputSchema(): yup.ObjectSchema { return yup.object({ - dropdown: yup.lazy(() => DropDownComponentInputSchema()) as never + dropdown: yup.lazy(() => DropDownComponentInputSchema()).optional() }) } -export function PageInputSchema(): yup.SchemaOf { +export function PageInputSchema(): yup.ObjectSchema { return yup.object({ - attributes: yup.array().of(yup.lazy(() => AttributeInputSchema().defined()) as never).optional(), - date: yup.mixed(), - height: yup.number().defined(), - id: yup.string().defined(), - layout: yup.lazy(() => LayoutInputSchema().defined()) as never, - pageType: PageTypeSchema.defined(), - postIDs: yup.array().of(yup.string().defined()).optional(), - show: yup.boolean().defined(), - tags: yup.array().of(yup.string()).optional(), - title: yup.string().defined(), - width: yup.number().defined() + attributes: yup.array(yup.lazy(() => AttributeInputSchema().nonNullable())).defined().nullable().optional(), + date: yup.mixed().nullable().optional(), + height: yup.number().defined().nonNullable(), + id: yup.string().defined().nonNullable(), + layout: yup.lazy(() => LayoutInputSchema().nonNullable()), + pageType: PageTypeSchema.nonNullable(), + postIDs: yup.array(yup.string().defined().nonNullable()).defined().nullable().optional(), + show: yup.boolean().defined().nonNullable(), + tags: yup.array(yup.string().defined().nullable()).defined().nullable().optional(), + title: yup.string().defined().nonNullable(), + width: yup.number().defined().nonNullable() }) } -export const PageTypeSchema = yup.mixed().oneOf([PageType.BasicAuth, PageType.Lp, PageType.Restricted, PageType.Service]); +export const PageTypeSchema = yup.string().oneOf([PageType.BasicAuth, PageType.Lp, PageType.Restricted, PageType.Service]).defined(); -export function UserSchema(): yup.SchemaOf { +export function UserSchema(): yup.ObjectSchema { return yup.object({ - __typename: yup.mixed().oneOf(['User', undefined]), - createdAt: yup.mixed(), - email: yup.string(), - id: yup.string(), - kind: UserKindSchema(), - name: yup.string(), - password: yup.string(), - updatedAt: yup.mixed() + __typename: yup.string<'User'>().optional(), + createdAt: yup.mixed().nullable().optional(), + email: yup.string().defined().nullable().optional(), + id: yup.string().defined().nullable().optional(), + kind: UserKindSchema().nullable().optional(), + name: yup.string().defined().nullable().optional(), + password: yup.string().defined().nullable().optional(), + updatedAt: yup.mixed().nullable().optional() }) } -export function UserKindSchema(): yup.BaseSchema { +export function UserKindSchema(): yup.MixedSchema { return union(AdminSchema(), GuestSchema()) } diff --git a/src/yup/index.ts b/src/yup/index.ts index 94bacd23..e4cdce47 100644 --- a/src/yup/index.ts +++ b/src/yup/index.ts @@ -36,12 +36,12 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema '\n' + new DeclarationBlock({}) .asKind('function') - .withName('union(...schemas: ReadonlyArray>): yup.BaseSchema') + .withName('union(...schemas: ReadonlyArray>): yup.MixedSchema') .withBlock( [ - indent('return yup.mixed().test({'), + indent('return yup.mixed().test({'), indent('test: (value) => schemas.some((schema) => schema.isValidSync(value))', 2), - indent('})'), + indent('}).defined()'), ].join('\n') ).string ); @@ -52,13 +52,16 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema importTypes.push(name); const shape = node.fields - ?.map(field => generateFieldYupSchema(config, tsVisitor, schema, field, 2)) + ?.map(field => { + const fieldSchema = generateFieldYupSchema(config, tsVisitor, schema, field, 2); + return isNonNullType(field.type) ? fieldSchema : `${fieldSchema}.optional()`; + }) .join(',\n'); return new DeclarationBlock({}) .export() .asKind('function') - .withName(`${name}Schema(): yup.SchemaOf<${name}>`) + .withName(`${name}Schema(): yup.ObjectSchema<${name}>`) .withBlock([indent(`return yup.object({`), shape, indent('})')].join('\n')).string; }, }, @@ -68,17 +71,20 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema importTypes.push(name); const shape = node.fields - ?.map(field => generateFieldYupSchema(config, tsVisitor, schema, field, 2)) + ?.map(field => { + const fieldSchema = generateFieldYupSchema(config, tsVisitor, schema, field, 2); + return isNonNullType(field.type) ? fieldSchema : `${fieldSchema}.optional()`; + }) .join(',\n'); return new DeclarationBlock({}) .export() .asKind('function') - .withName(`${name}Schema(): yup.SchemaOf<${name}>`) + .withName(`${name}Schema(): yup.ObjectSchema<${name}>`) .withBlock( [ indent(`return yup.object({`), - indent(`__typename: yup.mixed().oneOf(['${node.name.value}', undefined]),`, 2), + indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2), shape, indent('})'), ].join('\n') @@ -91,13 +97,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema importTypes.push(enumname); if (config.enumsAsTypes) { + const enums = node.values?.map(enumOption => `'${enumOption.name.value}'`); + return new DeclarationBlock({}) .export() .asKind('const') .withName(`${enumname}Schema`) - .withContent( - `yup.mixed().oneOf([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])` - ).string; + .withContent(`yup.string().oneOf([${enums?.join(', ')}]).defined()`).string; } const values = node.values @@ -113,7 +119,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema .export() .asKind('const') .withName(`${enumname}Schema`) - .withContent(`yup.mixed().oneOf([${values}])`).string; + .withContent(`yup.string<${enumname}>().oneOf([${values}]).defined()`).string; }, }, UnionTypeDefinition: { @@ -129,7 +135,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema return new DeclarationBlock({}) .export() .asKind('function') - .withName(`${unionName}Schema(): yup.BaseSchema<${unionName}>`) + .withName(`${unionName}Schema(): yup.MixedSchema<${unionName}>`) .withBlock(union).string; }, }, @@ -180,22 +186,28 @@ const generateFieldTypeYupSchema = ( if (isListType(type)) { const gen = generateFieldTypeYupSchema(config, tsVisitor, schema, type.type, type); if (!isNonNullType(parentType)) { - return `yup.array().of(${maybeLazy(type.type, gen)}).optional()`; + return `yup.array(${maybeLazy(type.type, gen)}).defined().nullable()`; } - return `yup.array().of(${maybeLazy(type.type, gen)})`; + return `yup.array(${maybeLazy(type.type, gen)}).defined()`; } if (isNonNullType(type)) { const gen = generateFieldTypeYupSchema(config, tsVisitor, schema, type.type, type); - const nonNullGen = maybeNonEmptyString(config, tsVisitor, gen, type.type); - return maybeLazy(type.type, nonNullGen); + return maybeLazy(type.type, gen); } if (isNamedType(type)) { const gen = generateNameNodeYupSchema(config, tsVisitor, schema, type.name); + if (isNonNullType(parentType)) { + if (config.notAllowEmptyString === true) { + const tsType = tsVisitor.scalars[type.name.value]; + if (tsType === 'string') return `${gen}.required()`; + } + return `${gen}.nonNullable()`; + } const typ = schema.getType(type.name.value); - if (typ?.astNode?.kind === 'ObjectTypeDefinition') { - return `${gen}.optional()`; + if (typ?.astNode?.kind === 'InputObjectTypeDefinition') { + return `${gen}`; } - return gen; + return `${gen}.nullable()`; } console.warn('unhandled type:', type); return ''; @@ -236,40 +248,23 @@ const generateNameNodeYupSchema = ( const maybeLazy = (type: TypeNode, schema: string): string => { if (isNamedType(type) && isInput(type.name.value)) { // https://github.com/jquense/yup/issues/1283#issuecomment-786559444 - return `yup.lazy(() => ${schema}) as never`; + return `yup.lazy(() => ${schema})`; } return schema; }; -const maybeNonEmptyString = ( - config: ValidationSchemaPluginConfig, - tsVisitor: TsVisitor, - 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()`; - } - } - // fallback - return `${schema}.defined()`; -}; - const yup4Scalar = (config: ValidationSchemaPluginConfig, tsVisitor: TsVisitor, scalarName: string): string => { if (config.scalarSchemas?.[scalarName]) { - return config.scalarSchemas[scalarName]; + return `${config.scalarSchemas[scalarName]}.defined()`; } const tsType = tsVisitor.scalars[scalarName]; switch (tsType) { case 'string': - return `yup.string()`; + return `yup.string().defined()`; case 'number': - return `yup.number()`; + return `yup.number().defined()`; case 'boolean': - return `yup.boolean()`; + return `yup.boolean().defined()`; } console.warn('unhandled name:', scalarName); return `yup.mixed()`; diff --git a/tests/yup.spec.ts b/tests/yup.spec.ts index a5a81779..13705bf0 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -15,7 +15,7 @@ describe('yup', () => { } `, [ - 'export function PrimitiveInputSchema(): yup.SchemaOf', + 'export function PrimitiveInputSchema(): yup.ObjectSchema', 'a: yup.string().defined()', 'b: yup.string().defined()', 'c: yup.boolean().defined()', @@ -36,13 +36,13 @@ describe('yup', () => { } `, [ - 'export function PrimitiveInputSchema(): yup.SchemaOf', + 'export function PrimitiveInputSchema(): yup.ObjectSchema', // alphabet order - 'a: yup.string(),', - 'b: yup.string(),', - 'c: yup.boolean(),', - 'd: yup.number(),', - 'e: yup.number(),', + 'a: yup.string().defined().nullable().optional(),', + 'b: yup.string().defined().nullable().optional(),', + 'c: yup.boolean().defined().nullable().optional(),', + 'd: yup.number().defined().nullable().optional(),', + 'e: yup.number().defined().nullable().optional(),', ], ], [ @@ -58,13 +58,13 @@ describe('yup', () => { } `, [ - 'export function ArrayInputSchema(): yup.SchemaOf', - 'a: yup.array().of(yup.string()).optional(),', - 'b: yup.array().of(yup.string().defined()).optional(),', - 'c: yup.array().of(yup.string().defined()).defined(),', - '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()', + 'export function ArrayInputSchema(): yup.ObjectSchema', + 'a: yup.array(yup.string().defined().nullable()).defined().nullable().optional(),', + 'b: yup.array(yup.string().defined().nonNullable()).defined().nullable().optional(),', + 'c: yup.array(yup.string().defined().nonNullable()).defined(),', + 'd: yup.array(yup.array(yup.string().defined().nullable()).defined().nullable()).defined().nullable().optional(),', + 'e: yup.array(yup.array(yup.string().defined().nullable()).defined()).defined().nullable().optional(),', + 'f: yup.array(yup.array(yup.string().defined().nullable()).defined()).defined()', ], ], [ @@ -81,12 +81,12 @@ describe('yup', () => { } `, [ - 'export function AInputSchema(): yup.SchemaOf', - 'b: yup.lazy(() => BInputSchema().defined()) as never', - 'export function BInputSchema(): yup.SchemaOf', - 'c: yup.lazy(() => CInputSchema().defined()) as never', - 'export function CInputSchema(): yup.SchemaOf', - 'a: yup.lazy(() => AInputSchema().defined()) as never', + 'export function AInputSchema(): yup.ObjectSchema', + 'b: yup.lazy(() => BInputSchema().nonNullable())', + 'export function BInputSchema(): yup.ObjectSchema', + 'c: yup.lazy(() => CInputSchema().nonNullable())', + 'export function CInputSchema(): yup.ObjectSchema', + 'a: yup.lazy(() => AInputSchema().nonNullable())', ], ], [ @@ -98,9 +98,9 @@ describe('yup', () => { } `, [ - 'export function NestedInputSchema(): yup.SchemaOf', - 'child: yup.lazy(() => NestedInputSchema()) as never,', - 'childrens: yup.array().of(yup.lazy(() => NestedInputSchema()) as never).optional()', + 'export function NestedInputSchema(): yup.ObjectSchema', + 'child: yup.lazy(() => NestedInputSchema()).optional(),', + 'childrens: yup.array(yup.lazy(() => NestedInputSchema())).defined().nullable().optional()', ], ], [ @@ -115,9 +115,9 @@ describe('yup', () => { } `, [ - 'export const PageTypeSchema = yup.mixed().oneOf([PageType.Public, PageType.BasicAuth])', - 'export function PageInputSchema(): yup.SchemaOf', - 'pageType: PageTypeSchema.defined()', + 'export const PageTypeSchema = yup.string().oneOf([PageType.Public, PageType.BasicAuth]).defined();', + 'export function PageInputSchema(): yup.ObjectSchema', + 'pageType: PageTypeSchema.nonNullable()', ], ], [ @@ -136,10 +136,10 @@ describe('yup', () => { scalar URL # unknown scalar, should be any (yup.mixed()) `, [ - 'export function HttpInputSchema(): yup.SchemaOf', - 'export const HttpMethodSchema = yup.mixed().oneOf([HttpMethod.Get, HttpMethod.Post])', - 'method: HttpMethodSchema', - 'url: yup.mixed().defined()', + 'export function HttpInputSchema(): yup.ObjectSchema', + 'export const HttpMethodSchema = yup.string().oneOf([HttpMethod.Get, HttpMethod.Post]).defined();', + 'method: HttpMethodSchema.nullable().optional(),', + 'url: yup.mixed().nonNullable()', ], ], ])('%s', async (_, textSchema, wantContains) => { @@ -210,7 +210,9 @@ describe('yup', () => { }, {} ); - expect(result.content).toContain("export const PageTypeSchema = yup.mixed().oneOf(['PUBLIC', 'BASIC_AUTH'])"); + expect(result.content).toContain( + "export const PageTypeSchema = yup.string().oneOf(['PUBLIC', 'BASIC_AUTH']).defined();" + ); }); it('with notAllowEmptyString', async () => { @@ -232,12 +234,12 @@ describe('yup', () => { {} ); const wantContains = [ - 'export function PrimitiveInputSchema(): yup.SchemaOf', - 'a: yup.string().required(),', - 'b: yup.string().required(),', - 'c: yup.boolean().defined(),', - 'd: yup.number().defined(),', - 'e: yup.number().defined()', + 'export function PrimitiveInputSchema(): yup.ObjectSchema', + 'a: yup.string().defined().required(),', + 'b: yup.string().defined().required(),', + 'c: yup.boolean().defined().nonNullable(),', + 'd: yup.number().defined().nonNullable(),', + 'e: yup.number().defined().nonNullable()', ]; for (const wantContain of wantContains) { expect(result.content).toContain(wantContain); @@ -266,10 +268,10 @@ describe('yup', () => { {} ); const wantContains = [ - 'export function ScalarsInputSchema(): yup.SchemaOf', - 'date: yup.date().defined(),', - 'email: yup.string().email(),', - 'str: yup.string().defined()', + 'export function ScalarsInputSchema(): yup.ObjectSchema', + 'date: yup.date().defined().nonNullable(),', + 'email: yup.string().email().defined().nullable().optional(),', + 'str: yup.string().defined().nonNullable()', ]; for (const wantContain of wantContains) { expect(result.content).toContain(wantContain); @@ -292,7 +294,7 @@ describe('yup', () => { {} ); expect(result.prepend).toContain("import { ISay } from './types'"); - expect(result.content).toContain('export function ISaySchema(): yup.SchemaOf {'); + expect(result.content).toContain('export function ISaySchema(): yup.ObjectSchema {'); }); it('with typesSuffix', async () => { @@ -311,7 +313,7 @@ describe('yup', () => { {} ); expect(result.prepend).toContain("import { SayI } from './types'"); - expect(result.content).toContain('export function SayISchema(): yup.SchemaOf {'); + expect(result.content).toContain('export function SayISchema(): yup.ObjectSchema {'); }); describe('with withObjectType', () => { it('not generate if withObjectType false', async () => { @@ -329,7 +331,7 @@ describe('yup', () => { }, {} ); - expect(result.content).not.toContain('export function UserSchema(): yup.SchemaOf {'); + expect(result.content).not.toContain('export function UserSchema(): yup.ObjectSchema {'); }); it('generate object type contains object type', async () => { @@ -359,20 +361,20 @@ describe('yup', () => { {} ); const wantContains = [ - 'export function AuthorSchema(): yup.SchemaOf {', - "__typename: yup.mixed().oneOf(['Author', undefined]),", - 'books: yup.array().of(BookSchema().optional()).optional(),', - 'name: yup.string()', - - 'export function BookSchema(): yup.SchemaOf {', - "__typename: yup.mixed().oneOf(['Book', undefined]),", - 'author: AuthorSchema().optional(),', - 'title: yup.string()', - - 'export function Book2Schema(): yup.SchemaOf {', - "__typename: yup.mixed().oneOf(['Book2', undefined]),", - 'author: AuthorSchema().optional().defined(),', - 'title: yup.string().defined()', + 'export function AuthorSchema(): yup.ObjectSchema {', + "__typename: yup.string<'Author'>().optional(),", + 'books: yup.array(BookSchema().nullable()).defined().nullable().optional(),', + 'name: yup.string().defined().nullable().optional()', + + 'export function BookSchema(): yup.ObjectSchema {', + "__typename: yup.string<'Book'>().optional(),", + 'author: AuthorSchema().nullable().optional(),', + 'title: yup.string().defined().nonNullable()', + + 'export function Book2Schema(): yup.ObjectSchema {', + "__typename: yup.string<'Book2'>().optional(),", + 'author: AuthorSchema().nonNullable(),', + 'title: yup.string().defined().nullable().optional()', ]; for (const wantContain of wantContains) { expect(result.content).toContain(wantContain); @@ -429,19 +431,19 @@ describe('yup', () => { ); const wantContains = [ // User Create Input - 'export function UserCreateInputSchema(): yup.SchemaOf {', - 'name: yup.string().defined(),', - 'date: yup.date().defined(),', - 'email: yup.string().email().defined()', + 'export function UserCreateInputSchema(): yup.ObjectSchema {', + 'name: yup.string().defined().nonNullable(),', + 'date: yup.date().defined().nonNullable(),', + 'email: yup.string().email().defined().nonNullable()', // User - 'export function UserSchema(): yup.SchemaOf {', - "__typename: yup.mixed().oneOf(['User', undefined]),", - 'id: yup.string().defined(),', - 'name: yup.string(),', - 'age: yup.number(),', - 'isMember: yup.boolean(),', - 'email: yup.string().email(),', - 'createdAt: yup.date().defined()', + 'export function UserSchema(): yup.ObjectSchema {', + "__typename: yup.string<'User'>().optional(),", + 'id: yup.string().defined().nonNullable(),', + 'name: yup.string().defined().nullable().optional(),', + 'age: yup.number().defined().nullable().optional(),', + 'isMember: yup.boolean().defined().nullable().optional(),', + 'email: yup.string().email().defined().nullable().optional(),', + 'createdAt: yup.date().defined().nonNullable()', ]; for (const wantContain of wantContains) { expect(result.content).toContain(wantContain); @@ -475,7 +477,7 @@ describe('yup', () => { const wantContains = [ // Shape Schema - 'export function ShapeSchema(): yup.BaseSchema {', + 'export function ShapeSchema(): yup.MixedSchema {', 'union(CircleSchema(), SquareSchema())', '}', ]; @@ -510,10 +512,10 @@ describe('yup', () => { ); const wantContains = [ - 'export function GeometrySchema(): yup.SchemaOf {', + 'export function GeometrySchema(): yup.ObjectSchema {', 'return yup.object({', - "__typename: yup.mixed().oneOf(['Geometry', undefined]),", - 'shape: ShapeSchema()', + "__typename: yup.string<'Geometry'>().optional(),", + 'shape: ShapeSchema().nullable().optional()', '})', ]; for (const wantContain of wantContains) { @@ -541,7 +543,7 @@ describe('yup', () => { const wantContains = [ // Shape Schema - 'export function ShapeSchema(): yup.BaseSchema {', + 'export function ShapeSchema(): yup.MixedSchema {', 'return union(CircleSchema())', '}', ]; @@ -576,9 +578,9 @@ describe('yup', () => { ); const wantContains = [ // User Create Input - 'export function UserCreateInputSchema(): yup.SchemaOf {', - 'name: yup.string().defined().matches(/^Sir/),', - 'age: yup.number().defined().min(0).max(100)', + 'export function UserCreateInputSchema(): yup.ObjectSchema {', + 'name: yup.string().defined().nonNullable().matches(/^Sir/),', + 'age: yup.number().defined().nonNullable().min(0).max(100)', ]; for (const wantContain of wantContains) { expect(result.content).toContain(wantContain);