Skip to content

Commit

Permalink
Update required validator in Field to correctly handle falsey values (#…
Browse files Browse the repository at this point in the history
…2499)

Previously, the validator incorrectly returned errors for all falsey
values, e.g. the number `0`.
Now, it only returns an error for `undefined`, `null`, `false` and empty
strings.
  • Loading branch information
thomasdax98 authored Sep 6, 2024
1 parent 5286166 commit 2286234
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/quiet-bats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@comet/admin": patch
---

Update required validator in `Field` to correctly handle falsey values

Previously, the validator incorrectly returned errors for all falsey values, e.g. the number `0`.
Now, it only returns an error for `undefined`, `null`, `false` and empty strings.
7 changes: 6 additions & 1 deletion packages/admin/admin/src/form/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { FormattedMessage } from "react-intl";
import { FieldContainer, FieldContainerProps } from "./FieldContainer";
import { useFinalFormContext } from "./FinalFormContextProvider";

const requiredValidator = (value: any) => (value ? undefined : <FormattedMessage id="comet.form.required" defaultMessage="Required" />);
const requiredValidator = (value: any) => {
if (value === undefined || value === null || value === false || value === "") {
return <FormattedMessage id="comet.form.required" defaultMessage="Required" />;
}
return undefined;
};

const composeValidators =
(...validators: Array<(value: any, allValues: object) => any>) =>
Expand Down

0 comments on commit 2286234

Please sign in to comment.