{"version":3,"file":"literal-validator.mjs","names":[],"sources":["../../../../../../../@warlock.js/seal/src/validators/literal-validator.ts"],"sourcesContent":["import { literalRule } from \"../rules/common/literal\";\nimport { applyNullable } from \"../standard-schema/json-schema\";\nimport type { JsonSchemaResult, JsonSchemaTarget } from \"../standard-schema/json-schema\";\nimport { BaseValidator } from \"./base-validator\";\n\n/**\n * Literal validator class\n *\n * Accepts a fixed tuple of primitive literal values (string, number, boolean).\n * The TypeScript type narrows to the union of those literals — `v.literal(\"a\", \"b\")`\n * infers as `\"a\" | \"b\"`, not `string`. Use for discriminator fields, enum-like\n * unions of constants, and any case where `oneOf([...])` would lose literal types.\n *\n * @example\n * v.literal(\"items\")               // type: \"items\"\n * v.literal(\"draft\", \"published\")  // type: \"draft\" | \"published\"\n * v.literal(1, 2, 3)               // type: 1 | 2 | 3\n * v.literal(true)                  // type: true\n */\nexport class LiteralValidator<\n  T extends readonly (string | number | boolean)[] = readonly (string | number | boolean)[],\n> extends BaseValidator {\n  public values: T;\n\n  public constructor(values: T, errorMessage?: string) {\n    super();\n    this.values = values;\n    this.addMutableRule(literalRule, errorMessage, { values });\n  }\n\n  /**\n   * Check if value is one of the configured literals\n   */\n  public matchesType(value: any): boolean {\n    return (this.values as readonly any[]).includes(value);\n  }\n\n  /**\n   * Clone the validator, preserving the literal `values` set.\n   *\n   * The base `clone()` only copies `BaseValidator` fields, so without this\n   * override a cloned literal loses its public `values` array — which breaks\n   * any consumer that reads it (e.g. `discriminatedUnion` branch routing).\n   */\n  public override clone(): this {\n    const cloned = super.clone();\n    cloned.values = this.values;\n    return cloned;\n  }\n\n  /**\n   * @inheritdoc\n   *\n   * Single literal → `{ const: <value> }`. Multiple → `{ enum: [...] }`.\n   *\n   * @example\n   * ```ts\n   * v.literal(\"items\").toJsonSchema()\n   * // → { const: \"items\" }\n   *\n   * v.literal(\"draft\", \"published\").toJsonSchema()\n   * // → { enum: [\"draft\", \"published\"] }\n   * ```\n   */\n  public override toJsonSchema(target: JsonSchemaTarget = \"draft-2020-12\"): JsonSchemaResult {\n    const schema: JsonSchemaResult =\n      this.values.length === 1 ? { const: this.values[0] } : { enum: [...this.values] };\n    if (this.isNullable) applyNullable(schema, target);\n    return schema;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,IAAa,mBAAb,cAEU,cAAc;CAGtB,AAAO,YAAY,QAAW,cAAuB;EACnD,MAAM;EACN,KAAK,SAAS;EACd,KAAK,eAAe,aAAa,cAAc,EAAE,OAAO,CAAC;CAC3D;;;;CAKA,AAAO,YAAY,OAAqB;EACtC,OAAQ,KAAK,OAA0B,SAAS,KAAK;CACvD;;;;;;;;CASA,AAAgB,QAAc;EAC5B,MAAM,SAAS,MAAM,MAAM;EAC3B,OAAO,SAAS,KAAK;EACrB,OAAO;CACT;;;;;;;;;;;;;;;CAgBA,AAAgB,aAAa,SAA2B,iBAAmC;EACzF,MAAM,SACJ,KAAK,OAAO,WAAW,IAAI,EAAE,OAAO,KAAK,OAAO,GAAG,IAAI,EAAE,MAAM,CAAC,GAAG,KAAK,MAAM,EAAE;EAClF,IAAI,KAAK,YAAY,cAAc,QAAQ,MAAM;EACjD,OAAO;CACT;AACF"}