grmpyprogrammer, (edited )
@grmpyprogrammer@phpc.social avatar

I spent this afternoon trying to replace my series of chain-of-responsibility validators with a #laravel pipeline…but think it is wrong to do so as I am not sure how to break out of the pipe on the first validation failure. Ideas on how to do this are appreciated #php

ntzm,
@ntzm@hachyderm.io avatar
grmpyprogrammer,
@grmpyprogrammer@phpc.social avatar

@ntzm Turns out I needed ‘first()’ with a callback

splateric,
@splateric@phpc.social avatar

@grmpyprogrammer oh, alternatively, what about using first? The callback returns the first object that meets a condition. So you could call first on the validator collection, returning true when the validator is not valid. You could then access the messages on the returned validator, or know that all the validators have passed if no result is returned.

splateric,
@splateric@phpc.social avatar

@grmpyprogrammer could you use a collection of validators and call each on them with a callback that returns the result of validation? Each will stop executing if false is returned.

Accessing the validation messages could then be achieved by filtering the collection to retrieve messages.

grmpyprogrammer,
@grmpyprogrammer@phpc.social avatar

@splateric That sounds close to what I am looking for

Crell,
@Crell@phpc.social avatar

@grmpyprogrammer What do the validators look like now? Sounds to me like maybe monad would do exactly what you want. (Or a result monad, if you care about the error messages.)

grmpyprogrammer, (edited )
@grmpyprogrammer@phpc.social avatar

@Crell they are objects that have the same four public methods on them — I want to execute validate() then isValid() and then if isValid() is false, stop the chain and return what validationMessage() returns. Each validator has some common elements for the constructor but often needs more stuff than the basics

grmpyprogrammer,
@grmpyprogrammer@phpc.social avatar

@Crell and then I want to execute them in a chain, one after another and stopping on any failures. My chain-of-responsibility implementation was slightly flawed and was repeating validations

Crell,
@Crell@phpc.social avatar

@grmpyprogrammer Yes, a maybe or result monad would do that. Or, rather, it would still step over the later items but skip over them.

Other option is a psr14 dispatcher, or something modeled on it. The StoppableEventInterface seems close to what you want to do. See the dispatcher in Crell/Tukio for an example.

grmpyprogrammer,
@grmpyprogrammer@phpc.social avatar

@Crell Since this is a Laravel app, I was hoping to lean on stuff like native pipeline + native collections but having never implemented that stuff before I’m blocked

Crell,
@Crell@phpc.social avatar

@grmpyprogrammer I'm no expert there, so can't say. Building something bespoke like this shouldn't be too hard if you want to do that.

Crell,
@Crell@phpc.social avatar

@grmpyprogrammer If you have the structure you describe already, you could wrap each in a function that normalized the calls into an Either.

Not hard to do, but on a phone atm so can't really type out an example.

josh,
@josh@joshbutts.social avatar

@grmpyprogrammer perhaps you could implement a pattern where failure throws? That should bubble up control flow. A very light wrapper around the core pipelines could make it automatic.

grmpyprogrammer,
@grmpyprogrammer@phpc.social avatar

@josh I was gonna ask in the Laravel channel at work tomorrow — I feel like I am missing a few concepts in the Laravel context

kboyd,
@kboyd@phpc.social avatar

@grmpyprogrammer goto

Or, what about exceptions? Although validation failure probably doesn't meet the typical criteria for an "exceptional" situation.

grmpyprogrammer,
@grmpyprogrammer@phpc.social avatar

@kboyd No, that will not work

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • mdbf
  • rosin
  • everett
  • cisconetworking
  • love
  • Youngstown
  • slotface
  • Durango
  • ngwrru68w68
  • kavyap
  • tacticalgear
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • Leos
  • cubers
  • modclub
  • InstantRegret
  • ethstaker
  • osvaldo12
  • GTA5RPClips
  • khanakhh
  • anitta
  • provamag3
  • normalnudes
  • tester
  • megavids
  • JUstTest
  • All magazines