Latest / North Meets South Web Podcast / Handling error state, reviewing code, and request data objects
Transcript
- 0:08Hey, I'm Michael Dyrynda.
- 0:10And I'm Jake Bennett.
- 0:11and welcome to episode 152 of the North Meets South web podcast.
- 0:20You know, I like our long intro music.
- 0:22I just, it was such a good song, such a good intro.
- 0:25I mean, you know?
- 0:26but you listen to all the podcasts now and all of them are just straight into it and,
- 0:31and since that is where everybody else.
- 0:36We're just following the whole, you know, no uniqueness necessary.
- 0:41Just you know, follow the limit lemmings right off the cliff.
- 0:44No, no, I put all of my uniqueness into other things.
- 0:48It's my hair, my hair.
- 0:52Someone said to me the other day, what color is coming next?
- 0:55I said, I don't know.
- 0:56It's just, Eli's got his school sports day coming up next week, I think.
- 1:01So I will, I will go, he's in team blue.
- 1:04So I will, I'll put some blue stuff in my hair for that.
- 1:07There you go, there you go.
- 1:10Nice.
- 1:11Hey, it's like that surfer blonde.
- 1:14It's nearing the end of the summer, you're mourning the loss of summertime.
- 1:18So hey, I get it.
- 1:20I understand, I understand.
- 1:21Hey, today I got, see if you can tell what I'm showing you.
- 1:26got some new AirPods at last.
- 1:28Yes, because the old ones were absolute garbage.
- 1:32And the only way I know they did it.
- 1:37Yeah, and they would the left.
- 1:41Yeah, yeah.
- 1:42then you reset them a few times and they get better and then they crackle again.
- 1:47And then it's like really annoying, but you're like, no, it's not that bad.
- 1:51Like I don't need to, and then re, and I was complaining to re about it.
- 1:54And she's like, just get new ones.
- 1:55I said, no, that's fine.
- 1:56And then she had
- 1:58reason to use them one time and she was like, get new ones now you can't you can't
- 2:03do this now
- 2:04And like honestly, like my right mic, they wouldn't work if the microphone was on the
- 2:08right ear.
- 2:08So I had to switch it to the left ear because the microphone stopped working.
- 2:11And so like if I ever just had the right one in a phone call came and I couldn't
- 2:14pick it up, so that's kind of left was annoying.
- 2:16And then when I would put my finger on my left one to like adjust it, it would
- 2:18whistle like a whee in my ear.
- 2:21Really high pitched.
- 2:22Oh, so annoying.
- 2:23every now and then.
- 2:24And it would just happen at random as well.
- 2:25And you'd be like, ah.
- 2:27So I think, you know, it's, it's just the AirPods, but yeah, the new, the new ones
- 2:31are nice.
- 2:32I, they often fall out of my pocket.
- 2:35And so they just end up somewhere and it's the worst when they fall out of my pocket
- 2:39when I do, you know, bedtime with, with the kids and they're out, they're like
- 2:44left in the bed.
- 2:45And it's like, so now, you know, they've got fine my they're much better with, but
- 2:50like
- 2:51The little microphone at the bottom.
- 2:54Yeah.
- 2:55when you, you know, suspect that you've dropped them in someone's bed because you
- 2:58don't want to wake them up with that chirping, but they are quite handy.
- 3:02Much easier to find now.
- 3:03ones, so let's see if you if you couldn't find them, you can find them.
- 3:05There was no, you know, yeah.
- 3:08And so
- 3:08Orenstein, Adam Wathen school of AirPods, as soon as you have to spend more than 30
- 3:13seconds looking for them, you just go on apple.com and you buy Nuance.
- 3:16That's it.
- 3:18They'll turn up eventually, but you just get another pair.
- 3:21I think Adam's got like five or six fixed pairs.
- 3:23The last time I saw his, he's, he's like, you know, you know, you go to the swingers
- 3:27party and they put the keys in the bowl where he's just got a bowl full of
- 3:30AirPods.
- 3:31Mm-hmm.
- 3:31Yep, just doesn't matter.
- 3:32They're all interchangeable.
- 3:33Just pick up a pair, put them in your ears.
- 3:35That's pretty funny.
- 3:36Yeah, that's pretty funny.
- 3:39I was looking at AirPod Max and I'm like, I don't know.
- 3:43I would consider getting some just for like when I'm sitting down for a long
- 3:45coding session.
- 3:46Yeah, do you use them?
- 3:48Uh, usually only when I go to the office, cause of the, cause like these are good
- 3:53for noise canceling, but they don't like block sound, whereas those are much
- 3:58better, the over eating to just block out.
- 4:01Um, but I don't, when I'm at home, I've just got the app, the home pods here.
- 4:05So I've just got the home pods going on all the time.
- 4:07I don't really, they're good at night when I want, like there are some TV shows that
- 4:12I want to, you know, like we just watch.
- 4:16And it's fine if it's like it's dialogue or there's not much going on, but there
- 4:20are some shows like Halo, for example.
- 4:22I want to watch that with full volume.
- 4:24So I'll put the, the max on.
- 4:28Is it on what?
- 4:29What channel is that?
- 4:30Is it on Netflix or is it on Apple TV?
- 4:31Plot?
- 4:31What is it?
- 4:33Oh, Paramount.
- 4:33Oh, OK.
- 4:34I could get there.
- 4:35one had a really strong pilot and then a lot of like filler between episodes like
- 4:41two and episode seven, and then like two good episodes or whatever it was, three
- 4:46good episodes to close out the season and then kind of ended.
- 4:50And then this season I think is much more in line with like the action aspects of
- 4:57Halo.
- 4:59But I think there's a lot of reticence around the fact that for spoilers, skip
- 5:05forward 30 seconds, um, that he, like that they're not wearing their suits.
- 5:11Like there's a lot of the, the like not act, there's still action parts, but there
- 5:16are a lot of like action parts out of their like master chief suit.
- 5:22So, um, I think there's some complaints about that, but they've, they've spent a
- 5:26lot of time.
- 5:26I look, I, I enjoy it.
- 5:29You know, the episodes are 45, 50 minutes, an hour long.
- 5:32Did you play Halo as well growing up?
- 5:34Like did you know Halo, it's storyline, all that stuff?
- 5:36finished all of the like single player Halo games.
- 5:40I never got into like ODST or the multiplayer stuff, but yeah.
- 5:44to me that'd be like if they came out with like a Half-Life show or something.
- 5:48Because I was like, you know, that was my jam when I was a kid, loved that stuff.
- 5:52But...
- 5:52um, what's going on?
- 5:54Something comes out next month.
- 5:56There's borderlands, which is a movie that trailer came out a couple of weeks ago.
- 6:01And there is fallout.
- 6:02I think HBO has picked up and the fallout, um, is, is out next month, which, which
- 6:09would be interesting to watch.
- 6:10I've, I've never played fallout.
- 6:11I couldn't, it gave me the, gave me the, the headaches and the.
- 6:16And the head spins of there, there are some games that just do it to me where
- 6:19they just, I can't play them because the motion or whatever, they just give me
- 6:23terrible headaches and, and stuff.
- 6:25So that was one of those games, but I'm, I'm into.
- 6:29Watching, you know, TV shows and things like that.
- 6:31So I would definitely give that a, give that a go.
- 6:35I'm more about a movie than TV series.
- 6:38Now, some people will be like, oh yeah, but a TV series can give like so much
- 6:41further in depth and everything.
- 6:42And it's like, yeah, it can, but I just don't have the patience or the commitment.
- 6:45It's like, I want to go and watch two hour movies.
- 6:47So I want to watch Dune Two, for example.
- 6:49Like it was like three hours.
- 6:50It was so good though.
- 6:51So good.
- 6:53Really long, but really good.
- 6:54And I'm glad they didn't make it a TV series.
- 6:56It's just not the same.
- 6:57Like you can't watch a TV series in IMAX.
- 6:59You know what I mean?
- 7:00So it's like.
- 7:01Well, they're doing Harry Potter.
- 7:03They're revisiting Harry Potter.
- 7:04I think HBO or Max or whoever they've picked it up and they're going to do like
- 7:07each book will be a season of the show.
- 7:10So it'd be interesting to see how they kind of do that and pull it off.
- 7:14be fine.
- 7:15You know, Harry Potter is so popular even I mean, just with even with like the young
- 7:19generation now, like my kids love the Harry Potter books.
- 7:22And so that's funny.
- 7:23It just sticks around.
- 7:25It's got a lot of staying power.
- 7:27Hey, help me understand here.
- 7:28What does reticence mean?
- 7:31Like, how about it?
- 7:34Yeah.
- 7:37It's a Redison like his reservation.
- 7:42Yeah.
- 7:42It's like when you kind of.
- 7:44Resort like people were kind of against it because it's like, people want essentially
- 7:50pick up the story and all of the action and whatever, like basically pick up the
- 7:53campaign from the game and put it into a TV series, which is like.
- 7:59Gamers are always like that.
- 8:00They're like, it's not exactly how I want it, then it's no good.
- 8:03And it's like, just enjoy the thing for what it is.
- 8:06Yeah, yeah, yeah.
- 8:06it's like, I read the books, it's not like the books.
- 8:08It's like, I know it's different on purpose.
- 8:10Like it's not supposed to be the book.
- 8:11I learned about this in like eighth grade English, ninth grade English, it's like
- 8:15when they adapt books to different mediums, whether TV or movie, they can't
- 8:22just do the same thing.
- 8:23Like the storytelling has to be different in the visual medium.
- 8:28You don't get, you know, the, um, the ex the exposition and like all of the
- 8:34explanatory stuff, you don't know what the characters are thinking.
- 8:36Like you have to tell the story in a different way.
- 8:39And so, yeah, look, it's like, maybe it's not true to the story.
- 8:45You know, everyone goes on, there's lots of law around it.
- 8:48Um, and, and things like that.
- 8:50When Mortal Kombat, the new Mortal Kombat movie that came out a couple of years ago.
- 8:56Everyone was kind of like this, there's so much law and the story to Mortal Kombat
- 9:02that you didn't have to, like you, you inserted a character into the movie to
- 9:07tell the story of like.
- 9:09what Mortal Kombat is.
- 9:11And I think I see it from that point of view where you're making a movie for a
- 9:16certain demographic.
- 9:18Like you're making a movie for Mortal Kombat fans.
- 9:20But I think they went to the point where they were like, we're trying to bring new
- 9:25people into it.
- 9:26And I think when you do that, you are making a different piece of media.
- 9:33totally.
- 9:34to pick up a new audience.
- 9:35It's like there's, there's 20 or 30 years of Mortal Kombat.
- 9:38People can go back.
- 9:40They don't need the backstory.
- 9:41They don't need to know, like there are elements of it, but don't like create a
- 9:45character to tell the story because you, you don't want to risk people, you know,
- 9:50not understanding something so they can go and check that stuff out after.
- 9:54I think, you know, that's, that's the same with halo.
- 9:57Like just tell a story.
- 10:00There we go.
- 10:01Well, uh, those, uh, those kind of, uh, 10 minutes of something you didn't come to
- 10:06maybe to hear, but maybe you did.
- 10:07I don't know.
- 10:08Sometimes people are like, Oh no, I like the sort of pre show who literally who
- 10:12does.
- 10:13I mean, I remember back when we first started, it was, we had people on all the
- 10:16time from like, you know, we had, um, freak on and we had Marcel and we had
- 10:22Taylor and Adam and you know, all those folks, and now we just get on a chat.
- 10:26I mean, maybe we should line up more guests.
- 10:28I don't know.
- 10:29Maybe.
- 10:31Yeah, so guess what I broke out again today?
- 10:34Actually yesterday.
- 10:36State machines, of course.
- 10:39X state for the win.
- 10:41So there was this, here's, let me tell you the scenario, okay?
- 10:44There was a field and we needed to be able to have a file number entered in the
- 10:48field.
- 10:49And then when they pressed enter or tabbed out, it needed to go fetch three different
- 10:53things.
- 10:54And as long as all three of them returned successfully, you needed to put a green
- 10:57checkbox in.
- 10:59but if it did not, you needed to put a red error symbol.
- 11:03Well, if any of them fail, you can immediately put the error symbol in, but
- 11:07you have to wait until all three of them succeed to put the green checkbox in,
- 11:11right?
- 11:12And if you get one success and an error, you just show up throw error.
- 11:16But if you get a success after an error, you have to make sure it doesn't change
- 11:19back to success because it's not success, it's error, right?
- 11:23And then you can't do anything to change that error state or that success state
- 11:27unless you
- 11:28change the file number, right?
- 11:30So if you had, let's say like one, two, three, and it errored, well, we're not
- 11:35gonna retry one, two, three unless you change it, because it's invalid, right?
- 11:40So it's like, if you change it, if you do on change, then you need to change it back
- 11:45to a ready state so it can go from ready to loading again, right?
- 11:49So this was a complicated issue.
- 11:50And like it became apparent because the developer who was working on it was
- 11:55inspecting like five different fields.
- 11:59all at the same time to determine what state he was in.
- 12:01And it became obvious to me that it wasn't going to work.
- 12:03It wasn't going to be sustainable when I was having two icons overlaid on top of
- 12:08each other at the same time, right?
- 12:10There was like an error icon and a loading icon and a success icon at the same time.
- 12:14It was like, how do you get into that weird state?
- 12:16But it's possible because you have all these weird...
- 12:18Yeah, right.
- 12:19Exactly.
- 12:20And so Schrodinger's icon.
- 12:23Yeah.
- 12:23And so anyway, really interesting.
- 12:26I should share it.
- 12:27But
- 12:28state machines to the rescue again.
- 12:30Exactly, that's literally what it was.
- 12:32And so we had a ready state, a loading state, a success state and an error state.
- 12:36But the thing that was cool about this is in the success state, you had two
- 12:42different, well, you had a event that I was listening for in the, sorry, in the
- 12:47loading state, you had an event that you could listen for, success, okay, or error.
- 12:54So if you had error, you'd immediately transition to the error state, right?
- 12:57But if you had success,
- 12:59you wouldn't do anything immediately.
- 13:00You would just kind of wait, right?
- 13:03So if you had a success state, what you would do is instead of transitioning to
- 13:05another event, you would run an action.
- 13:07And the action was increment success count.
- 13:11So when you're on loading state, you could accept one of two events, error or
- 13:14success.
- 13:15Error take you immediately to the error state.
- 13:17Success increments the success count.
- 13:20But every time you would come to the loading state, again, every time any event
- 13:25that it was listening for would come in,
- 13:26there was this thing called always, and it would evaluate this statement every time.
- 13:30And so what it was, is it was an always, and it was a conditional that said, if
- 13:33you've reached a success count of three, then you should target the success state.
- 13:39And so it would just, you would hit it, you know, I got success on one of them.
- 13:43Okay, great, we'll increment the count.
- 13:45Success on another one.
- 13:45Okay, great, we'll increment the count.
- 13:47Success on the third one.
- 13:47Okay, great, we'll increment the count.
- 13:49Oh, is it three?
- 13:50Yes, it is.
- 13:50Transition to that other state.
- 13:52So that was the magic sort of.
- 13:54stuff there.
- 13:55And then the nice thing is, you know, if you transition from the loading state to
- 13:58the error state, error state doesn't listen for success events anymore.
- 14:01So it doesn't matter.
- 14:02You don't if you got a success event after an error, it's not going to change it back
- 14:05to success because it can't, it's not listening for that anymore.
- 14:08So it was perfect.
- 14:09It was just the perfect use case for when you would need the state machine.
- 14:12And it's so beautiful.
- 14:14Like no, I got rid of literally eight variables.
- 14:17Like it was just so nice.
- 14:19And so yeah, if you're looking
- 14:21that real world example as well, like mindful of the fact that you have this
- 14:25talk coming up to kind of like refresh it a little bit if you wanted, you've got a
- 14:29real example to use rather than the contrived, you know, I mean, the invoice
- 14:34is a good one as any, but having, you know, we're telling the same story, but
- 14:39with a different example from a real world application is, is always nice to have.
- 14:44And it's not always possible, obviously.
- 14:47when, when you're giving a talk to translate like something from your work
- 14:52into the talk, but having the ability to do that and having like this tangible
- 14:58thing that it's like, I've not just come up with this contrived example, because
- 15:01then you have to think about all of the situations in which you might apply that
- 15:05or use that for the purposes of telling or giving the talk.
- 15:11You've got like, no, these are the things that I have proven to be true with a real
- 15:16concrete example.
- 15:17Yep, yep, exactly.
- 15:19Like this is a real world thing that I actually did that I used this for.
- 15:22So anyway, yeah, that was really cool.
- 15:25And it cleaned it up a lot and it felt really nice to be like, ah, validate it
- 15:30again.
- 15:31Like, yes, this is the correct solution for this location.
- 15:33Not always is it, but a lot of times it is.
- 15:35And so that felt good.
- 15:38I was trying to see if there's any of the libraries I could use to do it in a
- 15:40smaller form, like in a much smaller way, but it was just like, nah, I'll just go
- 15:44back to Xstate, Xstate for JavaScript stuff.
- 15:46So.
- 15:47So that's good.
- 15:47So that was sort of a recent thing I've been working on.
- 15:49I mean, other than that, it's basically been just all out sprints, man.
- 15:52We've just been killing it.
- 15:54Like, we've got so many things in flight right now.
- 15:56It's kind of crazy.
- 15:57I was messaging a guy that's like a senior and they come to me, Hey, dude, what would
- 16:01you think about spending a couple of hours a week, like a day a week, just reviewing
- 16:05pull requests for me?
- 16:06Just like I just need another set of eyes from a senior to just be take a look and
- 16:11say, like, oh, no, it doesn't look right.
- 16:13Like, or you didn't think about this or you know what I mean?
- 16:16I don't need them to write code.
- 16:17I just need them to look at code and just point out areas where it's like that
- 16:20doesn't seem like correct.
- 16:21You know, because it's like there's like six, seven, eight, nine, ten, twelve pull
- 16:24requests out there at a time.
- 16:25And it's like I can get to some of them, but I can't get to all of them.
- 16:28And it's like I don't want everybody waiting on me.
- 16:30So.
- 16:31Well, now that, now that we've kind of combined our backend and front end into a
- 16:35single repository, there are like 20 or 30 pull requests on the go at any one time
- 16:41and it's like, okay, I only want to look at the backend one.
- 16:44So I forced everyone to, to tag or label their, their PRs.
- 16:49And so I will only look at the backend ones and, and usually only either if I'm
- 16:54like between tasks or someone specifically asked me to look at something.
- 16:58because my code review, like we all review each other's code, which is good.
- 17:04And now that the team has grown, there's, there's people in at different levels that
- 17:07like, you can write the code, you can review the code, even like the junior
- 17:10reviewing up to a senior's code is good as well, because they can ask, they're going
- 17:14to ask questions, they're not just going to assume that something is correct.
- 17:17Or they're going to see things in a different way.
- 17:21They won't understand things necessarily.
- 17:23Like if you use a collection of pipeline, they might ask, what are you doing here?
- 17:26Or why did you do it this way?
- 17:27Whatever.
- 17:27So it's.
- 17:28It's good from that perspective to kind of get the team writing more approachable
- 17:32code, I think, rather than just like benevolent me reviewing everyone's and it
- 17:37like takes a lot of time as well, but I find that when, when I'm in a position to
- 17:41review code, the outcomes are very different to when, you know, other people
- 17:46are reviewing code and I'll find things that others won't see, or I have a
- 17:50different level.
- 17:51And there's like a lot of it is also the feel like, you know,
- 17:56When, when you're reviewing code, there's the feel of the code.
- 17:59preference.
- 18:00Yeah, it's just like, hey, this doesn't feel right.
- 18:02It's like, well, why?
- 18:03Where's the where's the rule that tells me that's not the case?
- 18:05It's like, well, there's no rule.
- 18:06It's just doesn't feel right.
- 18:07rule, but like when you, when you look at like a constructor and using constructor
- 18:13property promotion, like where do you draw the line between all of those things being
- 18:16on one line?
- 18:17Well, sorry, not constructed property promotion is probably not a good example,
- 18:21but like a method, where do you draw the line between having all of the parameters
- 18:25on one line and when do you start to split it up?
- 18:28So it's one per line, you know?
- 18:30And it's like,
- 18:32And I think like our sort of soft rule is when it gets to like 120 characters, you
- 18:37kind of start splitting it up.
- 18:38Just, just to kind of make it easier to read, especially now that we're kind of
- 18:43embracing types a bit more and we're using more complicated method signatures.
- 18:48When you've got like, this is an array or null, you know, that kind of stuff or a
- 18:53nullable array, and then you've got like, this is an object of type, whatever.
- 18:57And so these things then start taking up more room.
- 19:00And it's not just a matter of looking at like dollar variable equals, you know, now
- 19:05comma or whatever.
- 19:06Now you're looking at like two or three pieces of information potentially for
- 19:11every method argument.
- 19:13So splitting them up onto one line, you know, each of them being on one line, it
- 19:17makes it a bit easier to digest as well.
- 19:20And sometimes you might look at it and you go, I'm, you know, passing too many
- 19:23variables in here.
- 19:24Maybe I've missed something in that.
- 19:27You know, it's, you know, that kind of thing where
- 19:32rewriting the code in a, in a different visual presentation, you know, from one
- 19:37line to five lines makes you sort of stop and think a little bit more about what you
- 19:42have actually written and whether or not it is the most appropriate way of writing
- 19:48that piece of code.
- 19:49You might find, Oh, we need to actually set something as a, as an instance
- 19:53property on the, on the class and then reference that rather than passing it
- 19:56between methods and so on and so forth.
- 19:58So it does help.
- 20:00I think to, to reveal some patterns about your own code and certainly makes it
- 20:05easier to approach refactoring in terms of like, you know, what it ends up looking
- 20:10like.
- 20:11Yeah.
- 20:13Yeah, or things like, how often should you use a form request?
- 20:17If you only have one thing to validate, should you use a form request to do that?
- 20:20Or shouldn't you?
- 20:21Do you know what I mean?
- 20:22Which one should you use?
- 20:23So anyway, some of those things are coding standard things.
- 20:25You literally could probably, you could enforce those things with Pest's
- 20:31architecture plugin and things like that probably.
- 20:33But yeah.
- 20:35on form requests?
- 20:36Cause I know that you're a fan of them.
- 20:39Do you use sparsees Laravel data package at all?
- 20:45I don't.
- 20:46We've talked about it before.
- 20:47You and I have actually, and we've been talking about it a little bit inside of
- 20:52our other chat as far as like where should like the line be drawn for when a form
- 20:57request is used or if you should say like, yeah, we only use form requests up until
- 21:03this point.
- 21:04So we say, you know, once you're out of the controller, it should no longer be a
- 21:08form request.
- 21:08It should now be something your domain owns, it should be a DTO, it should be
- 21:11something like that.
- 21:12Yeah.
- 21:14We do have places in our code where the request is literally drilled three layers
- 21:18deep, right?
- 21:19That's not great.
- 21:22So I could see using Spasi's package.
- 21:24It certainly makes it a lot easier, right?
- 21:27But I'm sure there's some, using any tool for long enough, you'll find the spots in
- 21:32which it fails, right?
- 21:33Where it's insufficient and you have to do something else.
- 21:36So I can't speak to that, though, because we don't use it.
- 21:39So I know you guys do, though.
- 21:41How's it going with it?
- 21:42Yeah.
- 21:42All right.
- 21:43So I will pose you then a, it's not a hypothetical, it's hypothetical for you,
- 21:48but it's the reality for me.
- 21:49We have a form request, right?
- 21:51It does, it has all of its rules.
- 21:53It does the validation.
- 21:55We then take that and we pass it into the Laravel data object.
- 22:00Now the Laravel data objects have these magic creation methods.
- 22:05So you go like person name, colon, colon from.
- 22:11And then you pass it the request.
- 22:15And what it will do is it will look for either a from request method on the, um,
- 22:26on, on that DT, that data object and resolve it using that.
- 22:30And then you can do all kinds of creative things like from model or from user model.
- 22:35So you could
- 22:36You could specifically do this and then Lara the package itself uses reflection
- 22:40and some magic to kind of figure out like of these magic creation methods.
- 22:44So they all start with from they're all public, they're all static, which one best
- 22:49maps to this thing that I have been passed and it kind of instantiates the object now
- 22:55from model, from array, from string, these are all fine.
- 22:58What I find, and I want to hear your take on this is that if it's building the
- 23:05from, if it's building the data object from a request, it does its own validation
- 23:14to make sure that all of the input in the request.
- 23:18So the stuff you take from like request, input request, Jason, or whatever maps to
- 23:22a property on the data object before it calls the from request method.
- 23:30Right.
- 23:30So in the example of like person data, you might have
- 23:34your data object as name, email, phone number, for simplicity sake.
- 23:41But in the request, we're capturing first name, last name, email, phone number.
- 23:49So because, and because in the specific instance of a request, what it does is it
- 23:55passes it through this like pipeline of, um, stuff.
- 24:01where it normalizes the request, it does all this other kind of stuff.
- 24:03But the thing that has tripped me up is that we'll also do a level of validation,
- 24:10that there is a property on your data object that matches your request.
- 24:16So keeping in mind that I have a request that has first name, last name, phone, and
- 24:20email, and a data object that has name, phone, and email, in my from request
- 24:26method, I would then take first name, last name,
- 24:29concatenate it into name and then instantiate the object.
- 24:34But because the request does not match the DTO exactly, it hits the validation error
- 24:41in the Laravel data package that says this field is required, name, name is required,
- 24:47but it's not set.
- 24:48So then you've got to like do these gymnastics around using this other prepare
- 24:52for pipeline method, which then allows you to kind of massage the request into a
- 24:57format that matches.
- 24:59the DTO, but it just, it kind of muddies things up a little bit, I think.
- 25:05Um, and so, and so now we've got like two places doing validation where I would
- 25:11expect that all of that request validation happens in the request object and that you
- 25:17don't try and magic it up for me in the data object itself.
- 25:22Yeah, well, because it's also not even like the rules that you have specified in
- 25:24your form requests, right?
- 25:26It's different.
- 25:27It's like the it's magic rules that aren't even really defined anywhere except for
- 25:30you know that the package is going to do it.
- 25:33was it's based on the definition of the, so you've, so it's based on what's in the
- 25:40constructor, right?
- 25:40Public name, public string name.
- 25:43Yeah.
- 25:43Public string name, public string phone number, public string email.
- 25:46Like those are the three things.
- 25:47So it would look to see if there is a request.
- 25:51I guess the thing that would be nice is if it did something like if it had like a use
- 25:55validates before request or something like that, if there was like a trait or
- 25:59something that you could turn on or off, because then that would make a lot of
- 26:02sense.
- 26:03But to say like that from request method is just automatically sort of getting this
- 26:09decorator around it that's going to do all the validation stuff like that feels a
- 26:12little bit weird.
- 26:12It feels like because now what you have to do is you almost have to like
- 26:15create your own static method on that thing called from our requests, you know
- 26:19what I mean?
- 26:20And then pass the form request that way and then handle it by instantiating it
- 26:23yourself.
- 26:24yeah.
- 26:24I mean, the problem is, is that it specifically checks if the input to the
- 26:27from method is an instance of request.
- 26:32And if it is, it will shove it through this pipeline, right?
- 26:35So you don't even get access to your own magic method until it's run through that
- 26:39process.
- 26:41And so, you know, there are two options, right?
- 26:44It's either conform your, your request exactly to the data object.
- 26:50Or.
- 26:51actually there's three options.
- 26:52So the option one is to conform your request.
- 26:55The second option is to call from request directly, which then skips the magic
- 27:01creation.
- 27:02And the third one is to use, which is what we've done, is to use like the without
- 27:07validation attribute on those properties to specifically say, don't try and be
- 27:13smart about this.
- 27:14I know how I'm gonna massage this in the from request method.
- 27:18Like I don't...
- 27:19And I haven't gone into like the history, the commit history of the, of the package
- 27:23or, or to try and figure out like why this was implemented, like what the thought
- 27:26process was behind it, but, but for me, um, it, it feels a bit counter, like it's,
- 27:34it's not a PHP agnostic package either.
- 27:36It's specifically for Laravel.
- 27:39So it's like, I'm giving you a form request.
- 27:42You know that it's a form request, which I have already validated.
- 27:46I want to use the from request method, which I will do the mapping from what's in
- 27:50the request to what's in the DTO, but you are preventing that from happening.
- 27:56By, you know, doing this level of validation.
- 28:00So I just wanted like a second, third, fifth set of eyes at this point is to be
- 28:05like, is this weird, like, should I get onto Frank and be like, excuse me.
- 28:12because I think what they've done is they've decided to take the road of saying
- 28:18that we are going to define the conventions and now you can you can adhere
- 28:22to the conventions or you can do the long way and opt out by yourself.
- 28:26Right.
- 28:26And so I think that's probably the route that they've decided to go and that's very
- 28:29spousy of them right like now they so they give you an escape hatch.
- 28:33But they're basically saying if you want to use this package, this is the way that
- 28:36we use it.
- 28:37And we've decided to just
- 28:38follow strictly our requests, which maps straight into our DTOs, which maps
- 28:42straight into our models, which maps straight into our, you know what I mean?
- 28:44Or maybe something like that.
- 28:46So if it was me doing it, yeah.
- 28:53I mean, I think, let me think about how I would do it.
- 28:57If I was doing it myself, what I might do is I might call the from request method
- 29:02directly in my controller.
- 29:04Yeah.
- 29:05And then just let my from request method handle that.
- 29:06And then using from request would just do, you know, it's skip the validation stuff.
- 29:11And I could still use from anywhere else knowing it's, you know, if I'm using that
- 29:15request method, it's not if I'm using that request object, it's not going to work.
- 29:18But that's okay.
- 29:18I would just use from request in those instances.
- 29:20And I guess document well that that's why it is that way.
- 29:26Yeah.
- 29:28I suppose if you look at it from the perspective of the Laravel data object
- 29:34that is created from a form request is being done with the specific intent to
- 29:41give a type, to give typed, you know, in, uh, inferred access to the properties.
- 29:48Then yes, it makes sense that, you know, this is, this is to replace basically
- 29:53that, that request object wholesale.
- 29:57And so whatever you map in the DTO needs to match exactly what is in their request.
- 30:03And it's to stop you from passing the request around and using like request
- 30:06import all the way through.
- 30:08Cause you know, we know that that's error prone that you could have a typo in the
- 30:12string, you request input, request import, you know, name, and you forget the E and
- 30:18it's just nam and then you end up with null can be trickier to diagnose than to
- 30:23do request nam on.
- 30:25you know, the typed object, which will tell you, no, NAM doesn't exist here.
- 30:29So I guess if that's the intent, it's just that, you know, you're not always in
- 30:34control of the data coming into that request, I suppose, but you're always in
- 30:38control of the, of the data object.
- 30:40So maybe, maybe it's a PR that I send to the documentation to say that like, this
- 30:47is the specific intent, just to clarify that.
- 30:49And like, I'm happy, I'm happy to have the conversation.
- 30:52I think with, I think Ruben.
- 30:54is responsible for the package to say like, why is it like this?
- 30:58And if the, if my understanding based on what we've just said is, is correct, then
- 31:03can I send a PR to the docs to say like, this is the intent of using the form
- 31:09request, using the from request functionality, and then, and then it's
- 31:13like clear and then everyone understands.
- 31:15Cause like it tripped me up for ages and I was trying to find a way around it.
- 31:18And I was, you know, source diving through the package and it's, it's created in such
- 31:23a way that it's.
- 31:24Like it makes sense, but it's, you know, you've got to jump through a few classes
- 31:27to find out where this behavior is actually happening and then, and going,
- 31:30oh, okay, I understand why this is happening now, but I don't understand why
- 31:36it's happening kind of thing.
- 31:37So.
- 31:38Yeah, that makes sense.
- 31:39I thought when you originally saying it, you were saying that it would basically
- 31:42run the validation step again on your form request.
- 31:44I was like, that seems odd.
- 31:46Like, it seems like, I got it.
- 31:48Yeah.
- 31:49like, you know, it's using reflection to find out, you know, what are all the
- 31:52properties?
- 31:52What are their types?
- 31:53Does this thing exist?
- 31:54Is it of the correct type?
- 31:55And I, and like, because I haven't actually got it working in the way that I
- 31:59guess the package intends it to work.
- 32:01You know, I assume that if you type it as a string, it's going to use request input.
- 32:05But if you, if you type it as an integer, it'll use request integer or request
- 32:09Boolean to kind of make sure that all of those types match up for you.
- 32:12Here's one other option for you.
- 32:14Are you ready?
- 32:15The other option you could do is you could, in your form request object, you
- 32:21could append a full name attribute to the request in the form request object.
- 32:27So you could say like after validation, and then that's going to make sure that
- 32:32everything's valid.
- 32:33And then you could say, I think it's, I don't remember if it's like this request
- 32:36or this attribute push or something like that anyway.
- 32:38you can merge or whatever in there.
- 32:40Yeah.
- 32:40you can set the value, basically.
- 32:42And so then you could just do it in there.
- 32:44And then any time you get the form request, as long as it's valid, it's going
- 32:47to push that value in there.
- 32:48And then when you pass it through, it's just going to set it.
- 32:50No big deal.
- 32:51That way you don't have to do any of that other stuff.
- 32:53It would just work.
- 32:55And that would work every time.
- 32:56I mean, as long as you validated it, I suppose, but what you're going to do, I
- 33:00mean, it's going to happen automatically when you use form requests in the
- 33:03controller.
- 33:03So it's just where do you want to put the logic, you know?
- 33:07Yeah.
- 33:08I think now having this conversation, having said it out loud, I've like talked
- 33:11myself into believing that it is the correct approach with the assumption that
- 33:15like, that is exactly what the intent is that, you know, that, that the re that the
- 33:20data object should in fact map the request one to one.
- 33:25And so yes, you are defining the things in two places, but it does mean that you get
- 33:30a typed object as you go.
- 33:33you know, through the application depths.
- 33:36Yep.
- 33:37Exactly.
- 33:38hacked around it for now and we're just leaving it as at that, but, um, it'll be
- 33:41something to keep in mind for future because like none of the other people on
- 33:44my team that had been working with it had encountered this problem.
- 33:47And I'm like, have you not encountered, like, are you calling the from request
- 33:52method directly?
- 33:53Are you or, but I, but if I, I think I figured in the end that they were in fact,
- 33:58yeah, doing the mapping of the request, the form requests one to one with the, the
- 34:04data object, and that makes sense that they never hit the issue then.
- 34:07Yeah, exactly, exactly.
- 34:09Yeah, and it is really nice just to have that typed object you can pass around and
- 34:13new up in tests and all that stuff without having to have a form request and then
- 34:16pass it.
- 34:16Yeah, yeah, really nice.
- 34:19Well, hey dude, we're at our time.
- 34:22Yes, yes, indeed, we are at our time.
- 34:24Hey, thanks folks for hanging out with us, 152.
- 34:27I was gonna say, not Laravel News.
- 34:29I don't know if you saw that audio slash 152 for show notes.
- 34:32Hit us up on.
- 34:34twitter at Michael Dornett, Jacob Bennett or at North South Audio.
- 34:37Rate us up on your podcast, catch our choice, 5 stars would be amazing.
- 34:40Until next time folks, we'll see ya.
- 34:43Peace.
- 34:43Bye.