> password validation (the annoying part that asks for capital letter, digit, special character? -- I want to see the author implement a parser to parse possible inputs to password field, while also giving helpful error messages s.a. "you forgot to use a digit").
No. it's not the approach OP recommends. And that's why it's called validation. I have no idea why would you question that. OP wants to capture constraints on data as ML-style types. But, ML-style types have very limited expressive power, and, when it comes to real-life situation are practically useless outside of the most trivial cases.
I did not question [that they were different approaches], I explained, through example and counter-example, why they were the same approach. I will try again.
Alexis wrote both 'validate' and 'parse' examples in ML-style types:
The difference lies entirely in the return type: validateNonEmpty always returns (), the type that contains no information, but parseNonEmpty returns NonEmpty a, a refinement of the input type that preserves the knowledge gained in the type system. Both of these functions check the same thing, but parseNonEmpty gives the caller access to the information it learned, while validateNonEmpty just throws it away.
I chose OO-style types for my samples, because there's a large fraction of HN users who dismiss ML-ish stuff as academic, or "practically useless outside of the most trivial cases".
// OO-typed 'validate' (my straw man)
class User {
// returns void aka '()' aka "the type that contains no information"
void validateUser() throws InvalidUserEx {...}
}
/* OO-typed 'parse' (as per my baeldung link)
* "gives the caller access to the information it learned"
* In this case it gives back MORE than just the User,
* it also gives back 'why it went wrong', per your request above for password validation
* (In contrast with parseNonEmpty which just throws an exception.)
*/
class UserValidator {
Validation<Seq<String>, User> validateUser(...) {...}
}
> But, ML-style types have very limited expressive power
Hindley-Milner types are a godddamned crown-jewel of computer science.
This is what Applicative Functors were born to do. Here's a good article on it: https://www.baeldung.com/vavr-validation-api
Check the types:
Even though it's called "validation", it's still the approach the OP recommends.It reads as "If you have a Seq of Strings, you might be able to construct a User, or get back validation errors instead".
Contrast this with the wrong way of doing things: