json_schema_tools 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/schema_tools/cleaner.rb +1 -1
- data/lib/schema_tools/klass_factory.rb +1 -1
- data/lib/schema_tools/modules/attributes.rb +1 -1
- data/lib/schema_tools/modules/validations.rb +8 -7
- data/lib/schema_tools/version.rb +1 -1
- data/spec/fixtures/schemata/address.json +2 -2
- data/spec/fixtures/schemata/basic_definitions.json +1 -1
- data/spec/fixtures/schemata/client.json +4 -4
- data/spec/fixtures/schemata/contact.json +2 -2
- data/spec/fixtures/schemata/nested_schemas/person.json +1 -1
- data/spec/fixtures/schemata/page.json +7 -7
- data/spec/schema_tools/modules/attributes_spec.rb +2 -2
- data/spec/test_helpers.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZWY5YzJhODNmZTc5NWFhNmI0NTM3N2I2Mzg3MzcwNGI0MDkxMTQzNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
Mzk5MmQxZGMyM2E2MDQ3MTU2MzE0ZWY3NmU1ODVmN2JjMDlhNGMzMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YmYzNWEzYzI2ZjQ5ZWFmYzNhM2I5ZjA2MTk1MTZhYmJlNzlmMmU2Njk1Yzc3
|
10
|
+
NGVlYzFkYjJhMzc2NWQxZmVhY2E2NTgyMDUwYmU4ODljNTExOGQ5YjJiMTI0
|
11
|
+
MWQ3NGJmZTZmNTY1NzFmMTMzZWJmOWNkY2NkODZkMTlmYjQ3ZmQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTEwM2Q4MTU5ZWE0N2MxN2VhNTY4YWNlYTZkODdkYzZmNGZkMzZlOTk3MmM2
|
14
|
+
ZmM3ODE4ZjE4ZTU4ODhlNTdmZWQxMzYxYTJhOWRlODg3ZmM4NzVhZDk5ZWVh
|
15
|
+
NDI1ZDY5N2I1MmNkNjBiYzQ0MzdiYTY1ODQxYThhOWM5ZmRkZTQ=
|
data/lib/schema_tools/cleaner.rb
CHANGED
@@ -18,7 +18,7 @@ module SchemaTools
|
|
18
18
|
schema = SchemaTools::Reader.read(obj_name)
|
19
19
|
setters = []
|
20
20
|
# gather allowed properties
|
21
|
-
schema[:properties].each{ |k,v| setters << k if !v['
|
21
|
+
schema[:properties].each{ |k,v| setters << k if !v['readOnly'] }
|
22
22
|
setters += opts[:keep] if opts[:keep] && opts[:keep].is_a?(Array)
|
23
23
|
# kick readonly
|
24
24
|
params.delete_if { |k,v| !setters.include?("#{k}") }
|
@@ -52,7 +52,7 @@ module SchemaTools
|
|
52
52
|
include SchemaTools::Modules::Validations # +naming + transl + conversion
|
53
53
|
has_schema_attrs schema['name'], reader: reader
|
54
54
|
validate_with schema['name'], reader:reader
|
55
|
-
getter_names = schema['properties'].select{|name,prop| !prop['
|
55
|
+
getter_names = schema['properties'].select{|name,prop| !prop['readOnly'] }
|
56
56
|
.keys.map { |name| name.to_sym}
|
57
57
|
attr_accessor *getter_names
|
58
58
|
|
@@ -40,7 +40,7 @@ module SchemaTools
|
|
40
40
|
# make getter / setter methods
|
41
41
|
self.schema[:properties].each do |key, prop|
|
42
42
|
define_method(key) { schema_attrs[key] }
|
43
|
-
define_method("#{key}=") { |value| schema_attrs[key] = value } unless prop[
|
43
|
+
define_method("#{key}=") { |value| schema_attrs[key] = value } unless prop['readOnly']
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -34,27 +34,28 @@ module SchemaTools
|
|
34
34
|
schema = reader.read(schema, opts[:path])
|
35
35
|
# create validation methods
|
36
36
|
schema[:properties].each do |key, val|
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
is_required = schema['required'] && schema['required'].include?("#{key}")
|
38
|
+
validates_length_of key, validate_length_opts(val, is_required) if val['maxLength'] || val['minLength']
|
39
|
+
validates_presence_of key if is_required
|
40
|
+
validates_numericality_of key, validate_number_opts(val, is_required) if val['type'] == 'number' || val['type'] == 'integer'
|
40
41
|
#TODO array minItems, max unique, null, string
|
41
42
|
# format: date-time, regex color style, email,uri, ..
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
45
|
-
def validate_length_opts(attr)
|
46
|
+
def validate_length_opts(attr, is_required=false)
|
46
47
|
opts = {}
|
47
48
|
opts[:within] = attr['minLength']..attr['maxLength'] if attr['minLength'] && attr['maxLength']
|
48
49
|
opts[:maximum] = attr['maxLength'] if attr['maxLength'] && !attr['minLength']
|
49
50
|
opts[:minimum] = attr['minLength'] if attr['minLength'] && !attr['maxLength']
|
50
|
-
opts[:allow_blank] = true
|
51
|
+
opts[:allow_blank] = true unless is_required
|
51
52
|
opts
|
52
53
|
end
|
53
54
|
|
54
55
|
# @param [Hash<String>] attr property values
|
55
|
-
def validate_number_opts(attr)
|
56
|
+
def validate_number_opts(attr, is_required=false)
|
56
57
|
opts = {}
|
57
|
-
opts[:allow_blank] = true
|
58
|
+
opts[:allow_blank] = true unless is_required
|
58
59
|
# those vals should not be set both in one property
|
59
60
|
opts[:greater_than_or_equal_to] = attr['minimum'] if attr['minimum'].present?
|
60
61
|
opts[:less_than_or_equal_to] = attr['maximum'] if attr['maximum'].present?
|
data/lib/schema_tools/version.rb
CHANGED
@@ -2,18 +2,18 @@
|
|
2
2
|
"title": "address",
|
3
3
|
"name": "address",
|
4
4
|
"description":"An address .. Example partially taken from SalesKing",
|
5
|
+
"required": ["city"],
|
5
6
|
"properties":{
|
6
7
|
"id":{
|
7
8
|
"description":"Unique identifier - UUID",
|
8
9
|
"identity":true,
|
9
|
-
"
|
10
|
+
"readOnly":true,
|
10
11
|
"type":"string",
|
11
12
|
"maxLength": 22,
|
12
13
|
"minLength":22
|
13
14
|
},
|
14
15
|
"city":{
|
15
16
|
"description": "City for the address. Must at least be present for an address.",
|
16
|
-
"required":true,
|
17
17
|
"type":"string",
|
18
18
|
"maxLength": 100
|
19
19
|
},
|
@@ -2,11 +2,12 @@
|
|
2
2
|
"title": "client",
|
3
3
|
"name": "client",
|
4
4
|
"description": "A example client.",
|
5
|
+
"required": ["organisation"],
|
5
6
|
"properties":{
|
6
7
|
"id":{
|
7
8
|
"description":"Unique identifier - UUID",
|
8
9
|
"identity":true,
|
9
|
-
"
|
10
|
+
"readOnly":true,
|
10
11
|
"type":"string",
|
11
12
|
"maxLength": 22,
|
12
13
|
"minLength":22
|
@@ -18,7 +19,6 @@
|
|
18
19
|
},
|
19
20
|
"organisation":{
|
20
21
|
"description": "Name of a company. This or lastname must be present",
|
21
|
-
"required" : true,
|
22
22
|
"type":"string",
|
23
23
|
"maxLength": 100
|
24
24
|
},
|
@@ -40,13 +40,13 @@
|
|
40
40
|
"created_at":{
|
41
41
|
"description": "Date the record was created in SK. Never changes afterwards.",
|
42
42
|
"format":"date-time",
|
43
|
-
"
|
43
|
+
"readOnly":true,
|
44
44
|
"type":"string"
|
45
45
|
},
|
46
46
|
"updated_at":{
|
47
47
|
"description": "Last date when the record was edited.",
|
48
48
|
"format":"date-time",
|
49
|
-
"
|
49
|
+
"readOnly":true,
|
50
50
|
"type":"string"
|
51
51
|
},
|
52
52
|
"phone_mobile":{
|
@@ -2,11 +2,12 @@
|
|
2
2
|
"title": "Contact",
|
3
3
|
"name": "contact",
|
4
4
|
"description": "A simple contact",
|
5
|
+
"required": ["organisation"],
|
5
6
|
"properties":{
|
6
7
|
"id":{
|
7
8
|
"description":"Unique identifier ",
|
8
9
|
"identity":true,
|
9
|
-
"
|
10
|
+
"readOnly":true,
|
10
11
|
"type":"number"
|
11
12
|
},
|
12
13
|
"contact_source":{
|
@@ -15,7 +16,6 @@
|
|
15
16
|
},
|
16
17
|
"organisation":{
|
17
18
|
"description": "Name of a company. ",
|
18
|
-
"required" : true,
|
19
19
|
"type":"string",
|
20
20
|
"maxLength": 100
|
21
21
|
},
|
@@ -7,22 +7,22 @@
|
|
7
7
|
"id": {
|
8
8
|
"description": "The object identifier.",
|
9
9
|
"identity": true,
|
10
|
-
"
|
10
|
+
"readOnly": true,
|
11
11
|
"type": "integer"
|
12
12
|
},
|
13
13
|
"number": {
|
14
14
|
"description": "Number of this page inside the document",
|
15
|
-
"
|
15
|
+
"readOnly": true,
|
16
16
|
"type": "integer"
|
17
17
|
},
|
18
18
|
"related_object_id": {
|
19
19
|
"description": "ID of the PDF object the page belongs to.",
|
20
|
-
"
|
20
|
+
"readOnly": true,
|
21
21
|
"type": "integer"
|
22
22
|
},
|
23
23
|
"related_object_type": {
|
24
24
|
"description": "Type of the related object Pdf or Pdt (camelCased class name)",
|
25
|
-
"
|
25
|
+
"readOnly": true,
|
26
26
|
"type": "string",
|
27
27
|
"maxlength": 20
|
28
28
|
},
|
@@ -32,19 +32,19 @@
|
|
32
32
|
},
|
33
33
|
"created_at": {
|
34
34
|
"description": "Creation date.",
|
35
|
-
"
|
35
|
+
"readOnly": true,
|
36
36
|
"type": "string",
|
37
37
|
"format": "date-time"
|
38
38
|
},
|
39
39
|
"updated_at": {
|
40
40
|
"description": "Update date.",
|
41
|
-
"
|
41
|
+
"readOnly": true,
|
42
42
|
"type": "string",
|
43
43
|
"format": "date-time"
|
44
44
|
},
|
45
45
|
"user_id": {
|
46
46
|
"description": "User who created the object.",
|
47
|
-
"
|
47
|
+
"readOnly": true,
|
48
48
|
"type": "integer"
|
49
49
|
},
|
50
50
|
"screenshot": {
|
@@ -28,7 +28,7 @@ describe SchemaTools::Modules::Attributes do
|
|
28
28
|
subject.should respond_to('first_name=')
|
29
29
|
end
|
30
30
|
|
31
|
-
it 'should not add setter for
|
31
|
+
it 'should not add setter for readOnly properties' do
|
32
32
|
subject.should_not respond_to('id=')
|
33
33
|
subject.should_not respond_to('created_at=')
|
34
34
|
end
|
@@ -144,7 +144,7 @@ describe SchemaTools::Modules::Attributes do
|
|
144
144
|
subject.should respond_to('numbers=')
|
145
145
|
end
|
146
146
|
|
147
|
-
it 'should not add setter for
|
147
|
+
it 'should not add setter for readOnly properties' do
|
148
148
|
subject.should_not respond_to('id=')
|
149
149
|
end
|
150
150
|
end
|
data/spec/test_helpers.rb
CHANGED
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.6.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: 2015-
|
11
|
+
date: 2015-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|