Skip to content
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

Fix multi dimensional array validation issue #6435

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,7 @@ Documentation:
- ESM import bug (#6359)

## [Unreleased]

## Fixed

- Multi-dimensional arrays are now handled properly when parsing ABIs
12 changes: 11 additions & 1 deletion packages/web3-validator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,16 @@
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);

Check warning on line 198 in packages/web3-validator/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-validator/src/utils.ts#L197-L198

Added lines #L197 - L198 were not covered by tests
fullkomnun marked this conversation as resolved.
Show resolved Hide resolved
}
lastSchema = childSchema;
}

Expand Down Expand Up @@ -237,6 +246,7 @@
...convertEthType(abiType),
});
}
lastSchema = schema;
}

return schema;
Expand Down
122 changes: 122 additions & 0 deletions packages/web3-validator/test/fixtures/abi_to_json_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
];