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:
Thaïs
2024-03-05 07:32:30 -03:00
committed by GitHub
parent 0a2d8056bd
commit a9f4a66c4f
20 changed files with 332 additions and 189 deletions

View File

@ -0,0 +1,22 @@
import { SafeParseError } from 'zod';
import { camelCaseStringSchema } from '../camelCaseStringSchema';
describe('camelCaseStringSchema', () => {
it('validates a camel case string', () => {
const result = camelCaseStringSchema.safeParse('camelCaseString');
expect(result.success).toBe(true);
});
it('fails for non-camel case strings', () => {
const result = camelCaseStringSchema.safeParse('NotCamelCase');
expect(result.success).toBe(false);
expect((result as SafeParseError<string>).error.errors).toEqual([
{
code: 'custom',
message: 'String should be camel case',
path: [],
},
]);
});
});

View File

@ -0,0 +1,8 @@
import camelCase from 'lodash.camelcase';
import { z } from 'zod';
export const camelCaseStringSchema = z
.string()
.refine((value) => camelCase(value) === value, {
message: 'String should be camel case',
});