{"version":3,"file":"computed-validator.mjs","names":[],"sources":["../../../../../../../@warlock.js/seal/src/validators/computed-validator.ts"],"sourcesContent":["import type { SchemaContext, ValidationResult } from \"../types\";\nimport { BaseValidator } from \"./base-validator\";\nimport type { JsonSchemaResult, JsonSchemaTarget } from \"../standard-schema/json-schema\";\n\n/**\n * Callback function for computed fields\n * Receives validated & mutated data and full schema context\n */\nexport type ComputedCallback<TResult = any> = (\n  data: any,\n  context: SchemaContext,\n) => TResult | Promise<TResult>;\n\n/**\n * Computed field validator\n *\n * Computes a value based on other validated fields in the schema.\n * The computed value is persisted and can optionally be validated.\n *\n * @example\n * ```ts\n * // Basic computed field\n * const schema = v.object({\n *   title: v.string().required(),\n *   slug: v.computed(data => slugify(data.title)),\n * });\n *\n * // With result validation\n * const schema = v.object({\n *   title: v.string().required(),\n *   slug: v.computed(\n *     data => slugify(data.title),\n *     v.string().minLength(3)\n *   ),\n * });\n *\n * // Async computation\n * const schema = v.object({\n *   image: v.string().url(),\n *   thumbnail: v.computed(async data => {\n *     return await generateThumbnail(data.image);\n *   }),\n * });\n * ```\n */\nexport class ComputedValidator<TResult = any> extends BaseValidator {\n  /**\n   * Create a new computed field validator\n   *\n   * @param callback - Function to compute the value from validated data\n   * @param resultValidator - Optional validator to validate the computed result\n   */\n  public constructor(\n    protected callback: ComputedCallback<TResult>,\n    protected resultValidator?: BaseValidator,\n  ) {\n    super();\n  }\n\n  /**\n   * Execute the callback and optionally validate the result\n   */\n  public async validate(data: any, context: SchemaContext): Promise<ValidationResult> {\n    try {\n      // Execute the callback with validated data\n      const result = await this.callback(data, context);\n\n      // Optionally validate the computed result\n      if (this.resultValidator) {\n        const validation = await this.resultValidator.validate(result, context);\n\n        if (!validation.isValid) {\n          return {\n            isValid: false,\n            errors: validation.errors,\n            data: undefined,\n          };\n        }\n\n        return {\n          isValid: true,\n          errors: [],\n          data: validation.data,\n        };\n      }\n\n      // No validation - return computed result directly\n      return {\n        isValid: true,\n        errors: [],\n        data: result,\n      };\n    } catch (error) {\n      // Handle callback execution errors\n      return {\n        isValid: false,\n        errors: [\n          {\n            type: \"computed\",\n            error: error instanceof Error ? error.message : \"Computed field callback failed\",\n            input: context.path,\n          },\n        ],\n        data: undefined,\n      };\n    }\n  }\n\n  /**\n   * Clone this validator with all its configuration\n   * Critical for ObjectValidator.clone(), extend(), merge(), etc.\n   */\n  public override clone(): this {\n    const cloned = super.clone();\n\n    // Copy ComputedValidator-specific properties\n    cloned.callback = this.callback; // Functions are safe to copy by reference\n    cloned.resultValidator = this.resultValidator?.clone(); // Deep clone validator\n\n    return cloned;\n  }\n\n  /**\n   * Computed fields don't have a specific type to match\n   */\n  public matchesType(value: any): boolean {\n    return true; // Computed fields accept any input (they generate their own value)\n  }\n\n  /**\n   * @inheritdoc\n   *\n   * Computed fields are server-side runtime values — they have no input schema\n   * and cannot be represented in JSON Schema. Calling this method is always a\n   * programming error.\n   *\n   * @throws Error Always throws — computed/managed fields have no JSON Schema representation.\n   */\n  public override toJsonSchema(_target: JsonSchemaTarget = \"draft-2020-12\"): JsonSchemaResult {\n    throw new Error(\n      `[Seal] toJsonSchema() is not supported on ComputedValidator / ManagedValidator. ` +\n      `Computed fields are runtime-only and have no input JSON Schema representation. ` +\n      `ObjectValidator.toJsonSchema() automatically skips computed fields — ` +\n      `do not call toJsonSchema() on a computed validator directly.`,\n    );\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,IAAa,oBAAb,cAAsD,cAAc;;;;;;;CAOlE,AAAO,YACL,AAAU,UACV,AAAU,iBACV;EACA,MAAM;EAHI;EACA;CAGZ;;;;CAKA,MAAa,SAAS,MAAW,SAAmD;EAClF,IAAI;GAEF,MAAM,SAAS,MAAM,KAAK,SAAS,MAAM,OAAO;GAGhD,IAAI,KAAK,iBAAiB;IACxB,MAAM,aAAa,MAAM,KAAK,gBAAgB,SAAS,QAAQ,OAAO;IAEtE,IAAI,CAAC,WAAW,SACd,OAAO;KACL,SAAS;KACT,QAAQ,WAAW;KACnB,MAAM;IACR;IAGF,OAAO;KACL,SAAS;KACT,QAAQ,CAAC;KACT,MAAM,WAAW;IACnB;GACF;GAGA,OAAO;IACL,SAAS;IACT,QAAQ,CAAC;IACT,MAAM;GACR;EACF,SAAS,OAAO;GAEd,OAAO;IACL,SAAS;IACT,QAAQ,CACN;KACE,MAAM;KACN,OAAO,iBAAiB,QAAQ,MAAM,UAAU;KAChD,OAAO,QAAQ;IACjB,CACF;IACA,MAAM;GACR;EACF;CACF;;;;;CAMA,AAAgB,QAAc;EAC5B,MAAM,SAAS,MAAM,MAAM;EAG3B,OAAO,WAAW,KAAK;EACvB,OAAO,kBAAkB,KAAK,iBAAiB,MAAM;EAErD,OAAO;CACT;;;;CAKA,AAAO,YAAY,OAAqB;EACtC,OAAO;CACT;;;;;;;;;;CAWA,AAAgB,aAAa,UAA4B,iBAAmC;EAC1F,MAAM,IAAI,MACR,kSAIF;CACF;AACF"}