Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
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 @@ export const abiSchemaToJsonSchema = (
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 @@ export const abiSchemaToJsonSchema = (
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 @@ export const abiSchemaToJsonSchema = (
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({
$id: abiName,
...convertEthType(abiType),
});
Expand Down Expand Up @@ -505,4 +494,4 @@ export function ensureIfUint8Array<T = any>(data: T) {
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