3 ms·
> How many JSON validators have you seen in python and javascript? Why can't the static type checkers like mypy and pyright do this for us? Because JSON is ext
by dragonwriter 6y ago
> How many JSON validators have you seen in python and javascript? Why can't the static type checkers like mypy and pyright do this for us?
Because JSON is external data, mypy and pyright operate on Python code.
> They can tell you if you're assigning a string to an int, but not if you're assigning 200 to an int whose type allows only numbers in [10, 100].
Mypy can, in fact, do that (via a union of literal types), though the UX is horrible for that specific case and it only handles simple literals, but that's actually enough for JSON semantics. Of course, to validate JSON with it, you'd have to turn the JSON into Python code with type annotations and validate that.
- adsharma 6y agoLike you're suggesting, external data can be seen as a variant of literals embedded in code. https://www.python.org/dev/peps/pep-0589/ https://www.python.org/dev/peps/pep-0589/ movie: Movie = {'name': 'Blade Runner', 'year': 1982} It's all possible in theory, but the support in widely used static type checkers is very basic or has bad UX. On the other hand, this is exactly what you need to write secure code.