{"version":3,"file":"accepted-rule.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/scalar/accepted-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 isAcceptedValue = (value: any) => {\r\n  return [\"1\", \"true\", \"yes\", \"y\", \"on\", 1, true, \"Yes\", \"Y\", \"On\"].includes(value);\r\n};\r\n\r\n/**\r\n * Validate value as accepted if it equals:\r\n * 1 | \"1\" | true | \"true\" | \"yes\" | \"y\" | \"on\"\r\n */\r\nexport const acceptedRule: SchemaRule = {\r\n  name: \"accepted\",\r\n  defaultErrorMessage: \"The :input must be accepted\",\r\n  description:\r\n    \"The value must be accepted if it equals: 1 | '1' | true | 'true' | 'yes' | 'y' | 'on' | 1 | true | 'Yes' | 'Y' | 'On'\",\r\n  async validate(value: any, context) {\r\n    if (isAcceptedValue(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 * Accepted value if another field's value equals to a specific value\r\n */\r\nexport const acceptedIfRule: SchemaRule<{ field: string; value: any }> = {\r\n  name: \"acceptedIf\",\r\n  description: \"The field must be accepted if :field's value equals to :value\",\r\n  defaultErrorMessage: \"The :input must be accepted\",\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 (!isAcceptedValue(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 * Accepted if another field is not equal to the given value\r\n */\r\nexport const acceptedUnlessRule: SchemaRule<{ field: string; value: any }> = {\r\n  name: \"acceptedUnless\",\r\n  description: \"The field must be accepted if :field's value is not equal to :value\",\r\n  defaultErrorMessage: \"The :input must be accepted\",\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 (!isAcceptedValue(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 * Accepted if another field is required\r\n */\r\nexport const acceptedIfRequiredRule: SchemaRule<{ field: string }> = {\r\n  name: \"acceptedIfRequired\",\r\n  description: \"The field must be accepted if :field is required\",\r\n  defaultErrorMessage: \"The :input must be accepted\",\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 (!isAcceptedValue(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 * Accepted if another field is present\r\n */\r\nexport const acceptedIfPresentRule: SchemaRule<{ field: string }> = {\r\n  name: \"acceptedIfPresent\",\r\n  description: \"The field must be accepted if :field is present\",\r\n  defaultErrorMessage: \"The :input must be accepted\",\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 (!isAcceptedValue(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 * Accepted if another field is missing\r\n */\r\nexport const acceptedWithoutRule: SchemaRule<{ field: string }> = {\r\n  name: \"acceptedWithout\",\r\n  description: \"The field must be accepted if :field is missing\",\r\n  defaultErrorMessage: \"The :input must be accepted\",\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 (!isAcceptedValue(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;EAAQ;EAAO;EAAK;EAAM;EAAG;EAAM;EAAO;EAAK;CAAI,CAAC,CAAC,SAAS,KAAK;AAClF;;;;;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;EAC7D,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,m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