feat: simplify field preview logic in Settings (#5541)
Closes #5382 TODO: - [x] Test all field previews in app - [x] Fix tests - [x] Fix JSON preview
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
import { isString } from '@sniptt/guards';
|
||||
|
||||
import { FieldDefinition } from '@/object-record/record-field/types/FieldDefinition';
|
||||
import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata';
|
||||
import { isFieldAddress } from '@/object-record/record-field/types/guards/isFieldAddress';
|
||||
@ -26,8 +28,11 @@ import { isFieldSelectValue } from '@/object-record/record-field/types/guards/is
|
||||
import { isFieldText } from '@/object-record/record-field/types/guards/isFieldText';
|
||||
import { isFieldUuid } from '@/object-record/record-field/types/guards/isFieldUuid';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { stripSimpleQuotesFromString } from '~/utils/string/stripSimpleQuotesFromString';
|
||||
|
||||
const isValueEmpty = (value: unknown) => !isDefined(value) || value === '';
|
||||
const isValueEmpty = (value: unknown) =>
|
||||
!isDefined(value) ||
|
||||
(isString(value) && stripSimpleQuotesFromString(value) === '');
|
||||
|
||||
export const isFieldValueEmpty = ({
|
||||
fieldDefinition,
|
||||
@ -78,7 +83,8 @@ export const isFieldValueEmpty = ({
|
||||
if (isFieldFullName(fieldDefinition)) {
|
||||
return (
|
||||
!isFieldFullNameValue(fieldValue) ||
|
||||
isValueEmpty(fieldValue?.firstName + fieldValue?.lastName)
|
||||
(isValueEmpty(fieldValue?.firstName) &&
|
||||
isValueEmpty(fieldValue?.lastName))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode';
|
||||
import { currencyCodeSchema } from '@/object-record/record-field/validation-schemas/currencyCodeSchema';
|
||||
import { stripSimpleQuotesFromString } from '~/utils/string/stripSimpleQuotesFromString';
|
||||
import { simpleQuotesStringSchema } from '~/utils/validation-schemas/simpleQuotesStringSchema';
|
||||
|
||||
export const currencyFieldDefaultValueSchema = z.object({
|
||||
amountMicros: z.number().nullable(),
|
||||
currencyCode: simpleQuotesStringSchema.refine(
|
||||
(value): value is `'${CurrencyCode}'` =>
|
||||
currencyCodeSchema.safeParse(stripSimpleQuotesFromString(value)).success,
|
||||
{ message: 'String is not a valid currencyCode' },
|
||||
),
|
||||
});
|
||||
@ -0,0 +1,26 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem';
|
||||
import { stripSimpleQuotesFromString } from '~/utils/string/stripSimpleQuotesFromString';
|
||||
import { simpleQuotesStringSchema } from '~/utils/validation-schemas/simpleQuotesStringSchema';
|
||||
|
||||
export const multiSelectFieldDefaultValueSchema = (
|
||||
options?: FieldMetadataItemOption[],
|
||||
) => {
|
||||
if (!options?.length) return z.array(simpleQuotesStringSchema).nullable();
|
||||
|
||||
const optionValues = options.map(({ value }) => value);
|
||||
|
||||
return z
|
||||
.array(
|
||||
simpleQuotesStringSchema.refine(
|
||||
(value) => optionValues.includes(stripSimpleQuotesFromString(value)),
|
||||
{
|
||||
message: `String is not a valid multi-select option, available options are: ${options.join(
|
||||
', ',
|
||||
)}`,
|
||||
},
|
||||
),
|
||||
)
|
||||
.nullable();
|
||||
};
|
||||
@ -0,0 +1,22 @@
|
||||
import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem';
|
||||
import { stripSimpleQuotesFromString } from '~/utils/string/stripSimpleQuotesFromString';
|
||||
import { simpleQuotesStringSchema } from '~/utils/validation-schemas/simpleQuotesStringSchema';
|
||||
|
||||
export const selectFieldDefaultValueSchema = (
|
||||
options?: FieldMetadataItemOption[],
|
||||
) => {
|
||||
if (!options?.length) return simpleQuotesStringSchema.nullable();
|
||||
|
||||
const optionValues = options.map(({ value }) => value);
|
||||
|
||||
return simpleQuotesStringSchema
|
||||
.refine(
|
||||
(value) => optionValues.includes(stripSimpleQuotesFromString(value)),
|
||||
{
|
||||
message: `String is not a valid select option, available options are: ${options.join(
|
||||
', ',
|
||||
)}`,
|
||||
},
|
||||
)
|
||||
.nullable();
|
||||
};
|
||||
Reference in New Issue
Block a user