{"version":3,"file":"common.mjs","sources":["../../../../src/core-api/routes/validation/common.ts"],"sourcesContent":["import type { Core, UID } from '@strapi/types';\n\nimport { contentTypes, AbstractRouteValidator } from '@strapi/utils';\nimport * as z from 'zod/v4';\n\n/**\n * AbstractCoreRouteValidator provides the foundation for validating and managing core routes within a Strapi context for a specific model.\n *\n * This abstract class extends the base AbstractRouteValidator from utils to add schema-aware validation\n * logic for scalar and populatable fields defined in a model schema.\n *\n * It uses runtime information about Strapi models to derive and expose schema validations.\n *\n * @template {UID.Schema} T Representing the schema identifier to be validated.\n */\nexport abstract class AbstractCoreRouteValidator<\n T extends UID.Schema,\n> extends AbstractRouteValidator {\n /**\n * The `_strapi` variable serves as a reference to the core Strapi instance.\n * It is used for interacting with the loaded model.\n */\n protected readonly _strapi: Core.Strapi;\n\n /**\n * A unique identifier used to represent a model within Strapi.\n *\n * The type of the identifier is generic to allow flexibility and ensure type safety\n * when working with either content-types or components.\n */\n protected readonly _uid: T;\n\n /**\n * Protected constructor for initializing the class with the provided Strapi instance and unique identifier (UID).\n *\n * @param strapi The Strapi instance to be used.\n * @param uid The unique identifier for the instance.\n */\n public constructor(strapi: Core.Strapi, uid: T) {\n super();\n this._strapi = strapi;\n this._uid = uid;\n }\n\n /**\n * Retrieves an enum schema constructed from the keys of the scalar fields.\n *\n * @return A Zod enum containing the keys from the scalar fields.\n */\n public get scalarFieldsEnum() {\n return z.enum(Object.keys(this._scalarFields) as [string]);\n }\n\n /**\n * Retrieves an enum schema constructed from the keys of fields that can be populated (relations, components, files, etc.)\n *\n * @return A ZodEnum object containing the list of populatable field keys.\n */\n public get populatableFieldsEnum() {\n return z.enum(Object.keys(this._populatableFields) as [string]);\n }\n\n /**\n * Retrieves an array representation of the scalar fields.\n *\n * @return An array containing the scalar fields as defined by {@link scalarFieldsEnum}.\n */\n public get scalarFieldsArray() {\n return z.array(this.scalarFieldsEnum);\n }\n\n /**\n * Retrieves an array of populatable fields.\n *\n * @return A Zod array schema representing the available populatable fields as defined by {@link populatableFieldsEnum}.\n */\n public get populatableFieldsArray() {\n return z.array(this.populatableFieldsEnum);\n }\n\n /**\n * Retrieves the schema associated with the current model.\n *\n * The schema represents the structural definition of the model,\n * as retrieved from the Strapi model associated with the given UID.\n *\n * @return The schema of the model retrieved from Strapi.\n */\n protected get _schema() {\n return this._strapi.getModel(this._uid);\n }\n\n /**\n * Retrieves scalar fields from the object's schema attributes.\n *\n * Filters the schema attributes to include only those that are scalar and not private.\n *\n * @return An object composed of scalar fields from the schema attributes.\n */\n protected get _scalarFields() {\n const attributes = Object.entries(this._schema.attributes);\n\n const scalarEntries = attributes\n .filter(([, attribute]) => contentTypes.isScalarAttribute(attribute))\n .filter(([attributeName]) => !contentTypes.isPrivateAttribute(this._schema, attributeName));\n\n return Object.fromEntries(scalarEntries);\n }\n\n /**\n * Retrieves the populatable fields from the schema attributes.\n *\n * Filters the schema attributes to include only those that are populatable and not private.\n *\n * @return An object containing the populatable fields derived from the schema attributes.\n */\n protected get _populatableFields() {\n const attributes = Object.entries(this._schema.attributes);\n\n const populatableEntries = attributes\n .filter(([, attribute]) => !contentTypes.isScalarAttribute(attribute))\n .filter(([attributeName]) => !contentTypes.isPrivateAttribute(this._schema, attributeName));\n\n return Object.fromEntries(populatableEntries);\n }\n\n /**\n * Creates a Zod schema as a record with scalar fields as keys and the specified type as values.\n *\n * @param type - The Zod type to use for the record's values.\n * @return A Zod record schema with scalar fields as keys and the specified type as values.\n */\n public fieldRecord(type: z.ZodTypeAny) {\n return z.record(this.scalarFieldsEnum, type);\n }\n}\n"],"names":["AbstractCoreRouteValidator","AbstractRouteValidator","scalarFieldsEnum","z","enum","Object","keys","_scalarFields","populatableFieldsEnum","_populatableFields","scalarFieldsArray","array","populatableFieldsArray","_schema","_strapi","getModel","_uid","attributes","entries","scalarEntries","filter","attribute","contentTypes","isScalarAttribute","attributeName","isPrivateAttribute","fromEntries","populatableEntries","fieldRecord","type","record","strapi","uid"],"mappings":";;;AAKA;;;;;;;;;IA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