{"version":3,"file":"utilities.mjs","sources":["../../src/validation/utilities.ts"],"sourcesContent":["/**\n * @file This file contains utility functions for working with Zod schemas.\n * It provides functions to modify schemas (e.g., make them optional, readonly, or add default values),\n * and to safely register and create schemas within Zod's global registry.\n */\n\nimport * as z from 'zod/v4';\n\n/**\n * Transforms a Strapi UID into an OpenAPI-compliant component name.\n *\n * @param uid - The Strapi UID to transform (e.g., \"basic.seo\", \"api::category.category\", \"plugin::upload.file\")\n * @returns The OpenAPI-compliant component name (e.g., \"BasicSeoEntry\", \"ApiCategoryCategoryDocument\", \"PluginUploadFileDocument\")\n */\nexport const transformUidToValidOpenApiName = (uid: string): string => {\n const capitalize = (str: string): string => {\n return str.charAt(0).toUpperCase() + str.slice(1);\n };\n\n const toPascalCase = (str: string): string => {\n return str.split(/[-_]/).map(capitalize).join('');\n };\n\n // Check if it contains double colons (other namespaced UIDs)\n if (uid.includes('::')) {\n const [namespace, ...rest] = uid.split('::');\n const namespacePart = toPascalCase(namespace);\n const restParts = rest.join('.').split('.').map(toPascalCase).map(capitalize);\n return `${capitalize(namespacePart)}${restParts.join('')}Document`;\n }\n\n if (uid.includes('.')) {\n // basic.seo -> BasicSeoEntry\n const parts = uid.split('.');\n const transformedParts = parts.map(toPascalCase).map(capitalize);\n return `${transformedParts.join('')}Entry`;\n }\n\n return `${toPascalCase(capitalize(uid))}Schema`;\n};\n\n/**\n * Conditionally makes a Zod schema optional based on the `required` parameter.\n *\n * @param required - If `false` or `undefined`, the schema will be made optional. If `true`, the schema becomes non-optional.\n * @returns A function that takes a Zod schema and returns a modified schema (optional or required).\n * @example\n * ```typescript\n * const optionalString = maybeRequired(false)(z.string()); // z.ZodOptional\n *\n * const requiredString = maybeRequired(true)(z.string()); // z.ZodString\n * ```\n */\nexport const maybeRequired = (required?: boolean) => {\n return (schema: T) => {\n return required !== true ? schema.optional() : schema.nonoptional();\n };\n};\n\n/**\n * Conditionally makes a Zod schema readonly based on the `writable` parameter.\n *\n * @param writable - If `false`, the schema will be made readonly. If `true` or `undefined`, the schema remains unchanged.\n * @returns A function that takes a Zod schema and returns a modified schema (readonly or original).\n * @example\n * ```typescript\n * const readonlyNumber = maybeReadonly(false)(z.number()); // z.ZodReadonly\n * const writableNumber = maybeReadonly(true)(z.number()); // z.ZodNumber\n * ```\n */\nexport const maybeReadonly = (writable?: boolean) => {\n return (schema: T) => (writable !== false ? schema : schema.readonly());\n};\n\n/**\n * Conditionally adds a default value to a Zod schema based on the `defaultValue` parameter.\n *\n * @param defaultValue - The default value to apply to the schema. If `undefined`, no default value is added.\n * If `defaultValue` is a function, its return value will be used as the default.\n * @returns A function that takes a Zod schema and returns a modified schema (with default or original).\n * @example\n * ```typescript\n * const stringWithDefault = maybeWithDefault(\"default\")(z.string()); // z.ZodDefault\n * const numberWithFunctionDefault = maybeWithDefault(() => Math.random())(z.number());\n * ```\n */\nexport const maybeWithDefault = (defaultValue?: unknown) => {\n return (schema: T) => {\n if (defaultValue === undefined) {\n return schema;\n }\n\n const value = typeof defaultValue === 'function' ? defaultValue() : defaultValue;\n return schema.default(value);\n };\n};\n\n/**\n * Conditionally applies `min` and `max` constraints to a Zod string, number, or array schema.\n *\n * @param min - The minimum value/length. If `undefined`, no minimum constraint is applied.\n * @param max - The maximum value/length. If `undefined`, no maximum constraint is applied.\n * @returns A function that takes a Zod string, number, or array schema and returns a modified schema (with min/max constraints or original).\n * @example\n * ```typescript\n * const stringWithMinMax = maybeWithMinMax(5, 10)(z.string()); // z.ZodString with min(5) and max(10)\n * const numberWithMinMax = maybeWithMinMax(0, 100)(z.number()); // z.ZodNumber with min(0) and max(100)\n * ```\n */\nexport const maybeWithMinMax = (min?: number, max?: number) => {\n return >(schema: R) => {\n return min !== undefined && max !== undefined ? schema.min(min).max(max) : schema;\n };\n};\n\n/**\n * Applies a series of modifier functions to a Zod schema sequentially.\n *\n * @template T - The type of the Zod schema.\n * @param schema - The initial Zod schema to which modifiers will be applied.\n * @param modifiers - An array of functions, each taking a Zod schema and returning a modified schema.\n * @returns The final Zod schema after all modifiers have been applied.\n * @example\n * ```typescript\n * const modifiedSchema = augmentSchema(z.string(), [\n * maybeRequired(false),\n * maybeWithDefault(\"test\")\n * ]);\n * ```\n */\nexport const augmentSchema = (\n schema: T,\n modifiers: ((schema: T) => z.Schema)[]\n) => {\n return modifiers.reduce((acc, modifier) => modifier(acc) as T, schema);\n};\n"],"names":["transformUidToValidOpenApiName","uid","capitalize","str","charAt","toUpperCase","slice","toPascalCase","split","map","join","includes","namespace","rest","namespacePart","restParts","parts","transformedParts","maybeRequired","required","schema","optional","nonoptional","maybeReadonly","writable","readonly","maybeWithDefault","defaultValue","undefined","value","default","maybeWithMinMax","min","max","augmentSchema","modifiers","reduce","acc","modifier"],"mappings":"AAAA;;;;;;;;;IAcaA,MAAAA,8BAAAA,GAAiC,CAACC,GAAAA,GAAAA;AAC7C,IAAA,MAAMC,aAAa,CAACC,GAAAA,GAAAA;QAClB,OAAOA,GAAAA,CAAIC,MAAM,CAAC,CAAA,CAAA,CAAGC,WAAW,EAAKF,GAAAA,GAAAA,CAAIG,KAAK,CAAC,CAAA,CAAA;AACjD,KAAA;AAEA,IAAA,MAAMC,eAAe,CAACJ,GAAAA,GAAAA;QACpB,OAAOA,GAAAA,CAAIK,KAAK,CAAC,MAAA,CAAA,CAAQC,GAAG,CAACP,UAAAA,CAAAA,CAAYQ,IAAI,CAAC,EAAA,CAAA;AAChD,KAAA;;IAGA,IAAIT,GAAAA,CAAIU,QAAQ,CAAC,IAAO,CAAA,EAAA;AACtB,QAAA,MAAM,CAACC,SAAW,EAAA,GAAGC,KAAK,GAAGZ,GAAAA,CAAIO,KAAK,CAAC,IAAA,CAAA;AACvC,QAAA,MAAMM,gBAAgBP,YAAaK,CAAAA,SAAAA,CAAAA;AACnC,QAAA,MAAMG,SAAYF,GAAAA,IAAAA,CAAKH,IAAI,CAAC,GAAKF,CAAAA,CAAAA,KAAK,CAAC,GAAA,CAAA,CAAKC,GAAG,CAACF,YAAcE,CAAAA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