-
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Even safer parseJson #150
Comments
ℹ️ I've started work on this. |
I love the idea of being able to do this: > S.parseJson($.Array($.Number).test, 'XXX')
Nothing()
> S.parseJson($.Array($.Number).test, '["foo","bar","baz"]')
Nothing()
> S.parseJson($.Array($.Number).test, '[1,2,3]')
Just([1, 2, 3]) If we're to do this, we should address #149 as well. :) |
It's my intent. But since the two issues have been collecting dust I figured I'd get at the low hanging fruit first. I'll implement it using |
Adds a second argument to the `parseJson` function: A TypeRep which the result from parsing the JSON is validated against. This closes #150.
Adds a second argument to the `parseJson` function: A TypeRep which the result from parsing the JSON is validated against. This closes #150.
Adds a second argument to the `parseJson` function: A TypeRep which the result from parsing the JSON is validated against. This closes #150.
Adds a second argument to the `parseJson` function: A TypeRep which the result from parsing the JSON is validated against. This closes sanctuary-js#150.
I'm very fond of the
get
andgets
style of type validation, because it forces you to do it. It would've already saved me on multiple occasions ifparseJson
(or anyx -> Any
function for that matter) had this built-in, because after doing a "safe" JSON parse, it doesn't always occur to me the return value may still be of an unwanted type.An alternative is to extract this behaviour out to its own function, which is used solely for type validation:
I think this second solution is more elegant in terms of modularity but it lacks the safety that comes from forcing you to do the type validation.
Side note:
TypeRep
may be changed to a predicate, but the idea remains the same.The text was updated successfully, but these errors were encountered: