{"version":3,"file":"required-with-rules.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/conditional/required-with-rules.ts"],"sourcesContent":["import { get } from \"@mongez/reinforcements\";\r\nimport { getFieldValue, invalidRule, VALID_RULE } from \"../../helpers\";\r\nimport { isEmptyValue } from \"../../helpers/is-empty-value\";\r\nimport type { SchemaRule } from \"../../types\";\r\n\r\n/**\r\n * Required with rule - field is required if another field is present\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredWithRule: SchemaRule<{\r\n  field: string;\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredWith\",\r\n  description: \"The field is required if another field is present\",\r\n  sortOrder: -2,\r\n  requiresValue: false,\r\n  defaultErrorMessage: \"The :input is required\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n\r\n    // Field is required if the other field is present\r\n    if (isEmptyValue(value) && 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 * Required with all rule - field is required if all specified fields are present\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredWithAllRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredWithAll\",\r\n  description: \"The field is required if all specified fields are present\",\r\n  sortOrder: -2,\r\n  requiresValue: false,\r\n  defaultErrorMessage: \"The :input is required\",\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 is required if all other fields are present\r\n    if (isEmptyValue(value) && allPresent) {\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 * Required with any rule - field is required if any of the specified fields is present\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredWithAnyRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredWithAny\",\r\n  description: \"The field is required if any of the specified fields is present\",\r\n  sortOrder: -2,\r\n  requiresValue: false,\r\n  defaultErrorMessage: \"The :input is required\",\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 is required if any other field is present\r\n    if (isEmptyValue(value) && anyPresent) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n"],"mappings":";;;;;;;;;;;AASA,MAAa,mBAGR;CACH,MAAM;CACN,aAAa;CACb,WAAW;CACX,eAAe;CACf,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,aAAa,cAAc,MAAM,OAAO;EAG9C,IAAI,aAAa,KAAK,KAAK,eAAe,QAAW;GACnD,KAAK,QAAQ,mBAAmB,QAAQ,KAAK,QAAQ,QAAQ;GAC7D,OAAO,YAAY,MAAM,OAAO;EAClC;EAEA,OAAO;CACT;AACF;;;;;AAMA,MAAa,sBAGR;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,aAAa,KAAK,KAAK,YACzB,OAAO,YAAY,MAAM,OAAO;EAGlC,OAAO;CACT;AACF;;;;;AAMA,MAAa,sBAGR;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,MAAM,UAAU,IAAI,QAAQ,KAAK,MAAM,MAAS;EAG1E,IAAI,aAAa,KAAK,KAAK,YACzB,OAAO,YAAY,MAAM,OAAO;EAGlC,OAAO;CACT;AACF"}