jschema 0.1.0 → 0.1.1

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: 27963957079b8a36781b058fbfcc5a65d05bc982
4
- data.tar.gz: a095f424265697a18c1ba7b27359a4352393d06c
3
+ metadata.gz: d590fc0ec0125af0eddda1de53da9dacae3531ed
4
+ data.tar.gz: fac78348f9d9bc7a044b109e1c0402a6b572cf78
5
5
  SHA512:
6
- metadata.gz: 9868e28a5802ba5102e443adfdcdd1342371157a5aa77a7a6a0adfe84a6ed56024436c5ee9a232524f4c36d15a16a2a25fb53f51fb68067dfbaae85144c7138a
7
- data.tar.gz: 2455390ded90bb4a99f6f64cdc48a7bc29d2d4b55a3763b0e052fdb3b71555c092a3aeca3e068404763128893e39263b329182c2427c3856d779b0fc52571c03
6
+ metadata.gz: 3622b6dca13c53afe5454d08fead0af4aebcb3103f86f6d75c9119f79131ea2f6a7704d8f85f9da111adaa3b8d27328383a0663082d844073aaa7497bc4aed10
7
+ data.tar.gz: 744703a630e62f1aae528778c01332709fb83ea8383f4fbfe0a6c8346d6d5346a24c47eac6e45130b5a0eec3245a5d153bcc0e14793964ba3d593c3fc7f68c30
@@ -28,7 +28,7 @@ module JSchema
28
28
  end
29
29
 
30
30
  def validate(instance)
31
- if !applicable_types || applicable_types.include?(instance.class)
31
+ if !applicable_types || applicable_types.any? { |type| type === instance }
32
32
  validate_instance(instance)
33
33
  end
34
34
  end
@@ -24,7 +24,7 @@ module JSchema
24
24
  end
25
25
 
26
26
  def applicable_types
27
- [Fixnum, Bignum, Float, BigDecimal]
27
+ [Numeric]
28
28
  end
29
29
  end
30
30
  end
@@ -24,7 +24,7 @@ module JSchema
24
24
  end
25
25
 
26
26
  def applicable_types
27
- [Fixnum, Bignum, Float, BigDecimal]
27
+ [Numeric]
28
28
  end
29
29
  end
30
30
  end
@@ -26,7 +26,7 @@ module JSchema
26
26
  end
27
27
 
28
28
  def applicable_types
29
- [Fixnum, Bignum, Float, BigDecimal]
29
+ [Numeric]
30
30
  end
31
31
  end
32
32
  end
@@ -18,11 +18,11 @@ module JSchema
18
18
  end
19
19
 
20
20
  def validate_instance(instance)
21
- @required.each do |required_property|
22
- unless instance.key?(required_property)
23
- return "#{instance} must have property `#{required_property}`"
24
- end
25
- end and nil
21
+ missing_keys = @required - instance.keys
22
+ unless missing_keys.empty?
23
+ keys = missing_keys.map(&:inspect).join(', ')
24
+ "#{instance} must contain #{keys}"
25
+ end
26
26
  end
27
27
 
28
28
  def valid_required?(required)
@@ -17,11 +17,11 @@ module JSchema
17
17
  @json_types = Array(type)
18
18
  @ruby_classes = @json_types.map do |json_type|
19
19
  json_type_to_ruby_class(json_type)
20
- end.flatten.compact
20
+ end
21
21
  end
22
22
 
23
23
  def validate_instance(instance)
24
- unless @ruby_classes.one? { |type| instance.is_a?(type) }
24
+ unless @ruby_classes.one? { |type| type === instance }
25
25
  error_message(instance)
26
26
  end
27
27
  end
@@ -31,10 +31,10 @@ module JSchema
31
31
  when 'object' then Hash
32
32
  when 'null' then NilClass
33
33
  when 'string' then String
34
- when 'integer' then [Fixnum, Bignum]
34
+ when 'integer' then Integer
35
35
  when 'array' then Array
36
- when 'boolean' then [TrueClass, FalseClass]
37
- when 'number' then [Fixnum, Float, BigDecimal, Bignum]
36
+ when 'boolean' then Boolean
37
+ when 'number' then Numeric
38
38
  else invalid_schema('type', json_type)
39
39
  end
40
40
  end
@@ -52,6 +52,14 @@ module JSchema
52
52
 
53
53
  "#{instance.inspect} must be a #{types}"
54
54
  end
55
+
56
+ module Boolean
57
+ class << self
58
+ def ===(other)
59
+ other == true || other == false || super
60
+ end
61
+ end
62
+ end
55
63
  end
56
64
  end
57
65
  end
@@ -82,6 +82,16 @@ class TestSchema < Minitest::Test
82
82
  assert_equal schema_def_uri, definition.uri
83
83
  end
84
84
 
85
+ # TODO: Make it isolated.
86
+ def test_definitions
87
+ schema = JSchema::Schema.build(
88
+ 'definitions' => { 'schema1' => {} },
89
+ '$ref' => '#/definitions/schema1'
90
+ )
91
+
92
+ schema.valid?('')
93
+ end
94
+
85
95
  def test_that_exception_is_raised_when_schema_version_is_not_supported
86
96
  assert_raises(JSchema::InvalidSchema) do
87
97
  JSchema::Schema.build('$schema' => 'unsupported')
@@ -76,12 +76,19 @@ class TestSimpleValidator < Minitest::Test
76
76
  end
77
77
  end
78
78
 
79
- def test_applicable_types
79
+ def test_failing_validation_when_validator_is_applicable
80
80
  stub_validator [Fixnum], false do |vdr|
81
81
  refute vdr.valid?(0)
82
82
  end
83
83
  end
84
84
 
85
+ def test_failing_validation_when_instance_is_descendant_of_applicable_types
86
+ stub_validator [Hash], false do |vdr|
87
+ descendant_type = Class.new(Hash)
88
+ refute vdr.valid?(descendant_type.new)
89
+ end
90
+ end
91
+
85
92
  private
86
93
 
87
94
  def stub_validator(applicable_types, valid)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jschema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Papkovskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-22 00:00:00.000000000 Z
11
+ date: 2014-09-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Implementation of JSON Schema Draft 4
14
14
  email: konstantin@papkovskiy.com