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

Parse dates using ruby's own date parsing - not regular expressions #118

Merged
merged 2 commits into from
Oct 26, 2014

Conversation

iainbeeston
Copy link
Contributor

I've been having problems parsing date-times using json-schema. According to iso8601 'YYYY-MM-DD' is a valid date-time. Ruby respects that, but json-schema does not, because it uses a more simplistic regular expression to match date-times.

I've refactored the parsing of date and time formats to match what's done for URI, where we use the built-in ruby method, and add a validation error if there are any parsing errors.

This has brought up some subtle differences in how json-schema parses dates compared to ruby, for example, case sensitivity. I'd assume that ruby is correct in this, and I've updated the test cases to reflect that.

@hoxworth
Copy link
Contributor

hoxworth commented Sep 5, 2014

Ah, ok.

The problem here is that draft-01 through draft-03 specify the date-time format to mean the following:

date-time This SHOULD be a date in ISO 8601 format of YYYY-MM- DDThh:mm:ssZ in UTC time. This is the recommended form of date/timestamp.

Now, the spec does say SHOULD and not MUST, so there is room for interpretation (as there is for most format options). However, since there is a separate date format for these drafts, it makes sense to me to maintain this behavior for these drafts.

However, I agree that draft-04 should validate using the build-in ruby method. If you're unsure of how to separate these, I can help.

Thanks!

@iainbeeston
Copy link
Contributor Author

I'll look at how I can make that happen...

@iainbeeston
Copy link
Contributor Author

By the way, there's another unresolved question here - what should the
behaviour be when using a draft 4 schema if the date format is used? (date
isn't mentioned in draft 4, so far as I can tell)

@iainbeeston
Copy link
Contributor Author

Actually, @hoxworth - how would you prefer me to make this conditional on the schema version?

My preferred option would be to split the validation for each form type out into it's own method (eg. validate_date, validate_uri etc etc), then subclass the format validator for draft3, and overwrite the validate_datetime method to enforce the format of the date-time string (I might also remove support for date, email etc from draft4+ in the same way). What do you think?

@iainbeeston
Copy link
Contributor Author

I suppose it would be better to configure supported formats from the validator setup (ie. draft4.rb or draft3.rb)

@hoxworth
Copy link
Contributor

hoxworth commented Sep 9, 2014

@iainbeeston It would probably be best to do the latter of your options - configure the supported formats from the validator setup. I believe right now all formats are currently defined in just the one format attribute file, so this would require a little bit more work. If you'd like I can take this on, but I probably won't get around to it until this weekend or so.

@iainbeeston
Copy link
Contributor Author

Hey that's fine - there are a few things I'd like to add to json-schema, so I might as well start with this feature. If you have any ideas how you'd prefer it to be done, please let me know. I'll open a separate pull request for that.

I should have time to look at this in two weeks from now (if not sooner)

@iainbeeston
Copy link
Contributor Author

Sorry, I haven't had time to refactor the way that the accepted formats are selected (yet). Seems like there are a few pull requests that would benefit!

@iainbeeston
Copy link
Contributor Author

I'll revisit this now that #131 has been merged

own iso8601 parsing method

The initial reason for this was that date-time should allow 'YYYY-MM-DD'
according to iso8601. Ruby allows this but schema-form did not.
I assume that's a hangover from when they were all in the same file as
the uri format
@iainbeeston iainbeeston force-pushed the ruby_validation_of_dates branch from 73f44b3 to 784935c Compare October 26, 2014 11:50
@iainbeeston
Copy link
Contributor Author

@hoxworth I've updated this to use the new format classes now. Fixes a few bugs in the date-time parsing in the test suite. Could you please take another look?

@RST-J
Copy link
Contributor

RST-J commented Oct 26, 2014

Looks good to me.

@pd pd merged commit 784935c into voxpupuli:master Oct 26, 2014
pd added a commit that referenced this pull request Oct 26, 2014
Conflicts:
	lib/json-schema/attributes/formats/ip4.rb
	lib/json-schema/attributes/formats/ip6.rb
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.

4 participants