Skip to content

Commit

Permalink
fix(integer validation): limit range to the accepted range of the dhi…
Browse files Browse the repository at this point in the history
…s2-core (#364)

Closes DHIS2-14075
  • Loading branch information
Mohammer5 authored Dec 14, 2023
1 parent 14077d8 commit 0d3d41c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/data-workspace/inputs/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
createMinNumber,
createNumberRange,
email,
integer,
internationalPhoneNumber,
number,
url,
} from '@dhis2/ui-forms'
import { CAN_HAVE_LIMITS_TYPES, VALUE_TYPES } from '../../shared/index.js'
import {
CAN_HAVE_LIMITS_TYPES,
VALUE_TYPES,
isInteger,
} from '../../shared/index.js'

export const text = createMaxCharacterLength(50000)
export const letter = createMaxCharacterLength(1)
Expand Down Expand Up @@ -53,16 +56,16 @@ export const time = (value) =>
c: ':',
})

export const integerPositive = composeValidators(integer, createMinNumber(1))
export const integerPositive = composeValidators(isInteger, createMinNumber(1))
export const integerZeroOrPositive = composeValidators(
integer,
isInteger,
createMinNumber(0)
)
export const integerNegative = composeValidators(integer, createMaxNumber(-1))
export const integerNegative = composeValidators(isInteger, createMaxNumber(-1))

export const percentage = createNumberRange(0, 100)
const percentageInteger = composeValidators(
integer,
isInteger,
createMinNumber(0),
createMaxNumber(100)
)
Expand All @@ -73,7 +76,7 @@ export const validatorsByValueType = {
[VALUE_TYPES.DATE]: null, // todo (in case browser doesn't support special input)
[VALUE_TYPES.DATETIME]: null, // todo " "
[VALUE_TYPES.EMAIL]: email,
[VALUE_TYPES.INTEGER]: integer,
[VALUE_TYPES.INTEGER]: isInteger,
[VALUE_TYPES.INTEGER_POSITIVE]: integerPositive,
[VALUE_TYPES.INTEGER_NEGATIVE]: integerNegative,
[VALUE_TYPES.INTEGER_ZERO_OR_POSITIVE]: integerZeroOrPositive,
Expand Down Expand Up @@ -104,12 +107,12 @@ export const warningValidateByValueType = (valueType, limits) => {
}

export const minMaxValidatorsByValueType = {
[VALUE_TYPES.INTEGER]: integer,
[VALUE_TYPES.INTEGER]: isInteger,
[VALUE_TYPES.INTEGER_POSITIVE]: integerPositive,
[VALUE_TYPES.INTEGER_NEGATIVE]: integerNegative,
[VALUE_TYPES.INTEGER_ZERO_OR_POSITIVE]: integerZeroOrPositive,
// backend restricts minimum and maximum to integers
[VALUE_TYPES.NUMBER]: integer,
[VALUE_TYPES.NUMBER]: isInteger,
[VALUE_TYPES.PERCENTAGE]: percentageInteger,
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/validation/index.js
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'
36 changes: 36 additions & 0 deletions src/shared/validation/is-integer.js
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
}

1 comment on commit 0d3d41c

@dhis2-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.