openc-json_schema 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmE0MDA3M2IxMWZhMTlhZGU5MDdhNmMzNzJmMjA3MzYzZGI5MjE5Mg==
4
+ MmM4OTcxOWE4NDM5YTcxYzQyYWMwYjNmYTczMzhkN2I2YjVjNDA2Nw==
5
5
  data.tar.gz: !binary |-
6
- YjQyN2Q2YWYyMjgyYmJiZWJlN2YxMDk3MzU4ZWUzM2E5ZWMzZGI5ZA==
6
+ MThkMzRkMTNjYWYxYTNhYmRlNGFjYjg1YjQ3ZDc4OGNmOTZlZDBiOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTBjYTFhZTIyOWMyNzUzOTZkNTJkZTI5OTU2NzY4NWU0N2FiNzkxNWJiNjQ3
10
- MTViOTg0ODVjZjg0YTY3MjdiMzExMzZjMjQxNDQyOWE2ZDJlNzI1NTk4Mjg2
11
- ODI3YmRkZGJlMzg3NmMzZTBkNmM5ZTFmYTJkOTI0MmI4Njc5Yjg=
9
+ MjJjZWMzMjZlOWI4ODk2NDc4OGIyMjgyMzg5Y2Y5ZTNjOTY5OTcyMWZhNDkz
10
+ Y2MwZTI5ZTQ2ZGZhYjc5MTc1ODkzZTRiOGQ0Y2UyNzlkZjIzYmUxYjg3YzAw
11
+ YTgzOTFiNzg2ZmZmNzdhYjliMDUwMDIzNjdlMjk2MmFiYzc2YzY=
12
12
  data.tar.gz: !binary |-
13
- OTBjNjliN2EzOWUzZmRhMDcyMzk0ZDkyMzc4OWFkNzZjMzFhNjUxYTM0ZTNh
14
- MjFiODNlYTVlMTVkYzdhMDgxNjY0MzllMThjZTRkOGFjM2ZhYTMxMWQ4NTA1
15
- N2RmNmVjNWJlYWJkNmRkMGM2N2VhZTZlNzMwYjQ0YWFhMjk1ZWQ=
13
+ ZjRjNDZhNTkyNGU5MGVhY2ExMTU4NzkyNGY0NTg2Mzc4N2UxYmFlZDdhMzFj
14
+ OTY1MjFlNDE4YzNlMGY4M2VhYjdhNjk2OGM2ZDE4OTIzM2MyMjYwZmUwYmVl
15
+ ZGFkMjcyOTZjNzU1YTZiMDNmYjVkMjgwOTgyZTk5MDQxYmNkMTI=
data/Gemfile.lock CHANGED
@@ -9,7 +9,7 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- openc-json_schema (0.0.4)
12
+ openc-json_schema (0.0.5)
13
13
 
14
14
  GEM
15
15
  remote: https://rubygems.org/
@@ -1,7 +1,10 @@
1
1
  require 'json-schema'
2
2
 
3
- require 'openc/json_schema/validator'
3
+ require 'openc/json_schema/date_converter'
4
4
  require 'openc/json_schema/format_validators'
5
+ require 'openc/json_schema/utils'
6
+ require 'openc/json_schema/validator'
7
+ require 'openc/json_schema/version'
5
8
 
6
9
  module Openc
7
10
  module JsonSchema
@@ -10,5 +13,9 @@ module Openc
10
13
  def validate(schema_path, record)
11
14
  Validator.validate(schema_path, record)
12
15
  end
16
+
17
+ def convert_dates(schema_path, record)
18
+ DateConverter.convert_dates(schema_path, record)
19
+ end
13
20
  end
14
21
  end
