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 2d array bug of abiSchemaToJsonSchema in web3-validator #6836

Merged
merged 9 commits into from
Mar 20, 2024
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
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ describe('Contract', () => {
expect(error).toBeInstanceOf(Web3ValidatorError);
// eslint-disable-next-line jest/no-conditional-expect
expect((error as Web3ValidatorError).message).toBe(
'Web3 validator found 1 error[s]:\nmust NOT have more than 1 items',
'Web3 validator found 2 error[s]:\nmust NOT have more than 1 items\nvalue "true" at "/1" must pass "string" validation',
);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/web3-validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ Documentation:

- Fixed an issue with detecting Uint8Array (#6486)

## [Unreleased]
## [Unreleased]

### Fixed

- Multi-dimensional arrays(with a fix length) are now handled properly when parsing ABIs (#6798)
55 changes: 22 additions & 33 deletions packages/web3-validator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
for (let i = arraySizes.length - 1; i > 0; i -= 1) {
childSchema = {
type: 'array',
$id: abiName,
items: [],
maxItems: arraySizes[i],
minItems: arraySizes[i],
Expand All @@ -192,7 +193,7 @@
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 = [childSchema];
} // lastSchema.items is a non-empty Scheme array, append 'childSchema'
else {
lastSchema.items.push(childSchema);
Expand All @@ -205,43 +206,31 @@
nestedTuple.$id = abiName;
(lastSchema.items as JsonSchema[]).push(nestedTuple);
} else if (baseType === 'tuple' && isArray) {
const arraySize = arraySizes[0];
const item: JsonSchema = {
$id: abiName,
type: 'array',
items: abiSchemaToJsonSchema(abiComponents, abiName),
maxItems: arraySize,
minItems: arraySize,
};

if (arraySize < 0) {
delete item.maxItems;
delete item.minItems;
}

(lastSchema.items as JsonSchema[]).push(item);
const arraySize = arraySizes[0];
const item: JsonSchema = {
type: 'array',
$id: abiName,
items: abiSchemaToJsonSchema(abiComponents, abiName),
...(arraySize >= 0 && { minItems: arraySize, maxItems: arraySize }),
};

(lastSchema.items as JsonSchema[]).push(item);
} else if (isArray) {
const arraySize = arraySizes[0];
const item: JsonSchema = {
type: 'array',
$id: abiName,
items: convertEthType(String(baseType)),
minItems: arraySize,
maxItems: arraySize,
};

if (arraySize < 0) {
delete item.maxItems;
delete item.minItems;
}

(lastSchema.items as JsonSchema[]).push(item);
const arraySize = arraySizes[0];
const item: JsonSchema = {
type: 'array',
$id: abiName,
items: convertEthType(abiType),
...(arraySize >= 0 && { minItems: arraySize, maxItems: arraySize }),
};

(lastSchema.items as JsonSchema[]).push(item);
} else if (Array.isArray(lastSchema.items)) {
// Array of non-tuple items
lastSchema.items.push({ $id: abiName, ...convertEthType(abiType) });
} else {
// Nested object
((lastSchema.items as JsonSchema).items as JsonSchema[]).push({
(lastSchema.items as JsonSchema[]).push({

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

View check run for this annotation

Codecov / codecov/patch

packages/web3-validator/src/utils.ts#L233

Added line #L233 was not covered by tests
$id: abiName,
...convertEthType(abiType),
});
Expand Down Expand Up @@ -505,4 +494,4 @@
return Uint8Array.from(data as unknown as Uint8Array);
}
return data;
}
}
11 changes: 9 additions & 2 deletions packages/web3-validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const convertToZod = (schema: JsonSchema): ZodType => {
}

if (schema?.type === 'array' && schema?.items) {
if (Array.isArray(schema.items) && schema.items.length > 0) {
if (Array.isArray(schema.items) && schema.items.length > 1
&& schema.maxItems !== undefined
&& new Set(schema.items.map((item: JsonSchema) => item.$id)).size === schema.items.length) {
const arr: Partial<[ZodTypeAny, ...ZodTypeAny[]]> = [];
for (const item of schema.items) {
const zItem = convertToZod(item);
Expand All @@ -54,7 +56,12 @@ const convertToZod = (schema: JsonSchema): ZodType => {
}
return z.tuple(arr as [ZodTypeAny, ...ZodTypeAny[]]);
}
return z.array(convertToZod(schema.items as JsonSchema));
const nextSchema = Array.isArray(schema.items) ? schema.items[0] : schema.items;
let zodArraySchema = z.array(convertToZod(nextSchema));

zodArraySchema = schema.minItems !== undefined ? zodArraySchema.min(schema.minItems) : zodArraySchema;
zodArraySchema = schema.maxItems !== undefined ? zodArraySchema.max(schema.maxItems) : zodArraySchema;
return zodArraySchema;
}

if (schema.oneOf && Array.isArray(schema.oneOf)) {
Expand Down
Loading
Loading