Skip to content

A platform for community discussion. Free, open, simple.

DEV: improves integer validator (#37808)

Joffrey JAFFEUXf837298d1fd2
diff --git a/frontend/discourse/app/form-kit/lib/validator.js b/frontend/discourse/app/form-kit/lib/validator.js
index 21a289e3169..74ed903670f 100644
--- a/frontend/discourse/app/form-kit/lib/validator.js
+++ b/frontend/discourse/app/form-kit/lib/validator.js
@@ -46,6 +46,10 @@ export default class Validator {
}
integerValidator(value) {
+ if (isBlank(value)) {
+ return;
+ }
+
if (!Number.isInteger(Number(value))) {
return i18n("form_kit.errors.not_an_integer");
}
diff --git a/frontend/discourse/tests/unit/lib/form-kit/validator-test.js b/frontend/discourse/tests/unit/lib/form-kit/validator-test.js
index 59924693364..98b81bbdd34 100644
--- a/frontend/discourse/tests/unit/lib/form-kit/validator-test.js
+++ b/frontend/discourse/tests/unit/lib/form-kit/validator-test.js
@@ -118,6 +118,37 @@ module("Unit | Lib | FormKit | Validator", function (hooks) {
[],
"it returns no errors when the value is an integer"
);
+
+ errors = await new Validator("", { integer: {} }).validate();
+ assert.deepEqual(
+ errors,
+ [],
+ "it returns no errors when the value is an empty string"
+ );
+
+ errors = await new Validator(undefined, { integer: {} }).validate();
+ assert.deepEqual(
+ errors,
+ [],
+ "it returns no errors when the value is undefined"
+ );
+
+ errors = await new Validator(null, { integer: {} }).validate();
+ assert.deepEqual(errors, [], "it returns no errors when the value is null");
+
+ errors = await new Validator("5", { integer: {} }).validate();
+ assert.deepEqual(
+ errors,
+ [],
+ "it returns no errors when the string value represents an integer"
+ );
+
+ errors = await new Validator("5.5", { integer: {} }).validate();
+ assert.deepEqual(
+ errors,
+ [i18n("form_kit.errors.not_an_integer")],
+ "it returns an error when the string value represents a non-integer"
+ );
});
test("number", async function (assert) {