json_schema_tools 0.4.3 → 0.5.0
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 +8 -8
- data/CHANGELOG.md +15 -9
- data/lib/schema_tools/modules/attributes.rb +16 -2
- data/lib/schema_tools/modules/hash.rb +11 -1
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/data/contact_plain.json +2 -1
- data/spec/fixtures/schemata/contact.json +9 -0
- data/spec/schema_tools/hash_spec.rb +3 -3
- data/spec/schema_tools/modules/attributes_spec.rb +33 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTI1OGYwODJjNzlkNDQ2ODMwYTZjZmFiY2Q4MTY0NTAyYTU3Yzk4Mg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YTIyNWFkYjk2NTJiNmQ2YzcxZjEwN2VlODY2MDQyOTkzOTc4YzlkYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmQxOGI5ZDBjOGJiZjYyODQ0NWY4ODllNTkxODMwN2M4YmFmNWM3M2JjN2Rm
|
10
|
+
NDg0OGY1YTA5NzI4ODU4ZjJmNzllYzdhYmJkYTI3NzFiOTFhMWVmNDAxOGQ4
|
11
|
+
MDM2ODc4Njk3NGRkMGY2MjczMmI5MTVhNmJlZjlmN2Q5MThkMzY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjMyZTc0OWFhNGUxOTAwZTNjYmRjOTEyMTY0MzQ3MzNiZDNhNDM4NDFmZmI0
|
14
|
+
NDljYzExNmJlMDc1NDY5ZmFiNGM0YWY1Mzc2OWUwZGUzYTAwYTIwOTYwNDc4
|
15
|
+
ODM1NDhiYTNmY2ZiMzdlNGM1ZTNkMWMyYjlkNTcwNTUwMDY0OTc=
|
data/CHANGELOG.md
CHANGED
@@ -1,19 +1,25 @@
|
|
1
1
|
# Changelog JSON Schema Tools
|
2
2
|
|
3
|
-
2014-
|
3
|
+
##2014-12
|
4
|
+
|
5
|
+
* object.as_schema_hash converts values to types defined in the schema e.g strings, integers
|
6
|
+
* Object.from_hash casts object(json) field values into their ruby type for string, integer, date, date-time
|
7
|
+
|
8
|
+
|
9
|
+
##2014-11
|
4
10
|
|
5
11
|
* add from_json method to attributes module, to create new objects from a json string
|
6
12
|
* remove as_json method(alias of as_schema_json) conflicting with active_resource/active_record. Nothing stops one from using/aliasing it in his own code.
|
7
13
|
|
8
|
-
2014-10
|
14
|
+
##2014-10
|
9
15
|
|
10
16
|
* handle circular dependencies in $ref arguments, so schema.to_h always resolves $refs properly
|
11
17
|
|
12
|
-
2014-09
|
18
|
+
##2014-09
|
13
19
|
|
14
20
|
* add Schema class to represent a single schema, replaces simple Hash usage. Potentially breaks usage if you've gone crazy with Hash methods in your client.
|
15
21
|
|
16
|
-
2014-09
|
22
|
+
##2014-09
|
17
23
|
|
18
24
|
* refactor Reader to de-reference all pointers when initialized
|
19
25
|
* remove :exclude_root option for object to schema_hash BREAKING change if you want nesting define your schema accordingly
|
@@ -21,28 +27,28 @@
|
|
21
27
|
* support items definition for array type properties - BREAKING you must change old simple property definitions
|
22
28
|
* support oneOf definitions
|
23
29
|
|
24
|
-
2014-08
|
30
|
+
##2014-08
|
25
31
|
|
26
32
|
* add $ref resolver to merge property definitions from another file
|
27
33
|
|
28
|
-
2013-10
|
34
|
+
##2013-10
|
29
35
|
|
30
36
|
* allow all object properties in link href placeholders => contacts/{id}/{number}
|
31
37
|
* add base_url option to schema hash creation. Prepends an url to all links of a rendered object
|
32
38
|
* add to_schema_json for simpler model to json conversion
|
33
39
|
* add option to exclude_root in to_schema hash method
|
34
40
|
|
35
|
-
2013-06
|
41
|
+
##2013-06
|
36
42
|
|
37
43
|
* *has_schema_attr :schema=>{schema as ruby hash}* allow pass a schema as hash
|
38
44
|
* *SchemaTools::Reader.read('name', {Schema Hash}|'String Path' )* init a schema from hash additionally to passing a string as schema path
|
39
45
|
* rails 4 (ActiveModel) compatibility
|
40
46
|
* Testing with different ActiveModel Versions
|
41
47
|
|
42
|
-
2013-02
|
48
|
+
##2013-02
|
43
49
|
|
44
50
|
* add validations for classes generated by KlassFactory
|
45
51
|
|
46
|
-
2012-12
|
52
|
+
##2012-12
|
47
53
|
|
48
54
|
* initial version with reader, hasher, params cleaner, attributes module
|
@@ -85,8 +85,22 @@ module SchemaTools
|
|
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
87
|
# TODO type conversion string/integer/number/date/datetime?
|
88
|
-
|
89
|
-
|
88
|
+
field_props = self.schema['properties']["#{key}"]
|
89
|
+
conv_val = if field_props['type'] == 'string'
|
90
|
+
if field_props['format'] == 'date'
|
91
|
+
Date.parse(val) # or be explicit? Date.strptime('2001-02-03', '%Y-%m-%d')
|
92
|
+
elsif field_props['format'] == 'date-time'
|
93
|
+
Time.parse(val) # vs Time.strptime
|
94
|
+
else
|
95
|
+
"#{val}"
|
96
|
+
end
|
97
|
+
elsif field_props['type'] == 'integer'
|
98
|
+
val.to_i
|
99
|
+
else # rely on preceding call e.g from_json for boolean, number
|
100
|
+
val
|
101
|
+
end
|
102
|
+
# TODO if val is a hash / array => look for nested class & cast
|
103
|
+
obj.schema_attrs["#{key}"] = conv_val
|
90
104
|
end
|
91
105
|
obj
|
92
106
|
end
|
@@ -77,7 +77,17 @@ module SchemaTools
|
|
77
77
|
opts.delete(:class_name)
|
78
78
|
data[field] = parse_object(obj, field, prop, opts)
|
79
79
|
else # a simple field is only added if the object knows it
|
80
|
-
|
80
|
+
next unless obj.respond_to?(field)
|
81
|
+
raw_val = obj.send(field)
|
82
|
+
# convert field to schema type if set
|
83
|
+
conv_val = if prop['type'] == 'string' # rely on .to_s for format from date/datetime
|
84
|
+
"#{raw_val}"
|
85
|
+
elsif prop['type'] == 'integer'
|
86
|
+
raw_val.to_i
|
87
|
+
else # bool / number rely on .to_s in json lib
|
88
|
+
raw_val
|
89
|
+
end
|
90
|
+
data[field] = conv_val
|
81
91
|
end
|
82
92
|
end
|
83
93
|
data
|
data/lib/schema_tools/version.rb
CHANGED
@@ -28,6 +28,15 @@
|
|
28
28
|
"description": "First name of a person.",
|
29
29
|
"type":"string",
|
30
30
|
"maxLength": 50
|
31
|
+
},
|
32
|
+
"birthday":{
|
33
|
+
"description": "Date of birth",
|
34
|
+
"type":"string",
|
35
|
+
"format": "date"
|
36
|
+
},
|
37
|
+
"click_count":{
|
38
|
+
"description": "Count of clicks",
|
39
|
+
"type":"integer"
|
31
40
|
}
|
32
41
|
}
|
33
42
|
}
|
@@ -94,7 +94,7 @@ describe SchemaTools::Hash do
|
|
94
94
|
a1.zip = 50733
|
95
95
|
client.addresses = [a1]
|
96
96
|
hash = SchemaTools::Hash.from_schema(client)
|
97
|
-
hash['addresses'].should == [{"city"=>"Cologne", "zip"=>50733}]
|
97
|
+
hash['addresses'].should == [{"city"=>"Cologne", "zip"=>"50733"}]
|
98
98
|
end
|
99
99
|
|
100
100
|
it 'has nested array values without root' do
|
@@ -103,7 +103,7 @@ describe SchemaTools::Hash do
|
|
103
103
|
a1.zip = 50733
|
104
104
|
client.addresses = [a1]
|
105
105
|
hash = SchemaTools::Hash.from_schema(client, exclude_root: true)
|
106
|
-
hash['addresses'].should == [{"city"=>"Cologne", "zip"=>50733}]
|
106
|
+
hash['addresses'].should == [{"city"=>"Cologne", "zip"=>"50733"}]
|
107
107
|
end
|
108
108
|
|
109
109
|
it 'has nested object value' do
|
@@ -112,7 +112,7 @@ describe SchemaTools::Hash do
|
|
112
112
|
a1.zip = 50733
|
113
113
|
client.work_address = a1
|
114
114
|
hash = SchemaTools::Hash.from_schema(client)
|
115
|
-
hash['work_address'].should == {"city"=>"Cologne", "zip"=>50733}
|
115
|
+
hash['work_address'].should == {"city"=>"Cologne", "zip"=>"50733"}
|
116
116
|
end
|
117
117
|
|
118
118
|
it 'has nested oneOf type object ' do
|
@@ -81,7 +81,39 @@ describe SchemaTools::Modules::Attributes do
|
|
81
81
|
expect(obj.contact_source).to eq hash['contact']['contact_source']
|
82
82
|
expect(obj.first_name).to eq hash['contact']['first_name']
|
83
83
|
expect(obj.last_name).to eq hash['contact']['last_name']
|
84
|
-
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'converts strings' do
|
87
|
+
hash = { organisation: 123}
|
88
|
+
obj = TestContact.from_hash(hash)
|
89
|
+
expect(obj.organisation).to eq '123'
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'converts integers' do
|
93
|
+
hash = { click_count: '23'}
|
94
|
+
obj = TestContact.from_hash(hash)
|
95
|
+
expect(obj.click_count).to eq 23
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'converts dates' do
|
99
|
+
hash = { birthday: "1975-11-17"}
|
100
|
+
obj = TestContact.from_hash(hash)
|
101
|
+
expect(obj.birthday.class).to eq Date
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'converts datetime' do
|
105
|
+
hash = { created_at: "2014-12-06T04:30:26+01:00"}
|
106
|
+
obj = TestClient.from_hash(hash)
|
107
|
+
expect(obj.created_at.class).to eq Time
|
108
|
+
expect(obj.created_at.zone).to eq 'CET'
|
109
|
+
expect(obj.created_at.year).to eq 2014
|
110
|
+
|
111
|
+
hash = { created_at: "2014-12-04T10:39:50.000Z"}
|
112
|
+
obj = TestClient.from_hash(hash)
|
113
|
+
expect(obj.created_at.zone).to eq 'UTC'
|
114
|
+
expect(obj.created_at.hour).to eq 10
|
115
|
+
end
|
116
|
+
|
85
117
|
|
86
118
|
end
|
87
119
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Georg Leciejewski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|