From 41c0e2a66c182e3430da27412955c62f95ea599e Mon Sep 17 00:00:00 2001 From: swaysway <7465495+SwaySway@users.noreply.github.com> Date: Wed, 14 Sep 2022 20:21:54 +0000 Subject: [PATCH] fix: add inputType for form hook functions --- ...studio-ui-codegen-react-forms.test.ts.snap | 120 +++++++++--------- .../__snapshots__/type-helper.test.ts.snap | 16 +-- .../lib/__tests__/forms/type-helper.test.ts | 6 +- .../form-renderer-helper.test.ts.snap | 16 +-- .../lib/forms/form-renderer-helper.ts | 38 ++---- .../lib/forms/react-form-renderer.ts | 6 +- .../codegen-ui-react/lib/forms/type-helper.ts | 71 ++++++++--- 7 files changed, 149 insertions(+), 124 deletions(-) diff --git a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap index 56c6c2dc8..2dd0feed9 100644 --- a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap @@ -252,11 +252,12 @@ export declare type ValidationResponse = { errorMessage?: string; }; export declare type ValidationFunction = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise; -export declare type CustomDataFormInputValues = { - name?: ValidationFunction; - email?: ValidationFunction; - city?: ValidationFunction; - category?: ValidationFunction; +export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; +export declare type CustomDataFormInputValues = { + name?: UseBaseOrValidationType; + email?: UseBaseOrValidationType; + city?: UseBaseOrValidationType; + category?: UseBaseOrValidationType; }; export declare type FormProps = Partial & React.DOMAttributes; export declare type CustomDataFormOverridesProps = { @@ -269,10 +270,10 @@ export declare type CustomDataFormOverridesProps = { export declare type CustomDataFormProps = React.PropsWithChildren<{ overrides?: CustomDataFormOverridesProps | undefined | null; } & { - onSubmit: (fields: Record) => void; + onSubmit: (fields: CustomDataFormInputValues) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: CustomDataFormInputValues; + onChange?: (fields: CustomDataFormInputValues) => CustomDataFormInputValues; + onValidate?: CustomDataFormInputValues; }>; export default function CustomDataForm(props: CustomDataFormProps): React.ReactElement; " @@ -486,12 +487,13 @@ export declare type ValidationResponse = { errorMessage?: string; }; export declare type ValidationFunction = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise; -export declare type NestedJsonInputValues = { - firstName?: ValidationFunction; - lastName?: ValidationFunction; +export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; +export declare type NestedJsonInputValues = { + firstName?: UseBaseOrValidationType; + lastName?: UseBaseOrValidationType; bio?: { - favoriteQuote?: ValidationFunction; - favoriteAnimal?: ValidationFunction; + favoriteQuote?: UseBaseOrValidationType; + favoriteAnimal?: UseBaseOrValidationType; }; }; export declare type FormProps = Partial & React.DOMAttributes; @@ -506,10 +508,10 @@ export declare type NestedJsonOverridesProps = { export declare type NestedJsonProps = React.PropsWithChildren<{ overrides?: NestedJsonOverridesProps | undefined | null; } & { - onSubmit: (fields: Record) => void; + onSubmit: (fields: NestedJsonInputValues) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: NestedJsonInputValues; + onChange?: (fields: NestedJsonInputValues) => NestedJsonInputValues; + onValidate?: NestedJsonInputValues; }>; export default function NestedJson(props: NestedJsonProps): React.ReactElement; " @@ -657,8 +659,9 @@ export declare type ValidationResponse = { errorMessage?: string; }; export declare type ValidationFunction = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise; -export declare type CustomWithSectionalElementsInputValues = { - name?: ValidationFunction; +export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; +export declare type CustomWithSectionalElementsInputValues = { + name?: UseBaseOrValidationType; }; export declare type FormProps = Partial & React.DOMAttributes; export declare type CustomWithSectionalElementsOverridesProps = { @@ -671,10 +674,10 @@ export declare type CustomWithSectionalElementsOverridesProps = { export declare type CustomWithSectionalElementsProps = React.PropsWithChildren<{ overrides?: CustomWithSectionalElementsOverridesProps | undefined | null; } & { - onSubmit: (fields: Record) => void; + onSubmit: (fields: CustomWithSectionalElementsInputValues) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: CustomWithSectionalElementsInputValues; + onChange?: (fields: CustomWithSectionalElementsInputValues) => CustomWithSectionalElementsInputValues; + onValidate?: CustomWithSectionalElementsInputValues; }>; export default function CustomWithSectionalElements(props: CustomWithSectionalElementsProps): React.ReactElement; " @@ -917,11 +920,12 @@ export declare type ValidationResponse = { errorMessage?: string; }; export declare type ValidationFunction = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise; -export declare type MyPostFormInputValues = { - caption?: ValidationFunction; - username?: ValidationFunction; - post_url?: ValidationFunction; - profile_url?: ValidationFunction; +export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; +export declare type MyPostFormInputValues = { + caption?: UseBaseOrValidationType; + username?: UseBaseOrValidationType; + post_url?: UseBaseOrValidationType; + profile_url?: UseBaseOrValidationType; }; export declare type FormProps = Partial & React.DOMAttributes; export declare type MyPostFormOverridesProps = { @@ -934,12 +938,12 @@ export declare type MyPostFormOverridesProps = { export declare type MyPostFormProps = React.PropsWithChildren<{ overrides?: MyPostFormOverridesProps | undefined | null; } & { - onSubmit?: (fields: Record) => Record; - onSuccess?: (fields: Record) => void; - onError?: (fields: Record, errorMessage: string) => void; + onSubmit?: (fields: MyPostFormInputValues) => MyPostFormInputValues; + onSuccess?: (fields: MyPostFormInputValues) => void; + onError?: (fields: MyPostFormInputValues, errorMessage: string) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: MyPostFormInputValues; + onChange?: (fields: MyPostFormInputValues) => MyPostFormInputValues; + onValidate?: MyPostFormInputValues; }>; export default function MyPostForm(props: MyPostFormProps): React.ReactElement; " @@ -1280,12 +1284,13 @@ export declare type ValidationResponse = { errorMessage?: string; }; export declare type ValidationFunction = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise; -export declare type MyPostFormInputValues = { - TextAreaFieldbbd63464?: ValidationFunction; - caption?: ValidationFunction; - username?: ValidationFunction; - profile_url?: ValidationFunction; - post_url?: ValidationFunction; +export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; +export declare type MyPostFormInputValues = { + TextAreaFieldbbd63464?: UseBaseOrValidationType; + caption?: UseBaseOrValidationType; + username?: UseBaseOrValidationType; + profile_url?: UseBaseOrValidationType; + post_url?: UseBaseOrValidationType; }; export declare type FormProps = Partial & React.DOMAttributes; export declare type MyPostFormOverridesProps = { @@ -1301,12 +1306,12 @@ export declare type MyPostFormProps = React.PropsWithChildren<{ } & { id?: string; post?: Post; - onSubmit?: (fields: Record) => Record; - onSuccess?: (fields: Record) => void; - onError?: (fields: Record, errorMessage: string) => void; + onSubmit?: (fields: MyPostFormInputValues) => MyPostFormInputValues; + onSuccess?: (fields: MyPostFormInputValues) => void; + onError?: (fields: MyPostFormInputValues, errorMessage: string) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: MyPostFormInputValues; + onChange?: (fields: MyPostFormInputValues) => MyPostFormInputValues; + onValidate?: MyPostFormInputValues; }>; export default function MyPostForm(props: MyPostFormProps): React.ReactElement; " @@ -1885,16 +1890,17 @@ export declare type ValidationResponse = { errorMessage?: string; }; export declare type ValidationFunction = (value: T, validationResponse: ValidationResponse) => ValidationResponse | Promise; -export declare type InputGalleryCreateFormInputValues = { - num?: ValidationFunction; - rootbeer?: ValidationFunction; - attend?: ValidationFunction; - maybeSlide?: ValidationFunction; - maybeCheck?: ValidationFunction; - arrayTypeField?: ValidationFunction; - timestamp?: ValidationFunction; - ippy?: ValidationFunction; - timeisnow?: ValidationFunction; +export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; +export declare type InputGalleryCreateFormInputValues = { + num?: UseBaseOrValidationType; + rootbeer?: UseBaseOrValidationType; + attend?: UseBaseOrValidationType; + maybeSlide?: UseBaseOrValidationType; + maybeCheck?: UseBaseOrValidationType; + arrayTypeField?: UseBaseOrValidationType; + timestamp?: UseBaseOrValidationType; + ippy?: UseBaseOrValidationType; + timeisnow?: UseBaseOrValidationType; }; export declare type FormProps = Partial & React.DOMAttributes; export declare type InputGalleryCreateFormOverridesProps = { @@ -1912,12 +1918,12 @@ export declare type InputGalleryCreateFormOverridesProps = { export declare type InputGalleryCreateFormProps = React.PropsWithChildren<{ overrides?: InputGalleryCreateFormOverridesProps | undefined | null; } & { - onSubmit?: (fields: Record) => Record; - onSuccess?: (fields: Record) => void; - onError?: (fields: Record, errorMessage: string) => void; + onSubmit?: (fields: InputGalleryCreateFormInputValues) => InputGalleryCreateFormInputValues; + onSuccess?: (fields: InputGalleryCreateFormInputValues) => void; + onError?: (fields: InputGalleryCreateFormInputValues, errorMessage: string) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: InputGalleryCreateFormInputValues; + onChange?: (fields: InputGalleryCreateFormInputValues) => InputGalleryCreateFormInputValues; + onValidate?: InputGalleryCreateFormInputValues; }>; export default function InputGalleryCreateForm(props: InputGalleryCreateFormProps): React.ReactElement; " diff --git a/packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/type-helper.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/type-helper.test.ts.snap index 58b733a96..0f3481d79 100644 --- a/packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/type-helper.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/forms/__snapshots__/type-helper.test.ts.snap @@ -1,16 +1,16 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should generate nested object should generate type for nested object 1`] = ` -"export declare type myCreateFormInputValues = { - firstName?: ValidationFunction; - isExplorer?: ValidationFunction; +"export declare type myCreateFormInputValues = { + firstName?: UseBaseOrValidationType; + isExplorer?: UseBaseOrValidationType; bio?: { favoriteAnimal?: { animalMeta?: { family?: { - genus?: ValidationFunction; + genus?: UseBaseOrValidationType; }; - earliestRecord?: ValidationFunction; + earliestRecord?: UseBaseOrValidationType; }; }; }; @@ -18,8 +18,8 @@ exports[`should generate nested object should generate type for nested object 1` `; exports[`should generate nested object should generate type for non nested object 1`] = ` -"export declare type myCreateFormInputValues = { - firstName?: ValidationFunction; - isExplorer?: ValidationFunction; +"export declare type myCreateFormInputValues = { + firstName?: UseBaseOrValidationType; + isExplorer?: UseBaseOrValidationType; };" `; diff --git a/packages/codegen-ui-react/lib/__tests__/forms/type-helper.test.ts b/packages/codegen-ui-react/lib/__tests__/forms/type-helper.test.ts index f2cd3d60c..b34b350be 100644 --- a/packages/codegen-ui-react/lib/__tests__/forms/type-helper.test.ts +++ b/packages/codegen-ui-react/lib/__tests__/forms/type-helper.test.ts @@ -15,7 +15,7 @@ */ import { FieldConfigMetadata } from '@aws-amplify/codegen-ui'; -import { generateOnValidationType } from '../../forms/type-helper'; +import { generateInputTypes } from '../../forms/type-helper'; import { genericPrinter } from '../__utils__'; describe('should generate nested object', () => { @@ -38,7 +38,7 @@ describe('should generate nested object', () => { validationRules: [], }, }; - const types = generateOnValidationType('myCreateForm', fieldConfigs); + const types = generateInputTypes('myCreateForm', fieldConfigs); const response = genericPrinter(types); expect(response).toMatchSnapshot(); }); @@ -54,7 +54,7 @@ describe('should generate nested object', () => { validationRules: [], }, }; - const types = generateOnValidationType('myCreateForm', fieldConfigs); + const types = generateInputTypes('myCreateForm', fieldConfigs); const response = genericPrinter(types); expect(response).toMatchSnapshot(); }); diff --git a/packages/codegen-ui-react/lib/__tests__/react-forms/__snapshots__/form-renderer-helper.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/react-forms/__snapshots__/form-renderer-helper.test.ts.snap index 0e28eadbc..38480e8c8 100644 --- a/packages/codegen-ui-react/lib/__tests__/react-forms/__snapshots__/form-renderer-helper.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/react-forms/__snapshots__/form-renderer-helper.test.ts.snap @@ -2,20 +2,20 @@ exports[`form-render utils should generate before & complete types if datastore config is set 1`] = ` "{ - onSubmit?: (fields: Record) => Record; - onSuccess?: (fields: Record) => void; - onError?: (fields: Record, errorMessage: string) => void; + onSubmit?: (fields: mySampleFormInputValues) => mySampleFormInputValues; + onSuccess?: (fields: mySampleFormInputValues) => void; + onError?: (fields: mySampleFormInputValues, errorMessage: string) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: mySampleFormInputValues; + onChange?: (fields: mySampleFormInputValues) => mySampleFormInputValues; + onValidate?: mySampleFormInputValues; }" `; exports[`form-render utils should generate regular onsubmit if dataSourceType is custom 1`] = ` "{ - onSubmit: (fields: Record) => void; + onSubmit: (fields: myCustomFormInputValues) => void; onCancel?: () => void; - onChange?: (fields: Record) => Record; - onValidate?: myCustomFormInputValues; + onChange?: (fields: myCustomFormInputValues) => myCustomFormInputValues; + onValidate?: myCustomFormInputValues; }" `; diff --git a/packages/codegen-ui-react/lib/forms/form-renderer-helper.ts b/packages/codegen-ui-react/lib/forms/form-renderer-helper.ts index c501449f9..a17c9b198 100644 --- a/packages/codegen-ui-react/lib/forms/form-renderer-helper.ts +++ b/packages/codegen-ui-react/lib/forms/form-renderer-helper.ts @@ -39,7 +39,7 @@ import { import { lowerCaseFirst } from '../helpers'; import { ImportCollection, ImportSource } from '../imports'; import { buildTargetVariable } from './event-targets'; -import { buildOnValidateType } from './type-helper'; +import { buildOnValidateType, getInputValuesTypeName } from './type-helper'; import { buildAccessChain, capitalizeFirstLetter, setFieldState } from './form-state'; export const buildMutationBindings = (form: StudioForm) => { @@ -79,6 +79,7 @@ export const buildMutationBindings = (form: StudioForm) => { */ export const buildFormPropNode = (form: StudioForm) => { const { + name: formName, dataType: { dataSourceType }, formActionType, } = form; @@ -114,17 +115,11 @@ export const buildFormPropNode = (form: StudioForm) => { undefined, 'fields', undefined, - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), undefined, ), ], - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), ), ), factory.createPropertySignature( @@ -140,10 +135,7 @@ export const buildFormPropNode = (form: StudioForm) => { undefined, factory.createIdentifier('fields'), undefined, - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), undefined, ), ], @@ -163,10 +155,7 @@ export const buildFormPropNode = (form: StudioForm) => { undefined, factory.createIdentifier('fields'), undefined, - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), undefined, ), factory.createParameterDeclaration( @@ -199,10 +188,7 @@ export const buildFormPropNode = (form: StudioForm) => { undefined, 'fields', undefined, - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), undefined, ), ], @@ -233,17 +219,11 @@ export const buildFormPropNode = (form: StudioForm) => { undefined, factory.createIdentifier('fields'), undefined, - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), undefined, ), ], - factory.createTypeReferenceNode(factory.createIdentifier('Record'), [ - factory.createKeywordTypeNode(SyntaxKind.StringKeyword), - factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), - ]), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), ), ), buildOnValidateType(form.name), diff --git a/packages/codegen-ui-react/lib/forms/react-form-renderer.ts b/packages/codegen-ui-react/lib/forms/react-form-renderer.ts index 71dec2c87..adfd10eab 100644 --- a/packages/codegen-ui-react/lib/forms/react-form-renderer.ts +++ b/packages/codegen-ui-react/lib/forms/react-form-renderer.ts @@ -71,8 +71,9 @@ import { } from './form-renderer-helper'; import { buildUseStateExpression, capitalizeFirstLetter, getUseStateHooks } from './form-state'; import { + baseValidationConditionalType, formOverrideProp, - generateOnValidationType, + generateInputTypes, validationFunctionType, validationResponseType, } from './type-helper'; @@ -293,7 +294,8 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer< return [ validationResponseType, validationFunctionType, - generateOnValidationType(formName, fieldConfigs), + baseValidationConditionalType, + generateInputTypes(formName, fieldConfigs), formOverrideProp, overrideTypeAliasDeclaration, factory.createTypeAliasDeclaration( diff --git a/packages/codegen-ui-react/lib/forms/type-helper.ts b/packages/codegen-ui-react/lib/forms/type-helper.ts index 15c920f2c..ea1141766 100644 --- a/packages/codegen-ui-react/lib/forms/type-helper.ts +++ b/packages/codegen-ui-react/lib/forms/type-helper.ts @@ -36,7 +36,7 @@ const getSyntaxKindType = (dataType?: DataFieldDataType) => { return typescriptType; }; -const getInputValuesTypeName = (formName: string): string => { +export const getInputValuesTypeName = (formName: string): string => { return `${formName}InputValues`; }; @@ -64,7 +64,8 @@ export const generateTypeNodeFromObject = (obj: Node): Pr const value: TypeNode = typeof child === 'object' && Object.keys(obj[key]).length ? factory.createTypeLiteralNode(generateTypeNodeFromObject(child)) - : factory.createTypeReferenceNode(factory.createIdentifier('ValidationFunction'), [ + : factory.createTypeReferenceNode(factory.createIdentifier('UseBaseOrValidationType'), [ + factory.createTypeReferenceNode(factory.createIdentifier('useBase'), undefined), factory.createKeywordTypeNode(child as KeywordTypeSyntaxKind), ]); return factory.createPropertySignature( @@ -77,27 +78,31 @@ export const generateTypeNodeFromObject = (obj: Node): Pr }; /** + * this generates the input types for onSubmit, onSuccess, onChange, and onValidate + * onValidate is the one case where it passes true to get the ValidationType + * instead of the base type * - * export declare type MyPostCreateFormInputValues = { - caption: ValidationFunction; - username: ValidationFunction; - post_url: ValidationFunction; - profile_url: ValidationFunction; - status: ValidationFunction; + * export declare type MyPostCreateFormInputValues = { + caption: UseBaseOrValidationType; + phoneNumber: UseBaseOrValidationType; + username: UseBaseOrValidationType; + post_url: UseBaseOrValidationType; + profile_url: UseBaseOrValidationType; + status: UseBaseOrValidationType; bio: { - favoriteQuote: ValidationFunction; + favoriteQuote: UseBaseOrValidationType; favoiteAnimal: { - genus: ValidationFunction; - } - }; - }; + genus: UseBaseOrValidationType; + } + }; + * }; * * * @param formName * @param fieldConfigs * @returns */ -export const generateOnValidationType = (formName: string, fieldConfigs: Record) => { +export const generateInputTypes = (formName: string, fieldConfigs: Record) => { const nestedPaths: [fieldName: string, dataType?: DataFieldDataType][] = []; const typeNodes: TypeElement[] = []; Object.entries(fieldConfigs).forEach(([fieldName, { dataType }]) => { @@ -110,7 +115,8 @@ export const generateOnValidationType = (formName: string, fieldConfigs: Record< undefined, factory.createIdentifier(fieldName), factory.createToken(SyntaxKind.QuestionToken), - factory.createTypeReferenceNode(factory.createIdentifier('ValidationFunction'), [ + factory.createTypeReferenceNode(factory.createIdentifier('UseBaseOrValidationType'), [ + factory.createTypeReferenceNode(factory.createIdentifier('useBase'), undefined), factory.createKeywordTypeNode(getSyntaxKindType(dataType)), ]), ), @@ -127,11 +133,40 @@ export const generateOnValidationType = (formName: string, fieldConfigs: Record< undefined, [factory.createModifier(SyntaxKind.ExportKeyword), factory.createModifier(SyntaxKind.DeclareKeyword)], factory.createIdentifier(getInputValuesTypeName(formName)), - undefined, + [ + factory.createTypeParameterDeclaration( + factory.createIdentifier('useBase'), + factory.createKeywordTypeNode(SyntaxKind.BooleanKeyword), + factory.createLiteralTypeNode(factory.createTrue()), + ), + ], factory.createTypeLiteralNode(typeNodes), ); }; +/** + * used to validate if value should be using the base type or ValdiationFunction using base type + * + * export declare type UseBaseOrValidationType = Flag extends true ? T : ValidationFunction; + */ +export const baseValidationConditionalType = factory.createTypeAliasDeclaration( + undefined, + [factory.createModifier(SyntaxKind.ExportKeyword), factory.createModifier(SyntaxKind.DeclareKeyword)], + factory.createIdentifier('UseBaseOrValidationType'), + [ + factory.createTypeParameterDeclaration(factory.createIdentifier('Flag'), undefined, undefined), + factory.createTypeParameterDeclaration(factory.createIdentifier('T'), undefined, undefined), + ], + factory.createConditionalTypeNode( + factory.createTypeReferenceNode(factory.createIdentifier('Flag'), undefined), + factory.createLiteralTypeNode(factory.createTrue()), + factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined), + factory.createTypeReferenceNode(factory.createIdentifier('ValidationFunction'), [ + factory.createTypeReferenceNode(factory.createIdentifier('T'), undefined), + ]), + ), +); + /** * export declare type ValidationResponse = { * hasError: boolean; @@ -192,7 +227,9 @@ export const buildOnValidateType = (formName: string) => { undefined, factory.createIdentifier('onValidate'), factory.createToken(SyntaxKind.QuestionToken), - factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), undefined), + factory.createTypeReferenceNode(factory.createIdentifier(getInputValuesTypeName(formName)), [ + factory.createLiteralTypeNode(factory.createFalse()), + ]), ); };