Skip to content

Instantly share code, notes, and snippets.

@coffeejunk
Created March 31, 2017 04:32
Show Gist options
  • Save coffeejunk/9f0a6edc85ca8dc83d586fad44c5fed3 to your computer and use it in GitHub Desktop.
Save coffeejunk/9f0a6edc85ca8dc83d586fad44c5fed3 to your computer and use it in GitHub Desktop.
require 'json-schema'
require 'json'
schema = JSON.parse(
<<JSONSCHEMA
{
"type": "object",
"required": [
"ceterms:foundingDate"
],
"properties": {
"ceterms:foundingDate": {
"anyOf": [
{
"type": "string",
"format": "date"
},
{
"type": "string",
"format": "date-time"
}
]
}
}
}
JSONSCHEMA
)
puts schema.to_json
%w{
2017
2017-03
2017-03-31
2017-03-31T01:37:59Z
20170331T013759Z
2017-03-31T01:37:59+00:00
}.each do |date|
data = { 'ceterms:foundingDate' => date }
print data.to_json
print ' valid? '
puts JSON::Validator.validate(schema, data)
end
@coffeejunk
Copy link
Author

$ ruby date_examples.rb
{"type":"object","required":["ceterms:foundingDate"],"properties":{"ceterms:foundingDate":{"anyOf":[{"type":"string","format":"date"},{"type":"string","format":"date-time"}]}}}
{"ceterms:foundingDate":"2017"} valid? true
{"ceterms:foundingDate":"2017-03"} valid? true
{"ceterms:foundingDate":"2017-03-31"} valid? true
{"ceterms:foundingDate":"2017-03-31T01:37:59Z"} valid? true
{"ceterms:foundingDate":"20170331T013759Z"} valid? true
{"ceterms:foundingDate":"2017-03-31T01:37:59+00:00"} valid? true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment