Skip to content

fix: validate queryEvents parameter#23

Open
LI-YONG-QI wants to merge 1 commit into
MystenLabs:mainfrom
LI-YONG-QI:fix/event
Open

fix: validate queryEvents parameter#23
LI-YONG-QI wants to merge 1 commit into
MystenLabs:mainfrom
LI-YONG-QI:fix/event

Conversation

@LI-YONG-QI
Copy link
Copy Markdown

@LI-YONG-QI LI-YONG-QI commented Jan 21, 2025

Descriptiona

Fix the issue that parameter of queryEvents can't be a multi objects.

The Typescript type checker won’t throw error if developers pass multi objects as query, but this is invalid format for sending request to rpc and the error message returned from rpc response isn't clear. Therefore, I added a validation logic and more clear error message before request to check keyof input object

      await toolbox.client.queryEvents({
		query: {
			TimeRange: { startTime: '0', endTime: Date.now().toString() },
			Sender: toolbox.address(), // not throw error in VSCode type safe checker
		},
	});

I have not found any additional rules or pattern for contributing open source code, so if I make something wrong, please tell me

Test plan

Added error test in e2e test cases


@LI-YONG-QI LI-YONG-QI requested a review from a team as a code owner January 21, 2025 12:14
@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
sui-typescript-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 21, 2025 0:17am

@LI-YONG-QI LI-YONG-QI changed the title fix: validate queryEvent parameter fix: validate queryEvents parameter Jan 21, 2025
@hayes-mysten hayes-mysten added the enhancement New feature or request label Jan 23, 2025
@LI-YONG-QI LI-YONG-QI had a problem deploying to sui-typescript-aws-kms-test-env January 23, 2025 17:59 — with GitHub Actions Failure
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ 🦋 Changesets Warning: This PR has changes to public npm packages, but does not contain a changeset. You can create a changeset easily by running pnpm changeset in the root of the Sui repo, and following the prompts. If your change does not need a changeset (e.g. a documentation-only change), you can ignore this message. This warning will be removed when a changeset is added to this pull request.

Learn more about Changesets.

*/
async queryEvents(input: QueryEventsParams): Promise<PaginatedEvents> {
if (Object.keys(input.query).length > 1)
throw new Error('Invalid query parameters, please provide only one query object');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't generally do runtime validation of query inputs, and have generally relied on errors returned from the API and typescript to enforce correct usage.

I am a little hesitant to start adding this kind of validation only to specific methods, leading to an inconsistent experience.

If you wanted to improve this, I think the correct way to do this would be to update the generator that creates these types to be more strict:

return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);

This could either be by updating the types to be something like { TimeRange: string, Sender?: never } | { TimeRange?: never, Sender: string } or by creating a wrapping type utility that dynamically transforms these union types

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hayes-mysten Thanks for your reviewing! I might need some time to try to improve and learn how to fix that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants