Live data from Hacker News

Parse, Don't Validate (2019)

lexi-lambda.github.io

41–50 of 288 posts

Re: Parse, Don't Validate (2019)

#41
post #8

That's how the validation tool for Python Maat works. By creating a completely new dictionary. https://github.com/Attumm/Maat

For js / typescript I like: https://github.com/paperhive/fefe basically ist’s just functions that take a value of one type and return a other one

There is also joi, zod, myzod just to name a few.

I personally use myzod as its fast it parsing, zero dependancies and you can infre types from your schemas.

Re: Parse, Don't Validate (2019)

#42
post #10

Earlier quoted context omitted.

"A kind of" is precisely its formal use from the PoV of a type theorist. Two things are the same type of thing if they share all of their extensional properties. That is what it means for two things to be identical/equal.

But what I am saying is that parsing is a kind of validation. But all validation is not parsing. For example let's say that I have written an HTTP API that accepts application/x-www-form-urlencoded data to one of its endpoints. Let's say `POST /users`, and this is where the client-side application posts data to. Now I can implement this in many ways. I can for example define pub struct Person { name: String, phone_nu…

You are over-complicating this into obscurity.

General case: Validating random data as input into some program.

Particular case: Validating random source code (data) as input into some compiler (program).

Do compilers parse or validate?

"parsing is validation, but validation is not parsing" if that were true then you should be able to give an example of a compiler doing some sort of validation on the random source code (data) that is not parsing.

The very thing which determines the validity of random source code is the compiler's ability to parse it.

Re: Parse, Don't Validate (2019)

#43
post #36
post #8

That's how the validation tool for Python Maat works. By creating a completely new dictionary. https://github.com/Attumm/Maat

How does Maat compare with pydantic? https://github.com/samuelcolvin/pydantic

If only for conciseness, readability and speed, I'd take Pydantic over any day. Being able to express 80% of type checking using Python native type hints + dataclasses is just so intuitive!

And it's getting some wide adoption, for instance FastAPI which uses it for request validations.

Re: Parse, Don't Validate (2019)

#44

When I think of validation I think of receiving a data file and checking that all rows and columns are correct and generating a report about all the problems. Does my thing have a different name? Where can I read up on how to do that best?

You can use the now widely adopted Great Expections[0] library, which fits exactly this use-case for data validation!

[0] https://greatexpectations.io

Re: Parse, Don't Validate (2019)

#45

This is a great post. I come back to it frequently. There's beautiful clarity in the articulation, and the essence is easy to grasp yet powerful. It reminds me a bit of Scott Wlaschin's Railway Oriented Programming (ROP) [0]. As a technique, ROP nicely complements "parse don't validate". As an explanation, it's similarly simple yet wonderfully effective. I've a real admiration for people who can explain and present t…

I agree. It's a very well-written post. I am not a Haskell person, but it was quite clear to me.

I feel that we don't put enough value, these days, on the ability to write clear, articulate exposition. Also, I believe that many people are not willing to read articles, books, or papers, of any meaningful length.

Everything needs to be boiled down to <10 min. read time, or <18 min. TED talks.

Re: Parse, Don't Validate (2019)

#46

When I think of validation I think of receiving a data file and checking that all rows and columns are correct and generating a report about all the problems. Does my thing have a different name? Where can I read up on how to do that best?

I thought of input validation for web forms. Similar thing I guess. In Haskell you can create a type that you know is a validated email address but you still need a validation function from String -> Maybe Email to actually validate it at runtime

That's just a parser though. Like described in the post, parsers sometimes can fail but importantly they always pass along the result if they succeed. Validation functions on the other hand only validate that said data is valid.

The argument is that if you need to interact with or operate on some data you shouldn't be designing functions to validate the data but rather to render it into a useful output with well defined behaviour.

Re: Parse, Don't Validate (2019)

#48
From the Twitter link:

> IME, people in dynamic languages almost never program this way, though—they prefer to use validation and some form of shotgun parsing. My guess as to why? Writing that kind of code in dynamically-typed languages is often a lot more boilerplate than it is in statically-typed ones!

I feel that once you've got experience working in (usually functional) programming languages with strong static type checking, flakey dynamic code that relies on runtime checks and just being careful to avoid runtime errors makes your skin crawl, and you'll intuitively gravitate towards designs that takes advantage of strong static type checks.

When all you know is dynamic languages, the design guidance you get from strong static type checking is lost so there's more bad design paths you can go down. Patching up flakey code with ad-hoc runtime checks and debugging runtime errors becomes the norm because you just don't know any better and the type system isn't going to teach you.

More general advice would be "prefer strong static type checking over runtime checks" as it makes a lot of design and robustness problems go away.

Even if you can't use e.g. Haskell or OCaml in your daily work, a few weeks or just of few days of trying to learn them will open your eyes and make you a better coder elsewhere. Map/filter/reduce, immutable data structures, non-nullable types etc. have been in other languages for over 30 years before these ideas became more mainstream best practices for example (I'm still waiting for pattern matching + algebraic data types).

It's weird how long it's taking for people to rediscover why strong static types were a good idea.

Re: Parse, Don't Validate (2019)

#49
post #36
post #8

That's how the validation tool for Python Maat works. By creating a completely new dictionary. https://github.com/Attumm/Maat

How does Maat compare with pydantic? https://github.com/samuelcolvin/pydantic

Pydantic is using classes and typehinting. The new dataclasses style. Currently Maat doens't have a parser for dataclasses, it could come in the future. Pydantic works great with typehinting.

Maat was created before dataclasses existed. For validation Maat offers the same. But it also allows for some really neat features such as validation on encrypted data. https://github.com/Attumm/Maat/blob/main/tests/test_validati...

Since validation is written as dictionaries its possible to store the validations in caching db such as Redis.

And since its simple its easy to extend for anyone use case. And there are no other dependencies.

Benchmarks of pydantic has Maat around twice as Pydantic.

Re: Parse, Don't Validate (2019)

#50

When I think of validation I think of receiving a data file and checking that all rows and columns are correct and generating a report about all the problems. Does my thing have a different name? Where can I read up on how to do that best?

I thought of input validation for web forms. Similar thing I guess. In Haskell you can create a type that you know is a validated email address but you still need a validation function from String -> Maybe Email to actually validate it at runtime

I think for the usecase GP gives it'd be even better to have a function `String -> Either (LineNumber,String,[Problem]) Email`, so that you can report back which of the lines had problems and what kind of problems. For web form validation you can skip the line number but it'd still be useful to keep the list of problems, so that you can report back to the user what about their input did not conform to expectations.
Post reply on HN