Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor(ui): check required field

robin4bddea96da3c
diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index 7fff9ebe..bbec52ef 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -1154,3 +1154,4 @@ ui:
empty: cannot be empty
invalid: is invalid
btn_submit: Save
+ not_found_props: "Required property {{ key }} not found."
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index dfbcc167..35311066 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -85,7 +85,16 @@ const SchemaForm: FC<IProps> = ({
const { t } = useTranslation('translation', {
keyPrefix: 'form',
});
- const { properties } = schema;
+
+ const { required = [], properties } = schema;
+
+ // check required field
+ const excludes = required.filter((key) => !properties[key]);
+
+ if (excludes.length > 0) {
+ console.error(t('not_found_props', { key: excludes.join(', ') }));
+ }
+
const keys = Object.keys(properties);
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@@ -97,7 +106,6 @@ const SchemaForm: FC<IProps> = ({
};
const requiredValidator = () => {
- const required = schema.required || [];
const errors: string[] = [];
required.forEach((key) => {
if (!formData[key] || !formData[key].value) {
@@ -160,7 +168,7 @@ const SchemaForm: FC<IProps> = ({
isInvalid: true,
errorMsg:
uiSchema[cur]?.['ui:options']?.empty ||
- `${schema.properties[cur].title} ${t('empty')}`,
+ `${schema.properties[cur]?.title} ${t('empty')}`,
};
return acc;
}, formData);