|
|
|
|
|
by mtlynch
867 days ago
|
|
I'm not a Haskell developer, so it's possible that I misunderstood the original "Parse, Don't Validate" post. >If you are doing the validation inside of a constructor, you are still doing validation instead of parsing. Why that would be considered validation rather than parsing? From the original post: >Consider: what is a parser? Really, a parser is just a function that consumes less-structured input and produces more-structured output. That's the key idea to me. A parser enforces checks on an input and produces an output. And if you define an output type that's distinct from the input type, you allow the type system "preserve" the fact that the data passed a parser at some point in its life. But again, I don't know Haskell, so I'm interested to know if I'm misunderstanding Lexi Lambda's post. |
|
The idea is that your parsed representation and serializer are likely produce a much smaller and more predictable set of values than may pass the validator.
As an example there was a network control plane outage in GCP because the Java frontend validated an IP address then stored it (as a string) in the database. The C++ network control plane then crashed because the IP address actually contained non-ASCII "digits" that Java with its Unicode support accepted.
If instead the address was parsed into 4 or 8 integers and was reserialized before being written to the DB this outage wouldn't have happened. The parsing was still probably more lax than it should have been, but at least the value written to the DB was valid.
In this case it was funny Unicode, but it could be as simple as 1.2.3.04 vs 1.2.3.4. By parsing then re-serializing you are going to produce the more canonical and expected form.