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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTU2OGFjZGFhZGI4YzVlNGM4MGI5ZGI4MDI0MjhjMDFiNTc4MjMzYg==
4
+ ZWY5YzJhODNmZTc5NWFhNmI0NTM3N2I2Mzg3MzcwNGI0MDkxMTQzNg==
5
5
  data.tar.gz: !binary |-
6
- NzRkYTVkMjY2M2ZhYjJhMTA3MWUyMzU3MGZmZTQyOWJmODI1YzdkYQ==
6
+ Mzk5MmQxZGMyM2E2MDQ3MTU2MzE0ZWY3NmU1ODVmN2JjMDlhNGMzMQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjM2MzVlMTUwZDgzNDFhMmQyY2Q3ZTBlNWFkNjg4NDQ4NTBhNzNhNWQxY2Q3
10
- Mjg1NmUzYTkxYzM1MzEzOTNiYjg4ZTkyZDI3Y2M2MjQxYmUyZGQ4NGI4ZWVm
11
- YmE4YWU0OWZkNmU0NzM2MGI4NWI5ZTkxNTBmYjEzMDAyZWI2NWQ=
9
+ YmYzNWEzYzI2ZjQ5ZWFmYzNhM2I5ZjA2MTk1MTZhYmJlNzlmMmU2Njk1Yzc3
10
+ NGVlYzFkYjJhMzc2NWQxZmVhY2E2NTgyMDUwYmU4ODljNTExOGQ5YjJiMTI0
11
+ MWQ3NGJmZTZmNTY1NzFmMTMzZWJmOWNkY2NkODZkMTlmYjQ3ZmQ=
12
12
  data.tar.gz: !binary |-
13
- MjUyZjE5NDM2ZDE2MTlkZDIzZWRhOGQ5Y2FmOGFmMjQ2Yzg4YTVkMDZlODFj
14
- MDI1YmQ2ZTNhNzRhZWU3MWU5NDAxN2UwN2Q2YjIxZTlmMWI1ZWM4N2M4YmQw
15
- OTBkYmFjMzdlOGMxYzJiYWE2Nzk0ZWE1M2I1ZWFlMTJjNTJhMmQ=
13
+ MTEwM2Q4MTU5ZWE0N2MxN2VhNTY4YWNlYTZkODdkYzZmNGZkMzZlOTk3MmM2
14
+ ZmM3ODE4ZjE4ZTU4ODhlNTdmZWQxMzYxYTJhOWRlODg3ZmM4NzVhZDk5ZWVh
15
+ NDI1ZDY5N2I1MmNkNjBiYzQ0MzdiYTY1ODQxYThhOWM5ZmRkZTQ=
@@ -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['readonly'] }
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['readonly'] }
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[:readonly]
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
- validates_length_of key, validate_length_opts(val) if val['maxLength'] || val['minLength']
38
- validates_presence_of key if val['required'] || val['required'] == 'true'
39
- validates_numericality_of key, validate_number_opts(val) if val['type'] == 'number' || val['type'] == 'integer'
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 if !attr['required']
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?
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.5.3'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -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
- "readonly":true,
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,7 +2,7 @@
2
2
  "definitions": {
3
3
  "id": {
4
4
  "description": "some description",
5
- "readonly": true,
5
+ "readOnly": true,
6
6
  "type": "integer"
7
7
  },
8
8
  "some_other_property": {
@@ -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
- "readonly":true,
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
- "readonly":true,
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
- "readonly":true,
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
- "readonly":true,
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
  },
@@ -6,7 +6,7 @@
6
6
  "id":{
7
7
  "description":"Unique identifier ",
8
8
  "identity":true,
9
- "readonly":true,
9
+ "readOnly":true,
10
10
  "type":"number"
11
11
  },
12
12
  "last_name":{
@@ -7,22 +7,22 @@
7
7
  "id": {
8
8
  "description": "The object identifier.",
9
9
  "identity": true,
10
- "readonly": true,
10
+ "readOnly": true,
11
11
  "type": "integer"
12
12
  },
13
13
  "number": {
14
14
  "description": "Number of this page inside the document",
15
- "readonly": true,
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
- "readonly": true,
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
- "readonly": true,
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
- "readonly": true,
35
+ "readOnly": true,
36
36
  "type": "string",
37
37
  "format": "date-time"
38
38
  },
39
39
  "updated_at": {
40
40
  "description": "Update date.",
41
- "readonly": true,
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
- "readonly": true,
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 readonly properties' do
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 readonly properties' do
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
@@ -22,7 +22,7 @@ def schema_as_ruby_object
22
22
  },
23
23
  "id" => {
24
24
  "type" => "number",
25
- "readonly" => true
25
+ "readOnly" => true
26
26
  },
27
27
  "additionalProperties" => false
28
28
  }
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.3
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-01-08 00:00:00.000000000 Z
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json