json_schema 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,7 @@ module JsonSchema
20
20
 
21
21
  def validate(data)
22
22
  @errors = []
23
+ @visits = {}
23
24
  validate_data(@schema, data, @errors, ['#'])
24
25
  @errors.size == 0
25
26
  end
@@ -32,6 +33,19 @@ module JsonSchema
32
33
 
33
34
  private
34
35
 
36
+ def first_visit(schema, errors, path)
37
+ pointer = schema.pointer
38
+ if !@visits.key?(pointer) || !@visits[pointer].key?(path.join("/"))
39
+ @visits[pointer] ||= {}
40
+ @visits[pointer][path] = true
41
+ true
42
+ else
43
+ message = %{Validation loop detected.}
44
+ errors << ValidationError.new(schema, path, message)
45
+ false
46
+ end
47
+ end
48
+
35
49
  # for use with additionalProperties and strictProperties
36
50
  def get_extra_keys(schema, data)
37
51
  extra = data.keys - schema.properties.keys
@@ -53,6 +67,11 @@ module JsonSchema
53
67
  def validate_data(schema, data, errors, path)
54
68
  valid = true
55
69
 
70
+ # detect a validation loop
71
+ if !first_visit(schema, errors, path)
72
+ return false
73
+ end
74
+
56
75
  # validation: any
57
76
  valid = strict_and valid, validate_all_of(schema, data, errors, path)
58
77
  valid = strict_and valid, validate_any_of(schema, data, errors, path)
@@ -680,6 +680,15 @@ describe JsonSchema::Validator do
680
680
  assert_equal "#/visibility", @validator.errors[0].pointer
681
681
  end
682
682
 
683
+ it "handles a validation loop" do
684
+ pointer("#/definitions/app").merge!(
685
+ "not" => { "$ref" => "#/definitions/app" }
686
+ )
687
+ data_sample["visibility"] = "personal"
688
+ refute validate
689
+ assert_includes error_messages, %{Validation loop detected.}
690
+ end
691
+
683
692
  def data_sample
684
693
  @data_sample ||= DataScaffold.data_sample
685
694
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-27 00:00:00.000000000 Z
12
+ date: 2014-05-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: