From fd0454da41a806bf3c3d882c5126da31ed366c52 Mon Sep 17 00:00:00 2001 From: Luciano Vernaschi Date: Tue, 10 Dec 2024 15:21:14 +0100 Subject: [PATCH] Accept NaN as valid --- packages/ts/lit-form/src/Validators.ts | 10 ++++++---- packages/ts/lit-form/test/Validators.test.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/ts/lit-form/src/Validators.ts b/packages/ts/lit-form/src/Validators.ts index ad54ca4bda..07f4a3adaa 100644 --- a/packages/ts/lit-form/src/Validators.ts +++ b/packages/ts/lit-form/src/Validators.ts @@ -268,7 +268,8 @@ export class Negative extends AbstractValidator { } override validate(value: T): boolean { - return toFloat(String(value)) < 0; + const fv = toFloat(String(value)); + return isNaN(fv) || fv < 0; } readonly name = 'Negative'; @@ -280,7 +281,7 @@ export class NegativeOrZero extends AbstractValidator { } override validate(value: T): boolean { - return toFloat(String(value)) <= 0; + return (toFloat(String(value)) || 0) <= 0; } readonly name = 'NegativeOrZero'; @@ -292,7 +293,8 @@ export class Positive extends AbstractValidator { } override validate(value: T): boolean { - return toFloat(String(value)) > 0; + const fv = toFloat(String(value)); + return isNaN(fv) || fv > 0; } readonly name = 'Positive'; @@ -304,7 +306,7 @@ export class PositiveOrZero extends AbstractValidator { } override validate(value: T): boolean { - return toFloat(String(value)) >= 0; + return (toFloat(String(value)) || 0) >= 0; } readonly name = 'PositiveOrZero'; diff --git a/packages/ts/lit-form/test/Validators.test.ts b/packages/ts/lit-form/test/Validators.test.ts index fb019465bb..efe4055f4e 100644 --- a/packages/ts/lit-form/test/Validators.test.ts +++ b/packages/ts/lit-form/test/Validators.test.ts @@ -211,6 +211,9 @@ describe('@vaadin/hilla-lit-form', () => { assert.isTrue(validator.validate(-0.01)); assert.isFalse(validator.validate(0)); assert.isFalse(validator.validate(1)); + assert.isTrue(validator.validate(undefined)); + assert.isTrue(validator.validate(null)); + assert.isTrue(validator.validate('')); }); it('NegativeOrZero', () => { @@ -221,6 +224,9 @@ describe('@vaadin/hilla-lit-form', () => { assert.isTrue(validator.validate(-0.01)); assert.isTrue(validator.validate(0)); assert.isFalse(validator.validate(1)); + assert.isTrue(validator.validate(undefined)); + assert.isTrue(validator.validate(null)); + assert.isTrue(validator.validate('')); }); it('Positive', () => { @@ -231,6 +237,9 @@ describe('@vaadin/hilla-lit-form', () => { assert.isFalse(validator.validate(-0.01)); assert.isFalse(validator.validate(0)); assert.isTrue(validator.validate(0.01)); + assert.isTrue(validator.validate(undefined)); + assert.isTrue(validator.validate(null)); + assert.isTrue(validator.validate('')); }); it('PositiveOrZero', () => { @@ -241,6 +250,9 @@ describe('@vaadin/hilla-lit-form', () => { assert.isFalse(validator.validate(-0.01)); assert.isTrue(validator.validate(0)); assert.isTrue(validator.validate(0.01)); + assert.isTrue(validator.validate(undefined)); + assert.isTrue(validator.validate(null)); + assert.isTrue(validator.validate('')); }); it('Size', () => {