-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor data validation utilities and improve type safety #31
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
Conversation
…n, improve type safety, and implement new `validateRequestData` utility for request validation.
…`, and `syncpack` dependencies.
Summary of ChangesHello @hckhanh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly overhauls the API schema typing and validation infrastructure to provide a more robust, type-safe, and flexible system. The core changes revolve around introducing a more generic data type for API interactions, centralizing and strengthening request validation logic, and refining the API fetching mechanism to integrate these improvements seamlessly. The goal is to enhance developer experience, reduce common API-related errors, and ensure greater consistency in data handling. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
CodSpeed Performance ReportMerging #31 will degrade performances by 13.33%Comparing Summary
Benchmarks breakdown
Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #31 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 3 3
Lines 48 72 +24
Branches 14 21 +7
=========================================
+ Hits 48 72 +24 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@copilot I make a lot of changes to the codebase, please update documentations and tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request is a great step forward in improving the type safety and flexibility of the API client. The introduction of the generic ApiData type and the validateRequestData utility are excellent changes.
I've identified a few areas for improvement:
- There's a critical bug in
validateRequestDatawhere a stateful regular expression is used, which can lead to incorrect runtime behavior. - The
validateRequestDatafunction currently uses a type assertion that discards the specific types of the validated data. I've suggested a fix which involves a small change to theApiDatatype to better handle optional schema parts, thereby preserving the strong types and fully achieving the goals of this refactoring.
Addressing these points will make the implementation more robust and fully leverage the power of TypeScript for a truly type-safe API client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the schema validation and type system to improve flexibility and type safety. The main change replaces the specific ApiResponse type with a more general ApiData type that can extract any schema option (params, query, body, or response), and introduces a new validateRequestData utility to validate request data and enforce parameterized path requirements.
- Introduces
ApiData<Schemas, Path, Option>type for flexible schema data extraction - Adds
validateRequestDatautility to validate request params, query, and body together - Enhances body handling to explicitly check for
undefinedandnullbefore stringifying
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| src/types.ts | Replaces ApiResponse with more flexible ApiData type that accepts an Option parameter to extract any schema field; updates type parameter names for clarity |
| src/utils.ts | Refactors validateData to use new ApiData type and accept dynamic schema options; adds new validateRequestData function to validate all request data and check parameterized paths |
| src/index.ts | Updates imports/exports to use ApiData; integrates validateRequestData for request validation; improves body handling to avoid sending undefined/null |
| deno.lock | Adds schema library dependencies (arktype, valibot, zod) for testing; updates syncpack and other dependency versions |
validateData and createFetch to enhance schema validation, improve type safety, and implement new validateRequestData utility for request validation.…teRequestData utility (#32) * Initial plan * Add comprehensive tests for validateRequestData and validateData utilities Co-authored-by: hckhanh <[email protected]> * Update documentation to reflect new ApiData type and validation behavior Co-authored-by: hckhanh <[email protected]> * Refactor API schema typing and validation to enhance type safety, streamline request validation, and improve flexibility in `createFetch`. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: hckhanh <[email protected]> Co-authored-by: Khánh Hoàng <[email protected]>
…improve type safety, enhance schema validation, and unify request validation logic.
|
@copilot update tests |
* Initial plan * Fix validateRequestData to return object and address review comments Co-authored-by: hckhanh <[email protected]> * Revert validateRequestData to return array as requested Co-authored-by: hckhanh <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: hckhanh <[email protected]>
|



This pull request refactors the API schema typing and validation logic to improve type safety and flexibility for API requests and responses. The main changes include replacing the
ApiResponsetype with a more generalApiDatatype, updating validation utilities to support dynamic schema options, and enhancing request validation for parameterized paths.Type system improvements
ApiResponsetype with a new genericApiDatatype, allowing extraction of any schema option ('params','query','body','response') for a given API path. This change provides more flexible and accurate typing for API data throughout the codebase.ApiDatainstead ofApiResponse, and clarified type parameter names for better readability and maintainability. [1] [2] [3]Validation logic improvements
validateDatautility to use the newApiDatatype and accept dynamic schema options, improving reusability and type safety for validating different parts of an API request or response.validateRequestDatautility that validatesparams,query, andbodyfor a given API path, and throws an error if a parameterized path lacks a correspondingparamsschema. This ensures runtime safety for parameterized API endpoints.API fetch function changes
createFetchfunction to usevalidateRequestDatafor validating request options and to return the correctApiDatatype for responses. Also improved handling of request bodies to avoid sendingundefinedornullvalues. [1] [2]