json_schema 0.17.2 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 635babaf9176db9c16a7564e0cdb8804ba209201
4
- data.tar.gz: c866943726dbe55237d5c7931793d5ecf149945a
3
+ metadata.gz: 2d133299adf9a9c71dad5525df64e25d4846e35e
4
+ data.tar.gz: 4d9aed1582b80eefcfc12656dddaf65b77fd894a
5
5
  SHA512:
6
- metadata.gz: 5caf545cbf4684cad41d64fc889d5c167f2e4f3980b91014a7367fef4f070a4fd5b77b138f80f12f34b3d62fbae11bd0890a67d1a490228db59a4cccf9b1a263
7
- data.tar.gz: ff16d212597fbeab145f40023052e1e792014cd78c7ebafc65bd75af7c505879622a65cbbe037037ff287ceb3274288da603f6afcf21c79eb5628a4dd398ee8f
6
+ metadata.gz: 6f156d37e4067bae3bbd2e7a471f921d362afea8e2900c88c13278b383efd52a5f7fefd3129418c2dd406a0ec5a103b748aa7183238d7445e67dbbe969f63b4b
7
+ data.tar.gz: 9e118b7e172ed7c52b8b9cddd26a29e6a8e872e0f8782f6a57f34bdd323165b3996b4d405e8639441fb4a5f93fbb155f24b5fd59ecf98cfead00e4a49dee0ad6
@@ -43,10 +43,18 @@ module JsonSchema
43
43
  end
44
44
  end
45
45
  end
46
+
47
+ if options[:clear_cache]
48
+ remove_method(:"#{attr}=")
49
+ define_method(:"#{attr}=") do |value|
50
+ instance_variable_set(options[:clear_cache], nil)
51
+ instance_variable_set(ref, value)
52
+ end
53
+ end
46
54
  end
47
55
 
48
56
  def attr_schema(attr, options = {})
49
- attr_copyable(attr, :default => options[:default])
57
+ attr_copyable(attr, :default => options[:default], :clear_cache => options[:clear_cache])
50
58
  self.schema_attrs[options[:schema_name] || attr] = attr
51
59
  end
52
60
 
@@ -2,6 +2,16 @@ require "json"
2
2
 
3
3
  module JsonSchema
4
4
  class Schema
5
+ TYPE_MAP = {
6
+ "array" => Array,
7
+ "boolean" => [FalseClass, TrueClass],
8
+ "integer" => Integer,
9
+ "number" => [Integer, Float],
10
+ "null" => NilClass,
11
+ "object" => Hash,
12
+ "string" => String,
13
+ }
14
+
5
15
  include Attributes
6
16
 
7
17
  def initialize
@@ -129,7 +139,7 @@ module JsonSchema
129
139
  # of strings.
130
140
  #
131
141
  # Type: Array[String]
132
- attr_schema :type, :default => []
142
+ attr_schema :type, :default => [], :clear_cache => :@type_parsed
133
143
 
134
144
  # validation: array
135
145
  attr_schema :additional_items, :default => true, :schema_name => :additionalItems
@@ -199,6 +209,14 @@ module JsonSchema
199
209
  true
200
210
  end
201
211
 
212
+ # An array of Ruby classes that are equivalent to the types defined in the
213
+ # schema.
214
+ #
215
+ # Type: Array[Class]
216
+ def type_parsed
217
+ @type_parsed ||= type.flat_map { |t| TYPE_MAP[t] }.compact
218
+ end
219
+
202
220
  def inspect
203
221
  "\#<JsonSchema::Schema pointer=#{pointer}>"
204
222
  end
@@ -2,16 +2,6 @@ require "uri"
2
2
 
3
3
  module JsonSchema
4
4
  class Validator
5
- TYPE_MAP = {
6
- "array" => Array,
7
- "boolean" => [FalseClass, TrueClass],
8
- "integer" => Integer,
9
- "number" => [Integer, Float],
10
- "null" => NilClass,
11
- "object" => Hash,
12
- "string" => String,
13
- }
14
-
15
5
  attr_accessor :errors
16
6
 
17
7
  def initialize(schema)
@@ -512,8 +502,7 @@ module JsonSchema
512
502
 
513
503
  def validate_type(schema, data, errors, path)
514
504
  return true if !schema.type || schema.type.empty?
515
- valid_types = schema.type.flat_map { |t| TYPE_MAP[t] }.compact
516
- if valid_types.any? { |t| data.is_a?(t) }
505
+ if schema.type_parsed.include?(data.class)
517
506
  true
