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

Json RPC params are now properly castable to Json type #51

Merged
merged 4 commits into from Nov 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/json.test.ts
@@ -1,4 +1,5 @@
import * as superstructModule from 'superstruct';
import { validate } from 'superstruct';
import {
ARRAY_OF_DIFFRENT_KINDS_OF_NUMBERS,
COMPLEX_OBJECT,
Expand All @@ -13,26 +14,38 @@ import {
NON_SERIALIZABLE_NESTED_OBJECT,
} from './__fixtures__';
import {
assert,
assertIsJsonRpcError,
assertIsJsonRpcFailure,
assertIsJsonRpcNotification,
assertIsJsonRpcRequest,
assertIsJsonRpcResponse,
assertIsJsonRpcSuccess,
assertIsPendingJsonRpcResponse,
getJsonRpcIdValidator,
isJsonRpcError,
isJsonRpcFailure,
isJsonRpcNotification,
isJsonRpcRequest,
isJsonRpcResponse,
isJsonRpcSuccess,
isPendingJsonRpcResponse,
isValidJson,
JsonStruct,
validateJsonAndGetSize,
isJsonRpcError,
assertIsJsonRpcError,
} from '.';

describe('json', () => {
describe('JsonStruct', () => {
it('returns error message', () => {
const [error] = validate(undefined, JsonStruct);
assert(error !== undefined);
expect(error.message).toStrictEqual(
'Expected a valid JSON-serializable value',
);
});
});

// TODO: Make this test suite exhaustive.
// The current implementation is guaranteed to be correct, but in the future
// we may opt for a bespoke implementation that is more performant, but may
Expand Down Expand Up @@ -425,11 +438,11 @@ describe('json', () => {
};

const validateAll = (
validate: ReturnType<typeof getJsonRpcIdValidator>,
validator: ReturnType<typeof getJsonRpcIdValidator>,
inputs: ReturnType<typeof getInputs>,
) => {
for (const input of Object.values(inputs)) {
expect(validate(input.value)).toStrictEqual(input.expected);
expect(validator(input.value)).toStrictEqual(input.expected);
}
};

Expand Down
13 changes: 9 additions & 4 deletions src/json.ts
Expand Up @@ -10,22 +10,26 @@ import {
object,
omit,
optional,
record,
string,
Struct,
union,
unknown,
} from 'superstruct';
import { AssertionErrorConstructor, assertStruct } from './assert';
import {
calculateNumberSize,
calculateStringSize,
isPlainObject,
JsonSize,
} from './misc';
import { AssertionErrorConstructor, assertStruct } from './assert';

export const JsonStruct = define<Json>('Json', (value) => {
const [isValid] = validateJsonAndGetSize(value, true);
return isValid;
if (!isValid) {
return 'Expected a valid JSON-serializable value';
}
return true;
});

/**
Expand Down Expand Up @@ -99,8 +103,9 @@ export type JsonRpcError = OptionalField<
'data'
>;

export const JsonRpcParamsStruct = optional(union([object(), array()]));

export const JsonRpcParamsStruct = optional(
union([record(string(), JsonStruct), array(JsonStruct)]),
);
export type JsonRpcParams = Infer<typeof JsonRpcParamsStruct>;

export const JsonRpcRequestStruct = object({
Expand Down