Repository navigation
feat: introduce custom validator for addon - #2633
Conversation
📝 WalkthroughWalkthroughThis pull request updates several areas of the product catalog and validation logic. In the add-on modules, it renames method receivers and adds new methods for validation and data transformation. The service layer now features a dedicated type for managing add-on versions along with associated methods, and the plan service updates its pagination approach. Additionally, the models package has been extended with generic validator types and functions to support flexible multi-validator use. Changes
Possibly related PRs
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms (13)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
openmeter/productcatalog/addon/service/addon.go (1)
80-118: Good implementation of addonVersions with sort interfaceThe new addonVersions type with its related methods provides a clean abstraction for managing collections of addon versions. The implementation of the sort.Interface (Len, Less, Swap) is correct and the additional utility methods improve code readability.
However, there's a potential issue with the Latest() method:
- It assumes the collection is already sorted, but this is not enforced or documented
- It returns a pointer to an element in the slice, which could be problematic if the slice is modified later
Consider adding documentation to indicate that Latest() should only be called after Sort(), or have Latest() call Sort() internally:
// Latest returns add-on with the latest version regardless of its deleted status. +// The collection should be sorted before calling this method, or you can call Sort() +// first to ensure correct ordering. func (a addonVersions) Latest() *addon.Addon { if len(a) == 0 { return nil } + + // Ensure the collection is sorted + a.Sort() return &a[len(a)-1] }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
openmeter/productcatalog/addon.go(4 hunks)openmeter/productcatalog/addon/addon.go(1 hunks)openmeter/productcatalog/addon/service/addon.go(18 hunks)openmeter/productcatalog/plan/service/plan.go(0 hunks)pkg/models/validator.go(1 hunks)
💤 Files with no reviewable changes (1)
- openmeter/productcatalog/plan/service/plan.go
⏰ Context from checks skipped due to timeout of 90000ms (10)
- GitHub Check: Artifacts / Container image
- GitHub Check: Test
- GitHub Check: Quickstart
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: E2E
- GitHub Check: Developer environment
- GitHub Check: CI
- GitHub Check: Commit hooks
- GitHub Check: Build
🔇 Additional comments (16)
openmeter/productcatalog/addon/addon.go (1)
46-51: Great implementation of the AsProductCatalogAddon conversion methodThe method provides a clean transformation from the addon package's Addon type to the productcatalog package's Addon, with proper field mapping and reuse of the RateCards conversion method.
pkg/models/validator.go (3)
3-4: Good addition of the errors importProperly importing the standard errors package to use errors.Join which provides cleaner error aggregation.
10-14: Well-designed generic validation interfacesThe generic ValidatorFunc and CustomValidator definitions provide a flexible, type-safe way to implement custom validation logic. This pattern will make validation more composable and reusable across different entity types.
16-26: Solid implementation of the generic Validate functionThis function elegantly applies multiple validators and aggregates errors, making validation more modular. The implementation correctly handles the case when no errors are found by using NewNillableGenericValidationError.
openmeter/productcatalog/addon/service/addon.go (5)
120-133: Well-implemented getAddonVersions helper methodThis method cleanly encapsulates the logic for retrieving addon versions, improving code reusability. The error handling is good with proper context in the error message.
148-162: Improved code with refactored version handlingThe refactoring to use the new addonVersions type and its methods (HasDraft, Latest) has simplified the code and improved readability. The error handling is also more specific and informative.
217-221: Good use of the new validation frameworkThe code now uses the ValidateWith method with the WithAllowedAddonStatus validator, which makes the validation logic more explicit and maintainable. This approach will make it easier to add or modify validation rules in the future.
378-380: Improved validation with the Publishable methodUsing the Publishable method for validation before publishing is a good approach that ensures all required validations are performed consistently. This encapsulates the validation logic in the model instead of the service.
476-480: Good use of ValidateWith with appropriate validatorsThe code correctly uses the ValidateWith method to validate that an addon can only be archived if it has the Active status. This makes the validation requirements explicit and maintainable.
openmeter/productcatalog/addon.go (7)
132-134: Improved receiver naming for better readabilityChanging the receiver name from
atomin AddonMeta methods makes the code more descriptive and easier to understand, as it clearly refers to the AddonMeta type.
160-164: Good compile-time interface assertionsThese assertions ensure that Addon implements the specified interfaces (Validator, CustomValidator, Equaler), which helps catch errors at compile time rather than runtime.
173-175: Well-implemented ValidateWith methodThis method integrates nicely with the new validation framework, allowing for flexible validation of Addon instances using multiple validators.
204-213: Excellent Publishable validation methodThe Publishable method provides a higher-level validation specifically for publishing scenarios. It applies multiple validators to ensure all requirements are met, which is a good example of composing validators for a specific use case.
246-255: Well-designed validator function for addon statusThe WithAllowedAddonStatus validator function follows good functional programming patterns and provides clear error messages. It correctly uses the lo.Contains function for checking allowed statuses.
257-265: Good validator for billing cadence alignmentThis validator ensures that all rate cards in an addon have aligned billing cadences, which is an important business rule. The error message is clear and informative.
267-284: Comprehensive validator for instance type constraintsThis validator enforces complex business rules that prevent usage-based rate cards in multiple-instance addons. The switch statement handles all cases, including an explicit check for invalid instance types.
9bf6a8f to
ab82fac
Compare
ab82fac to
030c747
Compare
Overview
Add custom validator support for Addon type.
Summary by CodeRabbit
New Features
Refactor
Bug Fixes