json_schema_tools 0.5.1 → 0.5.2
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
|
+
MWMyMGVjNDg3M2IwYzhkMjU0N2Y3ZjY4OThjYTYxMTNkODY0MzYxZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NjZjYjhiMmFhZmZhOWNjYjE5MzllZWE5ODllZjQzY2VjOGU1MTRlMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MDdkNTk5MWMzMmExMDAxZTU0ZTI4MWYyZmU1OGFkZjRlZTRiODhiNDljNzJk
|
10
|
+
YWMxMjA2OTljZWI1ODBiMjY4YjE5MDc3N2JkNGNlMTM1ZWM1YzVmOGRjMWRj
|
11
|
+
ZDE2NzIzZTM0NWNjZDk2MWFmNDBiYzFhYjE4Yjg3NWZjNmJkYWM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZWI5YjZjZTU5MzUzZjliNTEwOTU2Nzc4YTRlMjlmNGMxMmYyNGJlODg1YzU4
|
14
|
+
OWU0ODJiYzc4MTBiODM5Yzk4MjYxNGIwODk0NDFkNDk3NzQ3YjYwNjFlOTE1
|
15
|
+
MTI1MjFlNGI3ZWNhMzQ5MzkzNTJhYjFhOWVhYmQxMzk5M2M2ODU=
|
@@ -73,13 +73,15 @@ module SchemaTools
|
|
73
73
|
# c = Contact.from_hash( {'contact'=>{ "id=>"123456", "last_name"=>"Meier" }} )
|
74
74
|
# c.id #=>123456
|
75
75
|
#
|
76
|
-
# @param [
|
77
|
-
|
76
|
+
# @param [Hash{String=>Mixed}] json string or hash
|
77
|
+
# @param [Object] obj if you want to update an existing objects
|
78
|
+
# attributes. e.g during an update
|
79
|
+
def from_hash(hash, obj=nil)
|
78
80
|
# test if hash is nested and shift up
|
79
81
|
if hash.length == 1 && hash["#{schema_name}"]
|
80
82
|
hash = hash["#{schema_name}"]
|
81
83
|
end
|
82
|
-
obj
|
84
|
+
obj ||= new
|
83
85
|
hash.each do |key, val|
|
84
86
|
next unless obj.respond_to?(key)
|
85
87
|
# set values to raw schema attributes, even if there are no setters
|
@@ -16,20 +16,10 @@ module SchemaTools
|
|
16
16
|
include ActiveModel::Conversion
|
17
17
|
include ActiveModel::Validations
|
18
18
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
# false otherwise.
|
24
|
-
#
|
25
|
-
# If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if
|
26
|
-
# <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not.
|
27
|
-
#
|
28
|
-
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
29
|
-
# some <tt>:on</tt> option will only run in the specified context.
|
30
|
-
def valid?(context = nil)
|
31
|
-
#context ||= (new_record? ? :create : :update)
|
32
|
-
output = super(context)
|
19
|
+
# Runs all the validations within the specified context.
|
20
|
+
# @return [Boolean] true if no errors are found, false otherwise
|
21
|
+
def valid?
|
22
|
+
output = super
|
33
23
|
errors.empty? && output
|
34
24
|
end
|
35
25
|
|
@@ -42,15 +32,13 @@ module SchemaTools
|
|
42
32
|
def validate_with(schema, opts={})
|
43
33
|
reader = opts[:reader] || SchemaTools::Reader
|
44
34
|
schema = reader.read(schema, opts[:path])
|
45
|
-
#
|
35
|
+
# create validation methods
|
46
36
|
schema[:properties].each do |key, val|
|
47
37
|
validates_length_of key, validate_length_opts(val) if val['maxLength'] || val['minLength']
|
48
38
|
validates_presence_of key if val['required'] || val['required'] == 'true'
|
49
|
-
validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number'
|
39
|
+
validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number' || val['type'] == 'integer'
|
50
40
|
#TODO array minItems, max unique, null, string
|
51
41
|
# format: date-time, regex color style, email,uri, ..
|
52
|
-
validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number'
|
53
|
-
#end
|
54
42
|
end
|
55
43
|
end
|
56
44
|
|
data/lib/schema_tools/version.rb
CHANGED
@@ -111,7 +111,7 @@ describe SchemaTools::Modules::Attributes do
|
|
111
111
|
hash = { created_at: "2014-12-06T04:30:26+01:00"}
|
112
112
|
obj = TestClient.from_hash(hash)
|
113
113
|
expect(obj.created_at.class).to eq Time
|
114
|
-
expect(obj.created_at.zone).to eq 'CET'
|
114
|
+
# expect(obj.created_at.zone).to eq 'CET' # fails on travis-ci .. strange
|
115
115
|
expect(obj.created_at.year).to eq 2014
|
116
116
|
|
117
117
|
hash = { created_at: "2014-12-04T10:39:50.000Z"}
|
@@ -120,6 +120,15 @@ describe SchemaTools::Modules::Attributes do
|
|
120
120
|
expect(obj.created_at.hour).to eq 10
|
121
121
|
end
|
122
122
|
|
123
|
+
it 'updates an object' do
|
124
|
+
obj = TestContact.from_hash( {first_name: 'Frieda'} )
|
125
|
+
expect(obj.first_name).to eq 'Frieda'
|
126
|
+
expect(obj.last_name).to eq nil
|
127
|
+
|
128
|
+
TestContact.from_hash({first_name: 'Paul', last_name: 'Hulk'}, obj)
|
129
|
+
expect(obj.first_name).to eq 'Paul'
|
130
|
+
expect(obj.last_name).to eq 'Hulk'
|
131
|
+
end
|
123
132
|
|
124
133
|
end
|
125
134
|
|
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.5.
|
4
|
+
version: 0.5.2
|
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-12-
|
11
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|