{"version":3,"file":"union.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/core/union.ts"],"sourcesContent":["import { invalidRule, VALID_RULE } from \"../../helpers\";\r\nimport type { SchemaRule } from \"../../types\";\r\nimport type { BaseValidator } from \"../../validators/base-validator\";\r\n\r\n/**\r\n * Union rule - value must match at least one of the provided validators\r\n */\r\nexport const unionRule: SchemaRule<{ validators: BaseValidator[] }> = {\r\n  name: \"union\",\r\n  defaultErrorMessage: \"Value must match one of the allowed types\",\r\n  async validate(value: any, context) {\r\n    const validators = this.context.options.validators;\r\n    const firstErrorOnly = context.configurations?.firstErrorOnly ?? true;\r\n    const allErrors: string[] = [];\r\n\r\n    // Try each validator\r\n    for (const validator of validators) {\r\n      // Skip if type doesn't match (optimization)\r\n      if (!validator.matchesType(value)) {\r\n        continue;\r\n      }\r\n\r\n      // Type matches - validate\r\n      const result = await validator.validate(value, context);\r\n\r\n      if (result.isValid) {\r\n        // Success! Validator matched and validated\r\n        return VALID_RULE;\r\n      }\r\n\r\n      // Failed - collect error message\r\n      const errorMsg = result.errors?.[0]?.error || \"Validation failed\";\r\n      allErrors.push(errorMsg);\r\n\r\n      // If firstErrorOnly, stop after first failed validator\r\n      if (firstErrorOnly) {\r\n        break;\r\n      }\r\n    }\r\n\r\n    // All failed or no validator matched the type\r\n    if (allErrors.length > 0) {\r\n      // At least one validator matched type but failed validation\r\n      this.context.errorMessage = firstErrorOnly\r\n        ? allErrors[0]\r\n        : allErrors.join(\"; \");\r\n    }\r\n\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n"],"mappings":";;;;;;;AAOA,MAAa,YAAyD;CACpE,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,aAAa,KAAK,QAAQ,QAAQ;EACxC,MAAM,iBAAiB,QAAQ,gBAAgB,kBAAkB;EACjE,MAAM,YAAsB,CAAC;EAG7B,KAAK,MAAM,aAAa,YAAY;GAElC,IAAI,CAAC,UAAU,YAAY,KAAK,GAC9B;GAIF,MAAM,SAAS,MAAM,UAAU,SAAS,OAAO,OAAO;GAEtD,IAAI,OAAO,SAET,OAAO;GAIT,MAAM,WAAW,OAAO,SAAS,EAAE,EAAE,SAAS;GAC9C,UAAU,KAAK,QAAQ;GAGvB,IAAI,gBACF;EAEJ;EAGA,IAAI,UAAU,SAAS,GAErB,KAAK,QAAQ,eAAe,iBACxB,UAAU,KACV,UAAU,KAAK,IAAI;EAGzB,OAAO,YAAY,MAAM,OAAO;CAClC;AACF"}