refactor: validate objectMetadataItem with Zod on creation and update… (#4270)
* refactor: validate objectMetadataItem with Zod on creation and update & remove logic from useObjectMetadataItemForSettings * refactor: review
This commit is contained in:
@ -0,0 +1,47 @@
|
||||
import { SafeParseSuccess } from 'zod';
|
||||
|
||||
import { CreateObjectInput } from '~/generated-metadata/graphql';
|
||||
|
||||
import { settingsCreateObjectInputSchema } from '..//settingsCreateObjectInputSchema';
|
||||
|
||||
describe('settingsCreateObjectInputSchema', () => {
|
||||
it('validates a valid input and adds name properties', () => {
|
||||
// Given
|
||||
const validInput = {
|
||||
description: 'A valid description',
|
||||
icon: 'IconPlus',
|
||||
labelPlural: ' Labels ',
|
||||
labelSingular: 'Label ',
|
||||
};
|
||||
|
||||
// When
|
||||
const result = settingsCreateObjectInputSchema.safeParse(validInput);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(true);
|
||||
expect((result as SafeParseSuccess<CreateObjectInput>).data).toEqual({
|
||||
description: validInput.description,
|
||||
icon: validInput.icon,
|
||||
labelPlural: 'Labels',
|
||||
labelSingular: 'Label',
|
||||
namePlural: 'labels',
|
||||
nameSingular: 'label',
|
||||
});
|
||||
});
|
||||
|
||||
it('fails for an invalid input', () => {
|
||||
// Given
|
||||
const invalidInput = {
|
||||
description: 123,
|
||||
icon: true,
|
||||
labelPlural: [],
|
||||
labelSingular: {},
|
||||
};
|
||||
|
||||
// When
|
||||
const result = settingsCreateObjectInputSchema.safeParse(invalidInput);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,50 @@
|
||||
import { SafeParseSuccess } from 'zod';
|
||||
|
||||
import { UpdateObjectInput } from '~/generated-metadata/graphql';
|
||||
|
||||
import { settingsUpdateObjectInputSchema } from '../settingsUpdateObjectInputSchema';
|
||||
|
||||
describe('settingsUpdateObjectInputSchema', () => {
|
||||
it('validates a valid input and adds name properties', () => {
|
||||
// Given
|
||||
const validInput = {
|
||||
description: 'A valid description',
|
||||
icon: 'IconName',
|
||||
labelPlural: 'Labels Plural ',
|
||||
labelSingular: ' Label Singular',
|
||||
labelIdentifierFieldMetadataId: '3fa85f64-5717-4562-b3fc-2c963f66afa6',
|
||||
};
|
||||
|
||||
// When
|
||||
const result = settingsUpdateObjectInputSchema.safeParse(validInput);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(true);
|
||||
expect((result as SafeParseSuccess<UpdateObjectInput>).data).toEqual({
|
||||
description: validInput.description,
|
||||
icon: validInput.icon,
|
||||
labelIdentifierFieldMetadataId: validInput.labelIdentifierFieldMetadataId,
|
||||
labelPlural: 'Labels Plural',
|
||||
labelSingular: 'Label Singular',
|
||||
namePlural: 'labelsPlural',
|
||||
nameSingular: 'labelSingular',
|
||||
});
|
||||
});
|
||||
|
||||
it('fails for an invalid input', () => {
|
||||
// Given
|
||||
const invalidInput = {
|
||||
description: 123,
|
||||
icon: true,
|
||||
labelPlural: [],
|
||||
labelSingular: {},
|
||||
labelIdentifierFieldMetadataId: 'invalid uuid',
|
||||
};
|
||||
|
||||
// When
|
||||
const result = settingsUpdateObjectInputSchema.safeParse(invalidInput);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,17 @@
|
||||
import camelCase from 'lodash.camelcase';
|
||||
|
||||
import { objectMetadataItemSchema } from '@/object-metadata/validation-schemas/objectMetadataItemSchema';
|
||||
import { CreateObjectInput } from '~/generated-metadata/graphql';
|
||||
|
||||
export const settingsCreateObjectInputSchema = objectMetadataItemSchema
|
||||
.pick({
|
||||
description: true,
|
||||
icon: true,
|
||||
labelPlural: true,
|
||||
labelSingular: true,
|
||||
})
|
||||
.transform<CreateObjectInput>((value) => ({
|
||||
...value,
|
||||
nameSingular: camelCase(value.labelSingular),
|
||||
namePlural: camelCase(value.labelPlural),
|
||||
}));
|
||||
@ -0,0 +1,23 @@
|
||||
import camelCase from 'lodash.camelcase';
|
||||
|
||||
import { objectMetadataItemSchema } from '@/object-metadata/validation-schemas/objectMetadataItemSchema';
|
||||
import { UpdateObjectInput } from '~/generated-metadata/graphql';
|
||||
|
||||
export const settingsUpdateObjectInputSchema = objectMetadataItemSchema
|
||||
.pick({
|
||||
description: true,
|
||||
icon: true,
|
||||
imageIdentifierFieldMetadataId: true,
|
||||
isActive: true,
|
||||
labelIdentifierFieldMetadataId: true,
|
||||
labelPlural: true,
|
||||
labelSingular: true,
|
||||
})
|
||||
.partial()
|
||||
.transform<UpdateObjectInput>((value) => ({
|
||||
...value,
|
||||
nameSingular: value.labelSingular
|
||||
? camelCase(value.labelSingular)
|
||||
: undefined,
|
||||
namePlural: value.labelPlural ? camelCase(value.labelPlural) : undefined,
|
||||
}));
|
||||
Reference in New Issue
Block a user