diff --git a/src/index.test.ts b/src/index.test.ts index 9e36ce70..08a0d657 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -41,6 +41,7 @@ describe('index', () => { "KnownCaipNamespace", "PendingJsonRpcResponseStruct", "StrictHexStruct", + "UnsafeFastJsonStruct", "UnsafeJsonStruct", "VersionRangeStruct", "VersionStruct", diff --git a/src/json.test.ts b/src/json.test.ts index ccd823e4..c4e620a5 100644 --- a/src/json.test.ts +++ b/src/json.test.ts @@ -236,7 +236,7 @@ describe('json', () => { const [error] = validate(undefined, JsonStruct); assert(error !== undefined); expect(error.message).toBe( - 'Expected the value to satisfy a union of `literal | boolean | finite number | string | array | record`, but received: undefined', + 'Expected a value of type `JSON`, but received: `undefined`', ); }); }); @@ -432,11 +432,7 @@ describe('json', () => { }, }, }; - const now = performance.now(); - expect(() => - assertIsJsonRpcRequest(request), - ).not.toThrow(); - console.log('Asserting took', performance.now() - now); + expect(() => assertIsJsonRpcRequest(request)).not.toThrow(); }); }); diff --git a/src/json.ts b/src/json.ts index f01a44c0..f8b75eb6 100644 --- a/src/json.ts +++ b/src/json.ts @@ -182,14 +182,28 @@ function validateJson(json: unknown): boolean { } if (typeof json === 'object') { + let every = true; if (Array.isArray(json)) { - return json.every((value) => validateJson(value)); + // eslint-disable-next-line @typescript-eslint/prefer-for-of + for (let i = 0; i < json.length; i++) { + if (!validateJson(json[i])) { + every = false; + break; + } + } + return every; } const entries = Object.entries(json); - return entries.every( - ([key, value]) => typeof key === 'string' && validateJson(value), - ); + // eslint-disable-next-line @typescript-eslint/prefer-for-of + for (let i = 0; i < entries.length; i++) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + if (typeof entries[i]![0] !== 'string' || !validateJson(entries[i]![1])) { + every = false; + break; + } + } + return every; } return false; @@ -197,7 +211,7 @@ function validateJson(json: unknown): boolean { /** * A struct to check if the given value is a valid JSON-serializable value. - * + * * A faster alternative to {@link UnsafeJsonStruct}. * * Note that this struct is unsafe. For safe validation, use {@link JsonStruct}. diff --git a/src/node.test.ts b/src/node.test.ts index 5f7d5ed4..b0cada9b 100644 --- a/src/node.test.ts +++ b/src/node.test.ts @@ -41,6 +41,7 @@ describe('node', () => { "KnownCaipNamespace", "PendingJsonRpcResponseStruct", "StrictHexStruct", + "UnsafeFastJsonStruct", "UnsafeJsonStruct", "VersionRangeStruct", "VersionStruct",