Skip to content

Fix strict validation of numbers with leading zeros in neo4j.int #988

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

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion packages/core/src/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,20 @@ class Integer {
}
}

/**
* @private
* @param num
* @param radix
* @param minSize
* @returns {string}
*/
function _convertNumberToString (num: number, radix: number, minSize: number): string {
const theNumberString = num.toString(radix)
const paddingLength = Math.max(minSize - theNumberString.length, 0)
const padding = '0'.repeat(paddingLength)
return `${padding}${theNumberString}`
}

/**
*
* @private
Expand All @@ -967,7 +981,7 @@ class Integer {
function _isValidNumberFromString (theString: string, theNumber: number, radix: number): boolean {
return !Number.isNaN(theString) &&
!Number.isNaN(theNumber) &&
theNumber.toString(radix).toLocaleLowerCase() === theString.toLocaleLowerCase()
_convertNumberToString(theNumber, radix, theString.length) === theString.toLowerCase()
}

type Integerable =
Expand Down
79 changes: 79 additions & 0 deletions packages/core/test/integer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ describe('Integer', () => {
test('int("7891123456789876a") not toThrow invalid character', () =>
expect(() => int('7891123456789876a')).not.toThrow())

test.each(malformedNumbers())('int("%s", { strictStringValidation: true }) toThrow invalid character', (theNumberString) =>
expect(() => int(theNumberString, { strictStringValidation: true })).toThrow())

test.each(wellFormedNumbersAndRadix())('Integer.fromString("%s", %n, { strictStringValidation: true }) not toThrown', (theNumberString, radix) =>
expect(() => Integer.fromString(theNumberString, radix, { strictStringValidation: true })).not.toThrow())

forEachStaticToNumberScenarios(({ input, expectedOutput }) =>
test(`Integer.toNumber(${mayIntegerToString(input)}) toEqual ${expectedOutput}`, () =>
expect(Integer.toNumber(input)).toEqual(expectedOutput))
Expand Down Expand Up @@ -1109,6 +1115,79 @@ function forEachStaticInSafeRangeScenarios (
].forEach(func)
}

function malformedNumbers (): string[] {
return [
'7a',
'7891123a',
'78911234a',
'789112345a',
'7891123456a',
'7891123456789876a',
'78911234567898765a',
'789112345678987654a',
'78911234567898765a2',
'7891123456789876a25',
'789112345678987a256',
'78911234567898a2567',
'7891123456789a25678',
'789112345678a256789',
'78911234567a2567898',
'7891123456a25678987',
'789112345a256789876',
'78911234a2567898765',
'7891123a25678987654',
'7891123ab2567898765',
'78911234ab256789876',
'789112345ab25678987',
'7891123456ab2567898',
'78911234567ab256789',
'78911234567abc25678',
'78911234567abcd2567',
'78911234567abcde256',
'78911234567abcdef25',
'78911234567abcdefg2',
'7891123456abcdefgh1',
'789112345abcdefgh12',
'78911234abcdefgh123',
'7891123abcdefgh1234',
'789112abcdefghij123',
'7kkkkabcdefghijklmn',
'7kkkkabcdefg12345mn',
'7kkkkabcdefg123456n',
'7kkkkab22efg123456n',
'7kkkkab22efg12345mn',
'7kkkkab223fg12345mn',
'kkkkk11223fg12345mn',
'kkkkk11223fg123456n',
'kkkkk11223fg1234567',
'kkkkk11223451234567',
'kkk111gkk3451234567',
'kkk111gkkkk51234567',
'kkk111gkkkkk123kk67',
'kkkk234',
'kkkk2345',
'kkkk23456',
'kkkk234567',
'kkkk2345679kk',
'kkkk2345679kkkkkk',
'kkk234567',
'kkk2345679',
'kk2345679',
'kkkkkkkkkkkkkkkkkkk'
]
}

function wellFormedNumbersAndRadix (): Array<[string, number]> {
return [
['01', 2],
['012', 3],
['0123', 4],
['0123456789', 10],
['0123456789ab', 12],
['0123456789abCde', 16]
]
}

interface AssertionPair<I, O> {
input: I
expectedOutput: O
Expand Down