Skip to content

Commit 3ee29b1

Browse files
authored
[offers][feat] Add positive number validation (#551)
1 parent 316c696 commit 3ee29b1

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

apps/portal/src/components/offers/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const emptyOption = {
1414
export enum FieldError {
1515
NON_NEGATIVE_NUMBER = 'Please fill in a non-negative number in this field.',
1616
NUMBER = 'Please fill in a number in this field.',
17+
POSITIVE_NUMBER = 'Please fill in a positive number in this field.',
1718
REQUIRED = 'Please fill in this field.',
1819
}
1920

apps/portal/src/components/offers/offersSubmission/submissionForm/BackgroundForm.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { FieldError } from '~/components/offers/constants';
66
import type { BackgroundPostData } from '~/components/offers/types';
77

88
import { CURRENCY_OPTIONS } from '~/utils/offers/currency/CurrencyEnum';
9+
import { validatePositiveNumber } from '~/utils/offers/form';
910

1011
import { EducationFieldOptions } from '../../EducationFields';
1112
import { EducationLevelOptions } from '../../EducationLevels';
@@ -47,7 +48,7 @@ function YoeSection() {
4748
label="Specific YOE 1"
4849
type="number"
4950
{...register(`background.specificYoes.0.yoe`, {
50-
min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 },
51+
validate: validatePositiveNumber,
5152
valueAsNumber: true,
5253
})}
5354
/>
@@ -63,7 +64,7 @@ function YoeSection() {
6364
label="Specific YOE 2"
6465
type="number"
6566
{...register(`background.specificYoes.1.yoe`, {
66-
min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 },
67+
validate: validatePositiveNumber,
6768
valueAsNumber: true,
6869
})}
6970
/>
@@ -146,6 +147,7 @@ function FullTimeJobFields({ defaultCurrency }: FullTimeJobFieldsProps) {
146147
type="number"
147148
{...register(`background.experiences.0.durationInMonths`, {
148149
min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 },
150+
validate: validatePositiveNumber,
149151
valueAsNumber: true,
150152
})}
151153
/>

apps/portal/src/utils/offers/form.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3+
import { FieldError } from '~/components/offers/constants';
4+
35
/**
46
* Removes empty objects, empty strings, `null`, `undefined`, and `NaN` values from an object.
57
* Does not remove empty arrays.
@@ -85,3 +87,13 @@ export function removeInvalidMoneyData(object: any) {
8587
});
8688
return object;
8789
}
90+
91+
export function validatePositiveNumber(v?: number | null) {
92+
return (
93+
v === null ||
94+
v === undefined ||
95+
v !== v ||
96+
v > 0 ||
97+
FieldError.POSITIVE_NUMBER
98+
);
99+
}

0 commit comments

Comments
 (0)