[offers][feat] Add positive number validation (#551)

pull/555/head
Ai Ling 2 years ago committed by GitHub
parent 316c696b63
commit 3ee29b1249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,6 +14,7 @@ export const emptyOption = {
export enum FieldError { export enum FieldError {
NON_NEGATIVE_NUMBER = 'Please fill in a non-negative number in this field.', NON_NEGATIVE_NUMBER = 'Please fill in a non-negative number in this field.',
NUMBER = 'Please fill in a number in this field.', NUMBER = 'Please fill in a number in this field.',
POSITIVE_NUMBER = 'Please fill in a positive number in this field.',
REQUIRED = 'Please fill in this field.', REQUIRED = 'Please fill in this field.',
} }

@ -6,6 +6,7 @@ import { FieldError } from '~/components/offers/constants';
import type { BackgroundPostData } from '~/components/offers/types'; import type { BackgroundPostData } from '~/components/offers/types';
import { CURRENCY_OPTIONS } from '~/utils/offers/currency/CurrencyEnum'; import { CURRENCY_OPTIONS } from '~/utils/offers/currency/CurrencyEnum';
import { validatePositiveNumber } from '~/utils/offers/form';
import { EducationFieldOptions } from '../../EducationFields'; import { EducationFieldOptions } from '../../EducationFields';
import { EducationLevelOptions } from '../../EducationLevels'; import { EducationLevelOptions } from '../../EducationLevels';
@ -47,7 +48,7 @@ function YoeSection() {
label="Specific YOE 1" label="Specific YOE 1"
type="number" type="number"
{...register(`background.specificYoes.0.yoe`, { {...register(`background.specificYoes.0.yoe`, {
min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 }, validate: validatePositiveNumber,
valueAsNumber: true, valueAsNumber: true,
})} })}
/> />
@ -63,7 +64,7 @@ function YoeSection() {
label="Specific YOE 2" label="Specific YOE 2"
type="number" type="number"
{...register(`background.specificYoes.1.yoe`, { {...register(`background.specificYoes.1.yoe`, {
min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 }, validate: validatePositiveNumber,
valueAsNumber: true, valueAsNumber: true,
})} })}
/> />
@ -146,6 +147,7 @@ function FullTimeJobFields({ defaultCurrency }: FullTimeJobFieldsProps) {
type="number" type="number"
{...register(`background.experiences.0.durationInMonths`, { {...register(`background.experiences.0.durationInMonths`, {
min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 }, min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 },
validate: validatePositiveNumber,
valueAsNumber: true, valueAsNumber: true,
})} })}
/> />

@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { FieldError } from '~/components/offers/constants';
/** /**
* Removes empty objects, empty strings, `null`, `undefined`, and `NaN` values from an object. * Removes empty objects, empty strings, `null`, `undefined`, and `NaN` values from an object.
* Does not remove empty arrays. * Does not remove empty arrays.
@ -85,3 +87,13 @@ export function removeInvalidMoneyData(object: any) {
}); });
return object; return object;
} }
export function validatePositiveNumber(v?: number | null) {
return (
v === null ||
v === undefined ||
v !== v ||
v > 0 ||
FieldError.POSITIVE_NUMBER
);
}

Loading…
Cancel
Save