From b771d3173faf607dbd94496bb0025ae7c2555eef Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 2 Sep 2024 20:46:04 +0200 Subject: [PATCH] FIX: ensures default required validator handles 0 (#28686) Prior to this fix `0` would be erroring as non-existing and would raise the required error. --- .../javascripts/discourse/app/form-kit/lib/validator.js | 3 ++- .../discourse/tests/unit/lib/form-kit/validator-test.js | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/form-kit/lib/validator.js b/app/assets/javascripts/discourse/app/form-kit/lib/validator.js index 817f43e702a..47c2d0fe7a3 100644 --- a/app/assets/javascripts/discourse/app/form-kit/lib/validator.js +++ b/app/assets/javascripts/discourse/app/form-kit/lib/validator.js @@ -1,3 +1,4 @@ +import { isBlank } from "@ember/utils"; import I18n from "discourse-i18n"; export default class Validator { @@ -114,7 +115,7 @@ export default class Validator { } break; default: - if (!value) { + if (isBlank(value)) { error = true; } } diff --git a/app/assets/javascripts/discourse/tests/unit/lib/form-kit/validator-test.js b/app/assets/javascripts/discourse/tests/unit/lib/form-kit/validator-test.js index 2f99ae79c27..1d594146e29 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/form-kit/validator-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/form-kit/validator-test.js @@ -267,5 +267,10 @@ module("Unit | Lib | FormKit | Validator", function (hooks) { [I18n.t("form_kit.errors.required")], "it returns an error when the value is undefined" ); + + errors = await new Validator(0, { + required: {}, + }).validate("xxx"); + assert.deepEqual(errors, [], "it returns no error when the value is 0"); }); });