feat: SelfValidator for custom and cross-field validation#25
Open
mlwelles wants to merge 1 commit 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
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.
Adds
SelfValidator, an opt-in seam that lets a type drive its own validation — building on the existingStructValidatorsupport rather than replacing it.What
SelfValidator { ValidateWith(ctx, v StructValidator) error }.Insert/Upsert/UpdateimplementsSelfValidator, the client callsValidateWithinstead of handing it straight to the configuredStructValidator.validateStructroutes each element through a newvalidateOnehelper that detectsSelfValidator(on the value or its address) and otherwise falls back toStructCtx— unchanged behavior for ordinary structs.Why
Struct tags handle field-at-a-time checks, but plenty of validation lives outside what tags can express.
SelfValidatoris the seam for that:End >= Start,password == confirm).ValidateWithreceives the configuredStructValidator, so an implementation can still run ordinary tag-based validation and layer custom logic on top.Tests
TestValidateRoutesToSelfValidator,TestValidateFallsBackToStructCtx,TestValidateSelfValidatorInSlice, andTestSelfValidatorCustomCrossFieldRule(a concrete cross-field example) cover the routing, the unchanged fallback, and the custom-rule utility.