-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Conversation
- 123 cannot be uint - test case("nested tuple object in nested array") has a wrong data structure of tuple[][3]
… to each dimension
@jdevcs public validate(schema: JsonSchema, data: Json, options?: { silent?: boolean }) {
const zod = convertToZod(schema);
const result = zod.safeParse(data);
if (!result.success) {
const errors = this.convertErrors(result.error?.issues ?? []);
if (errors) {
if (options?.silent) {
return errors;
}
throw new Web3ValidatorError(errors);
}
}
return undefined;
} The core issue lies in the first line, where the schema is converted to Zod. In convertToZod function, multi-dimensional arrays and tuple types are confused. if (schema?.type === 'array' && schema?.items) {
if (Array.isArray(schema.items) && schema.items.length > 0) {
// do somsthing
return z.tuple(arr as [ZodTypeAny, ...ZodTypeAny[]]);
}
return z.array(convertToZod(schema.items));
} So, I added additional conditions to distinguish between them. If it's a tuple
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) {
//do something
return z.tuple(arr as [ZodTypeAny, ...ZodTypeAny[]]);
} Also, in this method, the maxItems and minItems were lost during the conversion, resulting in the length judgment always being "must NOT have more than 1 item." 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; Besides that, the previous unit tests did not cover all test cases. After adding the abiToJsonSchemaCases in the web_validator.test, I found that:
Please review again, thx. |
I can't run tests of web-eth-contracts on my local environment, I saw the error in the auto pipeline and fixed. Not sure why, because I didn't change the codes of contracts packages. Please review. |
Hi, are there any updates? I noticed the pipeline failed, but there are no error logs provided. |
Thanks for detailed explanation of changes, there are some unstable tests ( not related with this PR ) and we are working in current sprint for fixing these. #6857, could you rebase your branch when 6857 is merged, |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## 4.x #6836 +/- ##
==========================================
- Coverage 91.99% 91.98% -0.02%
==========================================
Files 215 215
Lines 8172 8174 +2
Branches 2202 2207 +5
==========================================
+ Hits 7518 7519 +1
- Misses 654 655 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Description
We encountered a bug in the web3-validator code where the handling of multi-dimensional arrays was not implemented correctly.
In the original code snippet, the arraySize vatiable was only assigned the value of the first element in the arraySizes array. This prevented proper handling of multi-dimensional arrays.
To fix the bug, I added an additional loop to iterate over the arraySizes array to handle each dimension:
Besides, different from Issue #6486, if the multi-dimensional arrays has a fixed length, it will go to different code snippet.
Fixes #6798
Type of change
Checklist:
npm run lint
with success and extended the tests and types if necessary.npm run test:unit
with success.npm run test:coverage
and my test cases cover all the lines and branches of the added code.npm run build
and testeddist/web3.min.js
in a browser.CHANGELOG.md
file in the root folder.