feat: add RemovePropertiesByName method to Validator - #172
Merged
Conversation
This commit introduces the `RemovePropertiesByName` method to the `Validator` type, allowing users to remove specific property rules or included validators by their names. The method returns a modified `Validator` instance without altering the original validator. To support this functionality, the following changes were made: - Added a `getName` method to the `propertyRulesInterface` and its implementations (`PropertyRules`, `PropertyRulesForMap`, and `PropertyRulesForSlice`) to retrieve the name of a property. - Updated the `Validator` struct to filter out properties based on the provided names in the `RemovePropertiesByName` method. Comprehensive tests have been added to ensure the correctness of the new method, covering scenarios such as removing single or multiple properties, handling non-existent properties, and verifying that the original validator remains unchanged.
|
Test coverage changes:
|
The test cases in `TestValidatorRemovePropertiesByName` were updated to include pre-validation error checks before removing properties. This ensures that the validator behaves as expected when validating the initial state of the object. Additionally, the rules for `SliceMaxLength` and `MapMaxLength` were adjusted to use a length of `0` for more stringent testing.
There was a problem hiding this comment.
Pull Request Overview
This PR adds a RemovePropertiesByName method to the Validator type, enabling removal of specific validation rules by property name. This addresses the limitation where validators imported from external modules cannot have their rules modified after instantiation.
Key changes:
- Added
RemovePropertiesByNamemethod that filters out properties matching the provided names and returns a new validator instance - Implemented
getName()method across property rule types to support the filtering mechanism - Comprehensive test coverage for various removal scenarios including single/multiple properties, empty names, and different property types
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/govy/validator.go | Implements the core RemovePropertiesByName method that filters properties by name |
| pkg/govy/validation.go | Adds getName() to the propertyRulesInterface |
| pkg/govy/rules.go | Implements getName() for PropertyRules |
| pkg/govy/rules_for_slice.go | Implements getName() for PropertyRulesForSlice |
| pkg/govy/rules_for_map.go | Implements getName() for PropertyRulesForMap |
| pkg/govy/validator_test.go | Adds comprehensive test coverage for the new method |
| pkg/govy/example_test.go | Provides usage example demonstrating the feature |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Refactor the `RemovePropertiesByName` method in the `Validator` to use the `slices.Contains` function for checking if a property name exists in the provided list. This change simplifies the logic by replacing the manual loop and boolean flag with a more concise and efficient approach.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Sometimes you want to create a modified validator without certain rules, the only way to achieve that currently is during the validator's definition. If you are importing a module which defines a validator, you have currently no way to change its rules in any way.
That's why it would be useful to be able to remove properties' rules by name.
Related Changes
There's another PR which explores deleting properties by IDs: #171.
Release Notes
Added
govy.Validator.RemovePropertiesByNamemethod.