Skip to content

Commit

Permalink
Accept NaN as valid
Browse files Browse the repository at this point in the history
  • Loading branch information
cromoteca committed Dec 10, 2024
1 parent dfc0bca commit fd0454d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/ts/lit-form/src/Validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ export class Negative<T> extends AbstractValidator<T> {
}

override validate(value: T): boolean {
return toFloat(String(value)) < 0;
const fv = toFloat(String(value));
return isNaN(fv) || fv < 0;
}

readonly name = 'Negative';
Expand All @@ -280,7 +281,7 @@ export class NegativeOrZero<T> extends AbstractValidator<T> {
}

override validate(value: T): boolean {
return toFloat(String(value)) <= 0;
return (toFloat(String(value)) || 0) <= 0;
}

readonly name = 'NegativeOrZero';
Expand All @@ -292,7 +293,8 @@ export class Positive<T> extends AbstractValidator<T> {
}

override validate(value: T): boolean {
return toFloat(String(value)) > 0;
const fv = toFloat(String(value));
return isNaN(fv) || fv > 0;
}

readonly name = 'Positive';
Expand All @@ -304,7 +306,7 @@ export class PositiveOrZero<T> extends AbstractValidator<T> {
}

override validate(value: T): boolean {
return toFloat(String(value)) >= 0;
return (toFloat(String(value)) || 0) >= 0;
}

readonly name = 'PositiveOrZero';
Expand Down
12 changes: 12 additions & 0 deletions packages/ts/lit-form/test/Validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit fd0454d

Please sign in to comment.