{"version":3,"file":"when.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/core/when.ts"],"sourcesContent":["import { get } from \"@mongez/reinforcements\";\r\nimport { invalidRule, VALID_RULE } from \"../../helpers\";\r\nimport type { SchemaRule, WhenRuleOptions } from \"../../types\";\r\n\r\n/**\r\n * When rule - conditional validation based on another field value\r\n */\r\nexport const whenRule: SchemaRule<WhenRuleOptions> = {\r\n  name: \"when\",\r\n  description: \"Apply conditional validation based on another field value\",\r\n  async validate(value: any, context) {\r\n    const fieldToCheck = this.context.options.field;\r\n    const conditions = this.context.options.is;\r\n    const otherwise = this.context.options.otherwise;\r\n    const scope = this.context.options.scope || \"global\";\r\n\r\n    const fieldValue =\r\n      scope === \"global\"\r\n        ? get(context.allValues, fieldToCheck)\r\n        : get(context.parent, fieldToCheck);\r\n\r\n    // Convert field value to string for key lookup\r\n    // This handles boolean, number, and string field values\r\n    const fieldValueKey = String(fieldValue);\r\n\r\n    if (conditions[fieldValueKey]) {\r\n      const result = await conditions[fieldValueKey].validate(value, context);\r\n      if (result.isValid) {\r\n        return VALID_RULE;\r\n      }\r\n\r\n      // Safe error access\r\n      this.context.errorMessage =\r\n        result.errors?.[0]?.error || \"Validation failed\";\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    if (otherwise) {\r\n      const result = await otherwise.validate(value, context);\r\n\r\n      if (result.isValid) {\r\n        return VALID_RULE;\r\n      }\r\n\r\n      // Safe error access\r\n      this.context.errorMessage =\r\n        result.errors?.[0]?.error || \"Validation failed\";\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n"],"mappings":";;;;;;;;AAOA,MAAa,WAAwC;CACnD,MAAM;CACN,aAAa;CACb,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,eAAe,KAAK,QAAQ,QAAQ;EAC1C,MAAM,aAAa,KAAK,QAAQ,QAAQ;EACxC,MAAM,YAAY,KAAK,QAAQ,QAAQ;EAGvC,MAAM,cAFQ,KAAK,QAAQ,QAAQ,SAAS,cAGhC,WACN,IAAI,QAAQ,WAAW,YAAY,IACnC,IAAI,QAAQ,QAAQ,YAAY;EAItC,MAAM,gBAAgB,OAAO,UAAU;EAEvC,IAAI,WAAW,gBAAgB;GAC7B,MAAM,SAAS,MAAM,WAAW,cAAc,CAAC,SAAS,OAAO,OAAO;GACtE,IAAI,OAAO,SACT,OAAO;GAIT,KAAK,QAAQ,eACX,OAAO,SAAS,EAAE,EAAE,SAAS;GAC/B,OAAO,YAAY,MAAM,OAAO;EAClC;EAEA,IAAI,WAAW;GACb,MAAM,SAAS,MAAM,UAAU,SAAS,OAAO,OAAO;GAEtD,IAAI,OAAO,SACT,OAAO;GAIT,KAAK,QAAQ,eACX,OAAO,SAAS,EAAE,EAAE,SAAS;GAC/B,OAAO,YAAY,MAAM,OAAO;EAClC;EAEA,OAAO;CACT;AACF"}