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

#533: Add Validation for Discriminator with OneOf and Mapping. #536

Conversation

superstas
Copy link
Contributor

@superstas superstas commented Aug 6, 2024

It fixes #533.

@danielgtaylor, since it quite extends the current validation logic, I'm open to your feedback and requests for changes.

Thank you.

Summary by CodeRabbit

  • New Features

    • Introduced a new validation function for handling discriminators in schemas, enhancing schema validation accuracy.
    • Added new animal schema types (Cat and Dog) for improved validation scenarios.
  • Bug Fixes

    • Updated error messages for better clarity in case of validation failures when schemas do not match.
  • Tests

    • Enhanced validation tests with additional cases for schemas utilizing discriminators to ensure robustness of the validation framework.

Copy link

coderabbitai bot commented Aug 6, 2024

Walkthrough

The changes enhance the schema validation framework by introducing a new validateDiscriminator function, improving clarity in error messages, and expanding the test coverage for scenarios involving discriminators. These updates allow for more precise validation outcomes, particularly when handling invalid requests, ultimately enhancing the user experience during validation errors.

Changes

Files Change Summary
validate.go, messages.go Added validateDiscriminator function for improved handling of discriminators; updated error messages for clarity.
validate_test.go Introduced comprehensive test cases for discriminator usage, validating input types and schema mappings.

Assessment against linked issues

Objective Addressed Explanation
Validate with discriminator.mapping (#533)
Provide clear validation error messages (#533)
Ensure valid discriminator behavior (#533) The error messages do not specify validation failures related to the discriminator logic.

Poem

🐇 In fields of code, we leap and bound,
With discriminators, new joy is found.
Errors now sing, clear as day,
Guiding our users along the way.
Hooray for checks, both swift and bright,
In our validation, all feels just right! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9975252 and b776b7c.

Files selected for processing (3)
  • validate.go (3 hunks)
  • validate_test.go (3 hunks)
  • validation/messages.go (1 hunks)
Additional comments not posted (15)
validation/messages.go (3)

20-20: Renamed constant is appropriate.

The new name MsgExpectedMatchAtLeastOneSchema accurately reflects its purpose and is consistent with the other message constants.


21-21: New constant is appropriate.

The new constant MsgExpectedMatchExactlyOneSchema is correctly defined and appropriately named.


23-23: New constant is appropriate.

The new constant MsgExpectedPropertyNameInObject is correctly defined and appropriately named.

validate.go (6)

278-278: Updated error message is appropriate.

The new message constant validation.MsgExpectedMatchExactlyOneSchema is correctly used.


294-294: Updated error message is appropriate.

The new message constant validation.MsgExpectedMatchAtLeastOneSchema is correctly used.


298-324: New function validateDiscriminator is well-implemented.

The function correctly handles the validation logic for discriminators, ensuring the presence and type of the discriminator property, and validating against the appropriate schema based on the discriminator mapping.


349-353: Integration of validateDiscriminator is appropriate.

The Validate function correctly calls validateDiscriminator if a discriminator is present, ensuring the new validation logic is utilized.


350-350: Integration of validateDiscriminator is appropriate.

The Validate function correctly calls validateDiscriminator if a discriminator is present, ensuring the new validation logic is utilized.


352-352: Integration of validateOneOf is appropriate.

The Validate function correctly calls validateOneOf if no discriminator is present, ensuring the existing validation logic is utilized.

validate_test.go (6)

12-12: Addition of require package is appropriate.

The require package is correctly imported for assertions.


1259-1323: New test cases for schemas with discriminators are well-implemented.

The test cases correctly validate the behavior of schemas with discriminators, ensuring the new validation logic is thoroughly tested.


1460-1463: New type Cat is appropriately defined.

The Cat type is correctly defined with validation rules for the name and kind properties.


1465-1468: New type Dog is appropriately defined.

The Dog type is correctly defined with validation rules for the color and kind properties.


1470-1562: New test function Test_validateWithDiscriminator is well-implemented.

The test function correctly tests the validation logic for schemas with discriminators, ensuring the new validation logic is thoroughly validated.


1470-1470: New test function Test_validateWithDiscriminator is well-implemented.

The test function correctly tests the validation logic for schemas with discriminators, ensuring the new validation logic is thoroughly validated.

Copy link

codecov bot commented Aug 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.74%. Comparing base (a3e556b) to head (5cb51e9).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #536      +/-   ##
==========================================
+ Coverage   92.69%   92.74%   +0.05%     
==========================================
  Files          22       22              
  Lines        3845     3872      +27     
==========================================
+ Hits         3564     3591      +27     
  Misses        236      236              
  Partials       45       45              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b776b7c and 5cb51e9.

Files selected for processing (2)
  • validate.go (3 hunks)
  • validate_test.go (3 hunks)
Additional comments not posted (7)
validate.go (3)

278-278: Improved error message clarity.

The updated error message in validateOneOf enhances clarity, aligning with the goal of providing more precise validation feedback.


294-294: Improved error message clarity.

The updated error message in validateAnyOf enhances clarity, aligning with the goal of providing more precise validation feedback.


298-335: New function validateDiscriminator added.

The function effectively handles validation with discriminators, improving schema validation logic. The integration with existing validation functions appears seamless.

Ensure that this function is invoked correctly in all relevant scenarios.

validate_test.go (4)

12-12: Addition of require package for assertions.

The use of the require package improves the reliability of test assertions, ensuring that tests fail immediately on errors.


Line range hint 477-485: New structs Cat and Dog added for testing.

The addition of these structs with specific validation rules enhances the test coverage for discriminator functionality.


1259-1340: Comprehensive test cases for discriminator validation.

The new test cases effectively cover a range of scenarios for schemas with discriminators, ensuring robust validation logic.


1477-1597: Comprehensive test suite for validateWithDiscriminator.

The test suite covers various scenarios, ensuring that the discriminator logic is thoroughly validated. The use of require for assertions strengthens the test reliability.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

@superstas nice work! I hope you don't mind I added a small commit to support map[any]any instead of just map[string]any because some formats (CBOR/YAML/MsgPack/Protobuf/etc) support keys of any type and since Huma supports custom formats with client-driven content negotiation we should support them, but otherwise this looks great!

@danielgtaylor danielgtaylor merged commit 5c12ecb into danielgtaylor:main Aug 12, 2024
3 checks passed
@superstas superstas deleted the chore/discriminator-mapping-validation branch August 12, 2024 20:38
@superstas
Copy link
Contributor Author

@danielgtaylor sure, thank you for the addition and the quick merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation with discriminator.mapping
2 participants