Skip to content

Commit

Permalink
fix: fix Settings field form validation for certain field types (twen…
Browse files Browse the repository at this point in the history
…tyhq#5335)

Related to twentyhq#4295

Following twentyhq#5326, field types other than:
- `FieldMetadataType.Boolean`
- `FieldMetadataType.Currency`
- `FieldMetadataType.Relation`
- `FieldMetadataType.Select`
- `FieldMetadataType.MultiSelect`

Cannot be saved as they are not included in the form validation schema.
This PR makes sure they are included and can therefore be
created/edited.
  • Loading branch information
thaisguigon authored May 8, 2024
1 parent 18ff439 commit 87286f9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export const WithOpenSelect: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);

const inputField = await canvas.findByText('Unique ID');
const inputField = await canvas.findByText('Text');

await userEvent.click(inputField);

const input = await canvas.findByText('Text');
const input = await canvas.findByText('Unique ID');
await userEvent.click(input);

await userEvent.click(inputField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ export const SettingsObjectFieldEdit = () => {
try {
if (
formValues.type === FieldMetadataType.Relation &&
isNonEmptyString(relationFieldMetadataItem?.id) &&
'relation' in dirtyFields
'relation' in formValues &&
'relation' in dirtyFields &&
isNonEmptyString(relationFieldMetadataItem?.id)
) {
await updateOneFieldMetadataItem({
fieldMetadataIdToUpdate: relationFieldMetadataItem.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export const SettingsObjectNewFieldStep2 = () => {
const createdMetadataField = await createMetadataField({
...formValues,
defaultValue:
formValues.type === FieldMetadataType.Currency
formValues.type === FieldMetadataType.Currency &&
'defaultValue' in formValues
? {
...formValues.defaultValue,
amountMicros: null,
Expand Down

0 comments on commit 87286f9

Please sign in to comment.