@@ -0,0 +1,48 @@
1
+ module Openc
2
+ module JsonSchema
3
+ module DateConverter
4
+ extend self
5
+
6
+ def convert_dates(schema_path, record)
7
+ validator = Utils.load_validator(schema_path, record)
8
+ json_schema = Utils.extract_json_schema(validator)
9
+ _convert_dates(record, validator, json_schema, json_schema.schema)
10
+ end
11
+
12
+ def _convert_dates(record, validator, json_schema, schema)
13
+ return record if schema.nil?
14
+
15
+ if (ref = schema['$ref'])
16
+ schema_uri = validator.absolutize_ref_uri(ref, json_schema.uri)
17
+ json_schema = JSON::Validator.schema_reader.read(schema_uri)
18
+ schema = json_schema.schema
19
+ end
20
+
21
+ case record
22
+ when Hash
23
+ pairs = record.map do |k, v|
24
+ properties = schema['properties']
25
+ if properties.nil?
26
+ [k, v]
27
+ else
28
+ [k, _convert_dates(v, validator, json_schema, properties[k])]
29
+ end
30
+ end
31
+ Hash[pairs]
32
+ when Array
33
+ record.map {|e| _convert_dates(e, validator, json_schema, schema['items'])}
34
+ else
35
+ if schema['format'] == 'date'
36
+ begin
37
+ Date.strptime(record, '%Y-%m-%d').strftime('%Y-%m-%d')
38
+ rescue ArgumentError
39
+ record
40
+ end
41
+ else
42
+ record
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ module Openc
2
+ module JsonSchema
3
+ module Utils
4
+ extend self
5
+
6
+ def load_validator(schema_path, record)
7
+ validator = JSON::Validator.new(
8
+ schema_path,
9
+ record,
10
+ :record_errors => true,
11
+ :errors_as_objects => true,
12
+ :validate_schema => true
13
+ )
14
+ end
15
+
16
+ def extract_json_schema(validator)
17
+ validator.instance_variable_get(:@base_schema)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,13 +4,7 @@ module Openc
4
4
  extend self
5
5
 
6
6
  def validate(schema_path, record)
7
- validator = JSON::Validator.new(
8
- schema_path,
9
- record,
10
- :record_errors => true,
11
- :errors_as_objects => true,
12
- :validate_schema => true
13
- )
7
+ validator = Utils.load_validator(schema_path, record)
14
8
  errors = validator.validate
15
9
 
16
10
  # For now, we just handle the first error.
@@ -25,7 +19,7 @@ module Openc
25
19
  if error[:message].match(/did not match any/)
26
20
  path_elements = fragment_to_path(error[:fragment]).split('.')
27
21
 
28
- json_schema = validator.instance_variable_get(:@base_schema)
22
+ json_schema = Utils.extract_json_schema(validator)
29
23
  schema = json_schema.schema
30
24
 
31
25
  path_elements.each do |element|
@@ -1,5 +1,5 @@
1
1
  module Openc
2
2
  module JsonSchema
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
@@ -457,4 +457,70 @@ describe Openc::JsonSchema do
457
457
  )
458
458
  end
459
459
  end
460
+
461
+ describe '.convert_dates' do
462
+ it 'converts dates when schema has no $refs' do
463
+ schema = {
464
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
465
+ 'type' => 'object',
466
+ 'properties' => {
467
+ 'aaa' => {
468
+ 'type' => 'string',
469
+ 'format' => 'date'
470
+ },
471
+ 'bbb' => {
472
+ 'type' => 'array',
473
+ 'items' => {
474
+ 'type' => 'string',
475
+ 'format' => 'date'
476
+ }
477
+ },
478
+ 'ccc' => {
479
+ 'type' => 'object',
480
+ 'properties' => {
481
+ 'ddd' => {
482
+ 'type' => 'string',
483
+ 'format' => 'date'
484
+ }
485
+ }
486
+ }
487
+ }
488
+ }
489
+
490
+ record = {
491
+ 'aaa' => '2015-01-01 extra',
492
+ 'bbb' => ['2015-01-01 extra', '2015-01-01 extra'],
493
+ 'ccc' => {
494
+ 'ddd' => '2015-01-01 extra'
495
+ }
496
+ }
497
+
498
+ expect([schema, record]).to convert_dates_to({
499
+ 'aaa' => '2015-01-01',
500
+ 'bbb' => ['2015-01-01', '2015-01-01'],
501
+ 'ccc' => {
502
+ 'ddd' => '2015-01-01'
503
+ }
504
+ })
505
+ end
506
+
507
+ it 'converts dates when schema has $refs' do
508
+ schema_path = 'spec/schemas/yyy.json'
509
+ record = {
510
+ 'aaa' => '2015-01-01 extra',
511
+ 'bbb' => ['2015-01-01 extra', '2015-01-01 extra'],
512
+ 'ccc' => {
513
+ 'ddd' => '2015-01-01 extra'
514
+ }
515
+ }
516
+
517
+ expect([schema_path, record]).to convert_dates_to({
518
+ 'aaa' => '2015-01-01',
519
+ 'bbb' => ['2015-01-01', '2015-01-01'],
520
+ 'ccc' => {
521
+ 'ddd' => '2015-01-01'
522
+ }
523
+ })
524
+ end
525
+ end
460
526
  end
