-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(integer validation): limit range to the accepted range of the dhi…
…s2-core (#364) Closes DHIS2-14075
- Loading branch information
Showing
3 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { default as buildValidationResult } from './build-validation-result.js' | ||
export { isInteger } from './is-integer.js' | ||
export * from './query-key-factory.js' | ||
export { default as useImperativeValidate } from './use-imperative-validate.js' | ||
export { default as useValidationResult } from './use-validation-result.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { integer } from '@dhis2/ui-forms' | ||
|
||
/** | ||
* The `integer` validator of the `@dhis2/ui` library uses | ||
* `Number.isSafeInterger` to assess whether it the value is a valid integer or | ||
* not. The range allowed by that methid is -(2^53 - 1) to 2^53 - 1. | ||
* | ||
* The `isInteger` validator in the Java core uses the `isValid` method of | ||
* apache's `IntegerValidator` class (see | ||
* `dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java`), | ||
* which allows an integer range from | ||
* -2147483648 to 2147483647. | ||
* | ||
* This validator re-uses `@dhis2/ui`'s validator but restricts the integer to | ||
* the range allowed by the Java core. | ||
* | ||
* @param {string} value | ||
*/ | ||
export function isInteger(value) { | ||
const error = integer(value) | ||
if (error) { | ||
return error | ||
} | ||
|
||
const valueAsNumber = parseInt(value, 10) | ||
const exceedsLowerBound = valueAsNumber < -2147483648 | ||
const exceedsUpperBound = valueAsNumber > 2147483647 | ||
if (exceedsLowerBound || exceedsUpperBound) { | ||
return i18n.t( | ||
'Integer numbers have to be in the range from -2147483648 to 2147483647' | ||
) | ||
} | ||
|
||
return undefined | ||
} |
0d3d41c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://dhis2-data-entry.netlify.app as production
🚀 Deployed on https://657ad0cd426b4105ed7ffa78--dhis2-data-entry.netlify.app