 It's finally ready clearly. I had put out a video what? little over a week ago, I think detailing Why this got hung up and as it turns out that was wrong as well because Yeah, I I don't know I went to start recording this and It it This ain't problem. I don't know how it got fixed before and then came back, but I wound up eventually tracing it down to Let's just say complications with FAA D So I'm gonna get this out of the way just because I need to say this because this approach is Literally only being used here for this File system as a database or FAA D is a niche object persistence approach You overwhelmingly want to use some type of database system whether a relational database manager or an object oriented database manager or a unstructured database manager like key value stores or The so-called no sequel databases Your document databases This is a niche thing and literally only being used here to reduce the dependencies on the project This is to demonstrate How to use stringier for basic input validation and is not to be taken as a guideline for how to properly persist data Now that that's out of the way, let me explain what we've got here This is going to be a simple crud Interface to an employee registry that is create read update and delete or as I've named them here And not in that same order necessarily Add list remove an update The program itself is just a loop over this It keeps asking for these and you'll see how that works This is non-standard that these functions are not in the CLR despite especially here looking like they are these come from This if you're interested in what that project is I'll have a link down in the video description But that's not really that important. Just know that that this provides the main menu that you'll see and Actually, let me show that off real quick. So you know what I'm talking about You have this here If we go to list nothing's been added yet. I didn't start this off with anything. So there's just nothing there And there's nothing to do at this point I'm just gonna explain a little bit more but you have an idea of what that is You know have the implementation of these methods of these and And This is going to be working just with the employee object. It's Persisting and modifying them Inside of here is mostly just the stuff for describing the employee and doing the object persistence in a more serious scenario this You might not have these things here It would depend on how your object persistence framework works My understanding is those are normally in a different class But back to this Let's actually add an employee so I'm gonna do myself and We'll say I'm a programmer Sex is bail and my birthday now we can go and list and Just to show you that this persistence is working I close that we'll start it back up We can list again and you can see that record is still there updating and we'll get to the problem and why you want to do validation in just a moment, but We'll do sex is and I don't know I got a sex change to attack helicopter This is so cramped that you can't really see but it's And I can tell you just from experience. This is an argument exception because you're trying to assign a Value that just doesn't make sense. It's not part of the enumeration to type To a field of that enumeration type and then just it doesn't make any sense in a more serious like You actually have this running in a business Especially given how remote is being More of a thing. This is increasingly important But normally what would happen is you send this data from your client machine off to wherever the database server is It winds up Dealing with that exception sends back to you that I couldn't do that for Whatever reason depending on the system you've got you'll get a different error message But you wind up with that entire round trip That takes up network time that takes up network bandwidth Which in certain businesses especially or certain regions of the world can be a premium and Ideally, you'd like to catch as many of these errors as possible on the client Before ever sending them off to the server that is if you know the data is definitely bad Don't send it off. That's where input validation comes in and To be fair, it's not like stringier introduces this and it hasn't happened before The typical way you would accomplish this is with regex This is just an example of how to do the same kind of thing with stringier if you're interested in that for performance reasons Or you already have the patterns defined or whatever So we're going to want to We'll deal with the sex first. This also happens with the birth date The name and the title are just single strings The name is not split up if you know much about globalization You know that you do not want to split that up because global names are complicated Leave that as a single string, but this does happen with the sex and The and the birth date. So what we're going to want to do is add a pattern for sex and We're gonna need to put that in a few places. So let's just do that here This can be static Sex pattern and this can just be male or Female Right makes sense If you are familiar at all with parser combinators, you'll know that most parser Combinator libraries require you to actually like wrap these individual strings up because so literal is not a function Stringer is purely declarative. So you don't have to mess around with that, which is really convenient This is oh you Let me run this as is and I can show you a potential pitfall Actually, that won't show you the pitfall at all is when the end. Yeah, so Ideally you want these to be case insensitive because we're not talking about parsing like a programming language They like C sharp which this is written in Normal language isn't case sensitive. So you will want to Assign these a specific comparison type and you can do that with with case insensitive And then again for this one and now both of these comparisons will be done without regards to case Useful I am looking into a syntactic sugar for that by the way Some way of declaring that there's a set of possible options with one common comparison type because I Understand this can be a little bit tedious to write out Currently that's how it would be done, but these are meant to be composed in most cases So you'd still only declare one time for each Now we need to add this validation Let's do it for the update first and we'll just keep going incrementally so the update sex We get that string and then we do the enum parse to bring it into the actual enumeration type So you're gonna want to do the validation in between there. So we'll have If and then we called that sex pattern Can consume That yeah, that's fine So if that works Then obviously you can do the enum parse. This would basically be something that's happening on the actual server the remote server So this would be like your Actual sequel statement that goes off to the server to say update this record within this table This would normally be a very expensive operation obviously for the specific example It's not but normally this would be where you're send off and then if that doesn't work If that fails to validate I would say you would want to prompt again So what we'll do is console right line Write out the error So we'll get the error message and then we want to prompt again So easiest way to do this with out Possibly causing a stack issue later on would just be a go to now We run this that record is still there. So we're going to Give me a sex change to attack Hello Okay So now that error was caught on the client side and prompted for a correct value again Knowing that if you were to send it off to the server that it would Guaranteed not work and it would have to come back wasting a whole round trip Wasting time using up network bandwidth. We can say hey I'm a female now. I guess and we can list The employees and we've got female Okay, now let's change that back. Oh, I just removed it instead. Oops either way though You can see that if we do a new one We're gonna say programmer again, but we're gonna say attack off the door here as well and You get it's cramped again, but you get the exact same issue and that's because inside of this one we have we get the The string for their input for the sex and then Here where we're constructing the new employee object That's where we have the enum parse that fails. So immediately after this is where we should be doing the validation and conveniently since these are Already defined we can just more or less copy this entire approach Let's say sex prompt Sex uh, we don't need to do anything if This passes Because we're gonna do something with all of it if it passes at the very end So instead this is going to be if it fails to consume fails to parse we're Might as well just copy that error message actually Along with the go-to and we can prompt for that again you will want to do the same thing with the birth date and you may notice that I've been putting the birth date in an ISO format Despite getting it out here in my local format Quit out of this and I'll show you that when we go to list employees You can see the birth date doesn't have anything about the culture specified so it's always going to work on the local culture and When it comes to this I believe This will actually work with any birth date string that it recognizes Let me try this real quick update employee Change my birth date and we're gonna use the local format which is Okay, so it works either way Let's say for whatever reason like a database limitation you have to Use ISO birth date format This is sort of a good idea anyways just because you wind up with some potential confusion Especially if you're like an international company depending on what the Certain dates get really confusing like let's say my birth date was a week earlier on April 6th of 1990 6 for 1990 is also a valid date and depending on what part of the world you're in is The normal format for that date most of you especially with your European You know the kind of problems I'm talking about so let's Let's just add in some validation that requires it to be an ISO date This also helps deal with the round-trip problem in case it's not a valid date format So we're going to do Birth date pattern And In this instance, let's make sure yes, I am okay so birth date You have one or two digits for the Well, no because ISO is the year first. So you're going to have four digits there This is then going to be followed by and a posture or not a hyphen Because that's what the ISO dates If you're following the standard very strictly, that's what the ISO dates work use and Then you have So you're gonna have one or two digits I Do have as a feature Request that I need to implement Two versions from the current The ability to use ranges here so that this would be a little bit easier to write in The future you'll be able to just do one to two Inside the repeat and this will work but for now What you're gonna have to do is then and a decimal digit number and then Maybe a decimal digit number then we're going to have another hyphen and This whole thing again Obviously in this instance a little bit easier to write with regex. I'll give back to that After and show you a neat and useful trick that stringier supports for situations like this where regex is Really actually easier to write In most cases I find that this typically doesn't show up where there's not a huge difference between the overall syntax but obviously with something like decimal digit number versus the backslash D that regex uses One of these is obviously a bit easier to write And I've taken a much more collaborative approach when it comes to resolving these kinds of things So you can inject Regex as part of a pattern We want the update one, so let's not forget that we've got update birth date and date Would help if I could spell Date and we're going to do the same pattern for the validation up here Where you can have that and then if not it's not able to parse We'll write the error and prompt for the birth date So I can go ahead and remove My record I'll show off adding it and we'll do the date like before Okay, so it's saying that it expects an ISO date format, which my birth date in ISO would be that Which failed? Oh and that binding got broken. So let's Why? Did times I'm an idiot add the record I Programmer Mail and we use the local date format Which of course doesn't work we use the ISO date format Which does work and let's make sure that it got through correctly, which it did Okay now I was gonna show how you can actually use regex as part of this if You wind up in a situation like this where it's something I haven't gotten around to optimizing the syntax Wonderfully it's not quite there yet with this one. So Construct your regex like normal We need to include the appropriate Name space however Construct your regex like normal Just make sure that it is anchored to the very beginning and that's because how the adapter works The reason why the parser call is called consume with stringier is because there's an actual source object and The calls to the parser always consume characters within that so it's always advancing your position Often called consuming the source Because it always starts at the current position In order for regex to work with this approach you always has to be anchored at the start It is validated. If you try to use the adapter without the anchor It will throw an exception when the object is initialized stating exactly what I just described Just Explaining now that that is a thing you'll need to be aware of We can go and I'll use this just because No from experience. This is slightly faster and we know that you're only going to have Arabic numerals This is always going to be four and then we have a hyphen we've got zero through nine and we've got one or two and Then we can end that off that will pass it through the adapter doing all the validation and Nothing needs to change otherwise We can run this and an employee Hopefully I remove the last one because otherwise we will get an exception But I'm pretty sure I remembered to do that We'll do a local date format which should not pass and of course it doesn't ISO date format Which works just fine and good that's more or less it for this one It's fairly basic, but input validation is a basic task. There's not a lot of complexities behind it If you have any ideas for more stuff I can apply this to Definitely Suggest that down in the comments and I will get around to creating that video I will have a little link to the repo where these sources are located if you're interested in playing around with this such as Adding new fields to the employee and then testing out validation on those new fields. You will be able to do that That's part of the reason why I made sure that this doesn't use a database is so that it's easy to do that so that you don't have a Dependency on having a specific library already installed to you that you can access that database. This just works on everything everything What else to say for this one, so I'm just gonna wrap it up. Have a good one guys