From f1835705d66a107574caa703fa4aa9f44b0f7698 Mon Sep 17 00:00:00 2001 From: Or Noyman <17881579+fullkomnun@users.noreply.github.com> Date: Sun, 17 Sep 2023 21:38:11 +0300 Subject: [PATCH 01/11] add ABI test cases with single numeric parameter that have a size that is not a power of 2 --- .../test/fixtures/abi_to_json_schema.ts | 37 ++++++++++++++++++- .../test/unit/web3_validator.test.ts | 3 +- 2 files changed, 38 insertions(+), 2 deletions(-) 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 dcac24e2046..ece4437d0af 100644 --- a/packages/web3-validator/test/fixtures/abi_to_json_schema.ts +++ b/packages/web3-validator/test/fixtures/abi_to_json_schema.ts @@ -30,7 +30,7 @@ export type AbiToJsonSchemaCase = { data: Record | Array; }; }; -export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [ +const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [ { title: 'single param uint', abi: { @@ -1659,3 +1659,38 @@ export const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [ }, }, ]; + +function generateSingleParamNumericCase(type: string, bitSize: number) { + return { + title: `single param ${type}${bitSize}`, + abi: { + fullSchema: [{ name: 'a', type: `${type}${bitSize}` }], + shortSchema: [`${type}${bitSize}`], + data: [12], + }, + json: { + fullSchema: { + type: 'array', + items: [{ $id: 'a', format: `${type}${bitSize}`, required: true }], + minItems: 1, + maxItems: 1, + }, + shortSchema: { + type: 'array', + items: [{ $id: '/0/0', format: `${type}${bitSize}`, required: true }], + minItems: 1, + maxItems: 1, + }, + data: [12], + }, + }; +} + +for (let i = 256; i >= 8; i -= 8) { + abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('int', i)); + abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('uint', i)); +} + +export { + abiToJsonSchemaCases + }; diff --git a/packages/web3-validator/test/unit/web3_validator.test.ts b/packages/web3-validator/test/unit/web3_validator.test.ts index e1a49fa6e3a..3f5e0722c99 100644 --- a/packages/web3-validator/test/unit/web3_validator.test.ts +++ b/packages/web3-validator/test/unit/web3_validator.test.ts @@ -103,7 +103,8 @@ describe('web3-validator', () => { }); }); describe('validateJsonSchema', () => { - it.each(abiToJsonSchemaCases.slice(0, 5))('should pass for valid data', abi => { + // only single param test cases + it.each(abiToJsonSchemaCases.slice(0, 37))(`$title - should pass for valid data`, abi => { const jsonSchema = abi.json; expect( validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data), From dc5431914e0781068a1170dd2ddc2c33df5a5cdd Mon Sep 17 00:00:00 2001 From: Or Noyman <17881579+fullkomnun@users.noreply.github.com> Date: Sun, 17 Sep 2023 21:39:49 +0300 Subject: [PATCH 02/11] fail-fast by throwing an error if during conversion of JSON shceme to Zod 'check' function is undefined (due to unsupported type) --- packages/web3-validator/src/validator.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/web3-validator/src/validator.ts b/packages/web3-validator/src/validator.ts index c1779992125..b75b9c0bb83 100644 --- a/packages/web3-validator/src/validator.ts +++ b/packages/web3-validator/src/validator.ts @@ -67,6 +67,10 @@ const convertToZod = (schema: JsonSchema): ZodType => { } if (schema?.format) { + if (!formats[schema.format]) { + throw new Error(`format ${schema.format} is unsupported`); + } + return z.any().refine(formats[schema.format], (value: unknown) => ({ params: { value, format: schema.format }, })); From b1cfb0fbc8f644ed13aaf9d841502de2428416f1 Mon Sep 17 00:00:00 2001 From: Or Noyman <17881579+fullkomnun@users.noreply.github.com> Date: Sun, 17 Sep 2023 21:40:51 +0300 Subject: [PATCH 03/11] make sure validators are defined for all valid sized of numeric types --- packages/web3-validator/src/formats.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/web3-validator/src/formats.ts b/packages/web3-validator/src/formats.ts index da0c0ba6564..69d6fa9cf1f 100644 --- a/packages/web3-validator/src/formats.ts +++ b/packages/web3-validator/src/formats.ts @@ -41,8 +41,7 @@ const formats: { [key: string]: (data: unknown) => boolean } = { string: (data: unknown) => isString(data as ValidInputTypes), }; // generate formats for all numbers types -for (let i = 3; i <= 8; i += 1) { - const bitSize = 2 ** i; +for (let bitSize = 8; bitSize <= 256; bitSize += 8) { formats[`int${bitSize}`] = data => isInt(data as ValidInputTypes, { bitSize }); formats[`uint${bitSize}`] = data => isUInt(data as ValidInputTypes, { bitSize }); } From 09c7e755254e0b27d5cf9e9cfdfbb6b8d18b1bf0 Mon Sep 17 00:00:00 2001 From: Or Noyman <17881579+fullkomnun@users.noreply.github.com> Date: Sun, 17 Sep 2023 21:42:55 +0300 Subject: [PATCH 04/11] prettify --- packages/web3-validator/src/validator.ts | 6 +-- .../web3-validator/test/config/jest.config.js | 2 +- .../test/fixtures/abi_to_json_schema.ts | 54 +++++++++---------- .../test/unit/web3_validator.test.ts | 17 +++--- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/packages/web3-validator/src/validator.ts b/packages/web3-validator/src/validator.ts index b75b9c0bb83..d3bb4f56c93 100644 --- a/packages/web3-validator/src/validator.ts +++ b/packages/web3-validator/src/validator.ts @@ -67,9 +67,9 @@ const convertToZod = (schema: JsonSchema): ZodType => { } if (schema?.format) { - if (!formats[schema.format]) { - throw new Error(`format ${schema.format} is unsupported`); - } + if (!formats[schema.format]) { + throw new Error(`format ${schema.format} is unsupported`); + } return z.any().refine(formats[schema.format], (value: unknown) => ({ params: { value, format: schema.format }, diff --git a/packages/web3-validator/test/config/jest.config.js b/packages/web3-validator/test/config/jest.config.js index 4a60f95b353..c8c21e06e6a 100644 --- a/packages/web3-validator/test/config/jest.config.js +++ b/packages/web3-validator/test/config/jest.config.js @@ -12,7 +12,7 @@ module.exports = { }, moduleNameMapper: { '^(\\.{1,2}/.*)\\.js$': '$1', - }, + }, verbose: false, collectCoverage: false, coverageReporters: ['json'], 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 ece4437d0af..1a2cb7bd36f 100644 --- a/packages/web3-validator/test/fixtures/abi_to_json_schema.ts +++ b/packages/web3-validator/test/fixtures/abi_to_json_schema.ts @@ -1661,36 +1661,34 @@ const abiToJsonSchemaCases: AbiToJsonSchemaCase[] = [ ]; function generateSingleParamNumericCase(type: string, bitSize: number) { - return { - title: `single param ${type}${bitSize}`, - abi: { - fullSchema: [{ name: 'a', type: `${type}${bitSize}` }], - shortSchema: [`${type}${bitSize}`], - data: [12], - }, - json: { - fullSchema: { - type: 'array', - items: [{ $id: 'a', format: `${type}${bitSize}`, required: true }], - minItems: 1, - maxItems: 1, - }, - shortSchema: { - type: 'array', - items: [{ $id: '/0/0', format: `${type}${bitSize}`, required: true }], - minItems: 1, - maxItems: 1, - }, - data: [12], - }, - }; + return { + title: `single param ${type}${bitSize}`, + abi: { + fullSchema: [{ name: 'a', type: `${type}${bitSize}` }], + shortSchema: [`${type}${bitSize}`], + data: [12], + }, + json: { + fullSchema: { + type: 'array', + items: [{ $id: 'a', format: `${type}${bitSize}`, required: true }], + minItems: 1, + maxItems: 1, + }, + shortSchema: { + type: 'array', + items: [{ $id: '/0/0', format: `${type}${bitSize}`, required: true }], + minItems: 1, + maxItems: 1, + }, + data: [12], + }, + }; } for (let i = 256; i >= 8; i -= 8) { - abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('int', i)); - abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('uint', i)); + abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('int', i)); + abiToJsonSchemaCases.unshift(generateSingleParamNumericCase('uint', i)); } -export { - abiToJsonSchemaCases - }; +export { abiToJsonSchemaCases }; diff --git a/packages/web3-validator/test/unit/web3_validator.test.ts b/packages/web3-validator/test/unit/web3_validator.test.ts index 3f5e0722c99..b9bf10b82a8 100644 --- a/packages/web3-validator/test/unit/web3_validator.test.ts +++ b/packages/web3-validator/test/unit/web3_validator.test.ts @@ -103,13 +103,16 @@ describe('web3-validator', () => { }); }); describe('validateJsonSchema', () => { - // only single param test cases - it.each(abiToJsonSchemaCases.slice(0, 37))(`$title - should pass for valid data`, abi => { - const jsonSchema = abi.json; - expect( - validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data), - ).toBeUndefined(); - }); + // only single param test cases + it.each(abiToJsonSchemaCases.slice(0, 37))( + `$title - should pass for valid data`, + abi => { + const jsonSchema = abi.json; + expect( + validator.validateJSONSchema(jsonSchema.fullSchema, jsonSchema.data), + ).toBeUndefined(); + }, + ); it('should throw', () => { expect(() => { From b6678fc39fb5f335352b0c7216f441284ca27e88 Mon Sep 17 00:00:00 2001 From: Or Noyman <17881579+fullkomnun@users.noreply.github.com> Date: Sun, 17 Sep 2023 21:49:18 +0300 Subject: [PATCH 05/11] updated changelog --- packages/web3-validator/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/web3-validator/CHANGELOG.md b/packages/web3-validator/CHANGELOG.md index fea00e98075..f93a0645d0b 100644 --- a/packages/web3-validator/CHANGELOG.md +++ b/packages/web3-validator/CHANGELOG.md @@ -152,3 +152,4 @@ Documentation: ## Fixed - Multi-dimensional arrays are now handled properly when parsing ABIs +- Validator will now properly handle all valid numeric type sizes: intN / uintN where 8 <= N <= 256 and N % 8 == 0 From d408325f7c73127b2d6f3271e137fe575911add1 Mon Sep 17 00:00:00 2001 From: Or Noyman <17881579+fullkomnun@users.noreply.github.com> Date: Sun, 17 Sep 2023 23:57:58 +0300 Subject: [PATCH 06/11] include all newly generated single param test cases in validator test suite --- packages/web3-validator/test/unit/web3_validator.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-validator/test/unit/web3_validator.test.ts b/packages/web3-validator/test/unit/web3_validator.test.ts index b9bf10b82a8..1604b07f509 100644 --- a/packages/web3-validator/test/unit/web3_validator.test.ts +++ b/packages/web3-validator/test/unit/web3_validator.test.ts @@ -104,7 +104,7 @@ describe('web3-validator', () => { }); describe('validateJsonSchema', () => { // only single param test cases - it.each(abiToJsonSchemaCases.slice(0, 37))( + it.each(abiToJsonSchemaCases.slice(0, 69))( `$title - should pass for valid data`, abi => { const jsonSchema = abi.json; From 81fcd3671a330c13cfee31fda9ff803a708a7b51 Mon Sep 17 00:00:00 2001 From: luu-alex Date: Thu, 12 Oct 2023 14:15:14 -0400 Subject: [PATCH 07/11] add schemaError and unit test --- packages/web3-errors/src/error_codes.ts | 4 +++ .../web3-errors/src/errors/schema_errors.ts | 32 +++++++++++++++++++ packages/web3-errors/src/index.ts | 1 + packages/web3-validator/src/validator.ts | 3 +- .../test/unit/web3_validator.test.ts | 15 +++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 packages/web3-errors/src/errors/schema_errors.ts diff --git a/packages/web3-errors/src/error_codes.ts b/packages/web3-errors/src/error_codes.ts index 8d077e6a040..7b0814374ac 100644 --- a/packages/web3-errors/src/error_codes.ts +++ b/packages/web3-errors/src/error_codes.ts @@ -158,10 +158,14 @@ export const ERR_INVALID_NIBBLE_WIDTH = 1014; // Validation error codes export const ERR_VALIDATION = 1100; + // Core error codes export const ERR_CORE_HARDFORK_MISMATCH = 1101; export const ERR_CORE_CHAIN_MISMATCH = 1102; +// Schema error codes +export const ERR_SCHEMA = 1200; + // RPC error codes (EIP-1474) // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md export const ERR_RPC_INVALID_JSON = -32700; diff --git a/packages/web3-errors/src/errors/schema_errors.ts b/packages/web3-errors/src/errors/schema_errors.ts new file mode 100644 index 00000000000..df96d7cc1d1 --- /dev/null +++ b/packages/web3-errors/src/errors/schema_errors.ts @@ -0,0 +1,32 @@ +/* +This file is part of web3.js. + +web3.js is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +web3.js is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with web3.js. If not, see . +*/ + +import { ERR_SCHEMA } from '../error_codes.js'; +import { BaseWeb3Error } from '../web3_error_base.js'; + +export class SchemaError extends BaseWeb3Error { + public code = ERR_SCHEMA; + + public constructor(public format: string) { + super(`format ${format} is unsupported`); + } + + public toJSON() { + return { ...super.toJSON(), format: this.format }; + } + +} diff --git a/packages/web3-errors/src/index.ts b/packages/web3-errors/src/index.ts index 5916321f8a6..2e4ef7abca6 100644 --- a/packages/web3-errors/src/index.ts +++ b/packages/web3-errors/src/index.ts @@ -30,3 +30,4 @@ export * from './errors/response_errors.js'; export * from './errors/core_errors.js'; export * from './errors/rpc_errors.js'; export * from './errors/rpc_error_messages.js'; +export * from './errors/schema_errors.js'; \ No newline at end of file diff --git a/packages/web3-validator/src/validator.ts b/packages/web3-validator/src/validator.ts index d3bb4f56c93..b4e9833df8b 100644 --- a/packages/web3-validator/src/validator.ts +++ b/packages/web3-validator/src/validator.ts @@ -14,6 +14,7 @@ GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ +import { SchemaError } from 'web3-errors'; import { Web3ValidationErrorObject } from 'web3-types'; import { z, ZodType, ZodIssue, ZodIssueCode, ZodTypeAny } from 'zod'; @@ -68,7 +69,7 @@ const convertToZod = (schema: JsonSchema): ZodType => { if (schema?.format) { if (!formats[schema.format]) { - throw new Error(`format ${schema.format} is unsupported`); + throw new SchemaError(schema.format); } return z.any().refine(formats[schema.format], (value: unknown) => ({ diff --git a/packages/web3-validator/test/unit/web3_validator.test.ts b/packages/web3-validator/test/unit/web3_validator.test.ts index 1604b07f509..b17ca017f42 100644 --- a/packages/web3-validator/test/unit/web3_validator.test.ts +++ b/packages/web3-validator/test/unit/web3_validator.test.ts @@ -14,6 +14,7 @@ GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ +import { SchemaError } from 'web3-errors' import { abiToJsonSchemaCases } from '../fixtures/abi_to_json_schema'; import { Web3Validator } from '../../src/web3_validator'; import { Web3ValidatorError } from '../../src/errors'; @@ -101,6 +102,20 @@ describe('web3-validator', () => { ), ).toBeUndefined(); }); + + it('should throw due to unsupported format', () => { + expect(() => { + validator.validateJSONSchema( + { + type: 'array', + items: [{ $id: 'a', format: 'unsupportedFormat', required: true }], + minItems: 1, + maxItems: 1, + }, + ['0x2df0879f1ee2b2b1f2448c64c089c29e3ad7ccc5'], + ); + }).toThrow(SchemaError); + }); }); describe('validateJsonSchema', () => { // only single param test cases From eb564a211aef0ff780fefaf71b6e474f6025ba18 Mon Sep 17 00:00:00 2001 From: luu-alex Date: Thu, 12 Oct 2023 14:38:27 -0400 Subject: [PATCH 08/11] update errors --- packages/web3-errors/CHANGELOG.md | 4 +++- packages/web3-errors/src/error_codes.ts | 2 +- packages/web3-errors/src/errors/schema_errors.ts | 6 +++--- packages/web3-errors/test/unit/errors.test.ts | 2 ++ packages/web3-validator/CHANGELOG.md | 5 +++-- packages/web3-validator/src/validator.ts | 2 +- packages/web3-validator/test/unit/web3_validator.test.ts | 4 ++-- 7 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/web3-errors/CHANGELOG.md b/packages/web3-errors/CHANGELOG.md index 6c242768467..303bff74b62 100644 --- a/packages/web3-errors/CHANGELOG.md +++ b/packages/web3-errors/CHANGELOG.md @@ -154,4 +154,6 @@ Documentation: - Dependencies updated -## [Unreleased] \ No newline at end of file +## [Unreleased] + +- Added new SchemaFormatError (#6434) \ No newline at end of file diff --git a/packages/web3-errors/src/error_codes.ts b/packages/web3-errors/src/error_codes.ts index 7b0814374ac..c9ff68f6d7f 100644 --- a/packages/web3-errors/src/error_codes.ts +++ b/packages/web3-errors/src/error_codes.ts @@ -164,7 +164,7 @@ export const ERR_CORE_HARDFORK_MISMATCH = 1101; export const ERR_CORE_CHAIN_MISMATCH = 1102; // Schema error codes -export const ERR_SCHEMA = 1200; +export const ERR_SCHEMA_FORMAT = 1200; // RPC error codes (EIP-1474) // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md diff --git a/packages/web3-errors/src/errors/schema_errors.ts b/packages/web3-errors/src/errors/schema_errors.ts index df96d7cc1d1..a84e1a8392a 100644 --- a/packages/web3-errors/src/errors/schema_errors.ts +++ b/packages/web3-errors/src/errors/schema_errors.ts @@ -15,11 +15,11 @@ You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { ERR_SCHEMA } from '../error_codes.js'; +import { ERR_SCHEMA_FORMAT } from '../error_codes.js'; import { BaseWeb3Error } from '../web3_error_base.js'; -export class SchemaError extends BaseWeb3Error { - public code = ERR_SCHEMA; +export class SchemaFormatError extends BaseWeb3Error { + public code = ERR_SCHEMA_FORMAT; public constructor(public format: string) { super(`format ${format} is unsupported`); diff --git a/packages/web3-errors/test/unit/errors.test.ts b/packages/web3-errors/test/unit/errors.test.ts index f8dd8466fcd..fdc17f2b33e 100644 --- a/packages/web3-errors/test/unit/errors.test.ts +++ b/packages/web3-errors/test/unit/errors.test.ts @@ -26,6 +26,7 @@ import * as signatureErrors from '../../src/errors/signature_errors'; import * as transactionErrors from '../../src/errors/transaction_errors'; import * as utilsErrors from '../../src/errors/utils_errors'; import * as responseErrors from '../../src/errors/response_errors'; +import * as schemaErrors from '../../src/errors/schema_errors'; import { ConvertValueToString } from '../fixtures/errors'; import { BaseWeb3Error } from '../../src/web3_error_base'; @@ -50,6 +51,7 @@ describe('errors', () => { ...signatureErrors, ...transactionErrors, ...utilsErrors, + ...schemaErrors, })) { if (ErrorClass === transactionErrors.InvalidPropertiesForTransactionTypeError) break; // To disable error for the abstract class diff --git a/packages/web3-validator/CHANGELOG.md b/packages/web3-validator/CHANGELOG.md index f93a0645d0b..c4ae7be53b2 100644 --- a/packages/web3-validator/CHANGELOG.md +++ b/packages/web3-validator/CHANGELOG.md @@ -151,5 +151,6 @@ Documentation: ## Fixed -- Multi-dimensional arrays are now handled properly when parsing ABIs -- Validator will now properly handle all valid numeric type sizes: intN / uintN where 8 <= N <= 256 and N % 8 == 0 +- Multi-dimensional arrays are now handled properly when parsing ABIs (#6434) +- Validator will now properly handle all valid numeric type sizes: intN / uintN where 8 <= N <= 256 and N % 8 == 0 (#6434) +- Will now throw SchemaFormatError when unsupported format is given in `convertToZod` method (#6434) \ No newline at end of file diff --git a/packages/web3-validator/src/validator.ts b/packages/web3-validator/src/validator.ts index b4e9833df8b..1ba09ee0ca5 100644 --- a/packages/web3-validator/src/validator.ts +++ b/packages/web3-validator/src/validator.ts @@ -14,7 +14,7 @@ GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { SchemaError } from 'web3-errors'; +import { SchemaFormatError } from 'web3-errors'; import { Web3ValidationErrorObject } from 'web3-types'; import { z, ZodType, ZodIssue, ZodIssueCode, ZodTypeAny } from 'zod'; diff --git a/packages/web3-validator/test/unit/web3_validator.test.ts b/packages/web3-validator/test/unit/web3_validator.test.ts index b17ca017f42..0b910c31f17 100644 --- a/packages/web3-validator/test/unit/web3_validator.test.ts +++ b/packages/web3-validator/test/unit/web3_validator.test.ts @@ -14,7 +14,7 @@ GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ -import { SchemaError } from 'web3-errors' +import { SchemaFormatError } from 'web3-errors' import { abiToJsonSchemaCases } from '../fixtures/abi_to_json_schema'; import { Web3Validator } from '../../src/web3_validator'; import { Web3ValidatorError } from '../../src/errors'; @@ -114,7 +114,7 @@ describe('web3-validator', () => { }, ['0x2df0879f1ee2b2b1f2448c64c089c29e3ad7ccc5'], ); - }).toThrow(SchemaError); + }).toThrow(SchemaFormatError); }); }); describe('validateJsonSchema', () => { From 75b960cf3df774225b5ee9df7012ff5d393c9cad Mon Sep 17 00:00:00 2001 From: luu-alex Date: Thu, 12 Oct 2023 14:48:53 -0400 Subject: [PATCH 09/11] format --- packages/web3-validator/src/validator.ts | 2 +- packages/web3-validator/test/unit/load.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/web3-validator/src/validator.ts b/packages/web3-validator/src/validator.ts index 1ba09ee0ca5..d477a06d96b 100644 --- a/packages/web3-validator/src/validator.ts +++ b/packages/web3-validator/src/validator.ts @@ -69,7 +69,7 @@ const convertToZod = (schema: JsonSchema): ZodType => { if (schema?.format) { if (!formats[schema.format]) { - throw new SchemaError(schema.format); + throw new SchemaFormatError(schema.format); } return z.any().refine(formats[schema.format], (value: unknown) => ({ diff --git a/packages/web3-validator/test/unit/load.test.ts b/packages/web3-validator/test/unit/load.test.ts index 919214ef7af..9053be7e8f0 100644 --- a/packages/web3-validator/test/unit/load.test.ts +++ b/packages/web3-validator/test/unit/load.test.ts @@ -16,7 +16,7 @@ along with web3.js. If not, see . */ import { Web3Validator } from '../../src/web3_validator'; -import { Json, JsonSchema, ValidationSchemaInput } from '../..'; +import { Json, JsonSchema, ValidationSchemaInput } from '../../src/types'; const abi = [ { indexed: true, internalType: 'address', name: 'from', type: 'address' }, From ab461efe77ce040f26e3915d5cc56fbe49b78c2a Mon Sep 17 00:00:00 2001 From: luu-alex Date: Thu, 12 Oct 2023 21:40:22 -0400 Subject: [PATCH 10/11] test converage --- .../test/unit/__snapshots__/errors.test.ts.snap | 10 ++++++++++ packages/web3-errors/test/unit/errors.test.ts | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap index a0f890a4b88..37aca2ffb15 100644 --- a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap +++ b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap @@ -333,6 +333,16 @@ Object { } `; +exports[`errors SchemaFormatError should have valid json structure 1`] = ` +Object { + "code": 1200, + "format": "unsupported", + "innerError": undefined, + "message": "format unsupported is unsupported", + "name": "SchemaFormatError", +} +`; + exports[`errors TransactionError should have valid json structure 1`] = ` Object { "code": 400, diff --git a/packages/web3-errors/test/unit/errors.test.ts b/packages/web3-errors/test/unit/errors.test.ts index fdc17f2b33e..7428cc9b6b3 100644 --- a/packages/web3-errors/test/unit/errors.test.ts +++ b/packages/web3-errors/test/unit/errors.test.ts @@ -381,4 +381,13 @@ describe('errors', () => { ).toMatchSnapshot(); }); }); + + describe('SchemaFormatError', () => { + it('should have valid json structure', () => { + expect( + new schemaErrors.SchemaFormatError("unsupported" + ).toJSON(), + ).toMatchSnapshot(); + }); + }); }); From de550d84ae9785ce08bd45944874db655a0bb528 Mon Sep 17 00:00:00 2001 From: luu-alex Date: Tue, 17 Oct 2023 10:45:30 -0400 Subject: [PATCH 11/11] address feedback --- packages/web3-errors/src/errors/schema_errors.ts | 6 +++--- .../web3-errors/test/unit/__snapshots__/errors.test.ts.snap | 4 ++-- packages/web3-validator/CHANGELOG.md | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/web3-errors/src/errors/schema_errors.ts b/packages/web3-errors/src/errors/schema_errors.ts index a84e1a8392a..4d61684c616 100644 --- a/packages/web3-errors/src/errors/schema_errors.ts +++ b/packages/web3-errors/src/errors/schema_errors.ts @@ -21,12 +21,12 @@ import { BaseWeb3Error } from '../web3_error_base.js'; export class SchemaFormatError extends BaseWeb3Error { public code = ERR_SCHEMA_FORMAT; - public constructor(public format: string) { - super(`format ${format} is unsupported`); + public constructor(public type: string) { + super(`Format for the type ${type} is unsupported`); } public toJSON() { - return { ...super.toJSON(), format: this.format }; + return { ...super.toJSON(), type: this.type }; } } diff --git a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap index 37aca2ffb15..ab49d6aaa0a 100644 --- a/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap +++ b/packages/web3-errors/test/unit/__snapshots__/errors.test.ts.snap @@ -336,10 +336,10 @@ Object { exports[`errors SchemaFormatError should have valid json structure 1`] = ` Object { "code": 1200, - "format": "unsupported", "innerError": undefined, - "message": "format unsupported is unsupported", + "message": "Format for the type unsupported is unsupported", "name": "SchemaFormatError", + "type": "unsupported", } `; diff --git a/packages/web3-validator/CHANGELOG.md b/packages/web3-validator/CHANGELOG.md index 547f8869b36..75ea2a56606 100644 --- a/packages/web3-validator/CHANGELOG.md +++ b/packages/web3-validator/CHANGELOG.md @@ -154,4 +154,4 @@ Documentation: - Multi-dimensional arrays are now handled properly when parsing ABIs (#6435) - Fix issue with default config with babel (and React): "TypeError: Cannot convert a BigInt value to a number #6187" (#6506) - Validator will now properly handle all valid numeric type sizes: intN / uintN where 8 <= N <= 256 and N % 8 == 0 (#6434) -- Will now throw SchemaFormatError when unsupported format is given in `convertToZod` method (#6434) +- Will now throw SchemaFormatError when unsupported format is passed to `convertToZod` method (#6434)