Skip to content
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

Closed
Avaq opened this issue Feb 6, 2016 · 3 comments · Fixed by #168
Closed

Even safer parseJson #150

Avaq opened this issue Feb 6, 2016 · 3 comments · Fixed by #168

Comments

@Avaq
Copy link
Member

Avaq commented Feb 6, 2016

parseJson(Array, '{}') -> Nothing()

I'm very fond of the get and gets style of type validation, because it forces you to do it. It would've already saved me on multiple occasions if parseJson (or any x -> 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:

validate :: TypeRep -> a -> Maybe a

const parseJsonArray = compose(chain(validate(Array)), parseJson);

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.

@Avaq
Copy link
Member Author

Avaq commented Mar 23, 2016

ℹ️ I've started work on this.

@davidchambers
Copy link
Member

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. :)

@Avaq
Copy link
Member Author

Avaq commented Mar 23, 2016

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 TypeRep, once merged it would clear the way for the type validation overhaul, which would then also affect parseJson.

Avaq added a commit that referenced this issue Mar 23, 2016
Adds a second argument to the `parseJson` function:
A TypeRep which the result from parsing the JSON is
validated against. This closes #150.
Avaq added a commit that referenced this issue Apr 12, 2016
Adds a second argument to the `parseJson` function:
A TypeRep which the result from parsing the JSON is
validated against. This closes #150.
Avaq added a commit that referenced this issue Apr 13, 2016
Adds a second argument to the `parseJson` function:
A TypeRep which the result from parsing the JSON is
validated against. This closes #150.
svozza pushed a commit to svozza/sanctuary that referenced this issue Apr 19, 2016
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants