Validate input size in custom field server registry

This commit is contained in:
Rémi de Juvigny 2023-04-18 11:34:49 +02:00
parent f09fe7cc37
commit 7419f86063
2 changed files with 37 additions and 1 deletions

View File

@ -90,6 +90,25 @@ describe('Custom fields registry', () => {
);
});
it('validates inputSize', () => {
const mockCF = {
name: 'test',
type: 'text',
};
const customFields = customFieldsRegistry(strapi);
expect(() => customFields.add({ ...mockCF, inputSize: 'small' })).toThrowError(
`inputSize should be an object with 'default' and 'isResizable' keys`
);
expect(() =>
customFields.add({ ...mockCF, inputSize: { default: 99, isResizable: true } })
).toThrowError('Custom fields require a valid default input size');
expect(() =>
customFields.add({ ...mockCF, inputSize: { default: 12, isResizable: 'true' } })
).toThrowError('Custom fields should specify if their input is resizable');
});
it('confirms the custom field does not already exist', () => {
const mockCF = {
name: 'test',

View File

@ -44,7 +44,7 @@ const customFieldsRegistry = (strapi) => {
throw new Error(`Custom fields require a 'name' and 'type' key`);
}
const { name, plugin, type } = cf;
const { name, plugin, type, inputSize } = cf;
if (!ALLOWED_TYPES.includes(type)) {
throw new Error(
`Custom field type: '${type}' is not a valid Strapi type or it can't be used with a Custom Field`
@ -56,6 +56,23 @@ const customFieldsRegistry = (strapi) => {
throw new Error(`Custom field name: '${name}' is not a valid object key`);
}
// Validate inputSize when provided
if (inputSize) {
if (
typeof inputSize !== 'object' ||
!has('default', inputSize) ||
!has('isResizable', inputSize)
) {
throw new Error(`inputSize should be an object with 'default' and 'isResizable' keys`);
}
if (![4, 6, 8, 12].includes(inputSize.default)) {
throw new Error('Custom fields require a valid default input size');
}
if (typeof inputSize.isResizable !== 'boolean') {
throw new Error('Custom fields should specify if their input is resizable');
}
}
// When no plugin is specified, or it isn't found in Strapi, default to global
const uid = strapi.plugin(plugin) ? `plugin::${plugin}.${name}` : `global::${name}`;