Skip to content

Commit

Permalink
fix: fix Settings field form validation for certain field types
Browse files Browse the repository at this point in the history
Related to #4295
  • Loading branch information
thaisguigon committed May 8, 2024
1 parent 863554b commit ebea59b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useFormContext } from 'react-hook-form';
import styled from '@emotion/styled';
import omit from 'lodash.omit';
import { z } from 'zod';

import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
Expand All @@ -22,6 +23,7 @@ import {
settingsDataModelFieldSelectFormSchema,
} from '@/settings/data-model/components/SettingsObjectFieldSelectForm';
import { RELATION_TYPES } from '@/settings/data-model/constants/RelationTypes';
import { SETTINGS_FIELD_TYPE_CONFIGS } from '@/settings/data-model/constants/SettingsFieldTypeConfigs';
import {
SettingsDataModelFieldPreviewCard,
SettingsDataModelFieldPreviewCardProps,
Expand All @@ -46,13 +48,28 @@ const selectFieldFormSchema = z
})
.merge(settingsDataModelFieldSelectFormSchema);

const otherFieldsFormSchema = z.object({
type: z.enum(
Object.keys(
omit(SETTINGS_FIELD_TYPE_CONFIGS, [
FieldMetadataType.Boolean,
FieldMetadataType.Currency,
FieldMetadataType.Relation,
FieldMetadataType.Select,
FieldMetadataType.MultiSelect,
]),
) as [FieldMetadataType, ...FieldMetadataType[]],
),
});

export const settingsDataModelFieldSettingsFormSchema = z.discriminatedUnion(
'type',
[
booleanFieldFormSchema,
currencyFieldFormSchema,
relationFieldFormSchema,
selectFieldFormSchema,
otherFieldsFormSchema,
],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@/settings/data-model/constants/SettingsFieldTypeConfigs';
import { SettingsSupportedFieldType } from '@/settings/data-model/types/SettingsSupportedFieldType';
import { Select, SelectOption } from '@/ui/input/components/Select';
import { FieldMetadataType } from '~/generated-metadata/graphql';

export const settingsDataModelFieldTypeFormSchema = z.object({
type: z.enum(
Expand Down Expand Up @@ -57,7 +58,7 @@ export const SettingsDataModelFieldTypeSelect = ({
defaultValue={
fieldMetadataItem && fieldMetadataItem.type in fieldTypeConfigs
? (fieldMetadataItem.type as SettingsSupportedFieldType)
: undefined
: FieldMetadataType.Text
}
render={({ field: { onChange, value } }) => (
<Select
Expand Down

0 comments on commit ebea59b

Please sign in to comment.