{"version":3,"file":"declined-rule.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/scalar/declined-rule.ts"],"sourcesContent":["import { getFieldValue, invalidRule, VALID_RULE } from \"../../helpers\";\r\nimport { isEmptyValue } from \"../../helpers/is-empty-value\";\r\nimport type { SchemaRule } from \"../../types\";\r\n\r\nconst isDeclinedValue = (value: any) => {\r\n  return [\"0\", \"false\", \"no\", \"n\", \"off\", 0, false, \"No\", \"N\", \"Off\"].includes(value);\r\n};\r\n\r\n/**\r\n * Validate value as declined if it equals:\r\n * 0 | \"0\" | false | \"false\" | \"no\" | \"n\" | \"off\" | 0 | false | \"No\" | \"N\" | \"Off\"\r\n */\r\nexport const declinedRule: SchemaRule = {\r\n  name: \"declined\",\r\n  defaultErrorMessage: \"The :input must be declined\",\r\n  description:\r\n    \"The value must be declined if it equals: 0 | '0' | false | 'false' | 'no' | 'n' | 'off' | 0 | false | 'No' | 'N' | 'Off'\",\r\n  async validate(value: any, context) {\r\n    if (isDeclinedValue(value)) {\r\n      return VALID_RULE;\r\n    }\r\n\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * Declined value if another field's value equals to a specific value\r\n */\r\nexport const declinedIfRule: SchemaRule<{ field: string; value: any }> = {\r\n  name: \"declinedIf\",\r\n  description: \"The field must be declined if :field's value equals to :value\",\r\n  defaultErrorMessage: \"The :input must be declined\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n    const { value: expectedValue } = this.context.options;\r\n\r\n    this.context.translatableParams.field = this.context.options.field;\r\n    this.context.translatableParams.value = this.context.options.value;\r\n\r\n    if (fieldValue !== expectedValue) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    if (!isDeclinedValue(value)) {\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 * Declined if another field is not equal to the given value\r\n */\r\nexport const declinedUnlessRule: SchemaRule<{ field: string; value: any }> = {\r\n  name: \"declinedUnless\",\r\n  description: \"The field must be declined if :field's value is not equal to :value\",\r\n  defaultErrorMessage: \"The :input must be declined\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n    const { value: expectedValue } = this.context.options;\r\n\r\n    this.context.translatableParams.field = this.context.options.field;\r\n    this.context.translatableParams.value = this.context.options.value;\r\n    if (fieldValue === expectedValue) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    if (!isDeclinedValue(value)) {\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 * Declined if another field is required\r\n */\r\nexport const declinedIfRequiredRule: SchemaRule<{ field: string }> = {\r\n  name: \"declinedIfRequired\",\r\n  description: \"The field must be declined if :field is required\",\r\n  defaultErrorMessage: \"The :input must be declined\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n    this.context.translatableParams.field = this.context.options.field;\r\n\r\n    if (isEmptyValue(fieldValue)) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    if (!isDeclinedValue(value)) {\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 * Declined if another field is present\r\n */\r\nexport const declinedIfPresentRule: SchemaRule<{ field: string }> = {\r\n  name: \"declinedIfPresent\",\r\n  description: \"The field must be declined if :field is present\",\r\n  defaultErrorMessage: \"The :input must be declined\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n    this.context.translatableParams.field = this.context.options.field;\r\n\r\n    if (fieldValue === undefined) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    if (!isDeclinedValue(value)) {\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 * Declined if another field is missing\r\n */\r\nexport const declinedWithoutRule: SchemaRule<{ field: string }> = {\r\n  name: \"declinedWithout\",\r\n  description: \"The field must be declined if :field is missing\",\r\n  defaultErrorMessage: \"The :input must be declined\",\r\n  async validate(value: any, context) {\r\n    const fieldValue = getFieldValue(this, context);\r\n    this.context.translatableParams.field = this.context.options.field;\r\n\r\n    if (fieldValue !== undefined) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    if (!isDeclinedValue(value)) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    return VALID_RULE;\r\n  },\r\n};\r\n"],"mappings":";;;;;;AAIA,MAAM,mBAAmB,UAAe;CACtC,OAAO;EAAC;EAAK;EAAS;EAAM;EAAK;EAAO;EAAG;EAAO;EAAM;EAAK;CAAK,CAAC,CAAC,SAAS,KAAK;AACpF;;;;;AAMA,MAAa,eAA2B;CACtC,MAAM;CACN,qBAAqB;CACrB,aACE;CACF,MAAM,SAAS,OAAY,SAAS;EAClC,IAAI,gBAAgB,KAAK,GACvB,OAAO;EAGT,OAAO,YAAY,MAAM,OAAO;CAClC;AACF;;;;AAKA,MAAa,iBAA4D;CACvE,MAAM;CACN,aAAa;CACb,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,aAAa,cAAc,MAAM,OAAO;EAC9C,MAAM,EAAE,OAAO,kBAAkB,KAAK,QAAQ;EAE9C,KAAK,QAAQ,mBAAmB,QAAQ,KAAK,QAAQ,QAAQ;EAC7D,KAAK,QAAQ,mBAAmB,QAAQ,KAAK,QAAQ,QAAQ;EAE7D,IAAI,eAAe,eACjB,OAAO,YAAY,MAAM,OAAO;EAGlC,IAAI,CAAC,gBAAgB,KAAK,GACxB,OAAO,YAAY,MAAM,OAAO;EAGlC,OAAO;CACT;AACF;;;;AAKA,MAAa,qBAAgE;CAC3E,MAAM;CACN,aAAa;CACb,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,MAAM,aAAa,cAAc,MAAM,OAAO;EAC9C,MAAM,EAAE,OAAO,kBAAkB,KAAK,QAAQ;EAE9C,KAAK,QAAQ,mBAAmB,QAAQ,KAA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