@@ -0,0 +1,11 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "ddd": {
6
+ "type": "string",
7
+ "format": "date"
8
+ }
9
+ }
10
+ }
11
+
@@ -0,0 +1,20 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "aaa": {
6
+ "type": "string",
7
+ "format": "date"
8
+ },
9
+ "bbb": {
10
+ "type": "array",
11
+ "items": {
12
+ "type": "string",
13
+ "format": "date"
14
+ }
15
+ },
16
+ "ccc": {
17
+ "$ref": "includes/zzz.json"
18
+ }
19
+ }
20
+ }
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ SimpleCov.start
3
3
 
4
4
  require 'openc/json_schema'
5
5
 
6
- def get_error(schema_or_path, record)
6
+ def get_schema_path(schema_or_path)
7
7
  case schema_or_path
8
8
  when Hash
9
9
  json_data = schema_or_path.to_json
@@ -16,13 +16,13 @@ def get_error(schema_or_path, record)
16
16
  raise
17
17
  end
18
18
 
19
- error = Openc::JsonSchema.validate(schema_path, record)
19
+ schema_path
20
20
  end
21
21
 
22
22
  RSpec::Matchers.define(:fail_validation_with) do |expected|
23
23
  match do |actual|
24
24
  schema_or_path, record = actual
25
- @error = get_error(schema_or_path, record)
25
+ @error = Openc::JsonSchema.validate(get_schema_path(schema_or_path), record)
26
26
  expect(@error).to eq(expected)
27
27
  end
28
28
 
@@ -34,7 +34,17 @@ end
34
34
  RSpec::Matchers.define(:be_valid) do
35
35
  match do |actual|
36
36
  schema_or_path, record = actual
37
- error = get_error(schema_or_path, record)
37
+ error = Openc::JsonSchema.validate(get_schema_path(schema_or_path), record)
38
38
  expect(error).to eq(nil)
39
39
  end
40
40
  end
41
+
42
+ RSpec::Matchers.define(:convert_dates_to) do |expected|
43
+ match do |actual|
44
+ schema_or_path, record = actual
45
+ schema_path = get_schema_path(schema_or_path)
46
+
47
+ converted_record = Openc::JsonSchema.convert_dates(schema_path, record)
48
+ expect(converted_record).to eq(expected)
49
+ end
50
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openc-json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenCorporates
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-10 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,7 +65,9 @@ files:
65
65
  - README.md
66
66
  - Rakefile
67
67
  - lib/openc/json_schema.rb
68
+ - lib/openc/json_schema/date_converter.rb
68
69
  - lib/openc/json_schema/format_validators.rb
70
+ - lib/openc/json_schema/utils.rb
69
71
  - lib/openc/json_schema/validator.rb
70
72
  - lib/openc/json_schema/version.rb
71
73
  - openc-json_schema.gemspec
@@ -82,8 +84,10 @@ files:
82
84
  - spec/schemas/includes/hhh.json
83
85
  - spec/schemas/includes/jjj.json
84
86
  - spec/schemas/includes/mmm.json
87
+ - spec/schemas/includes/zzz.json
85
88
  - spec/schemas/kkk.json
86
89
  - spec/schemas/lll.json
90
+ - spec/schemas/yyy.json
87
91
  - spec/spec_helper.rb
88
92
  - spec/tmp/.gitkeep
89
93
  homepage:
@@ -123,8 +127,10 @@ test_files:
123
127
  - spec/schemas/includes/hhh.json
124
128
  - spec/schemas/includes/jjj.json
125
129
  - spec/schemas/includes/mmm.json
130
+ - spec/schemas/includes/zzz.json
126
131
  - spec/schemas/kkk.json
127
132
  - spec/schemas/lll.json
133
+ - spec/schemas/yyy.json
128
134
  - spec/spec_helper.rb
129
135
  - spec/tmp/.gitkeep
130
136
  has_rdoc: