From 11e81fe7e8732b6e2de553eb33b4873f58e2feb1 Mon Sep 17 00:00:00 2001 From: Justin Shih Date: Tue, 26 Jul 2022 15:57:55 -0700 Subject: [PATCH] chore: update validation type to match model --- ...studio-ui-codegen-react-forms.test.ts.snap | 40 +- .../lib/__tests__/forms/validation.test.ts | 136 +- .../react-forms/form-renderer-helper.test.ts | 3 + .../lib/forms/form-renderer-helper.ts | 6 +- .../lib/forms/react-form-renderer.ts | 10 +- .../lib/utils/forms/validation.ts | 1701 +++++++++-------- .../generate-form-definition.test.ts | 16 + .../helpers/map-elements.test.ts | 2 + .../lib/types/form/form-metadata.ts | 1 + .../lib/types/form/form-validation.ts | 8 +- packages/codegen-ui/lib/types/form/index.ts | 1 + .../lib/utils/form-component-metadata.ts | 1 + .../cypress/e2e/generate-spec.cy.ts | 3 +- .../cypress/e2e/primitives-spec.cy.ts | 2 +- ...asic-form-create.json => form-create.json} | 2 +- .../test-generator/lib/forms/form-update.json | 35 + packages/test-generator/lib/forms/index.ts | 3 +- packages/test-generator/lib/index.ts | 1 + 18 files changed, 1111 insertions(+), 860 deletions(-) rename packages/test-generator/lib/forms/{basic-form-create.json => form-create.json} (96%) create mode 100644 packages/test-generator/lib/forms/form-update.json 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 cd0a069e7..55d59ab8e 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 @@ -215,7 +215,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"caption\\"] ? await onValidate[\\"caption\\"](value) - : validateField(value, caption - validation - rules); + : validateField( + value, + undefined - caption - validation - rules + ); setCaptionFieldError({ ...captionFieldError, ...isValidResult }); setFormValid(!captionFieldError.hasError); setModelFields({ ...modelFields, caption: value }); @@ -239,7 +242,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"username\\"] ? await onValidate[\\"username\\"](value) - : validateField(value, username - validation - rules); + : validateField( + value, + undefined - username - validation - rules + ); setUsernameFieldError({ ...usernameFieldError, ...isValidResult, @@ -266,7 +272,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"post_url\\"] ? await onValidate[\\"post_url\\"](value) - : validateField(value, post_url - validation - rules); + : validateField( + value, + undefined - post_url - validation - rules + ); setPost_urlFieldError({ ...post_urlFieldError, ...isValidResult, @@ -293,7 +302,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"profile_url\\"] ? await onValidate[\\"profile_url\\"](value) - : validateField(value, profile_url - validation - rules); + : validateField( + value, + undefined - profile_url - validation - rules + ); setProfile_urlFieldError({ ...profile_urlFieldError, ...isValidResult, @@ -425,7 +437,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"caption\\"] ? await onValidate[\\"caption\\"](value) - : validateField(value, caption - validation - rules); + : validateField( + value, + undefined - caption - validation - rules + ); setCaptionFieldError({ ...captionFieldError, ...isValidResult }); setFormValid(!captionFieldError.hasError); setModelFields({ ...modelFields, caption: value }); @@ -449,7 +464,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"username\\"] ? await onValidate[\\"username\\"](value) - : validateField(value, username - validation - rules); + : validateField( + value, + undefined - username - validation - rules + ); setUsernameFieldError({ ...usernameFieldError, ...isValidResult, @@ -476,7 +494,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"post_url\\"] ? await onValidate[\\"post_url\\"](value) - : validateField(value, post_url - validation - rules); + : validateField( + value, + undefined - post_url - validation - rules + ); setPost_urlFieldError({ ...post_urlFieldError, ...isValidResult, @@ -503,7 +524,10 @@ export default function myPostForm(props) { const { value } = e.target; const isValidResult = onValidate?.[\\"profile_url\\"] ? await onValidate[\\"profile_url\\"](value) - : validateField(value, profile_url - validation - rules); + : validateField( + value, + undefined - profile_url - validation - rules + ); setProfile_urlFieldError({ ...profile_urlFieldError, ...isValidResult, diff --git a/packages/codegen-ui-react/lib/__tests__/forms/validation.test.ts b/packages/codegen-ui-react/lib/__tests__/forms/validation.test.ts index 7c64af0af..f1fc111ac 100644 --- a/packages/codegen-ui-react/lib/__tests__/forms/validation.test.ts +++ b/packages/codegen-ui-react/lib/__tests__/forms/validation.test.ts @@ -32,111 +32,129 @@ describe('validateField tests', () => { }); }); it('should validate START_WITH type', () => { - expect(validateField('abc', [{ type: ValidationTypes.START_WITH, values: [], validationMessage: 'test' }])).toEqual( - { hasError: true, errorMessage: 'test' }, - ); - expect(validateField('abc', [{ type: ValidationTypes.START_WITH, values: ['4'], validationMessage: '' }])).toEqual({ + expect( + validateField('abc', [{ type: ValidationTypes.START_WITH, strValues: [], validationMessage: 'test' }]), + ).toEqual({ hasError: false }); + expect( + validateField('abc', [{ type: ValidationTypes.START_WITH, strValues: ['4'], validationMessage: '' }]), + ).toEqual({ hasError: true, errorMessage: 'The value must start with 4', }); expect( - validateField('aardvark', [{ type: ValidationTypes.START_WITH, values: ['a', 'b'], validationMessage: '' }]), + validateField('aardvark', [{ type: ValidationTypes.START_WITH, strValues: ['a', 'b'], validationMessage: '' }]), ).toEqual({ hasError: false, errorMessage: 'The value must start with a, b' }); }); it('should validate END_WITH type', () => { - expect(validateField('abc', [{ type: ValidationTypes.END_WITH, values: ['c'], validationMessage: '' }])).toEqual({ + expect( + validateField('abc', [{ type: ValidationTypes.END_WITH, strValues: ['c'], validationMessage: 'test' }]), + ).toEqual({ hasError: false, - errorMessage: 'The value must end with c', + errorMessage: 'test', }); expect( - validateField('abc', [{ type: ValidationTypes.END_WITH, values: ['e', 'f'], validationMessage: '' }]), + validateField('abc', [{ type: ValidationTypes.END_WITH, strValues: ['e', 'f'], validationMessage: '' }]), ).toEqual({ hasError: true, errorMessage: 'The value must end with e, f' }); - expect(validateField('', [{ type: ValidationTypes.END_WITH, values: [], validationMessage: 'test' }])).toEqual({ - hasError: true, - errorMessage: 'test', + expect(validateField('', [{ type: ValidationTypes.END_WITH, strValues: [], validationMessage: 'test' }])).toEqual({ + hasError: false, }); }); it('should validate CONTAINS type', () => { - expect(validateField('abc', [{ type: ValidationTypes.CONTAINS, values: ['a'], validationMessage: '' }])).toEqual({ - hasError: false, - errorMessage: 'The value must contain a', - }); + expect(validateField('abc', [{ type: ValidationTypes.CONTAINS, strValues: ['a'], validationMessage: '' }])).toEqual( + { + hasError: false, + errorMessage: 'The value must contain a', + }, + ); expect( - validateField('abcd', [{ type: ValidationTypes.CONTAINS, values: ['a', 'e'], validationMessage: '' }]), + validateField('abcd', [{ type: ValidationTypes.CONTAINS, strValues: ['a', 'e'], validationMessage: '' }]), ).toEqual({ hasError: false, errorMessage: 'The value must contain a, e' }); expect( - validateField('abc', [{ type: ValidationTypes.CONTAINS, values: ['d'], validationMessage: 'test' }]), + validateField('abc', [{ type: ValidationTypes.CONTAINS, strValues: ['d'], validationMessage: 'test' }]), ).toEqual({ hasError: true, errorMessage: 'test' }); }); it('should validate NOT_CONTAINS type', () => { expect( - validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, values: ['4'], validationMessage: '' }]), + validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, strValues: ['4'], validationMessage: '' }]), ).toEqual({ hasError: false, errorMessage: 'The value must not contain 4' }); expect( - validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, values: ['d', 'a'], validationMessage: '' }]), + validateField('abc', [{ type: ValidationTypes.NOT_CONTAINS, strValues: ['d', 'a'], validationMessage: '' }]), ).toEqual({ hasError: true, errorMessage: 'The value must not contain d, a' }); - expect(validateField('', [{ type: ValidationTypes.NOT_CONTAINS, values: [], validationMessage: 'test' }])).toEqual({ + expect( + validateField('', [{ type: ValidationTypes.NOT_CONTAINS, strValues: [], validationMessage: 'test' }]), + ).toEqual({ hasError: false, - errorMessage: 'test', }); }); it('should validate LESS_THAN_CHAR_LENGTH type', () => { expect( - validateField('123', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, values: 4, validationMessage: '' }]), + validateField('123', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [4], validationMessage: '' }]), ).toEqual({ hasError: false, errorMessage: 'The value must be shorter than 4' }); expect( - validateField('', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, values: 0, validationMessage: '' }]), + validateField('', [{ type: ValidationTypes.LESS_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]), ).toEqual({ hasError: true, errorMessage: 'The value must be shorter than 0' }); }); it('should validate GREATER_THAN_CHAR_LENGTH type', () => { expect( - validateField('123', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, values: 0, validationMessage: '' }]), + validateField('123', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]), ).toEqual({ hasError: false, errorMessage: 'The value must be longer than 0' }); expect( - validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, values: 0, validationMessage: '' }]), + validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [0], validationMessage: '' }]), ).toEqual({ hasError: true, errorMessage: 'The value must be longer than 0' }); expect( - validateField('', [{ type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, values: 1, validationMessage: 'test' }]), + validateField('', [ + { type: ValidationTypes.GREATER_THAN_CHAR_LENGTH, numValues: [1], validationMessage: 'test' }, + ]), ).toEqual({ hasError: true, errorMessage: 'test' }); }); it('should validate LESS_THAN_NUM type', () => { - expect(validateField(1, [{ type: ValidationTypes.LESS_THAN_NUM, values: 10, validationMessage: '' }])).toEqual({ - hasError: false, - errorMessage: 'The value must be less than 10', - }); - expect(validateField(2, [{ type: ValidationTypes.LESS_THAN_NUM, values: 1, validationMessage: '' }])).toEqual({ + expect(validateField(1, [{ type: ValidationTypes.LESS_THAN_NUM, numValues: [10], validationMessage: '' }])).toEqual( + { + hasError: false, + errorMessage: 'The value must be less than 10', + }, + ); + expect(validateField(2, [{ type: ValidationTypes.LESS_THAN_NUM, numValues: [1], validationMessage: '' }])).toEqual({ hasError: true, errorMessage: 'The value must be less than 1', }); - expect(validateField(0, [{ type: ValidationTypes.LESS_THAN_NUM, values: 0, validationMessage: 'test' }])).toEqual({ + expect( + validateField(0, [{ type: ValidationTypes.LESS_THAN_NUM, numValues: [0], validationMessage: 'test' }]), + ).toEqual({ hasError: true, errorMessage: 'test', }); }); it('should validate GREATER_THAN_NUM type', () => { - expect(validateField(1, [{ type: ValidationTypes.GREATER_THAN_NUM, values: 0, validationMessage: '' }])).toEqual({ + expect( + validateField(1, [{ type: ValidationTypes.GREATER_THAN_NUM, numValues: [0], validationMessage: '' }]), + ).toEqual({ hasError: false, errorMessage: 'The value must be greater than 0', }); - expect(validateField(2, [{ type: ValidationTypes.GREATER_THAN_NUM, values: 3, validationMessage: '' }])).toEqual({ + expect( + validateField(2, [{ type: ValidationTypes.GREATER_THAN_NUM, numValues: [3], validationMessage: '' }]), + ).toEqual({ hasError: true, errorMessage: 'The value must be greater than 3', }); expect( - validateField(3, [{ type: ValidationTypes.GREATER_THAN_NUM, values: 3, validationMessage: 'test' }]), + validateField(3, [{ type: ValidationTypes.GREATER_THAN_NUM, numValues: [3], validationMessage: 'test' }]), ).toEqual({ hasError: true, errorMessage: 'test' }); }); it('should validate EQUAL_TO_NUM type', () => { - expect(validateField(1, [{ type: ValidationTypes.EQUAL_TO_NUM, values: [1, 2], validationMessage: '' }])).toEqual({ + expect( + validateField(1, [{ type: ValidationTypes.EQUAL_TO_NUM, numValues: [1, 2], validationMessage: '' }]), + ).toEqual({ hasError: false, errorMessage: 'The value must be equal to 1 or 2', }); - expect(validateField(2, [{ type: ValidationTypes.EQUAL_TO_NUM, values: 3, validationMessage: '' }])).toEqual({ + expect(validateField(2, [{ type: ValidationTypes.EQUAL_TO_NUM, numValues: [3], validationMessage: '' }])).toEqual({ hasError: true, errorMessage: 'The value must be equal to 3', }); expect( - validateField(3, [{ type: ValidationTypes.EQUAL_TO_NUM, values: [4, 5, 6], validationMessage: 'test' }]), + validateField(3, [{ type: ValidationTypes.EQUAL_TO_NUM, numValues: [4, 5, 6], validationMessage: 'test' }]), ).toEqual({ hasError: true, errorMessage: 'test' }); }); it('should validate BE_AFTER type', () => { @@ -144,25 +162,31 @@ describe('validateField tests', () => { const endDate1 = new Date('2021-01-09').toDateString(); const endDate2 = new Date('3000-01-09').toDateString(); expect( - validateField(startDate, [{ type: ValidationTypes.BE_AFTER, values: endDate1, validationMessage: '' }]), + validateField(startDate, [{ type: ValidationTypes.BE_AFTER, strValues: [endDate1], validationMessage: '' }]), ).toEqual({ hasError: false, errorMessage: `The value must be after ${endDate1}` }); expect( - validateField(startDate, [{ type: ValidationTypes.BE_AFTER, values: endDate2, validationMessage: '' }]), + validateField(startDate, [{ type: ValidationTypes.BE_AFTER, strValues: [endDate2], validationMessage: '' }]), ).toEqual({ hasError: true, errorMessage: `The value must be after ${endDate2}` }); expect( - validateField(startDate, [{ type: ValidationTypes.BE_AFTER, values: '', validationMessage: 'test' }]), + validateField(startDate, [{ type: ValidationTypes.BE_AFTER, strValues: [''], validationMessage: 'test' }]), ).toEqual({ hasError: true, errorMessage: 'test' }); const startTime = Date.now(); const endTime1 = startTime - 10; expect( - validateField(startTime, [{ type: ValidationTypes.BE_AFTER, values: endTime1, validationMessage: '' }]), + validateField(startTime, [ + { type: ValidationTypes.BE_AFTER, strValues: [endTime1.toString()], validationMessage: '' }, + ]), ).toEqual({ hasError: false, errorMessage: `The value must be after ${endTime1}` }); expect( - validateField(endTime1, [{ type: ValidationTypes.BE_AFTER, values: startTime, validationMessage: '' }]), + validateField(endTime1, [ + { type: ValidationTypes.BE_AFTER, strValues: [startTime.toString()], validationMessage: '' }, + ]), ).toEqual({ hasError: true, errorMessage: `The value must be after ${startTime}` }); expect( - validateField(endTime1, [{ type: ValidationTypes.BE_AFTER, values: startTime, validationMessage: 'test' }]), + validateField(endTime1, [ + { type: ValidationTypes.BE_AFTER, strValues: [startTime.toString()], validationMessage: 'test' }, + ]), ).toEqual({ hasError: true, errorMessage: 'test' }); }); it('should validate BE_BEFORE type', () => { @@ -171,25 +195,37 @@ describe('validateField tests', () => { const endDate2 = new Date('2021-01-09').toString(); expect( - validateField(startDate, [{ type: ValidationTypes.BE_BEFORE, values: endDate1, validationMessage: '' }]), + validateField(startDate, [ + { type: ValidationTypes.BE_BEFORE, strValues: [endDate1.toString()], validationMessage: '' }, + ]), ).toEqual({ hasError: false, errorMessage: `The value must be before ${endDate1}` }); expect( - validateField(startDate, [{ type: ValidationTypes.BE_BEFORE, values: endDate2, validationMessage: '' }]), + validateField(startDate, [ + { type: ValidationTypes.BE_BEFORE, strValues: [endDate2.toString()], validationMessage: '' }, + ]), ).toEqual({ hasError: true, errorMessage: `The value must be before ${endDate2}` }); expect( - validateField(startDate, [{ type: ValidationTypes.BE_BEFORE, values: endDate2, validationMessage: 'test' }]), + validateField(startDate, [ + { type: ValidationTypes.BE_BEFORE, strValues: [endDate2.toString()], validationMessage: 'test' }, + ]), ).toEqual({ hasError: true, errorMessage: 'test' }); const startTime = Date.now(); const endTime1 = startTime + 10; expect( - validateField(startTime, [{ type: ValidationTypes.BE_BEFORE, values: endTime1, validationMessage: '' }]), + validateField(startTime, [ + { type: ValidationTypes.BE_BEFORE, strValues: [endTime1.toString()], validationMessage: '' }, + ]), ).toEqual({ hasError: false, errorMessage: `The value must be before ${endTime1}` }); expect( - validateField(endTime1, [{ type: ValidationTypes.BE_BEFORE, values: startTime, validationMessage: '' }]), + validateField(endTime1, [ + { type: ValidationTypes.BE_BEFORE, strValues: [startTime.toString()], validationMessage: '' }, + ]), ).toEqual({ hasError: true, errorMessage: `The value must be before ${startTime}` }); expect( - validateField(endTime1, [{ type: ValidationTypes.BE_BEFORE, values: startTime, validationMessage: 'test' }]), + validateField(endTime1, [ + { type: ValidationTypes.BE_BEFORE, strValues: [startTime.toString()], validationMessage: 'test' }, + ]), ).toEqual({ hasError: true, errorMessage: 'test' }); }); it('should validate EMAIL type', () => { diff --git a/packages/codegen-ui-react/lib/__tests__/react-forms/form-renderer-helper.test.ts b/packages/codegen-ui-react/lib/__tests__/react-forms/form-renderer-helper.test.ts index 0507d52b4..055114649 100644 --- a/packages/codegen-ui-react/lib/__tests__/react-forms/form-renderer-helper.test.ts +++ b/packages/codegen-ui-react/lib/__tests__/react-forms/form-renderer-helper.test.ts @@ -31,6 +31,7 @@ describe('form-render utils', () => { it('should generate a datastore function', () => { const form: StudioForm = { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Post' }, @@ -47,6 +48,7 @@ describe('form-render utils', () => { it('should generate before & complete types if datastore config is set', () => { const form: StudioForm = { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Post' }, @@ -62,6 +64,7 @@ describe('form-render utils', () => { it('should generate regular onsubmit if dataSourceType is custom', () => { const form: StudioForm = { + id: '123', name: 'myCustomForm', formActionType: 'create', dataType: { dataSourceType: 'Custom', dataTypeName: 'Custom' }, 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 cc713eb48..d661d3bbc 100644 --- a/packages/codegen-ui-react/lib/forms/form-renderer-helper.ts +++ b/packages/codegen-ui-react/lib/forms/form-renderer-helper.ts @@ -316,7 +316,7 @@ export const buildStateMutationStatement = (name: string, defaultValue: Expressi ); }; -export const buildOnChangeStatement = (fieldName: string) => { +export const buildOnChangeStatement = (fieldName: string, id: string) => { return factory.createJsxAttribute( factory.createIdentifier('onChange'), factory.createJsxExpression( @@ -386,7 +386,7 @@ export const buildOnChangeStatement = (fieldName: string) => { factory.createToken(SyntaxKind.ColonToken), factory.createCallExpression(factory.createIdentifier('validateField'), undefined, [ factory.createIdentifier('value'), - factory.createIdentifier(`${fieldName}-validation-rules`), + factory.createIdentifier(`${id}-${fieldName}-validation-rules`), ]), ), ), @@ -449,7 +449,7 @@ export const addFormAttributes = ( const attributes = []; if (component.componentType.includes('Field')) { if (componentMetadata.formMetadata?.onChangeFields.includes(component.name)) { - attributes.push(buildOnChangeStatement(component.name)); + attributes.push(buildOnChangeStatement(component.name, componentMetadata.formMetadata.id)); } attributes.push( factory.createJsxAttribute( 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 dbd975a46..218478e12 100644 --- a/packages/codegen-ui-react/lib/forms/react-form-renderer.ts +++ b/packages/codegen-ui-react/lib/forms/react-form-renderer.ts @@ -128,7 +128,10 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer< if (this.componentMetadata.formMetadata?.onValidationFields) { Object.entries(this.componentMetadata.formMetadata?.onValidationFields).forEach( ([fieldName, validationRules]) => { - result = result.replace(`${fieldName}-validation-rules`, JSON.stringify(validationRules)); + result = result.replace( + `${this.componentMetadata.formMetadata?.id}-${fieldName}-validation-rules`, + JSON.stringify(validationRules), + ); }, ); } @@ -174,7 +177,10 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer< if (this.componentMetadata.formMetadata?.onValidationFields) { Object.entries(this.componentMetadata.formMetadata?.onValidationFields).forEach( ([fieldName, validationRules]) => { - componentText = componentText.replace(`${fieldName}-validation-rules`, JSON.stringify(validationRules)); + componentText = componentText.replace( + `${this.componentMetadata.formMetadata?.id}-${fieldName}-validation-rules`, + JSON.stringify(validationRules), + ); }, ); } diff --git a/packages/codegen-ui-react/lib/utils/forms/validation.ts b/packages/codegen-ui-react/lib/utils/forms/validation.ts index 899b1132d..44910cb9d 100644 --- a/packages/codegen-ui-react/lib/utils/forms/validation.ts +++ b/packages/codegen-ui-react/lib/utils/forms/validation.ts @@ -5,77 +5,85 @@ type ValidationResponse = { hasError: boolean; errorMessage?: string }; export const validateField = ( value: any, - validations: { type: string; values?: any; validationMessage: string }[], + validations: { type: string; strValues?: string[]; numValues?: number[]; validationMessage: string }[], ): ValidationResponse => { for (const validation of validations) { + if (validation.numValues?.length) { + switch (validation.type) { + case 'LessThanChar': + return { + hasError: !(value.length < validation.numValues[0]), + errorMessage: validation.validationMessage || `The value must be shorter than ${validation.numValues[0]}`, + }; + case 'GreaterThanChar': + return { + hasError: !(value.length > validation.numValues[0]), + errorMessage: validation.validationMessage || `The value must be longer than ${validation.numValues[0]}`, + }; + case 'LessThanNum': + return { + hasError: !(value < validation.numValues[0]), + errorMessage: validation.validationMessage || `The value must be less than ${validation.numValues[0]}`, + }; + case 'GreaterThanNum': + return { + hasError: !(value > validation.numValues[0]), + errorMessage: validation.validationMessage || `The value must be greater than ${validation.numValues[0]}`, + }; + case 'EqualTo': + return { + hasError: !validation.numValues.some((el) => el === value), + errorMessage: + validation.validationMessage || `The value must be equal to ${validation.numValues.join(' or ')}`, + }; + default: + } + } else if (validation.strValues?.length) { + switch (validation.type) { + case 'StartWith': + return { + hasError: !validation.strValues.some((el: any) => value.startsWith(el)), + errorMessage: + validation.validationMessage || `The value must start with ${validation.strValues.join(', ')}`, + }; + case 'EndWith': + return { + hasError: !validation.strValues.some((el: any) => value.endsWith(el)), + errorMessage: validation.validationMessage || `The value must end with ${validation.strValues.join(', ')}`, + }; + case 'Contains': + return { + hasError: !validation.strValues.some((el: any) => value.includes(el)), + errorMessage: validation.validationMessage || `The value must contain ${validation.strValues.join(', ')}`, + }; + case 'NotContains': + return { + hasError: !validation.strValues.every((el: any) => !value.includes(el)), + errorMessage: + validation.validationMessage || `The value must not contain ${validation.strValues.join(', ')}`, + }; + case 'BeAfter': + const afterTimeValue = parseInt(validation.strValues[0]); + const afterTimeValidator = Number.isNaN(afterTimeValue) ? validation.strValues[0] : afterTimeValue; + return { + hasError: !(new Date(value) > new Date(afterTimeValidator)), + errorMessage: validation.validationMessage || `The value must be after ${validation.strValues[0]}`, + }; + case 'BeBefore': + const beforeTimeValue = parseInt(validation.strValues[0]); + const beforeTimevalue = Number.isNaN(beforeTimeValue) ? validation.strValues[0] : beforeTimeValue; + return { + hasError: !(new Date(value) < new Date(beforeTimevalue)), + errorMessage: validation.validationMessage || `The value must be before ${validation.strValues[0]}`, + }; + } + } switch (validation.type) { case 'Required': return { hasError: value === undefined || value === '', errorMessage: validation.validationMessage || 'The value is required', }; - case 'StartWith': - return { - hasError: !validation.values?.some((el: any) => value.startsWith(el)), - errorMessage: validation.validationMessage || `The value must start with ${validation.values?.join(', ')}`, - }; - case 'EndWith': - return { - hasError: !validation.values?.some((el: any) => value.endsWith(el)), - errorMessage: validation.validationMessage || `The value must end with ${validation.values?.join(', ')}`, - }; - case 'Contains': - return { - hasError: !validation.values?.some((el: any) => value.includes(el)), - errorMessage: validation.validationMessage || `The value must contain ${validation.values?.join(', ')}`, - }; - case 'NotContains': - return { - hasError: !validation.values?.every((el: any) => !value.includes(el)), - errorMessage: validation.validationMessage || `The value must not contain ${validation.values?.join(', ')}`, - }; - case 'LessThanChar': - return { - hasError: !(value.length < validation.values), - errorMessage: validation.validationMessage || `The value must be shorter than ${validation.values}`, - }; - case 'GreaterThanChar': - return { - hasError: !(value.length > validation.values), - errorMessage: validation.validationMessage || `The value must be longer than ${validation.values}`, - }; - case 'LessThanNum': - return { - hasError: !(value < validation.values), - errorMessage: validation.validationMessage || `The value must be less than ${validation.values}`, - }; - case 'GreaterThanNum': - return { - hasError: !(value > validation.values), - errorMessage: validation.validationMessage || `The value must be greater than ${validation.values}`, - }; - case 'EqualTo': - if (Array.isArray(validation.values)) { - return { - hasError: !validation.values?.some((el) => el === value), - errorMessage: - validation.validationMessage || `The value must be equal to ${validation.values?.join(' or ')}`, - }; - } - return { - hasError: !(value === validation.values), - errorMessage: validation.validationMessage || `The value must be equal to ${validation.values}`, - }; - case 'BeAfter': - return { - hasError: !(new Date(value) > new Date(validation.values)), - errorMessage: validation.validationMessage || `The value must be after ${validation.values}`, - }; - case 'BeBefore': - return { - hasError: !(new Date(value) < new Date(validation.values)), - errorMessage: validation.validationMessage || `The value must be before ${validation.values}`, - }; case 'Email': const EMAIL_ADDRESS_REGEX = /^[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-*\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/; @@ -180,9 +188,15 @@ export const generateValidationFunction = () => { ), factory.createPropertySignature( undefined, - factory.createIdentifier('values'), + factory.createIdentifier('strValues'), + factory.createToken(ts.SyntaxKind.QuestionToken), + factory.createArrayTypeNode(factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)), + ), + factory.createPropertySignature( + undefined, + factory.createIdentifier('numValues'), factory.createToken(ts.SyntaxKind.QuestionToken), - factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), + factory.createArrayTypeNode(factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword)), ), factory.createPropertySignature( undefined, @@ -215,838 +229,961 @@ export const generateValidationFunction = () => { factory.createIdentifier('validations'), factory.createBlock( [ - factory.createSwitchStatement( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('type'), + factory.createIfStatement( + factory.createPropertyAccessChain( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createToken(ts.SyntaxKind.QuestionDotToken), + factory.createIdentifier('length'), ), - factory.createCaseBlock([ - factory.createCaseClause(factory.createStringLiteral('Required'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createBinaryExpression( - factory.createBinaryExpression( - factory.createIdentifier('value'), - factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), - factory.createIdentifier('undefined'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createBinaryExpression( - factory.createIdentifier('value'), - factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), - factory.createStringLiteral(''), - ), - ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createStringLiteral('The value is required'), - ), - ), - ], - true, + factory.createBlock( + [ + factory.createSwitchStatement( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('type'), ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('StartWith'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createCallChain( - factory.createPropertyAccessChain( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createCaseBlock([ + factory.createCaseClause(factory.createStringLiteral('LessThanChar'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( + [ + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression( + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('value'), + factory.createIdentifier('length'), + ), + factory.createToken(ts.SyntaxKind.LessThanToken), + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), + ), + ), + ), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('some'), ), - undefined, - undefined, - [ - factory.createArrowFunction( - undefined, - undefined, - [ - factory.createParameterDeclaration( - undefined, - undefined, - undefined, - factory.createIdentifier('el'), - undefined, - factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), - undefined, - ), - ], - undefined, - factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - factory.createCallExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('value'), - factory.createIdentifier('startsWith'), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be shorter than ', + 'The value must be shorter than ', ), - undefined, - [factory.createIdentifier('el')], + [ + factory.createTemplateSpan( + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), + ), + factory.createTemplateTail('', ''), + ), + ], ), ), - ], - ), + ), + ], + true, ), ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must start with ', - 'The value must start with ', - ), - [ - factory.createTemplateSpan( - factory.createCallChain( - factory.createPropertyAccessChain( + ]), + factory.createCaseClause(factory.createStringLiteral('GreaterThanChar'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( + [ + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression( + factory.createBinaryExpression( factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createIdentifier('value'), + factory.createIdentifier('length'), + ), + factory.createToken(ts.SyntaxKind.GreaterThanToken), + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('join'), ), - undefined, - undefined, - [factory.createStringLiteral(', ')], ), - factory.createTemplateTail('', ''), - ), - ], - ), - ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('EndWith'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createCallChain( - factory.createPropertyAccessChain( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('some'), ), - undefined, - undefined, - [ - factory.createArrowFunction( - undefined, - undefined, - [ - factory.createParameterDeclaration( - undefined, - undefined, - undefined, - factory.createIdentifier('el'), - undefined, - factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), - undefined, - ), - ], - undefined, - factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - factory.createCallExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('value'), - factory.createIdentifier('endsWith'), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be longer than ', + 'The value must be longer than ', ), - undefined, - [factory.createIdentifier('el')], + [ + factory.createTemplateSpan( + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), + ), + factory.createTemplateTail('', ''), + ), + ], ), ), - ], - ), + ), + ], + true, ), ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must end with ', - 'The value must end with ', - ), - [ - factory.createTemplateSpan( - factory.createCallChain( - factory.createPropertyAccessChain( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + ]), + factory.createCaseClause(factory.createStringLiteral('LessThanNum'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( + [ + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression( + factory.createBinaryExpression( + factory.createIdentifier('value'), + factory.createToken(ts.SyntaxKind.LessThanToken), + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('join'), ), - undefined, - undefined, - [factory.createStringLiteral(', ')], ), - factory.createTemplateTail('', ''), ), - ], - ), + ), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be less than ', + 'The value must be less than ', + ), + [ + factory.createTemplateSpan( + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), + ), + factory.createTemplateTail('', ''), + ), + ], + ), + ), + ), + ], + true, ), ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('Contains'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createCallChain( - factory.createPropertyAccessChain( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + ]), + factory.createCaseClause(factory.createStringLiteral('GreaterThanNum'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( + [ + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression( + factory.createBinaryExpression( + factory.createIdentifier('value'), + factory.createToken(ts.SyntaxKind.GreaterThanToken), + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), + ), + ), + ), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('some'), ), - undefined, - undefined, - [ - factory.createArrowFunction( - undefined, - undefined, - [ - factory.createParameterDeclaration( - undefined, - undefined, - undefined, - factory.createIdentifier('el'), - undefined, - factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), - undefined, - ), - ], - undefined, - factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - factory.createCallExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('value'), - factory.createIdentifier('includes'), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be greater than ', + 'The value must be greater than ', ), - undefined, - [factory.createIdentifier('el')], + [ + factory.createTemplateSpan( + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createNumericLiteral('0'), + ), + factory.createTemplateTail('', ''), + ), + ], ), ), - ], - ), + ), + ], + true, ), ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must contain ', - 'The value must contain ', - ), - [ - factory.createTemplateSpan( - factory.createCallChain( - factory.createPropertyAccessChain( + ]), + factory.createCaseClause(factory.createStringLiteral('EqualTo'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( + [ + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createCallExpression( + factory.createPropertyAccessExpression( factory.createPropertyAccessExpression( factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createIdentifier('numValues'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('join'), + factory.createIdentifier('some'), ), undefined, - undefined, - [factory.createStringLiteral(', ')], + [ + factory.createArrowFunction( + undefined, + undefined, + [ + factory.createParameterDeclaration( + undefined, + undefined, + undefined, + factory.createIdentifier('el'), + undefined, + undefined, + undefined, + ), + ], + undefined, + factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + factory.createBinaryExpression( + factory.createIdentifier('el'), + factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), + factory.createIdentifier('value'), + ), + ), + ], ), - factory.createTemplateTail('', ''), ), - ], - ), + ), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be equal to ', + 'The value must be equal to ', + ), + [ + factory.createTemplateSpan( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('numValues'), + ), + factory.createIdentifier('join'), + ), + undefined, + [factory.createStringLiteral(' or ')], + ), + factory.createTemplateTail('', ''), + ), + ], + ), + ), + ), + ], + true, ), ), - ], - true, - ), + ]), + factory.createDefaultClause([]), + ]), ), - ]), - factory.createCaseClause(factory.createStringLiteral('NotContains'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createCallChain( - factory.createPropertyAccessChain( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('every'), - ), - undefined, - undefined, + ], + true, + ), + factory.createIfStatement( + factory.createPropertyAccessChain( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createToken(ts.SyntaxKind.QuestionDotToken), + factory.createIdentifier('length'), + ), + factory.createBlock( + [ + factory.createSwitchStatement( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('type'), + ), + factory.createCaseBlock([ + factory.createCaseClause(factory.createStringLiteral('StartWith'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( [ - factory.createArrowFunction( - undefined, - undefined, - [ - factory.createParameterDeclaration( - undefined, - undefined, - undefined, - factory.createIdentifier('el'), - undefined, - factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), - undefined, - ), - ], - undefined, - factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), factory.createPrefixUnaryExpression( ts.SyntaxKind.ExclamationToken, factory.createCallExpression( factory.createPropertyAccessExpression( - factory.createIdentifier('value'), - factory.createIdentifier('includes'), + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('some'), ), undefined, - [factory.createIdentifier('el')], + [ + factory.createArrowFunction( + undefined, + undefined, + [ + factory.createParameterDeclaration( + undefined, + undefined, + undefined, + factory.createIdentifier('el'), + undefined, + factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), + undefined, + ), + ], + undefined, + factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('value'), + factory.createIdentifier('startsWith'), + ), + undefined, + [factory.createIdentifier('el')], + ), + ), + ], + ), + ), + ), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must start with ', + 'The value must start with ', + ), + [ + factory.createTemplateSpan( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('join'), + ), + undefined, + [factory.createStringLiteral(', ')], + ), + factory.createTemplateTail('', ''), + ), + ], ), ), ), ], + true, ), ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must not contain ', - 'The value must not contain ', - ), + ]), + factory.createCaseClause(factory.createStringLiteral('EndWith'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( [ - factory.createTemplateSpan( - factory.createCallChain( - factory.createPropertyAccessChain( + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createCallExpression( factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('some'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('join'), + undefined, + [ + factory.createArrowFunction( + undefined, + undefined, + [ + factory.createParameterDeclaration( + undefined, + undefined, + undefined, + factory.createIdentifier('el'), + undefined, + factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), + undefined, + ), + ], + undefined, + factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('value'), + factory.createIdentifier('endsWith'), + ), + undefined, + [factory.createIdentifier('el')], + ), + ), + ], ), - undefined, - undefined, - [factory.createStringLiteral(', ')], ), - factory.createTemplateTail('', ''), ), - ], - ), - ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('LessThanChar'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('value'), - factory.createIdentifier('length'), - ), - factory.createToken(ts.SyntaxKind.LessThanToken), - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - ), - ), - ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be shorter than ', - 'The value must be shorter than ', - ), - [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must end with ', + 'The value must end with ', + ), + [ + factory.createTemplateSpan( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('join'), + ), + undefined, + [factory.createStringLiteral(', ')], + ), + factory.createTemplateTail('', ''), + ), + ], + ), ), - factory.createTemplateTail('', ''), ), ], + true, ), ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('GreaterThanChar'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('value'), - factory.createIdentifier('length'), - ), - factory.createToken(ts.SyntaxKind.GreaterThanToken), - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - ), - ), - ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be longer than ', - 'The value must be longer than ', - ), + ]), + factory.createCaseClause(factory.createStringLiteral('Contains'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('some'), + ), + undefined, + [ + factory.createArrowFunction( + undefined, + undefined, + [ + factory.createParameterDeclaration( + undefined, + undefined, + undefined, + factory.createIdentifier('el'), + undefined, + factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), + undefined, + ), + ], + undefined, + factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('value'), + factory.createIdentifier('includes'), + ), + undefined, + [factory.createIdentifier('el')], + ), + ), + ], + ), ), - factory.createTemplateTail('', ''), ), - ], - ), - ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('LessThanNum'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createIdentifier('value'), - factory.createToken(ts.SyntaxKind.LessThanToken), - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must contain ', + 'The value must contain ', + ), + [ + factory.createTemplateSpan( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('join'), + ), + undefined, + [factory.createStringLiteral(', ')], + ), + factory.createTemplateTail('', ''), + ), + ], + ), + ), ), - ), + ], + true, ), ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be less than ', - 'The value must be less than ', - ), + ]), + factory.createCaseClause(factory.createStringLiteral('NotContains'), [ + factory.createReturnStatement( + factory.createObjectLiteralExpression( [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('every'), + ), + undefined, + [ + factory.createArrowFunction( + undefined, + undefined, + [ + factory.createParameterDeclaration( + undefined, + undefined, + undefined, + factory.createIdentifier('el'), + undefined, + factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword), + undefined, + ), + ], + undefined, + factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('value'), + factory.createIdentifier('includes'), + ), + undefined, + [factory.createIdentifier('el')], + ), + ), + ), + ], + ), ), - factory.createTemplateTail('', ''), ), - ], - ), - ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('GreaterThanNum'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createIdentifier('value'), - factory.createToken(ts.SyntaxKind.GreaterThanToken), - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must not contain ', + 'The value must not contain ', + ), + [ + factory.createTemplateSpan( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createIdentifier('join'), + ), + undefined, + [factory.createStringLiteral(', ')], + ), + factory.createTemplateTail('', ''), + ), + ], + ), + ), ), - ), - ), - ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), + ], + true, ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be greater than ', - 'The value must be greater than ', - ), + ), + ]), + factory.createCaseClause(factory.createStringLiteral('BeAfter'), [ + factory.createVariableStatement( + undefined, + factory.createVariableDeclarationList( [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createVariableDeclaration( + factory.createIdentifier('afterTimeValue'), + undefined, + undefined, + factory.createCallExpression( + factory.createIdentifier('parseInt'), + undefined, + [ + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createNumericLiteral('0'), + ), + ], ), - factory.createTemplateTail('', ''), ), ], + ts.NodeFlags.Const, ), ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('EqualTo'), [ - factory.createIfStatement( - factory.createCallExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('Array'), - factory.createIdentifier('isArray'), - ), - undefined, - [ - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - ], - ), - factory.createBlock( - [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createCallChain( - factory.createPropertyAccessChain( + factory.createVariableStatement( + undefined, + factory.createVariableDeclarationList( + [ + factory.createVariableDeclaration( + factory.createIdentifier('afterTimeValidator'), + undefined, + undefined, + factory.createConditionalExpression( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('Number'), + factory.createIdentifier('isNaN'), + ), + undefined, + [factory.createIdentifier('afterTimeValue')], + ), + factory.createToken(ts.SyntaxKind.QuestionToken), + factory.createElementAccessExpression( factory.createPropertyAccessExpression( factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createIdentifier('strValues'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('some'), + factory.createNumericLiteral('0'), ), - undefined, - undefined, - [ - factory.createArrowFunction( - undefined, - undefined, - [ - factory.createParameterDeclaration( - undefined, - undefined, - undefined, - factory.createIdentifier('el'), - undefined, - undefined, - undefined, - ), - ], - undefined, - factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), - factory.createBinaryExpression( - factory.createIdentifier('el'), - factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), - factory.createIdentifier('value'), + factory.createToken(ts.SyntaxKind.ColonToken), + factory.createIdentifier('afterTimeValue'), + ), + ), + ], + ts.NodeFlags.Const, + ), + ), + factory.createReturnStatement( + factory.createObjectLiteralExpression( + [ + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression( + factory.createBinaryExpression( + factory.createNewExpression( + factory.createIdentifier('Date'), + undefined, + [factory.createIdentifier('value')], + ), + factory.createToken(ts.SyntaxKind.GreaterThanToken), + factory.createNewExpression( + factory.createIdentifier('Date'), + undefined, + [factory.createIdentifier('afterTimeValidator')], ), ), - ], + ), ), ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be equal to ', - 'The value must be equal to ', + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), ), - [ - factory.createTemplateSpan( - factory.createCallChain( - factory.createPropertyAccessChain( + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be after ', + 'The value must be after ', + ), + [ + factory.createTemplateSpan( + factory.createElementAccessExpression( factory.createPropertyAccessExpression( factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createIdentifier('strValues'), ), - factory.createToken(ts.SyntaxKind.QuestionDotToken), - factory.createIdentifier('join'), + factory.createNumericLiteral('0'), ), - undefined, - undefined, - [factory.createStringLiteral(' or ')], + factory.createTemplateTail('', ''), ), - factory.createTemplateTail('', ''), - ), - ], + ], + ), ), ), - ), - ], - true, - ), - ), - ], - true, - ), - undefined, - ), - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createIdentifier('value'), - factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - ), + ], + true, ), ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be equal to ', - 'The value must be equal to ', - ), + ]), + factory.createCaseClause(factory.createStringLiteral('BeBefore'), [ + factory.createVariableStatement( + undefined, + factory.createVariableDeclarationList( [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createVariableDeclaration( + factory.createIdentifier('beforeTimeValue'), + undefined, + undefined, + factory.createCallExpression( + factory.createIdentifier('parseInt'), + undefined, + [ + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createNumericLiteral('0'), + ), + ], ), - factory.createTemplateTail('', ''), ), ], + ts.NodeFlags.Const, ), ), - ), - ], - true, - ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('BeAfter'), [ - factory.createReturnStatement( - factory.createObjectLiteralExpression( - [ - factory.createPropertyAssignment( - factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createNewExpression(factory.createIdentifier('Date'), undefined, [ - factory.createIdentifier('value'), - ]), - factory.createToken(ts.SyntaxKind.GreaterThanToken), - factory.createNewExpression(factory.createIdentifier('Date'), undefined, [ - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createVariableStatement( + undefined, + factory.createVariableDeclarationList( + [ + factory.createVariableDeclaration( + factory.createIdentifier('beforeTimevalue'), + undefined, + undefined, + factory.createConditionalExpression( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('Number'), + factory.createIdentifier('isNaN'), + ), + undefined, + [factory.createIdentifier('beforeTimeValue')], + ), + factory.createToken(ts.SyntaxKind.QuestionToken), + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createNumericLiteral('0'), + ), + factory.createToken(ts.SyntaxKind.ColonToken), + factory.createIdentifier('beforeTimeValue'), ), - ]), - ), + ), + ], + ts.NodeFlags.Const, ), ), - ), - factory.createPropertyAssignment( - factory.createIdentifier('errorMessage'), - factory.createBinaryExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('validationMessage'), - ), - factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be after ', - 'The value must be after ', - ), + factory.createReturnStatement( + factory.createObjectLiteralExpression( [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), + factory.createPropertyAssignment( + factory.createIdentifier('hasError'), + factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression( + factory.createBinaryExpression( + factory.createNewExpression( + factory.createIdentifier('Date'), + undefined, + [factory.createIdentifier('value')], + ), + factory.createToken(ts.SyntaxKind.LessThanToken), + factory.createNewExpression( + factory.createIdentifier('Date'), + undefined, + [factory.createIdentifier('beforeTimevalue')], + ), + ), + ), + ), + ), + factory.createPropertyAssignment( + factory.createIdentifier('errorMessage'), + factory.createBinaryExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('validationMessage'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createTemplateExpression( + factory.createTemplateHead( + 'The value must be before ', + 'The value must be before ', + ), + [ + factory.createTemplateSpan( + factory.createElementAccessExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('strValues'), + ), + factory.createNumericLiteral('0'), + ), + factory.createTemplateTail('', ''), + ), + ], + ), ), - factory.createTemplateTail('', ''), ), ], + true, ), ), - ), - ], - true, + ]), + ]), ), - ), - ]), - factory.createCaseClause(factory.createStringLiteral('BeBefore'), [ + ], + true, + ), + undefined, + ), + ), + factory.createSwitchStatement( + factory.createPropertyAccessExpression( + factory.createIdentifier('validation'), + factory.createIdentifier('type'), + ), + factory.createCaseBlock([ + factory.createCaseClause(factory.createStringLiteral('Required'), [ factory.createReturnStatement( factory.createObjectLiteralExpression( [ factory.createPropertyAssignment( factory.createIdentifier('hasError'), - factory.createPrefixUnaryExpression( - ts.SyntaxKind.ExclamationToken, - factory.createParenthesizedExpression( - factory.createBinaryExpression( - factory.createNewExpression(factory.createIdentifier('Date'), undefined, [ - factory.createIdentifier('value'), - ]), - factory.createToken(ts.SyntaxKind.LessThanToken), - factory.createNewExpression(factory.createIdentifier('Date'), undefined, [ - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - ]), - ), + factory.createBinaryExpression( + factory.createBinaryExpression( + factory.createIdentifier('value'), + factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), + factory.createIdentifier('undefined'), + ), + factory.createToken(ts.SyntaxKind.BarBarToken), + factory.createBinaryExpression( + factory.createIdentifier('value'), + factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken), + factory.createStringLiteral(''), ), ), ), @@ -1058,21 +1195,7 @@ export const generateValidationFunction = () => { factory.createIdentifier('validationMessage'), ), factory.createToken(ts.SyntaxKind.BarBarToken), - factory.createTemplateExpression( - factory.createTemplateHead( - 'The value must be before ', - 'The value must be before ', - ), - [ - factory.createTemplateSpan( - factory.createPropertyAccessExpression( - factory.createIdentifier('validation'), - factory.createIdentifier('values'), - ), - factory.createTemplateTail('', ''), - ), - ], - ), + factory.createStringLiteral('The value is required'), ), ), ], diff --git a/packages/codegen-ui/lib/__tests__/generate-form-definition/generate-form-definition.test.ts b/packages/codegen-ui/lib/__tests__/generate-form-definition/generate-form-definition.test.ts index 8ee32169a..ca8b4f98a 100644 --- a/packages/codegen-ui/lib/__tests__/generate-form-definition/generate-form-definition.test.ts +++ b/packages/codegen-ui/lib/__tests__/generate-form-definition/generate-form-definition.test.ts @@ -19,6 +19,7 @@ describe('generateFormDefinition', () => { it('should map DataStore model fields', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -45,6 +46,7 @@ describe('generateFormDefinition', () => { expect(() => generateFormDefinition({ form: { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -59,6 +61,7 @@ describe('generateFormDefinition', () => { it('should override field configurations from DataStore', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -84,6 +87,7 @@ describe('generateFormDefinition', () => { it('should not add overrides to the matrix', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -105,6 +109,7 @@ describe('generateFormDefinition', () => { it('should add fields that do not exist in DataStore', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -122,6 +127,7 @@ describe('generateFormDefinition', () => { it('should add fields that do not exist in DataStore to the matrix', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -137,6 +143,7 @@ describe('generateFormDefinition', () => { it('should add sectional elements', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'Custom', dataTypeName: 'dfjkajfl' }, @@ -161,6 +168,7 @@ describe('generateFormDefinition', () => { }; const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'Custom', dataTypeName: 'dfsdjflk' }, @@ -175,6 +183,7 @@ describe('generateFormDefinition', () => { it('should not leave empty rows in the matrix', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -210,6 +219,7 @@ describe('generateFormDefinition', () => { it('should correctly map positions', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -246,6 +256,7 @@ describe('generateFormDefinition', () => { it('should requeue if related element is not yet found', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'Custom', dataTypeName: 'dfjslkfj' }, @@ -268,6 +279,7 @@ it('should requeue if related element is not yet found', () => { it('should handle fields without position', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'Custom', dataTypeName: 'fjsldkfj' }, @@ -291,6 +303,7 @@ it('should handle fields without position', () => { it('should fill out styles using defaults', () => { const definitionForFormWithoutStyle = generateFormDefinition({ form: { + id: '123', name: 'mySampleForm', formActionType: 'create', dataType: { dataSourceType: 'Custom', dataTypeName: 'dfkjad' }, @@ -311,6 +324,7 @@ it('should fill out styles using defaults', () => { it('should skip read-only fields without overrides', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -338,6 +352,7 @@ it('should skip read-only fields without overrides', () => { it('should add read-only fields if it has overrides', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -370,6 +385,7 @@ it('should add read-only fields if it has overrides', () => { it('should skip adding id field if it has no overrides', () => { const formDefinition = generateFormDefinition({ form: { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, diff --git a/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts b/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts index e3d60ceac..a2383da78 100644 --- a/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts +++ b/packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/map-elements.test.ts @@ -47,6 +47,7 @@ describe('mapElements', () => { }; const form: StudioForm = { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, @@ -79,6 +80,7 @@ describe('mapElements', () => { const modelFieldsConfigs: ModelFieldsConfigs = {}; const form: StudioForm = { + id: '123', name: 'sampleForm', formActionType: 'create', dataType: { dataSourceType: 'DataStore', dataTypeName: 'Dog' }, diff --git a/packages/codegen-ui/lib/types/form/form-metadata.ts b/packages/codegen-ui/lib/types/form/form-metadata.ts index da7ead534..1b6fe578a 100644 --- a/packages/codegen-ui/lib/types/form/form-metadata.ts +++ b/packages/codegen-ui/lib/types/form/form-metadata.ts @@ -16,6 +16,7 @@ import { FieldValidationConfiguration } from './form-validation'; export type FormMetadata = { + id: string; name: string; fieldState: string; onChangeFields: string[]; diff --git a/packages/codegen-ui/lib/types/form/form-validation.ts b/packages/codegen-ui/lib/types/form/form-validation.ts index 0c412d4a3..fb17fb075 100644 --- a/packages/codegen-ui/lib/types/form/form-validation.ts +++ b/packages/codegen-ui/lib/types/form/form-validation.ts @@ -38,22 +38,22 @@ export type BaseValidation = { export type StringValidationType = { type: ValidationTypes.CONTAINS | ValidationTypes.NOT_CONTAINS | ValidationTypes.END_WITH | ValidationTypes.START_WITH; - values: string[]; + strValues: string[]; } & BaseValidation; export type StringLengthValidationType = { type: ValidationTypes.LESS_THAN_CHAR_LENGTH | ValidationTypes.GREATER_THAN_CHAR_LENGTH; - values: number; + numValues: number[]; } & BaseValidation; export type NumberValidationType = { type: ValidationTypes.LESS_THAN_NUM | ValidationTypes.GREATER_THAN_NUM | ValidationTypes.EQUAL_TO_NUM; - values: number[] | number; + numValues: number[]; } & BaseValidation; export type DateValidationType = { type: ValidationTypes.BE_BEFORE | ValidationTypes.BE_AFTER; - values: string | number; + strValues: string[]; } & BaseValidation; export type GenericValidationType = { diff --git a/packages/codegen-ui/lib/types/form/index.ts b/packages/codegen-ui/lib/types/form/index.ts index ad07a0e3f..371dddfa3 100644 --- a/packages/codegen-ui/lib/types/form/index.ts +++ b/packages/codegen-ui/lib/types/form/index.ts @@ -56,6 +56,7 @@ export type StudioForm = { export * from './form-definition-element'; export * from './style'; +export * from './form-validation'; export type { SectionalElement, diff --git a/packages/codegen-ui/lib/utils/form-component-metadata.ts b/packages/codegen-ui/lib/utils/form-component-metadata.ts index 0b85ac6c7..b6e331ff8 100644 --- a/packages/codegen-ui/lib/utils/form-component-metadata.ts +++ b/packages/codegen-ui/lib/utils/form-component-metadata.ts @@ -22,6 +22,7 @@ export const getFormFieldStateName = (formName: string) => { export const mapFormMetadata = (form: StudioForm, formDefinition: FormDefinition): FormMetadata => { return { + id: form.id, name: form.name, fieldState: getFormFieldStateName(form.name), onChangeFields: Object.entries(formDefinition.elements).reduce((fields, [key, value]) => { diff --git a/packages/test-generator/integration-test-templates/cypress/e2e/generate-spec.cy.ts b/packages/test-generator/integration-test-templates/cypress/e2e/generate-spec.cy.ts index 3c7c97156..e19db918a 100644 --- a/packages/test-generator/integration-test-templates/cypress/e2e/generate-spec.cy.ts +++ b/packages/test-generator/integration-test-templates/cypress/e2e/generate-spec.cy.ts @@ -28,7 +28,8 @@ const EXPECTED_SUCCESSFUL_CASES = new Set([ 'BasicComponentImage', 'BasicComponentText', 'BasicComponentCustomRating', - 'BasicForm', + 'FormCreate', + 'FormUpdate', 'ComponentWithDataBindingWithPredicate', 'ComponentWithDataBindingWithoutPredicate', 'ComponentWithSimplePropertyBinding', diff --git a/packages/test-generator/integration-test-templates/cypress/e2e/primitives-spec.cy.ts b/packages/test-generator/integration-test-templates/cypress/e2e/primitives-spec.cy.ts index 010511678..366b6f1b1 100644 --- a/packages/test-generator/integration-test-templates/cypress/e2e/primitives-spec.cy.ts +++ b/packages/test-generator/integration-test-templates/cypress/e2e/primitives-spec.cy.ts @@ -190,7 +190,7 @@ describe('Primitives', () => { .get('.amplify-pagination') .get('ol') .within(() => { - cy.get('li').eq(1).should('have.text', 'Current Page:1'); + cy.get('li').eq(1).should('have.text', 'Page:1'); cy.get('li').eq(2).should('have.text', '2'); cy.get('li').eq(6).should('have.text', '…'); cy.get('li').eq(7).should('have.text', '10'); diff --git a/packages/test-generator/lib/forms/basic-form-create.json b/packages/test-generator/lib/forms/form-create.json similarity index 96% rename from packages/test-generator/lib/forms/basic-form-create.json rename to packages/test-generator/lib/forms/form-create.json index 3b8a57853..ba837b4c3 100644 --- a/packages/test-generator/lib/forms/basic-form-create.json +++ b/packages/test-generator/lib/forms/form-create.json @@ -1,5 +1,5 @@ { - "name": "BasicFormCreate", + "name": "FormCreate", "formActionType": "create", "dataType": { "dataSourceType": "Custom", diff --git a/packages/test-generator/lib/forms/form-update.json b/packages/test-generator/lib/forms/form-update.json new file mode 100644 index 000000000..5c2acc701 --- /dev/null +++ b/packages/test-generator/lib/forms/form-update.json @@ -0,0 +1,35 @@ +{ + "name": "FormUpdate", + "formActionType": "update", + "dataType": { + "dataSourceType": "Custom", + "dataTypeName": "Post" + }, + "fields": { + "name": { + "inputType": { + "required": true, + "type": "TextField", + "name": "name", + "defaultValue": "John Doe" + }, + "label": "name", + "validations": [ + { + "type": "Required" + } + ] + }, + "email": { + "inputType": { + "required": true, + "type": "TextField", + "name": "email", + "defaultValue": "johndoe@amplify.com" + }, + "label": "E-mail" + } + }, + "sectionalElements": {}, + "style": {} +} \ No newline at end of file diff --git a/packages/test-generator/lib/forms/index.ts b/packages/test-generator/lib/forms/index.ts index d05edd67b..548c096d2 100644 --- a/packages/test-generator/lib/forms/index.ts +++ b/packages/test-generator/lib/forms/index.ts @@ -14,4 +14,5 @@ limitations under the License. */ -export { default as BasicForm } from './basic-form-create.json'; +export { default as FormCreate } from './form-create.json'; +export { default as FormUpdate } from './form-update.json'; diff --git a/packages/test-generator/lib/index.ts b/packages/test-generator/lib/index.ts index 94edeb352..28c71e4b9 100644 --- a/packages/test-generator/lib/index.ts +++ b/packages/test-generator/lib/index.ts @@ -16,3 +16,4 @@ export * from './components'; export * from './generators'; export * from './themes'; +export * from './forms';