feat: SelfValidator for custom and cross-field validation#25
Open
mlwelles wants to merge 2 commits into
Open
Conversation
Adds SelfValidator, an opt-in seam that lets a type drive its own validation. When a value passed to Insert, Upsert, or Update implements SelfValidator, the client calls ValidateWith instead of handing the value straight to the configured StructValidator. This covers validation that struct tags cannot express on their own: cross-field rules (one field constrained by another), conditional rules, checks on computed or setter-derived values, and broader business rules. ValidateWith receives the configured StructValidator, so an implementation can still run ordinary tag-based checks and layer custom logic on top. validateStruct routes each element through a new validateOne helper that detects SelfValidator (on the value or its address) and otherwise falls back to StructCtx exactly as before — behavior is unchanged for ordinary structs.
501b3ef to
504231c
Compare
…d example - TestValidateSelfValidatorInSlice now asserts the configured StructValidator is never called for a SelfValidator slice element, matching the scalar test; a regression invoking both paths would otherwise pass silently. - Add a runnable ExampleSelfValidator showing a cross-field rule that layers on the configured StructValidator.
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.
What this adds
SelfValidator, an opt-in seam that lets a type drive its own validation —building on the existing
StructValidatorsupport rather than replacing it.When a value passed to
Insert/Upsert/UpdateimplementsSelfValidator,the client calls
ValidateWithinstead of handing it straight to the configuredStructValidator. Plain structs are unaffected — they still flow throughStructCtxexactly as before.The problem it solves
Struct tags validate one field at a time. Plenty of real validation lives
outside what a tag can express:
End >= Start,password == confirm).SelfValidatoris the seam for those.ValidateWithreceives the configuredStructValidator, so an implementation can run ordinary tag-based validationfirst and layer custom logic on top:
validateStructroutes each element through avalidateOnehelper that detectsSelfValidator(on the value or its address) and otherwise falls back toStructCtx— unchanged behavior for ordinary structs.Tests
TestValidateRoutesToSelfValidator/TestValidateFallsBackToStructCtx—routing and the unchanged fallback.
TestValidateSelfValidatorInSlice— slice elements route correctly, and theconfigured
StructValidatoris not also invoked (strengthened this roundto assert the no-double-call, matching the scalar test).
TestSelfValidatorCustomCrossFieldRule— a concrete cross-field example.The automated review (cubic) found no issues in the implementation.
Documentation
A runnable
ExampleSelfValidatorshowing a cross-field rule that layers on theconfigured
StructValidator.