From 2893f36cf761d63b2dfd44ad4e75af80f1eaef88 Mon Sep 17 00:00:00 2001 From: Maks Turtiainen Date: Thu, 7 Dec 2023 15:56:56 +0200 Subject: [PATCH] fix: handler application validations (HL-991) --- .../companySection/CompanySection.tsx | 2 +- .../newApplication/utils/validation.ts | 182 +++++++++--------- frontend/shared/src/utils/string.utils.ts | 3 + 3 files changed, 96 insertions(+), 91 deletions(-) diff --git a/frontend/benefit/handler/src/components/newApplication/formContent/companySection/CompanySection.tsx b/frontend/benefit/handler/src/components/newApplication/formContent/companySection/CompanySection.tsx index c458b5fc62..45348d3b2b 100644 --- a/frontend/benefit/handler/src/components/newApplication/formContent/companySection/CompanySection.tsx +++ b/frontend/benefit/handler/src/components/newApplication/formContent/companySection/CompanySection.tsx @@ -427,7 +427,7 @@ const CompanySection: React.FC = ({ <$GridCell $colSpan={8}> { + if ( + organizationType?.toLowerCase() === + ORGANIZATION_TYPES.COMPANY.toLowerCase() + ) { + return typeof val === 'boolean'; + } + return true; + }, }), [APPLICATION_FIELD_KEYS.DE_MINIMIS_AID_SET]: Yup.array().of( getDeminimisValidationSchema(t).nullable() @@ -177,7 +191,7 @@ export const getValidationSchema = ( [APPLICATION_FIELD_KEYS.CO_OPERATION_NEGOTIATIONS_DESCRIPTION]: Yup.string(), [APPLICATION_FIELD_KEYS.PAY_SUBSIDY_GRANTED]: Yup.mixed().oneOf( - [null, ...Object.values(PAY_SUBSIDY_GRANTED)], + Object.values(PAY_SUBSIDY_GRANTED), t(VALIDATION_MESSAGE_KEYS.INVALID) ), [APPLICATION_FIELD_KEYS.APPRENTICESHIP_PROGRAM]: Yup.boolean() @@ -192,6 +206,81 @@ export const getValidationSchema = ( .nullable() .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), }), + [APPLICATION_FIELD_KEYS.EMPLOYEE]: Yup.object().shape({ + [EMPLOYEE_KEYS.FIRST_NAME]: Yup.string() + .matches(NAMES_REGEX, t(VALIDATION_MESSAGE_KEYS.INVALID)) + .max( + MAX_SHORT_STRING_LENGTH, + t(VALIDATION_MESSAGE_KEYS.STRING_MAX, { + max: MAX_LONG_STRING_LENGTH, + }) + ) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.LAST_NAME]: Yup.string() + .matches(NAMES_REGEX, t(VALIDATION_MESSAGE_KEYS.INVALID)) + .max( + MAX_SHORT_STRING_LENGTH, + t(VALIDATION_MESSAGE_KEYS.STRING_MAX, { + max: MAX_LONG_STRING_LENGTH, + }) + ) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.SOCIAL_SECURITY_NUMBER]: Yup.string() + .test({ + message: t(VALIDATION_MESSAGE_KEYS.SSN_INVALID), + test: (val) => (val ? FinnishSSN.validate(val) : true), + }) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.IS_LIVING_IN_HELSINKI]: Yup.boolean().oneOf( + [true], + t('common:applications.sections.fields.isLivingInHelsinki.error') + ), + [EMPLOYEE_KEYS.JOB_TITLE]: Yup.string() + .nullable() + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.WORKING_HOURS]: Yup.number() + .test( + 'is-decimal', + t(VALIDATION_MESSAGE_KEYS.NUMBER_TWO_DECIMALS), + (value: number): boolean => + value ? /^\d+.?\d{1,2}$/.test(String(value)) : false + ) + .transform((_value, originalValue) => + Number(getNumberValue(originalValue)) + ) + .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) + .nullable() + .min(EMPLOYEE_MIN_WORKING_HOURS, (param) => ({ + min: param.min, + key: VALIDATION_MESSAGE_KEYS.NUMBER_MIN, + })) + .max(EMPLOYEE_MAX_WORKING_HOURS, (param) => ({ + max: param.max, + key: VALIDATION_MESSAGE_KEYS.NUMBER_MAX, + })) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.MONTHLY_PAY]: Yup.number() + .transform((_value, originalValue) => + getNumberValueOrNull(originalValue) + ) + .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.VACATION_MONEY]: Yup.number() + .transform((_value, originalValue) => + getNumberValueOrNull(originalValue) + ) + .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.OTHER_EXPENSES]: Yup.number() + .transform((_value, originalValue) => + getNumberValueOrNull(originalValue) + ) + .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) + .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), + [EMPLOYEE_KEYS.COLLECTIVE_BARGAINING_AGREEMENT]: Yup.string().required( + t(VALIDATION_MESSAGE_KEYS.REQUIRED) + ), + }), [APPLICATION_FIELD_KEYS.START_DATE]: Yup.string() .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)) .test({ @@ -203,91 +292,4 @@ export const getValidationSchema = ( [APPLICATION_FIELD_KEYS.END_DATE]: Yup.string().required( t(VALIDATION_MESSAGE_KEYS.REQUIRED) ), - [APPLICATION_FIELD_KEYS.EMPLOYEE]: Yup.object() - .shape({ - [EMPLOYEE_KEYS.FIRST_NAME]: Yup.string() - .matches(NAMES_REGEX, t(VALIDATION_MESSAGE_KEYS.INVALID)) - .max( - MAX_SHORT_STRING_LENGTH, - t(VALIDATION_MESSAGE_KEYS.STRING_MAX, { - max: MAX_LONG_STRING_LENGTH, - }) - ) - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.LAST_NAME]: Yup.string() - .matches(NAMES_REGEX, t(VALIDATION_MESSAGE_KEYS.INVALID)) - .max( - MAX_SHORT_STRING_LENGTH, - t(VALIDATION_MESSAGE_KEYS.STRING_MAX, { - max: MAX_LONG_STRING_LENGTH, - }) - ) - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.SOCIAL_SECURITY_NUMBER]: Yup.string() - .test({ - message: t(VALIDATION_MESSAGE_KEYS.SSN_INVALID), - test: (val) => (val ? FinnishSSN.validate(val) : true), - }) - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.IS_LIVING_IN_HELSINKI]: Yup.boolean().oneOf( - [true], - t('common:applications.sections.fields.isLivingInHelsinki.error') - ), - }) - .when(APPLICATION_FIELD_KEYS.BENEFIT_TYPE, { - is: BENEFIT_TYPES.COMMISSION, - then: Yup.object().shape({ - [EMPLOYEE_KEYS.COMMISSION_DESCRIPTION]: Yup.string() - .nullable() - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.EMPLOYEE_COMMISSION_AMOUNT]: Yup.number() - .transform((_value, originalValue) => - Number(getNumberValue(originalValue)) - ) - .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) - .nullable() - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - }), - otherwise: Yup.object().shape({ - [EMPLOYEE_KEYS.JOB_TITLE]: Yup.string() - .nullable() - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.WORKING_HOURS]: Yup.number() - .test( - 'is-decimal', - t(VALIDATION_MESSAGE_KEYS.NUMBER_TWO_DECIMALS), - (value: number): boolean => - value ? /^\d+.?\d{1,2}$/.test(String(value)) : false - ) - .transform((_value, originalValue) => - Number(getNumberValue(originalValue)) - ) - .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) - .nullable() - .min(EMPLOYEE_MIN_WORKING_HOURS, (param) => ({ - min: param.min, - key: VALIDATION_MESSAGE_KEYS.NUMBER_MIN, - })) - .max(EMPLOYEE_MAX_WORKING_HOURS, (param) => ({ - max: param.max, - key: VALIDATION_MESSAGE_KEYS.NUMBER_MAX, - })) - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.VACATION_MONEY]: Yup.number() - .transform((_value, originalValue) => getNumberValue(originalValue)) - .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) - .nullable() - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.MONTHLY_PAY]: Yup.number() - .transform((_value, originalValue) => getNumberValue(originalValue)) - .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) - .nullable() - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - [EMPLOYEE_KEYS.OTHER_EXPENSES]: Yup.number() - .transform((_value, originalValue) => getNumberValue(originalValue)) - .typeError(t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID)) - .nullable() - .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), - }), - }), }); diff --git a/frontend/shared/src/utils/string.utils.ts b/frontend/shared/src/utils/string.utils.ts index 792e502391..f09fff8add 100644 --- a/frontend/shared/src/utils/string.utils.ts +++ b/frontend/shared/src/utils/string.utils.ts @@ -33,6 +33,9 @@ export const isEmpty = (value?: string): boolean => export const getNumberValue = (s: unknown): number => Number(isString(s) ? s.toString().replace(/,/, '.') : s); +export const getNumberValueOrNull = (s: unknown): number | null => + !s ? null : getNumberValue(s); + export const stringFloatToFixed = (value: string): string => value.includes(',') ? value.slice(0, value.indexOf(',') + 3) : value;