Skip to content

Commit

Permalink
fix(CriteriaForm): validation rules for weight field
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Aug 3, 2023
1 parent 98de8e6 commit 0200ca1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
44 changes: 33 additions & 11 deletions src/components/CriteriaForm/CriteriaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const StyledSubmitButton = styled(Button)`
justify-content: center;
`;

const StyledGoalSuggest = styled(GoalSuggest)`
flex: 1;
`;

interface WeightFieldProps {
name: 'weight';
value?: string;
Expand Down Expand Up @@ -107,7 +111,7 @@ const CriteriaTitleField = forwardRef<HTMLInputElement, CriteriaTitleFieldProps>
/>

{type === 'search' ? (
<GoalSuggest
<StyledGoalSuggest
value={isItemSelected ? undefined : value}
showSuggest={!isItemSelected}
onChange={onSelect}
Expand Down Expand Up @@ -280,16 +284,34 @@ function patchZodSchema<T extends typeof criteriaSchema | typeof updateCriteriaS
title: schema.shape.title.refine((val) => !data.title.some((t) => t === val), {
message: tr('Title must be unique'),
}),
weight: schema.shape.weight
.transform((val) => Number(val))
.refine((val) => !Number.isNaN(val), { message: tr('Weight must be integer') })
.refine((val) => val >= minPossibleWeight && data.sum + val <= maxPossibleWeigth, {
message: tr
.raw('Weight must be in range', {
upTo: `${maxPossibleWeigth - data.sum}`,
})
.join(''),
}),
// INFO: https://github.com/colinhacks/zod#abort-early
weight: schema.shape.weight.superRefine((val, ctx): val is string => {
if (!val || !val.length) {
return z.NEVER;
}

const parsed = Number(val);

if (Number.isNaN(parsed)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: tr('Weight must be integer'),
});
}

if (parsed < minPossibleWeight || data.sum + parsed > maxPossibleWeigth) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: tr
.raw('Weight must be in range', {
upTo: `${maxPossibleWeigth - data.sum}`,
})
.join(''),
});
}

return z.NEVER;
}),
}),
);

Expand Down
6 changes: 3 additions & 3 deletions src/components/GoalCriteria/GoalCriteria.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({

const dataForValidateCriteria = useMemo(
() =>
criteriaList.reduce(
criteriaList.reduce<{ sum: number; title: string[] }>(
(acc, { weight, title }, criteriaIdx) => {
if (mode === 'edit' && index === criteriaIdx) {
return acc;
Expand All @@ -332,7 +332,7 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
},
{
sum: 0,
title: [] as string[],
title: [],
},
),
[criteriaList, mode, index],
Expand Down Expand Up @@ -405,7 +405,7 @@ export const GoalCriteria: React.FC<GoalCriteriaProps> = ({
goalId,
goalAsGriteria:
item.goalIdAsCriteria != null ? { id: item.goalIdAsCriteria } : undefined,
weight: String(item.weight),
weight: item.weight ? String(item.weight) : '',
}}
onSubmit={onUpdateCriteria}
onReset={() =>
Expand Down

0 comments on commit 0200ca1

Please sign in to comment.