json_schema_tools 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MTVkNGZkMjUxMjk5YTQ0ZTg5NzFlMTg4OGU4MGJmMjQ3NWVkOGVhOQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTFlMjliNzNkMGZiYzFmOWRjYjM3ODQ3MTBiYzQ5YjQ4ODdjN2EwYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
M2UyNTA5ZWQyMjcxYzQ2YTliNjgwZGQ2MWM1MmQ0NDUzNzY0ZDNiN2M2MTg3
|
10
|
+
MWE5YzZjN2VkMWI2MmVkNWE1ZGI4ZmU2OTc1YWY2ZTIwNDY0YTYwZDU0NzRh
|
11
|
+
ZTMzOTVhZjUyM2VkZmE0NmY2OWVjM2RhMGVkYzVlZjg4NWI5ZWI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
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
|
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
|
data/lib/schema_tools/version.rb
CHANGED
@@ -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
|
-
|
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)
|