{"version":3,"file":"required-if-rules.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/conditional/required-if-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 if rule - field is required if another field equals a specific value\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfRule: SchemaRule<{\r\n  field: string;\r\n  value: any;\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIf\",\r\n  description: \"The field is required if another field equals a specific value\",\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 { value: expectedValue } = this.context.options;\r\n    const fieldValue = getFieldValue(this, context);\r\n\r\n    // Field is required if the other field equals the expected value\r\n    if (isEmptyValue(value) && fieldValue === expectedValue) {\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 if empty rule - field is required if another field is empty\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfEmptyRule: SchemaRule<{\r\n  field: string;\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfEmpty\",\r\n  description: \"The field is required if :field is empty\",\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 empty\r\n    if (isEmptyValue(value) && isEmptyValue(fieldValue)) {\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 if not empty rule - field is required if another field is not empty\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfNotEmptyRule: SchemaRule<{\r\n  field: string;\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfNotEmpty\",\r\n  description: \"The field is required if :field is not empty\",\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 not empty\r\n    if (isEmptyValue(value) && !isEmptyValue(fieldValue)) {\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 if in rule - field is required if another field's value is in the given array\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfInRule: SchemaRule<{\r\n  field: string;\r\n  values: any[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfIn\",\r\n  description: \"The field is required if :field value is in the given array\",\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 { values } = this.context.options;\r\n    const fieldValue = getFieldValue(this, context);\r\n\r\n    // Field is required if the other field's value is in the array\r\n    if (isEmptyValue(value) && values.includes(fieldValue)) {\r\n      this.context.translatableParams.field = this.context.options.field;\r\n      this.context.translationParams.values = this.context.options.values.join(\", \");\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 if not in rule - field is required if another field's value is NOT in the given array\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfNotInRule: SchemaRule<{\r\n  field: string;\r\n  values: any[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfNotIn\",\r\n  description: \"The field is required if another field's value is NOT in the given array\",\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 { values } = this.context.options;\r\n    const fieldValue = getFieldValue(this, context);\r\n\r\n    // Field is required if the other field's value is NOT in the array\r\n    if (isEmptyValue(value) && !values.includes(fieldValue)) {\r\n      this.context.translatableParams.field = this.context.options.field;\r\n      this.context.translationParams.values = this.context.options.values.join(\", \");\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 * Get multiple field values based on scope\r\n */\r\nfunction getFieldsValues(\r\n  rule: SchemaRule<{ fields: string[]; scope?: \"global\" | \"sibling\" }>,\r\n  context: any,\r\n): any[] {\r\n  const { fields, scope = \"global\" } = (rule as any).context.options;\r\n  const source = scope === \"sibling\" ? context.parent : context.allValues;\r\n  return fields.map((field: string) => get(source, field));\r\n}\r\n\r\n/**\r\n * Required if all empty rule - field is required if ALL specified fields are empty\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfAllEmptyRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfAllEmpty\",\r\n  description: \"The field is required if all :fields are empty\",\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 fieldValues = getFieldsValues(this, context);\r\n\r\n    // Field is required if ALL other fields are empty\r\n    const allEmpty = fieldValues.every((v) => isEmptyValue(v));\r\n    if (isEmptyValue(value) && allEmpty) {\r\n      this.context.options.fields.forEach((field) => {\r\n        this.context.translatableParams.field = field;\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 * Required if any empty rule - field is required if ANY of the specified fields is empty\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfAnyEmptyRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfAnyEmpty\",\r\n  description: \"The field is required if any of the :fields is empty\",\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 fieldValues = getFieldsValues(this, context);\r\n\r\n    // Field is required if ANY other field is empty\r\n    const anyEmpty = fieldValues.some((v) => isEmptyValue(v));\r\n    if (isEmptyValue(value) && anyEmpty) {\r\n      this.context.options.fields.forEach((field) => {\r\n        this.context.translatableParams.field = field;\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 * Required if all not empty rule - field is required if ALL specified fields are NOT empty\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfAllNotEmptyRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfAllNotEmpty\",\r\n  description: \"The field is required if all :fields are not empty\",\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 fieldValues = getFieldsValues(this, context);\r\n\r\n    // Field is required if ALL other fields are NOT empty\r\n    const allNotEmpty = fieldValues.every((v) => !isEmptyValue(v));\r\n    if (isEmptyValue(value) && allNotEmpty) {\r\n      this.context.options.fields.forEach((field) => {\r\n        this.context.translatableParams.field = field;\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 * Required if any not empty rule - field is required if ANY of the specified fields is NOT empty\r\n * Supports both global and sibling scope\r\n */\r\nexport const requiredIfAnyNotEmptyRule: SchemaRule<{\r\n  fields: string[];\r\n  scope?: \"global\" | \"sibling\";\r\n}> = {\r\n  name: \"requiredIfAnyNotEmpty\",\r\n  description: \"The field is required if any of the :fields is not empty\",\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 fieldValues = getFieldsValues(this, context);\r\n\r\n    // Field is required if ANY other field is NOT empty\r\n    const anyNotEmpty = fieldValues.some((v) => !isEmptyValue(v));\r\n    if (isEmptyValue(value) && anyNotEmpty) {\r\n      this.context.options.fields.forEach((field) => {\r\n        this.context.translatableParams.field = field;\r\n      });\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n"],"mappings":";;;;;;;;;;;AASA,MAAa,iBAIR;CACH,MAAM;CACN,aAAa;CACb,WAAW;CACX,eAAe;CACf,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,EAAE,OAAO,kBAAkB,KAAK,QAAQ;EAC9C,MAAM,aAAa,cAAc,MAAM,OAAO;EAG9C,IAAI,aAAa,KAAK,KAAK,eAAe,eAAe;GACvD,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,aAAa,cAAc,MAAM,OAAO;EAG9C,IAAI,aAAa,KAAK,KAAK,aAAa,UAAU,GAAG;GACnD,KAAK,QAAQ,mBAAmB,QAAQ,KAAK,QAAQ,QAAQ;GAC7D,OAAO,YAAY,MAAM,OAAO;EAClC;EAEA,OAAO;CACT;AACF;;;;;AAMA,MAAa,yBAGR;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,CAAC,aAAa,UAAU,GAAG;GACpD,KAAK,QAAQ,mBAAmB,QAAQ,KAAK,QAAQ,QAAQ;GAC7D,OAAO,YAAY,MAAM,OAAO;EAClC;EAEA,OAAO;CACT;AACF;;;;;AA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