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

Change parseJson to perform type validation #168

Merged
merged 1 commit into from
Apr 13, 2016

Conversation

Avaq
Copy link
Member

@Avaq Avaq commented 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.

[$.String, $Maybe($.Any)],
encase(JSON.parse));
[TypeRep, $.String, $Maybe(a)],
function(type, json) { return filter(is(type), encase(JSON.parse, json)); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name the second parameter s rather than json, as it's not necessarily a valid JSON string.

@davidchambers
Copy link
Member

This is very nice, Aldwin!

//. otherwise.
//. Takes a [type representative](#type-representatives) and a string which
//. may or may not be valid JSON, and returns Just the result of applying
//. `JSON.parse` to the string *if* the result is of the specified type
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about the cursive if? I thought it's a nice way to break up the long sentence, but I'm not sure about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for me, but the one thing that's not clear from this description is the handling of invalid strings.

What do you think of this phrasing?

… and returns Just x if the given string is a JSON representation of x, a value of the specified type (according to is); Nothing otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered it, but I think only specifying the latter is good enough, because it implies the first. That combined with the code example makes it pretty clear. I'm worried going into the specifics take away from the ease of reading and clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'm happy with your current wording also.

@Avaq
Copy link
Member Author

Avaq commented Mar 27, 2016

@davidchambers Did you see I did the thing?

@davidchambers
Copy link
Member

Thanks, @Avaq. Now that we've merged sanctuary-js/sanctuary-def#38 I can publish a minor release of sanctuary-def and a patch release of this project (to fix #165). That will free us to start merging changes for the v0.10.0 release.

@Avaq
Copy link
Member Author

Avaq commented Mar 27, 2016

I see, so we're awaiting the release of sanctuary-def before we'll rebase and squash. Cool. :)

@davidchambers
Copy link
Member

Please rebase this branch when you get a chance.

@Avaq Avaq force-pushed the av-validated-parsejson branch from d61b9c3 to 30b3b7a Compare April 12, 2016 11:47
@Avaq
Copy link
Member Author

Avaq commented Apr 12, 2016

ℹ️ @davidchambers Noticed the rebase? I'm not sure if it would've notified you.

'1) "String" :: String\n' +
'\n' +
'The value at position 1 is not a member of ‘TypeRep’.\n'));
throws(function() { S.parseJson(Array, [1, 2, 3]); },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding an empty line between these blocks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Adds a second argument to the `parseJson` function:
A TypeRep which the result from parsing the JSON is
validated against. This closes #150.
@Avaq Avaq force-pushed the av-validated-parsejson branch from 30b3b7a to 97f2327 Compare April 13, 2016 08:03
@davidchambers
Copy link
Member

🌳 Thanks very much, @Avaq!

@davidchambers davidchambers merged commit c09b210 into master Apr 13, 2016
@davidchambers davidchambers deleted the av-validated-parsejson branch April 13, 2016 17:03
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 this pull request may close these issues.

Even safer parseJson
2 participants