Skip to content

feat: allow removing property rules from validator by ids - #171

Open
nieomylnieja wants to merge 25 commits into
mainfrom
allow-removing-property-rules-by-id
Open

feat: allow removing property rules from validator by ids#171
nieomylnieja wants to merge 25 commits into
mainfrom
allow-removing-property-rules-by-id

Conversation

@nieomylnieja

@nieomylnieja nieomylnieja commented Nov 12, 2025

Copy link
Copy Markdown
Collaborator

Motivation

Other packages can export validators. Callers need to remove selected property rules without rebuilding the validator or depending on property paths.

Summary

  • Added caller-defined IDs through WithID for scalar, slice, and map property rules.
  • Added immutable Validator.RemovePropertiesByID to remove matching direct property rules without traversing included validators.
  • Treated empty IDs as unset.

Related Changes

PR #172 added removal by property paths. This PR provides an alternative that does not depend on paths.

Testing

  • Added unit coverage for caller-defined IDs, multiple matches, empty and unknown IDs, immutable derivation, and the included-validator boundary.
  • Added a testable example that shows the original validator remains unchanged.

Release Notes

Property rules can now receive caller-defined IDs with WithID. Callers can use Validator.RemovePropertiesByID to derive a validator without matching direct property rules.

- Updated `golangci-lint` from version 2.5.0 to 2.6.1 in `devbox.json` and `devbox.lock`.
- Introduced a new `uuid` package for generating UUIDs. The `GenerateUUID` function creates UUIDs compliant with version 4 specifications.
- Added comprehensive unit tests for the `uuid` package to ensure correctness, including format validation, uniqueness, length checks, and bit correctness.
- Enhanced the `govy` package with new features:
  - Added `instanceID` for unique identification of `Validator` and `PropertyRules` instances.
  - Introduced `WithID` and `GetID` methods for `Validator` and `PropertyRules` to manage and retrieve identifiers.
  - Implemented `RemoveProperties` in `Validator` to allow selective removal of properties or nested validators.
- Updated benchmarks in `rules` package to use `b.Loop()` for improved readability and consistency.
- Added new examples and tests to demonstrate and validate the usage of `RemoveProperties` and `GetID` functionalities.
@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 12, 2025
@github-actions

github-actions Bot commented Nov 12, 2025

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/inferpath 76% 76% ✔️
github.com/nobl9/govy/internal/messagetemplates 89% 89% ✔️
github.com/nobl9/govy/internal/stringconvert 86% 86% ✔️
github.com/nobl9/govy/internal/typeinfo 94% 94% ✔️
github.com/nobl9/govy/pkg/govy 94% 94% ✔️
github.com/nobl9/govy/pkg/govytest 100% 100% ✔️
github.com/nobl9/govy/pkg/jsonpath 85% 85% ✔️
github.com/nobl9/govy/pkg/rules 99% 99% ✔️

This update introduces the ability to set custom identifiers for property rules using the `WithID` method. The feature is implemented across `PropertyRules`, `PropertyRulesForSlice`, and `PropertyRulesForMap`. These identifiers can be used for referencing specific rules, such as when removing properties from a validator.

The changes include:
- Implementation of the `WithID` method in `PropertyRules`, `PropertyRulesForSlice`, and `PropertyRulesForMap`.
- Updates to the `GetID` method to prioritize user-supplied IDs over names and auto-generated UUIDs.
- Modifications to example and test files to demonstrate and validate the new functionality.
- Adjustments to the `Makefile` to ignore `node_modules` during markdown linting.
This update introduces a shift from name-based property removal to ID-based property removal in the `govy` package. The changes include:

- Replacing `RemoveProperties` with `RemovePropertiesByID` in the `Validator` API.
- Modifying `PropertyRules.GetID` and `Validator.GetID` to return only user-supplied IDs or auto-generated UUIDs, removing reliance on property names.
- Updating examples and tests to reflect the new ID-based approach, ensuring clarity and consistency.
- Simplifying the internal logic for ID retrieval by consolidating methods in the `instanceID` struct.

These changes enhance the flexibility and reliability of property rule management, particularly in dynamic or complex validation scenarios.
@nieomylnieja nieomylnieja changed the title feat: allow removing property rules from validator feat: allow removing property rules from validator by ids Nov 13, 2025
nieomylnieja added a commit that referenced this pull request Nov 13, 2025
## 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.
…ty-rules-by-id

# Conflicts:
#	devbox.json
#	devbox.lock
#	pkg/govy/example_test.go
#	pkg/govy/rules.go
#	pkg/govy/rules_for_map.go
#	pkg/govy/rules_for_slice.go
#	pkg/govy/rules_test.go
#	pkg/govy/validation.go
#	pkg/govy/validator.go
Avoid mutating the enum value before bounds checking so unknown keys render
their original numeric value and valid lookups use a safe zero-based index.
Ensure `ForPointer` and `Transform` create generated property IDs so
`RemovePropertiesByID` can filter those rules consistently with `For`.
@nieomylnieja
nieomylnieja marked this pull request as ready for review June 16, 2026 17:33
Replace optimized ID-removal paths with a single filtering implementation and delegate builder inheritance to public methods. Update tests and examples to cover direct property removal behavior.
…ty-rules-by-id

# Conflicts:
#	pkg/govy/example_test.go

@n9-machine-user n9-machine-user left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Govy Go Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 59342fa Previous: 95e0216 Ratio
BenchmarkStringUUID/valid (github.com/nobl9/govy/pkg/rules) 522.9 ns/op 10.00 validations/op 0 B/op 0 allocs/op 251.4 ns/op 10.00 validations/op 0 B/op 0 allocs/op 2.08
BenchmarkStringUUID/valid (github.com/nobl9/govy/pkg/rules) - ns/op 522.9 ns/op 251.4 ns/op 2.08
BenchmarkStringUUIDRFC4122/valid (github.com/nobl9/govy/pkg/rules) 403.1 ns/op 9.000 validations/op 0 B/op 0 allocs/op 234.6 ns/op 9.000 validations/op 0 B/op 0 allocs/op 1.72
BenchmarkStringUUIDRFC4122/valid (github.com/nobl9/govy/pkg/rules) - ns/op 403.1 ns/op 234.6 ns/op 1.72
BenchmarkStringUUIDv4/valid (github.com/nobl9/govy/pkg/rules) 194.3 ns/op 5.000 validations/op 0 B/op 0 allocs/op 128.4 ns/op 5.000 validations/op 0 B/op 0 allocs/op 1.51
BenchmarkStringUUIDv4/valid (github.com/nobl9/govy/pkg/rules) - ns/op 194.3 ns/op 128.4 ns/op 1.51
BenchmarkStringUUIDv5/valid (github.com/nobl9/govy/pkg/rules) 230 ns/op 5.000 validations/op 0 B/op 0 allocs/op 132.8 ns/op 5.000 validations/op 0 B/op 0 allocs/op 1.73
BenchmarkStringUUIDv5/valid (github.com/nobl9/govy/pkg/rules) - ns/op 230 ns/op 132.8 ns/op 1.73

This comment was automatically generated by workflow using github-action-benchmark.

CC: @nieomylnieja

Traverse included validators, including nested slice and map rules, when removing properties by ID. Add coverage for recursive removal while preserving immutable validator behavior.
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.

2 participants