json_schema_tools 0.5.0 → 0.5.1

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
- YTI1OGYwODJjNzlkNDQ2ODMwYTZjZmFiY2Q4MTY0NTAyYTU3Yzk4Mg==
4
+ MTVkNGZkMjUxMjk5YTQ0ZTg5NzFlMTg4OGU4MGJmMjQ3NWVkOGVhOQ==
5
5
  data.tar.gz: !binary |-
6
- YTIyNWFkYjk2NTJiNmQ2YzcxZjEwN2VlODY2MDQyOTkzOTc4YzlkYg==
6
+ NTFlMjliNzNkMGZiYzFmOWRjYjM3ODQ3MTBiYzQ5YjQ4ODdjN2EwYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NmQxOGI5ZDBjOGJiZjYyODQ0NWY4ODllNTkxODMwN2M4YmFmNWM3M2JjN2Rm
10
- NDg0OGY1YTA5NzI4ODU4ZjJmNzllYzdhYmJkYTI3NzFiOTFhMWVmNDAxOGQ4
11
- MDM2ODc4Njk3NGRkMGY2MjczMmI5MTVhNmJlZjlmN2Q5MThkMzY=
9
+ M2UyNTA5ZWQyMjcxYzQ2YTliNjgwZGQ2MWM1MmQ0NDUzNzY0ZDNiN2M2MTg3
10
+ MWE5YzZjN2VkMWI2MmVkNWE1ZGI4ZmU2OTc1YWY2ZTIwNDY0YTYwZDU0NzRh
11
+ ZTMzOTVhZjUyM2VkZmE0NmY2OWVjM2RhMGVkYzVlZjg4NWI5ZWI=
12
12
  data.tar.gz: !binary |-
13
- YjMyZTc0OWFhNGUxOTAwZTNjYmRjOTEyMTY0MzQ3MzNiZDNhNDM4NDFmZmI0
14
- NDljYzExNmJlMDc1NDY5ZmFiNGM0YWY1Mzc2OWUwZGUzYTAwYTIwOTYwNDc4
15
- ODM1NDhiYTNmY2ZiMzdlNGM1ZTNkMWMyYjlkNTcwNTUwMDY0OTc=
13
+ OWRhYzZiMzk1NjgzNmRkMWE1OWVjZTk4M2FhZTc3MzJkOTNlYzczOTU5OTNm
14
+ MjAyOTU0NWVlZmIzYzNjYzgzY2QwNjJmZGRlZGQxMGFjZmQ3MTIwY2IyOTA2
15
+ MjAyNzhjNzEyNTJmMTY5NzMzOTkzYzkzZTM1OWFiMTViYjUzNTM=
@@ -84,9 +84,10 @@ module SchemaTools
84
84
  next unless obj.respond_to?(key)
85
85
  # set values to raw schema attributes, even if there are no setters
86
86
  # assuming this objects comes from a remote site
87
- # TODO type conversion string/integer/number/date/datetime?
88
87
  field_props = self.schema['properties']["#{key}"]
89
- conv_val = if field_props['type'] == 'string'
88
+ conv_val = if val.nil?
89
+ val
90
+ elsif field_props['type'] == 'string'
90
91
  if field_props['format'] == 'date'
91
92
  Date.parse(val) # or be explicit? Date.strptime('2001-02-03', '%Y-%m-%d')
92
93
  elsif field_props['format'] == 'date-time'
@@ -80,7 +80,9 @@ module SchemaTools
80
80
  next unless obj.respond_to?(field)
81
81
  raw_val = obj.send(field)
82
82
  # convert field to schema type if set
83
- conv_val = if prop['type'] == 'string' # rely on .to_s for format from date/datetime
83
+ conv_val = if raw_val.nil?
84
+ raw_val
85
+ elsif prop['type'] == 'string' # rely on .to_s for format from date/datetime
84
86
  "#{raw_val}"
85
87
  elsif prop['type'] == 'integer'
86
88
  raw_val.to_i
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -12,8 +12,8 @@ class Address
12
12
  end
13
13
 
14
14
  class Contact
15
- attr_accessor :first_name, :last_name, :addresses, :id
16
- end
15
+ attr_accessor :first_name, :last_name, :addresses, :id, :organisation
16
+ end
17
17
 
18
18
  class OneOfDefinition
19
19
  attr_accessor :person
@@ -48,6 +48,18 @@ describe SchemaTools::Hash do
48
48
  hash['last_name'].should == 'Paul'
49
49
  end
50
50
 
51
+ it 'keeps nil values' do
52
+ hash = SchemaTools::Hash.from_schema(contact)
53
+ hash.keys.should include('organisation')
54
+ hash['organisation'].should be_nil
55
+ # hash['birthday'].should be_nil
56
+ end
57
+ it 'skips unknown fields values' do
58
+ hash = SchemaTools::Hash.from_schema(contact)
59
+ # not defined in the Contact class above, but defined in the contact.json schema
60
+ hash.keys.should_not include('birthday')
61
+ end
62
+
51
63
  it 'should use custom schema path' do
52
64
  custom_path = File.expand_path('../../fixtures/schemata', __FILE__)
53
65
  hash = SchemaTools::Hash.from_schema(contact, path: custom_path)
@@ -101,6 +101,12 @@ describe SchemaTools::Modules::Attributes do
101
101
  expect(obj.birthday.class).to eq Date
102
102
  end
103
103
 
104
+ it 'skips nil values' do
105
+ hash = { created_at: nil}
106
+ obj = TestClient.from_hash(hash)
107
+ expect(obj.created_at).to eq nil
108
+ end
109
+
104
110
  it 'converts datetime' do
105
111
  hash = { created_at: "2014-12-06T04:30:26+01:00"}
106
112
  obj = TestClient.from_hash(hash)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Georg Leciejewski