Skip to content

feat: add RemovePropertiesByName method to Validator - #172

Merged
nieomylnieja merged 3 commits into
mainfrom
allow-removing-property-rules-by-name
Nov 13, 2025
Merged

feat: add RemovePropertiesByName method to Validator#172
nieomylnieja merged 3 commits into
mainfrom
allow-removing-property-rules-by-name

Conversation

@nieomylnieja

@nieomylnieja nieomylnieja commented Nov 13, 2025

Copy link
Copy Markdown
Collaborator

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.RemovePropertiesByName method.

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.
@n9-machine-user n9-machine-user added enhancement New feature or request go Pull requests that update Go code minor New functionality with at most minor brekaing changes labels Nov 13, 2025
@github-actions

Copy link
Copy Markdown

Test coverage changes:

Package Before After Diff
github.com/nobl9/govy/cmd/govy 0% 0% ✔️
github.com/nobl9/govy/internal 73% 73% ✔️
github.com/nobl9/govy/internal/collections 100% 100% ✔️
github.com/nobl9/govy/internal/jsonpath 100% 100% ✔️
github.com/nobl9/govy/internal/messagetemplates 89% 89% ✔️
github.com/nobl9/govy/internal/nameinfer 12% 12% ✔️
github.com/nobl9/govy/internal/stringconvert 86% 86% ✔️
github.com/nobl9/govy/internal/typeinfo 94% 94% ✔️
github.com/nobl9/govy/pkg/govy 93% 93% ✔️
github.com/nobl9/govy/pkg/govyconfig 92% 92% ✔️
github.com/nobl9/govy/pkg/govytest 100% 100% ✔️
github.com/nobl9/govy/pkg/rules 98% 98% ✔️

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.

Copilot AI left a comment

Copy link
Copy Markdown

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 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 RemovePropertiesByName method 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.

Comment thread pkg/govy/validator.go
Comment thread pkg/govy/validator.go Outdated
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.
@nieomylnieja
nieomylnieja merged commit 2e4f04a into main Nov 13, 2025
6 checks passed
@nieomylnieja
nieomylnieja deleted the allow-removing-property-rules-by-name branch November 13, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update Go code minor New functionality with at most minor brekaing changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants