{"version":3,"file":"validation-helpers.mjs","names":[],"sources":["../../../../../../../@warlock.js/seal/src/helpers/validation-helpers.ts"],"sourcesContent":["import { getSealConfig } from \"../config\";\nimport type { ContextualSchemaRule, RuleResult, RuleTranslation, SchemaContext } from \"../types\";\n\nexport const VALID_RULE: RuleResult = {\n  isValid: true,\n};\n/**\n * Resolve a single attribute value through the full translation priority chain:\n * 1. attributesList[key]       — developer direct text override (highest priority)\n * 2. translatedAttributes[key] — developer explicit translation key\n * 3. translator(rawValue)      — auto-translate the raw value (fallback)\n *\n * Use this in rule bodies when you need per-item translation\n * (e.g. translating each enum value before joining them).\n *\n * @example\n * // Translate each enum value individually then join\n * const enumList = enumValues\n *   .map(v => resolveTranslation(String(v), String(v), this, context))\n *   .join(\", \");\n * this.context.translationParams.enumList = enumList;\n */\nexport const resolveTranslation = ({\n  key,\n  rawValue,\n  rule,\n  context,\n}: {\n  key: string;\n  rawValue: any;\n  rule: ContextualSchemaRule;\n  context: SchemaContext;\n}): string => {\n  const translator = getSealConfig().translateAttribute;\n\n  // 1. Direct text override wins\n  const direct = rule.context.attributesList?.[key];\n  if (direct && typeof direct === \"string\") return direct;\n\n  if (!translator) return String(rawValue ?? key);\n\n  // 2. Explicit translation key\n  const transKey = rule.context.translatedAttributes?.[key];\n  if (transKey) return translator({ attribute: transKey, context, rule });\n\n  // 3. Auto-translate the raw value (fallback)\n  return translator({ attribute: String(rawValue ?? key), context, rule });\n};\n\n// Internal alias — invalidRule uses the same function without re-importing config\nconst resolveAttribute = resolveTranslation;\n\nexport const invalidRule = (rule: ContextualSchemaRule, context: SchemaContext): RuleResult => {\n  // `input` is always translatable — rules may add more (e.g., field references in sameAs)\n  // Rule-defined translatableParams override the default input if the key matches\n  const translatableWithInput: Record<string, string> = {\n    // Fall back to \"schema\" when validating at the root level (no key)\n    input: context.key || \"schema\",\n    ...rule.context.translatableParams,\n  };\n\n  // Resolve all translatable params through the full priority chain\n  const resolvedParams = Object.fromEntries(\n    Object.entries(translatableWithInput).map(([key, rawValue]) => [\n      key,\n      resolveAttribute({ key, rawValue, rule, context }),\n    ]),\n  );\n\n  const attributes: RuleTranslation[\"attributes\"] = {\n    path: context.path,\n    key: context.key,\n    value: context.value,\n    // Raw placeholders (:enumList, :min, :max, etc.) — no translation\n    ...rule.context.translationParams,\n    // Translated placeholders (:input, :field, etc.) — override raws if key collides\n    ...resolvedParams,\n    // Satisfy TypeScript's required field (always present via resolvedParams)\n    input: resolvedParams.input,\n  };\n\n  const rawError =\n    rule.context.errorMessage ||\n    rule.errorMessage ||\n    context.translateRule?.({ rule, context, attributes }) ||\n    rule.defaultErrorMessage!;\n\n  // Fallback interpolation: replace :placeholder patterns from attributes\n  // This kicks in when translateRule is absent or returns \"\" (not configured)\n  const error = rawError.replace(/:([a-zA-Z_]+)/g, (match, key) =>\n    key in attributes ? String(attributes[key as keyof typeof attributes]) : match,\n  );\n\n  return {\n    isValid: false,\n    error,\n    input: attributes.input, // use resolved input, not raw context.key (may be \"\")\n    path: context.path,\n  };\n};\n"],"mappings":";;;AAGA,MAAa,aAAyB,EACpC,SAAS,KACX;;;;;;;;;;;;;;;;;AAiBA,MAAa,sBAAsB,EACjC,KACA,UACA,MACA,cAMY;CACZ,MAAM,aAAa,cAAc,CAAC,CAAC;CAGnC,MAAM,SAAS,KAAK,QAAQ,iBAAiB;CAC7C,IAAI,UAAU,OAAO,WAAW,UAAU,OAAO;CAEjD,IAAI,CAAC,YAAY,OAAO,OAAO,YAAY,GAAG;CAG9C,MAAM,WAAW,KAAK,QAAQ,uBAAuB;CACrD,IAAI,UAAU,OAAO,WAAW;EAAE,WAAW;EAAU;EAAS;CAAK,CAAC;CAGtE,OAAO,WAAW;EAAE,WAAW,OAAO,YAAY,GAAG;EAAG;EAAS;CAAK,CAAC;AACzE;AAGA,MAAM,mBAAmB;AAEzB,MAAa,eAAe,MAA4B,YAAuC;CAG7F,MAAM,wBAAgD;EAEpD,OAAO,QAAQ,OAAO;EACtB,GAAG,KAAK,QAAQ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