diff --git a/packages/web3-validator/CHANGELOG.md b/packages/web3-validator/CHANGELOG.md index 394d1ac8c64..fea00e98075 100644 --- a/packages/web3-validator/CHANGELOG.md +++ b/packages/web3-validator/CHANGELOG.md @@ -147,4 +147,8 @@ Documentation: - Dependencies updated -## [Unreleased] \ No newline at end of file +## [Unreleased] + +## Fixed + +- Multi-dimensional arrays are now handled properly when parsing ABIs diff --git a/packages/web3-validator/src/utils.ts b/packages/web3-validator/src/utils.ts index 93191c2fb1d..28270c99c3d 100644 --- a/packages/web3-validator/src/utils.ts +++ b/packages/web3-validator/src/utils.ts @@ -187,7 +187,16 @@ export const abiSchemaToJsonSchema = ( delete childSchema.minItems; } - lastSchema.items = childSchema; + // lastSchema.items is a Schema, concat with 'childSchema' + if (!Array.isArray(lastSchema.items)) { + lastSchema.items = [lastSchema.items as JsonSchema, childSchema]; + } // lastSchema.items is an empty Scheme array, set it to 'childSchema' + else if (lastSchema.items.length === 0) { + lastSchema.items = childSchema; + } // lastSchema.items is a non-empty Scheme array, append 'childSchema' + else { + lastSchema.items.push(childSchema); + } lastSchema = childSchema; } @@ -237,6 +246,7 @@ export const abiSchemaToJsonSchema = ( ...convertEthType(abiType), }); } + lastSchema = schema; } return schema; diff --git a/packages/web3-validator/test/fixtures/abi_to_json_schema.ts b/packages/web3-validator/test/fixtures/abi_to_json_schema.ts index 0593cd55056..dcac24e2046 100644 --- a/packages/web3-validator/test/fixtures/abi_to_json_schema.ts +++ b/packages/web3-validator/test/fixtures/abi_to_json_schema.ts @@ -1536,4 +1536,126 @@ export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [ ], }, }, + { + title: 'multi-dimensional array', + abi: { + fullSchema: [ + { + name: 'x1', + type: 'uint256[][]', + }, + { + name: 'x2', + type: 'uint256[][]', + }, + { + name: 'x3', + type: 'uint256', + }, + ], + shortSchema: ['uint256[][]', 'uint256[][]', 'uint256'], + data: [ + [ + [1, 1], + [2, 2], + ], + [ + [1, 1, 1], + [2, 2, 2], + [3, 3, 3], + ], + 42, + ], + }, + json: { + fullSchema: { + type: 'array', + items: [ + { + type: 'array', + items: [ + { + type: 'array', + $id: 'x1', + items: { + format: 'uint', + required: true, + }, + }, + ], + }, + { + type: 'array', + items: [ + { + type: 'array', + $id: 'x2', + items: { + format: 'uint', + required: true, + }, + }, + ], + }, + { + $id: 'x3', + format: 'uint256', + required: true, + }, + ], + maxItems: 3, + minItems: 3, + }, + shortSchema: { + type: 'array', + items: [ + { + type: 'array', + items: [ + { + type: 'array', + $id: '/0/0', + items: { + format: 'uint', + required: true, + }, + }, + ], + }, + { + type: 'array', + items: [ + { + type: 'array', + $id: '/0/1', + items: { + format: 'uint', + required: true, + }, + }, + ], + }, + { + $id: '/0/2', + format: 'uint256', + required: true, + }, + ], + maxItems: 3, + minItems: 3, + }, + data: { + x1: [ + [1, 1], + [2, 2], + ], + x2: [ + [1, 1, 1], + [2, 2, 2], + [3, 3, 3], + ], + x3: 42, + }, + }, + }, ];