{"version":3,"file":"scalar-validator.mjs","names":[],"sources":["../../../../../../../@warlock.js/seal/src/validators/scalar-validator.ts"],"sourcesContent":["import { invalidRule, VALID_RULE } from \"../helpers\";\r\nimport { numberMutator, stringMutator } from \"../mutators\";\r\nimport {\r\n  acceptedIfPresentRule,\r\n  acceptedIfRequiredRule,\r\n  acceptedIfRule,\r\n  acceptedRule,\r\n  acceptedUnlessRule,\r\n  acceptedWithoutRule,\r\n  declinedIfPresentRule,\r\n  declinedIfRequiredRule,\r\n  declinedIfRule,\r\n  declinedRule,\r\n  declinedUnlessRule,\r\n  declinedWithoutRule,\r\n} from \"../rules/scalar\";\r\nimport { PrimitiveValidator } from \"./primitive-validator\";\r\nimport { getRuleOptions } from \"../standard-schema/json-schema\";\r\nimport type { JsonSchemaResult, JsonSchemaTarget } from \"../standard-schema/json-schema\";\r\n\r\n/**\r\n * Scalar validator class\r\n *\r\n * Core validator for scalar values (string, number, boolean).\r\n * Extends PrimitiveValidator (inherits enum/in/oneOf/allowsOnly/forbids/notIn)\r\n * and additionally provides type-coercion mutators and accepted/declined rules.\r\n *\r\n * Database methods (unique, exists, etc.) are injected by the framework\r\n */\r\nexport class ScalarValidator extends PrimitiveValidator {\r\n  public constructor(errorMessage?: string) {\r\n    super();\r\n    this.addMutableRule(\r\n      {\r\n        name: \"scalar\",\r\n        defaultErrorMessage: \"The :input must be a scalar value\",\r\n        async validate(value, context) {\r\n          if ([\"string\", \"number\", \"boolean\"].includes(typeof value)) {\r\n            return VALID_RULE;\r\n          }\r\n          return invalidRule(this, context);\r\n        },\r\n      },\r\n      errorMessage,\r\n    );\r\n  }\r\n\r\n  /**\r\n   * Add matches type\r\n   */\r\n  public matchesType(value: any) {\r\n    return typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\";\r\n  }\r\n\r\n  /**\r\n   * Mutate the scalar value to be number\r\n   */\r\n  public asNumber() {\r\n    return this.addMutator(numberMutator);\r\n  }\r\n\r\n  /**\r\n   * Mutate the scalar value to be string\r\n   */\r\n  public asString() {\r\n    return this.addMutator(stringMutator);\r\n  }\r\n\r\n  /**\r\n   * Accepted value\r\n   * The value will be valid if it equals 1 | \"1\" | true | \"true\" | \"yes\" | \"y\" | \"on\"\r\n   */\r\n  public accepted(errorMessage?: string) {\r\n    return this.addRule(acceptedRule, errorMessage);\r\n  }\r\n\r\n  /**\r\n   * Accepted value if another field's value equals to a specific value\r\n   */\r\n  public acceptedIf(field: string, value: any, errorMessage?: string) {\r\n    return this.addRule(acceptedIfRule, errorMessage, { field, value });\r\n  }\r\n\r\n  /**\r\n   * Accepted value if another field's value is not equal to the given value\r\n   */\r\n  public acceptedUnless(field: string, value: any, errorMessage?: string) {\r\n    return this.addRule(acceptedUnlessRule, errorMessage, { field, value });\r\n  }\r\n\r\n  /**\r\n   * Accepted value if another field is required\r\n   */\r\n  public acceptedIfRequired(field: string, errorMessage?: string) {\r\n    return this.addRule(acceptedIfRequiredRule, errorMessage, { field });\r\n  }\r\n\r\n  /**\r\n   * Accepted value if another field is present\r\n   */\r\n  public acceptedIfPresent(field: string, errorMessage?: string) {\r\n    return this.addRule(acceptedIfPresentRule, errorMessage, { field });\r\n  }\r\n\r\n  /**\r\n   * Accepted value if another field is missing\r\n   */\r\n  public acceptedWithout(field: string, errorMessage?: string) {\r\n    return this.addRule(acceptedWithoutRule, errorMessage, { field });\r\n  }\r\n\r\n  /**\r\n   * Declined value\r\n   * The value will be valid if it equals 0 | \"0\" | false | \"false\" | \"no\" | \"n\" | \"off\"\r\n   */\r\n  public declined(errorMessage?: string) {\r\n    return this.addRule(declinedRule, errorMessage);\r\n  }\r\n\r\n  /**\r\n   * Declined value if another field's value equals to a specific value\r\n   */\r\n  public declinedIf(field: string, value: any, errorMessage?: string) {\r\n    return this.addRule(declinedIfRule, errorMessage, { field, value });\r\n  }\r\n\r\n  /**\r\n   * Declined value if another field's value is not equal to the given value\r\n   */\r\n  public declinedUnless(field: string, value: any, errorMessage?: string) {\r\n    return this.addRule(declinedUnlessRule, errorMessage, { field, value });\r\n  }\r\n\r\n  /**\r\n   * Declined value if another field is required\r\n   */\r\n  public declinedIfRequired(field: string, errorMessage?: string) {\r\n    return this.addRule(declinedIfRequiredRule, errorMessage, { field });\r\n  }\r\n\r\n  /**\r\n   * Declined value if another field is present\r\n   */\r\n  public declinedIfPresent(field: string, errorMessage?: string) {\r\n    return this.addRule(declinedIfPresentRule, errorMessage, { field });\r\n  }\r\n\r\n  /**\r\n   * Declined value if another field is missing\r\n   */\r\n  public declinedWithout(field: string, errorMessage?: string) {\r\n    return this.addRule(declinedWithoutRule, errorMessage, { field });\r\n  }\r\n\r\n  /**\r\n   * @inheritdoc\r\n   *\r\n   * A scalar accepts string | number | boolean. If `.in()` / `.enum()` is used,\r\n   * output collapses to a simple `enum` list instead.\r\n   *\r\n   * @example\r\n   * ```ts\r\n   * v.scalar().toJsonSchema(\"draft-2020-12\")\r\n   * // → { oneOf: [{ type: \"string\" }, { type: \"number\" }, { type: \"boolean\" }] }\r\n   *\r\n   * v.scalar().in([\"active\", \"inactive\"]).toJsonSchema(\"draft-2020-12\")\r\n   * // → { enum: [\"active\", \"inactive\"] }\r\n   * ```\r\n   */\r\n  public override toJsonSchema(_target: JsonSchemaTarget = \"draft-2020-12\"): JsonSchemaResult {\r\n    // If a value set is constrained, collapse to enum\r\n    const inOpts = getRuleOptions(this.rules, \"in\");\r\n    if (inOpts?.values && Array.isArray(inOpts.values)) {\r\n      return { enum: inOpts.values };\r\n    }\r\n\r\n    const enumOpts = getRuleOptions(this.rules, \"enum\");\r\n    if (enumOpts?.enum && Array.isArray(enumOpts.enum)) {\r\n      return { enum: enumOpts.enum };\r\n    }\r\n\r\n    return {\r\n      oneOf: [\r\n        { type: \"string\" },\r\n        { type: \"number\" },\r\n        { type: \"boolean\" },\r\n      ],\r\n    };\r\n  }\r\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6BA,IAAa,kBAAb,cAAqC,mBAAmB;CACtD,AAAO,YAAY,cAAuB;EAC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