{"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<z.ZodString>\n *\n * const requiredString = maybeRequired(true)(z.string());  // z.ZodString\n * ```\n */\nexport const maybeRequired = (required?: boolean) => {\n  return <T extends z.Schema>(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<z.ZodNumber>\n * const writableNumber = maybeReadonly(true)(z.number());  // z.ZodNumber\n * ```\n */\nexport const maybeReadonly = (writable?: boolean) => {\n  return <T extends z.Schema>(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<z.ZodString>\n * const numberWithFunctionDefault = maybeWithDefault(() => Math.random())(z.number());\n * ```\n */\nexport const maybeWithDefault = (defaultValue?: unknown) => {\n  return <T extends z.Schema>(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 <R extends z.ZodString | z.ZodEmail | z.ZodNumber | z.ZodArray<z.ZodAny>>(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 = <T extends z.Schema>(\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;;;;;;;;;IAcO,MAAMA,8BAAAA,GAAiC,CAACC,GAAAA,GAAAA;AAC7C,IAAA,MAAMC,aAAa,CAACC,GAAAA,GAAAA;QAClB,OAAOA,GAAAA,CAAIC,MAAM,CAAC,CAAA,CAAA,CAAGC,WAAW,EAAA,GAAKF,GAAAA,CAAIG,KAAK,CAAC,CAAA,CAAA;AACjD,IAAA,CAAA;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,IAAA,CAAA;;IAGA,IAAIT,GAAAA,CAAIU,QAAQ,CAAC,IAAA,CAAA,EAAO;AACtB,QAAA,MAAM,CAACC,SAAAA,EAAW,GAAGC,KAAK,GAAGZ,GAAAA,CAAIO,KAAK,CAAC,IAAA,CAAA;AAC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