xrpl.js icon indicating copy to clipboard operation
xrpl.js copied to clipboard

Fix issue #2858: Improve error messages in field validation functions

Open lacostenycoder opened this issue 8 months ago • 2 comments

High Level Overview of Change

  • Improved error messages in field validation functions to include expected and actual types
  • Added tests to verify the improved error messages work correctly
  • Fixed line endings in test files

Context of Change

The validation functions in the XRPL.js library previously provided generic error messages when field validation failed. This PR improves these error messages by including both the expected type and the actual type received, making it easier for developers to debug issues with their transactions.

The changes specifically enhance the validateRequiredField and validateOptionalField functions in the transaction validation system to provide more descriptive error messages, and adds comprehensive test coverage for these improved messages.

Type of Change

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] Refactor (non-breaking change that only restructures code)
  • [x] Tests (You added tests for code that already exists, or your new feature included in this PR)
  • [ ] Documentation Updates
  • [ ] Release

Did you update HISTORY.md?

  • [x] Yes
  • [ ] No, this change does not impact library users

Test Plan

The changes have been verified by:

  1. Adding unit tests in packages/xrpl/test/models/transactions/common.test.ts that verify the improved error messages
  2. Testing that validation functions properly include expected and actual types in error messages
  3. Ensuring backward compatibility with existing code by maintaining the same error types and general message format

The tests specifically verify:

  • Error messages now include both expected and actual types
  • Validation still works correctly for valid values
  • Validation still produces proper errors for missing fields
  • The optional field validation still behaves as expected

lacostenycoder avatar Apr 09 '25 20:04 lacostenycoder

Walkthrough

This pull request modifies the validation behavior in the XRPL transaction model. The changes introduce an additional optional parameter, expectedType, to both the validateRequiredField and validateOptionalField functions in the common module. This update enhances the clarity of error messages by reporting the expected versus received type. Additionally, the related transaction field validations in validateBaseTransaction are updated, and corresponding unit tests have been added to cover various scenarios for both functions.

Changes

File(s) Change Summary
packages/xrpl/src/.../transactions/common.ts Updated validateRequiredField and validateOptionalField to include an optional expectedType parameter; error messages now report expected and actual types; updated calls in validateBaseTransaction.
packages/xrpl/test/.../transactions/common.test.ts Added a new test suite for the validation functions, covering various scenarios including type mismatches and missing fields.
packages/xrpl/test/.eslintrc.js Introduced a new ESLint configuration file that disables the requirement for JSDoc comments in the codebase.

Possibly related issues

  • XRPLF/xrpl.js#2858: The changes implement the suggestion to include the expectedType parameter in validation functions for more specific error messages.

Suggested reviewers

  • mvadari
  • achowdhry-ripple
  • ckeshava
  • khancode

Poem

I hopped through lines of code so neat,
Adding types to every beat.
Error messages now sing their song,
Telling what went right or wrong.
In fields required, a clear type view,
A bunny's hop brings bug-fixing through!
🐇✨ Happy coding, on we go!

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 09 '25 20:04 coderabbitai[bot]

I prefer the solution in #2895, since that doesn't involve as much duplication in the function when writing it.

mvadari avatar Apr 10 '25 16:04 mvadari