Skip to content

Commit

Permalink
Changed the adjacent PIN number repetition validation to a number typ…
Browse files Browse the repository at this point in the history
…e rule 'max_repeated_numbers', and refactor the test cases relevently

Signed-off-by: Jian Wang <[email protected]>
  • Loading branch information
jian4on committed Feb 3, 2025
1 parent bafa89c commit fc6edee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/legacy/core/App/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const PINRules: PINValidationRules = {
only_numbers: true,
min_length: 6,
max_length: 6,
no_repeated_numbers: false,
max_repeated_numbers: 5,
no_repetition_of_the_two_same_numbers: false,
no_series_of_numbers: false,
no_even_or_odd_series_of_numbers: false,
Expand Down
9 changes: 4 additions & 5 deletions packages/legacy/core/App/types/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ export enum AuthLevel {
}

/*
no_repeated_numbers - forbid adjacent numbers repeating or not, and specify the forbidden repeating times when repeating allowed
true | 0 | 1 | 2: forbid adjacent numbers repetition
false: no check for adjacent numbers repeating
other numbers: the length of adjacent repeating numbers is forbidden, e.g. '3' will forbid 055567, but 055678 is allowed
max_repeated_numbers - adjacent characters allowed repeating times
0 - Forbid adjacent characters repeating
n > 0 - permit repeating times, e.g. '1 === n' for '11' permitted but '111' forbidden
*/
export interface PINValidationRules {
only_numbers: boolean
min_length: number
max_length: number
no_repeated_numbers: boolean | number
max_repeated_numbers: number
no_repetition_of_the_two_same_numbers: boolean | number
no_series_of_numbers: boolean
no_even_or_odd_series_of_numbers: boolean
Expand Down
12 changes: 6 additions & 6 deletions packages/legacy/core/App/utils/PINCreationValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ export const PINCreationValidations = (PIN: string, PINRules: PINValidationRules
} as PINValidationsType)
}

if ('number' === typeof(PINRules.no_repeated_numbers) && PINRules.no_repeated_numbers > 2 ) {
const repetitionPattern = new RegExp(String.raw`(\d)\1{${PINRules.no_repeated_numbers - 1},}`, "g")
if (0 === PINRules.max_repeated_numbers) {
const repetitionPattern = new RegExp(/(\d)\1{1,}/)
PINValidations.push({
isInvalid: repetitionPattern.test(PIN),
errorName: PINError.MaxAdjacentNumberRepetitionValidation
errorName: PINError.NoRepetitionOfTheSameNumbersValidation
} as PINValidationsType)
} else if (PINRules.no_repeated_numbers || 0 === PINRules.no_repeated_numbers) {
const repetitionPattern = new RegExp(/(\d)\1{1,}/)
} else {
const repetitionPattern = new RegExp(String.raw`(\d)\1{${PINRules.max_repeated_numbers + 1},}`, "g")
PINValidations.push({
isInvalid: repetitionPattern.test(PIN),
errorName: PINError.NoRepetitionOfTheSameNumbersValidation
errorName: PINError.MaxAdjacentNumberRepetitionValidation
} as PINValidationsType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const defaultPINRules = {
only_numbers: false,
min_length: 6,
max_length: 6,
no_repeated_numbers: false,
max_repeated_numbers: 5,
no_series_of_numbers: false,
no_repetition_of_the_two_same_numbers: false,
no_even_or_odd_series_of_numbers: false,
Expand Down Expand Up @@ -74,10 +74,10 @@ describe('PIN creation validations', () => {
}
})

test('PIN with repeated numbers and repeated numbers validation to true, so the validation use the default of two repeated numbers, should return NoRepetitionOfTheSameNumbersValidation as invalid', async () => {
test('PIN with repeated numbers and max repeated numbers is 0, should return NoRepetitionOfTheSameNumbersValidation as invalid', async () => {
const PINRulesWithRepeatedNumbers = {
...defaultPINRules,
no_repeated_numbers: 0,
max_repeated_numbers: 0,
}

const PINWithRepeatedNumbers = '113456'
Expand All @@ -90,10 +90,10 @@ describe('PIN creation validations', () => {
}
})

test('PIN with repeated numbers and repeated numbers validation to a number, so the validation use that number as validation limit, should return NoRepetitionOfTheSameNumbersValidation as invalid', async () => {
test('PIN with adhacent numbers repeating 2 times and max repeated numbers is 1, so the validation use that number as validation max limit, should return NoRepetitionOfTheSameNumbersValidation as invalid', async () => {
const PINRulesWithRepeatedNumbers = {
...defaultPINRules,
no_repeated_numbers: 3,
max_repeated_numbers: 1,
}

const PINWithRepeatedNumbers = '111456'
Expand All @@ -106,13 +106,13 @@ describe('PIN creation validations', () => {
}
})

test('PIN with a numbers repeated two times and repeated numbers validation set to 3 should return NoRepetitionOfTheSameNumbersValidation as valid', async () => {
test('PIN with adhacent numbers repeating 2 times and max repeated numbers is 2, so the validation use that number as validation max limit, should return NoRepetitionOfTheSameNumbersValidation as valid', async () => {
const PINRulesWithRepeatedNumbers = {
...defaultPINRules,
no_repeated_numbers: 3,
max_repeated_numbers: 2,
}

const PINWithRepeatedNumbers = '112456'
const PINWithRepeatedNumbers = '111456'
const PINValidations = PINCreationValidations(PINWithRepeatedNumbers, PINRulesWithRepeatedNumbers)

for (const PINValidation of PINValidations) {
Expand Down

0 comments on commit fc6edee

Please sign in to comment.