Skip to content

Commit

Permalink
fix: validator logic
Browse files Browse the repository at this point in the history
  • Loading branch information
caichi-t committed Nov 28, 2024
1 parent 6a74dd4 commit 33997a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion web/src/components/atoms/Form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Form, FormInstance } from "antd";
import { Rule } from "antd/lib/form";
import { Rule, RuleObject } from "antd/lib/form";
import { FormItemProps } from "antd/lib/form/FormItem";
import { FormItemLabelProps } from "antd/lib/form/FormItemLabel";
import { FieldError, ValidateErrorEntity } from "rc-field-form/lib/interface";
Expand All @@ -12,5 +12,6 @@ export type {
FieldError,
FormInstance,
Rule,
RuleObject,
ValidateErrorEntity,
};
9 changes: 6 additions & 3 deletions web/src/components/molecules/Content/Form/fields/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Rule } from "@reearth-cms/components/atoms/Form";
import { RuleObject } from "@reearth-cms/components/atoms/Form";

export const checkIfEmpty = (value: unknown) =>
value === undefined || value === null || value === "";

export const requiredValidator = (_: Rule, value: unknown) => {
if (checkIfEmpty(value) || (Array.isArray(value) && value.every(v => checkIfEmpty(v)))) {
export const requiredValidator = (rule: RuleObject, value: unknown) => {
if (
rule.required &&
(checkIfEmpty(value) || (Array.isArray(value) && value.every(v => checkIfEmpty(v))))
) {
return Promise.reject();
}
return Promise.resolve();
Expand Down

0 comments on commit 33997a4

Please sign in to comment.