{"version":3,"file":"present-with-rules.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/conditional/present-with-rules.ts"],"sourcesContent":["import { get } from \"@mongez/reinforcements\";\r\nimport { getFieldValue, invalidRule, VALID_RULE } from \"../../helpers\";\r\nimport type { SchemaRule } from \"../../types\";\r\n\r\n/**\r\n * Present with rule - field must be present if another field is present\r\n * Supports both global and sibling scope\r\n */\r\nexport const presentWithRule: SchemaRule<{\r\n  field: string;\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"presentWith\",\r\n  description: \"The field must be present if another field is present\",\r\n  sortOrder: -2,\r\n  requiresValue: false,\r\n  defaultErrorMessage: \"The :input field must be present\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n\r\n    // The field must be present if the other field is present\r\n    if (value === undefined && fieldValue !== undefined) {\r\n      this.context.translatableParams.field = this.context.options.field;\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n\r\n/**\r\n * Present with all rule - field must be present if all specified fields are present\r\n * Supports both global and sibling scope\r\n */\r\nexport const presentWithAllRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"presentWithAll\",\r\n  description: \"The field must be present if all specified fields are present\",\r\n  sortOrder: -2,\r\n  requiresValue: false,\r\n  defaultErrorMessage: \"The :input field must be present\",\r\n  async validate(value: any, context) {\r\n    const { fields, scope = \"global\" } = this.context.options;\r\n    const source = scope === \"sibling\" ? context.parent : context.allValues;\r\n\r\n    // Check if all fields are present\r\n    const allPresent = fields.every((field) => get(source, field) !== undefined);\r\n\r\n    // Field must be present if all other fields are present\r\n    if (value === undefined && allPresent) {\r\n      fields.forEach((field) => {\r\n        this.context.translatableParams.field = field;\r\n      });\r\n\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n\r\n/**\r\n * Present with any rule - field must be present if any of the specified fields is present\r\n * Supports both global and sibling scope\r\n */\r\nexport const presentWithAnyRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"presentWithAny\",\r\n  description: \"The field must be present if any of the specified fields is present\",\r\n  sortOrder: -2,\r\n  requiresValue: false,\r\n  defaultErrorMessage: \"The :input field must be present\",\r\n  async validate(value: any, context) {\r\n    const { fields, scope = \"global\" } = this.context.options;\r\n    const source = scope === \"sibling\" ? context.parent : context.allValues;\r\n\r\n    // Check if any field is present\r\n    const anyPresent = fields.some((field) => get(source, field) !== undefined);\r\n\r\n    // Field must be present if any other field is present\r\n    if (value === undefined && anyPresent) {\r\n      fields.forEach((field) => {\r\n        this.context.translatableParams.field = field;\r\n      });\r\n\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n"],"mappings":";;;;;;;;;;AAQA,MAAa,kBAGR;CACH,MAAM;CACN,aAAa;CACb,WAAW;CACX,eAAe;CACf,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,aAAa,cAAc,MAAM,OAAO;EAG9C,IAAI,UAAU,UAAa,eAAe,QAAW;GACnD,KAAK,QAAQ,mBAAmB,QAAQ,KAAK,QAAQ,QAAQ;GAC7D,OAAO,YAAY,MAAM,OAAO;EAClC;EAEA,OAAO;CACT;AACF;;;;;AAMA,MAAa,qBAGR;CACH,MAAM;CACN,aAAa;CACb,WAAW;CACX,eAAe;CACf,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,EAAE,QAAQ,QAAQ,aAAa,KAAK,QAAQ;EAClD,MAAM,SAAS,UAAU,YAAY,QAAQ,SAAS,QAAQ;EAG9D,MAAM,aAAa,OAAO,OAAO,UAAU,IAAI,QAAQ,KAAK,MAAM,MAAS;EAG3E,IAAI,UAAU,UAAa,YAAY;GACrC,OAAO,SAAS,UAAU;IACxB,KAAK,QAAQ,mBAAmB,QAAQ;GAC1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