Skip to content

Commit

Permalink
Additional validation for integers
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetDealer committed Apr 18, 2024
1 parent e7a53c8 commit f71bffd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elsci-io/ui-essential",
"version": "1.0.56",
"version": "1.0.57",
"description": "Material Design components created for products built by elsci.io",
"main": "src/index.js",
"type": "module",
Expand Down
23 changes: 21 additions & 2 deletions src/TextInput/TextInputValidityState.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ export default class TextInputValidityState {
{badInput, customError, patternMismatch, typeMismatch, valid, rangeOverflow, rangeUnderflow, tooLong,
tooShort, valueMissing, stepMismatch},
this.#isBlanknessConstraintViolated(),
this.#isRangeConstraintViolated()
this.#isRangeConstraintViolated(),
this.#isStepConstraintViolated()
);
cv.valid = !cv.valueMissing && !cv.rangeUnderflow && !cv.rangeOverflow;
cv.valid = !cv.valueMissing && !cv.rangeUnderflow && !cv.rangeOverflow && !cv.stepMismatch;
return cv;
}

Expand All @@ -82,6 +83,24 @@ export default class TextInputValidityState {
return {rangeUnderflow: (min !== "" && +value < +min), rangeOverflow: (max !== "" && +value > +max)};
}

/**
* This method serves to supplement internal validation to determine whether a float qualifies as an integer.
* The internal validation for an input type="number" step="1" doesn't consistently cover all cases.
* For example, the number 2.00000001 might be considered an integer by the internal validator.
* The aim is to widen this validation range. Now, the comparison for an integer works correctly when a number
* has no more than 16 digits. However, if a number has 17 digits or more, this function returns incorrect result.
* @return {{stepMismatch: boolean}}
*/
#isStepConstraintViolated() {
if (this.#inputElement.type !== "number")
return {stepMismatch: false};
const {valueAsNumber, step} = this.#inputElement;
if (!step || step !== "1")
return {stepMismatch: false};
const stepMismatch = !Number.isInteger(valueAsNumber);
return {stepMismatch: stepMismatch};
}

#getValidationMessage(validityState) {
const isTypeNumber = this.#inputElement.type === "number";
const {badInput, rangeOverflow, rangeUnderflow, tooLong, tooShort, valueMissing, stepMismatch} = validityState;
Expand Down

0 comments on commit f71bffd

Please sign in to comment.