518
507
  else
519
508
  key = find_parent(schema)
@@ -5,7 +5,7 @@ require "json_reference"
5
5
  describe JsonReference::Reference do
6
6
  it "expands a reference without a URI" do
7
7
  ref = reference("#/definitions")
8
- assert_equal nil, ref.uri
8
+ assert_nil ref.uri
9
9
  assert_equal "#/definitions", ref.pointer
10
10
  end
11
11
 
@@ -17,7 +17,7 @@ describe JsonReference::Reference do
17
17
 
18
18
  it "expands just a root sign" do
19
19
  ref = reference("#")
20
- assert_equal nil, ref.uri
20
+ assert_nil ref.uri
21
21
  assert_equal "#", ref.pointer
22
22
  end
23
23
 
@@ -29,13 +29,13 @@ describe JsonReference::Reference do
29
29
 
30
30
  it "normalizes pointers by adding a root sign prefix" do
31
31
  ref = reference("/definitions")
32
- assert_equal nil, ref.uri
32
+ assert_nil ref.uri
33
33
  assert_equal "#/definitions", ref.pointer
34
34
  end
35
35
 
36
36
  it "normalizes pointers by stripping a trailing slash" do
37
37
  ref = reference("#/definitions/")
38
- assert_equal nil, ref.uri
38
+ assert_nil ref.uri
39
39
  assert_equal "#/definitions", ref.pointer
40
40
  end
41
41
 
@@ -14,7 +14,7 @@ describe JsonSchema::Attributes do
14
14
  obj = TestAttributes.new
15
15
  obj.schema = "foo"
16
16
  assert_equal "foo", obj.schema
17
- assert_equal({:schema => :schema, :named => :schema_named},
17
+ assert_equal({:schema => :schema, :named => :schema_named, :cached => :cached},
18
18
  obj.class.schema_attrs)
19
19
  end
20
20
 
@@ -48,7 +48,7 @@ describe JsonSchema::Attributes do
48
48
  obj.schema = "schema"
49
49
 
50
50
  assert_raises NoMethodError do
51
- assert_equal nil, obj[:copyable]
51
+ assert_nil obj[:copyable]
52
52
  end
53
53
 
54
54
  assert_equal "schema", obj[:schema]
@@ -71,8 +71,18 @@ describe JsonSchema::Attributes do
71
71
  obj = TestAttributes.new
72
72
 
73
73
  # should produce a nil value *without* a Ruby warning
74
- assert_equal nil, obj.copyable
75
- assert_equal nil, obj.schema
74
+ assert_nil obj.copyable
75
+ assert_nil obj.schema
76
+ end
77
+
78
+ it "cleans cached values when assigning parent attribute" do
79
+ obj = TestAttributes.new
80
+
81
+ obj.cached = "test"
82
+ assert_equal "test_123", obj.cached_parsed
83
+
84
+ obj.cached = "other"
85
+ assert_equal "other_123", obj.cached_parsed
76
86
  end
77
87
 
78
88
  class TestAttributes
@@ -87,6 +97,11 @@ describe JsonSchema::Attributes do
87
97
  attr_schema :schema
88
98
  attr_schema :schema_named, :schema_name => :named
89
99
 
100
+ attr_schema :cached, :clear_cache => :@cached_parsed
101
+ def cached_parsed
102
+ @cached_parsed ||= "#{cached}_123"
103
+ end
104
+
90
105
  attr_copyable :copyable_default, :default => []
91
106
  attr_copyable :copyable_default_with_string, :default => "application/json"
92
107
  attr_copyable :copyable_default_with_object, :default => {}
@@ -34,4 +34,13 @@ describe JsonSchema::Schema do
34
34
  schema[:expanded]
35
35
  end
36
36
  end
37
+
38
+ it "updates type_parsed when type is changed" do
39
+ schema = JsonSchema::Schema.new
40
+ schema.type = ["integer"]
41
+ assert_equal [Integer], schema.type_parsed
42
+
43
+ schema.type = ["string"]
44
+ assert_equal [String], schema.type_parsed
45
+ end
37
46
  end
@@ -7,7 +7,7 @@ describe JsonSchema do
7
7
  it "succeeds" do
8
8
  schema, errors = JsonSchema.parse(schema_sample)
9
9
  assert schema
10
- assert_equal nil, errors
10
+ assert_nil errors
11
11
  end
12
12
 
13
13
  it "returns errors on a parsing problem" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.2
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-21 00:00:00.000000000 Z
11
+ date: 2018-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecma-re-validator