mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
Validate input size in custom field server registry
This commit is contained in:
parent
f09fe7cc37
commit
7419f86063
@ -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',
|
||||
|
@ -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}`;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user