feat: add support for custom field attributes in schema mapping (#24310)

This commit is contained in:
Jamie Howard 2025-09-08 09:27:16 +01:00 committed by GitHub
parent f0bfcece10
commit 34e034f28a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,17 @@ import * as z from 'zod/v4';
// eslint-disable-next-line import/no-cycle // eslint-disable-next-line import/no-cycle
import * as attributes from './attributes'; import * as attributes from './attributes';
const isCustomFieldAttribute = (
attribute: unknown
): attribute is { type: 'customField'; customField: string } => {
return (
!!attribute &&
typeof attribute === 'object' &&
(attribute as any).type === 'customField' &&
typeof (attribute as any).customField === 'string'
);
};
/** /**
* Creates a Zod schema for a collection of Strapi attributes. * Creates a Zod schema for a collection of Strapi attributes.
* *
@ -131,6 +142,22 @@ export const mapAttributeToSchema = (attribute: Schema.Attribute.AnyAttribute):
case 'uid': case 'uid':
return attributes.uidToSchema(attribute); return attributes.uidToSchema(attribute);
default: { default: {
if (isCustomFieldAttribute(attribute)) {
const attrCF = attribute as { type: 'customField'; customField: string };
const strapiInstance = global.strapi;
if (!strapiInstance) {
throw new Error('Strapi instance not available for custom field conversion');
}
const customField = strapiInstance.get('custom-fields').get(attrCF.customField);
if (!customField) {
throw new Error(`Custom field '${attrCF.customField}' not found`);
}
// Re-dispatch with the resolved underlying Strapi kind
return mapAttributeToSchema({ ...attrCF, type: customField.type });
}
const { type } = attribute as Schema.Attribute.AnyAttribute; const { type } = attribute as Schema.Attribute.AnyAttribute;
throw new Error(`Unsupported attribute type: ${type}`); throw new Error(`Unsupported attribute type: ${type}`);
@ -224,6 +251,22 @@ export const mapAttributeToInputSchema = (
case 'uid': case 'uid':
return attributes.uidToInputSchema(attribute); return attributes.uidToInputSchema(attribute);
default: { default: {
if (isCustomFieldAttribute(attribute)) {
const attrCF = attribute as { type: 'customField'; customField: string };
const strapiInstance = global.strapi;
if (!strapiInstance) {
throw new Error('Strapi instance not available for custom field conversion');
}
const customField = strapiInstance.get('custom-fields').get(attrCF.customField);
if (!customField) {
throw new Error(`Custom field '${attrCF.customField}' not found`);
}
// Re-dispatch with the resolved underlying Strapi kind
return mapAttributeToInputSchema({ ...attrCF, type: customField.type });
}
const { type } = attribute as Schema.Attribute.AnyAttribute; const { type } = attribute as Schema.Attribute.AnyAttribute;
throw new Error(`Unsupported attribute type: ${type}`); throw new Error(`Unsupported attribute type: ${type}`);