Files
twenty/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts
gitstart-app[bot] b3ed6cb903 [Bug] Select options names can't start with a number (#7079)
This PR was created by [GitStart](https://gitstart.com/) to address the
requirements from this ticket:
[TWNTY-6980](https://clients.gitstart.com/twenty/5449/tickets/TWNTY-6980).
This ticket was imported from:
[TWNTY-6980](https://github.com/twentyhq/twenty/issues/6980)

 --- 

### Description

- **fix**: added a transformation step that prefixes the newly added
option with an underscore before the Graphql enum is generated so it
saves successfully and passes the default GraphQL validation.

### Demo

- <https://www.loom.com/share/feda2198ed8b4e558f96520a0d051725>

### Refs

#6980

Fixes #6980

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: Weiko <corentin@twenty.com>
2024-09-19 14:05:28 +02:00

57 lines
1.4 KiB
TypeScript

import { themeColorSchema } from 'twenty-ui';
import { z } from 'zod';
import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem';
import { computeOptionValueFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-option-value-from-label.utils';
const selectOptionSchema = z
.object({
color: themeColorSchema,
id: z.string(),
label: z.string().trim().min(1),
position: z.number(),
value: z.string(),
})
.refine(
(option) => {
try {
computeOptionValueFromLabelOrThrow(option.label);
return true;
} catch (error) {
return false;
}
},
{
message: 'Label is not transliterable',
},
) satisfies z.ZodType<FieldMetadataItemOption>;
export const selectOptionsSchema = z
.array(selectOptionSchema)
.min(1)
.refine(
(options) => {
const optionIds = options.map(({ id }) => id);
return new Set(optionIds).size === options.length;
},
{
message: 'Options must have unique ids',
},
)
.refine(
(options) => {
const optionValues = options.map(({ value }) => value);
return new Set(optionValues).size === options.length;
},
{
message: 'Options must have unique values',
},
)
.refine(
(options) =>
[...options].sort().every((option, index) => option.position === index),
{
message: 'Options positions must be sequential',
},
);