Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept null values as valid for positive/negative validators #2986

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Lodin marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading