membrane 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +1 -0
  2. data/.travis.yml +6 -0
  3. data/LICENSE +58 -6869
  4. data/NOTICE +10 -0
  5. data/README.md +130 -30
  6. data/lib/membrane/schema_parser.rb +25 -24
  7. data/lib/membrane/schemas/any.rb +8 -0
  8. data/lib/membrane/{schema → schemas}/base.rb +1 -6
  9. data/lib/membrane/schemas/bool.rb +31 -0
  10. data/lib/membrane/schemas/class.rb +35 -0
  11. data/lib/membrane/schemas/dictionary.rb +69 -0
  12. data/lib/membrane/schemas/enum.rb +49 -0
  13. data/lib/membrane/schemas/list.rb +63 -0
  14. data/lib/membrane/schemas/record.rb +96 -0
  15. data/lib/membrane/schemas/regexp.rb +60 -0
  16. data/lib/membrane/schemas/tuple.rb +90 -0
  17. data/lib/membrane/schemas/value.rb +37 -0
  18. data/lib/membrane/schemas.rb +5 -0
  19. data/lib/membrane/version.rb +1 -1
  20. data/lib/membrane.rb +1 -1
  21. data/spec/schema_parser_spec.rb +79 -75
  22. data/spec/{any_schema_spec.rb → schemas/any_spec.rb} +2 -2
  23. data/spec/{base_schema_spec.rb → schemas/base_spec.rb} +2 -2
  24. data/spec/{bool_schema_spec.rb → schemas/bool_spec.rb} +2 -3
  25. data/spec/{class_schema_spec.rb → schemas/class_spec.rb} +2 -2
  26. data/spec/{dictionary_schema_spec.rb → schemas/dictionary_spec.rb} +11 -11
  27. data/spec/{enum_schema_spec.rb → schemas/enum_spec.rb} +4 -4
  28. data/spec/{list_schema_spec.rb → schemas/list_spec.rb} +8 -8
  29. data/spec/schemas/record_spec.rb +108 -0
  30. data/spec/{regexp_schema_spec.rb → schemas/regexp_spec.rb} +2 -2
  31. data/spec/{tuple_schema_spec.rb → schemas/tuple_spec.rb} +4 -4
  32. data/spec/{value_schema_spec.rb → schemas/value_spec.rb} +2 -2
  33. metadata +37 -35
  34. data/lib/membrane/schema/any.rb +0 -13
  35. data/lib/membrane/schema/bool.rb +0 -20
  36. data/lib/membrane/schema/class.rb +0 -24
  37. data/lib/membrane/schema/dictionary.rb +0 -40
  38. data/lib/membrane/schema/enum.rb +0 -30
  39. data/lib/membrane/schema/list.rb +0 -37
  40. data/lib/membrane/schema/record.rb +0 -45
  41. data/lib/membrane/schema/regexp.rb +0 -29
  42. data/lib/membrane/schema/tuple.rb +0 -48
  43. data/lib/membrane/schema/value.rb +0 -22
  44. data/lib/membrane/schema.rb +0 -17
  45. data/spec/record_schema_spec.rb +0 -56
@@ -1,29 +1,29 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Membrane::Schema::List do
3
+ describe Membrane::Schemas::List do
4
4
  describe "#validate" do
5
5
  it "should return an error if the validated object isn't an array" do
6
- schema = Membrane::Schema::List.new(nil)
6
+ schema = Membrane::Schemas::List.new(nil)
7
7
 
8
8
  expect_validation_failure(schema, "hi", /instance of Array/)
9
9
  end
10
10
 
11
11
  it "should invoke validate each list item against the supplied schema" do
12
- item_schema = mock("item_schema")
12
+ item_schema = double("item_schema")
13
13
 
14
14
  data = [0, 1, 2]
15
15
 
16
16
  data.each { |x| item_schema.should_receive(:validate).with(x) }
17
17
 
18
- list_schema = Membrane::Schema::List.new(item_schema)
18
+ list_schema = Membrane::Schemas::List.new(item_schema)
19
19
 
20
20
  list_schema.validate(data)
21
21
  end
22
22
  end
23
23
 
24
24
  it "should return an error if any items fail to validate" do
25
- item_schema = Membrane::Schema::Class.new(Integer)
26
- list_schema = Membrane::Schema::List.new(item_schema)
25
+ item_schema = Membrane::Schemas::Class.new(Integer)
26
+ list_schema = Membrane::Schemas::List.new(item_schema)
27
27
 
28
28
  errors = nil
29
29
 
@@ -38,8 +38,8 @@ describe Membrane::Schema::List do
38
38
  end
39
39
 
40
40
  it "should return nil if all items validate" do
41
- item_schema = Membrane::Schema::Class.new(Integer)
42
- list_schema = Membrane::Schema::List.new(item_schema)
41
+ item_schema = Membrane::Schemas::Class.new(Integer)
42
+ list_schema = Membrane::Schemas::List.new(item_schema)
43
43
 
44
44
  list_schema.validate([1, 2, 3]).should be_nil
45
45
  end
@@ -0,0 +1,108 @@
1
+ require "spec_helper"
2
+
3
+ describe Membrane::Schemas::Record do
4
+ describe "#validate" do
5
+ it "should return an error if the validated object isn't a hash" do
6
+ schema = Membrane::Schemas::Record.new(nil)
7
+
8
+ expect_validation_failure(schema, "test", /instance of Hash/)
9
+ end
10
+
11
+ it "should return an error for missing keys" do
12
+ key_schemas = { "foo" => Membrane::Schemas::Any.new }
13
+ rec_schema = Membrane::Schemas::Record.new(key_schemas)
14
+
15
+ expect_validation_failure(rec_schema, {}, /foo => Missing/)
16
+ end
17
+
18
+ it "should validate the value for each key" do
19
+ data = {
20
+ "foo" => 1,
21
+ "bar" => 2,
22
+ }
23
+
24
+ key_schemas = {
25
+ "foo" => double("foo"),
26
+ "bar" => double("bar"),
27
+ }
28
+
29
+ key_schemas.each { |k, m| m.should_receive(:validate).with(data[k]) }
30
+
31
+ rec_schema = Membrane::Schemas::Record.new(key_schemas)
32
+
33
+ rec_schema.validate(data)
34
+ end
35
+
36
+ it "should return all errors for keys or values that didn't validate" do
37
+ key_schemas = {
38
+ "foo" => Membrane::Schemas::Any.new,
39
+ "bar" => Membrane::Schemas::Class.new(String),
40
+ }
41
+
42
+ rec_schema = Membrane::Schemas::Record.new(key_schemas)
43
+
44
+ errors = nil
45
+
46
+ begin
47
+ rec_schema.validate({ "bar" => 2 })
48
+ rescue Membrane::SchemaValidationError => e
49
+ errors = e.to_s
50
+ end
51
+
52
+ errors.should match(/foo => Missing key/)
53
+ errors.should match(/bar/)
54
+ end
55
+
56
+ context "when strict checking" do
57
+ it "raises an error if there are extra keys that are not matched in the schema" do
58
+ data = {
59
+ "key" => "value",
60
+ "other_key" => 2,
61
+ }
62
+
63
+ rec_schema = Membrane::Schemas::Record.new({
64
+ "key" => Membrane::Schemas::Class.new(String)
65
+ }, [], true)
66
+
67
+ expect {
68
+ rec_schema.validate(data)
69
+ }.to raise_error(/other_key .* was not specified/)
70
+ end
71
+ end
72
+
73
+ context "when not strict checking" do
74
+ it "doesnt raise an error" do
75
+ data = {
76
+ "key" => "value",
77
+ "other_key" => 2,
78
+ }
79
+
80
+ rec_schema = Membrane::Schemas::Record.new({
81
+ "key" => Membrane::Schemas::Class.new(String)
82
+ })
83
+
84
+ expect {
85
+ rec_schema.validate(data)
86
+ }.to_not raise_error
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "#parse" do
92
+ it "allows chaining/inheritance of schemas" do
93
+ base_schema = Membrane::SchemaParser.parse{{
94
+ "key" => String
95
+ }}
96
+
97
+ specific_schema = base_schema.parse{{
98
+ "another_key" => String
99
+ }}
100
+
101
+ input_hash = {
102
+ "key" => "value",
103
+ "another_key" => "another value",
104
+ }
105
+ specific_schema.validate(input_hash).should == nil
106
+ end
107
+ end
108
+ end
@@ -1,7 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Membrane::Schema::Regexp do
4
- let(:schema) { Membrane::Schema::Regexp.new(/bar/) }
3
+ describe Membrane::Schemas::Regexp do
4
+ let(:schema) { Membrane::Schemas::Regexp.new(/bar/) }
5
5
 
6
6
  describe "#validate" do
7
7
  it "should raise an error if the validated object isn't a string" do
@@ -1,10 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Membrane::Schema::Tuple do
3
+ describe Membrane::Schemas::Tuple do
4
4
  let(:schema) do
5
- Membrane::Schema::Tuple.new(Membrane::Schema::Class.new(String),
6
- Membrane::Schema::ANY,
7
- Membrane::Schema::Class.new(Integer))
5
+ Membrane::Schemas::Tuple.new(Membrane::Schemas::Class.new(String),
6
+ Membrane::Schemas::Any.new,
7
+ Membrane::Schemas::Class.new(Integer))
8
8
  end
9
9
 
10
10
  describe "#validate" do
@@ -1,9 +1,9 @@
1
1
  require "spec_helper"
2
2
 
3
3
 
4
- describe Membrane::Schema::Value do
4
+ describe Membrane::Schemas::Value do
5
5
  describe "#validate" do
6
- let(:schema) { Membrane::Schema::Value.new("test") }
6
+ let(:schema) { Membrane::Schemas::Value.new("test") }
7
7
 
8
8
  it "should return nil for values that are equal" do
9
9
  schema.validate("test").should be_nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: membrane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -68,41 +68,43 @@ extensions: []
68
68
  extra_rdoc_files: []
69
69
  files:
70
70
  - .gitignore
71
+ - .travis.yml
71
72
  - Gemfile
72
73
  - LICENSE
74
+ - NOTICE
73
75
  - README.md
74
76
  - Rakefile
75
77
  - lib/membrane.rb
76
78
  - lib/membrane/errors.rb
77
- - lib/membrane/schema.rb
78
- - lib/membrane/schema/any.rb
79
- - lib/membrane/schema/base.rb
80
- - lib/membrane/schema/bool.rb
81
- - lib/membrane/schema/class.rb
82
- - lib/membrane/schema/dictionary.rb
83
- - lib/membrane/schema/enum.rb
84
- - lib/membrane/schema/list.rb
85
- - lib/membrane/schema/record.rb
86
- - lib/membrane/schema/regexp.rb
87
- - lib/membrane/schema/tuple.rb
88
- - lib/membrane/schema/value.rb
89
79
  - lib/membrane/schema_parser.rb
80
+ - lib/membrane/schemas.rb
81
+ - lib/membrane/schemas/any.rb
82
+ - lib/membrane/schemas/base.rb
83
+ - lib/membrane/schemas/bool.rb
84
+ - lib/membrane/schemas/class.rb
85
+ - lib/membrane/schemas/dictionary.rb
86
+ - lib/membrane/schemas/enum.rb
87
+ - lib/membrane/schemas/list.rb
88
+ - lib/membrane/schemas/record.rb
89
+ - lib/membrane/schemas/regexp.rb
90
+ - lib/membrane/schemas/tuple.rb
91
+ - lib/membrane/schemas/value.rb
90
92
  - lib/membrane/version.rb
91
93
  - membrane.gemspec
92
- - spec/any_schema_spec.rb
93
- - spec/base_schema_spec.rb
94
- - spec/bool_schema_spec.rb
95
- - spec/class_schema_spec.rb
96
94
  - spec/complex_schema_spec.rb
97
- - spec/dictionary_schema_spec.rb
98
- - spec/enum_schema_spec.rb
99
- - spec/list_schema_spec.rb
100
- - spec/record_schema_spec.rb
101
- - spec/regexp_schema_spec.rb
102
95
  - spec/schema_parser_spec.rb
96
+ - spec/schemas/any_spec.rb
97
+ - spec/schemas/base_spec.rb
98
+ - spec/schemas/bool_spec.rb
99
+ - spec/schemas/class_spec.rb
100
+ - spec/schemas/dictionary_spec.rb
101
+ - spec/schemas/enum_spec.rb
102
+ - spec/schemas/list_spec.rb
103
+ - spec/schemas/record_spec.rb
104
+ - spec/schemas/regexp_spec.rb
105
+ - spec/schemas/tuple_spec.rb
106
+ - spec/schemas/value_spec.rb
103
107
  - spec/spec_helper.rb
104
- - spec/tuple_schema_spec.rb
105
- - spec/value_schema_spec.rb
106
108
  homepage: http://www.cloudfoundry.org
107
109
  licenses: []
108
110
  post_install_message:
@@ -128,17 +130,17 @@ signing_key:
128
130
  specification_version: 3
129
131
  summary: A DSL for validating data.
130
132
  test_files:
131
- - spec/any_schema_spec.rb
132
- - spec/base_schema_spec.rb
133
- - spec/bool_schema_spec.rb
134
- - spec/class_schema_spec.rb
135
133
  - spec/complex_schema_spec.rb
136
- - spec/dictionary_schema_spec.rb
137
- - spec/enum_schema_spec.rb
138
- - spec/list_schema_spec.rb
139
- - spec/record_schema_spec.rb
140
- - spec/regexp_schema_spec.rb
141
134
  - spec/schema_parser_spec.rb
135
+ - spec/schemas/any_spec.rb
136
+ - spec/schemas/base_spec.rb
137
+ - spec/schemas/bool_spec.rb
138
+ - spec/schemas/class_spec.rb
139
+ - spec/schemas/dictionary_spec.rb
140
+ - spec/schemas/enum_spec.rb
141
+ - spec/schemas/list_spec.rb
142
+ - spec/schemas/record_spec.rb
143
+ - spec/schemas/regexp_spec.rb
144
+ - spec/schemas/tuple_spec.rb
145
+ - spec/schemas/value_spec.rb
142
146
  - spec/spec_helper.rb
143
- - spec/tuple_schema_spec.rb
144
- - spec/value_schema_spec.rb
@@ -1,13 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Any < Membrane::Schema::Base
10
- def validate(object)
11
- nil
12
- end
13
- end
@@ -1,20 +0,0 @@
1
- require "set"
2
-
3
- require "membrane/errors"
4
- require "membrane/schema/base"
5
-
6
- module Membrane
7
- module Schema
8
- end
9
- end
10
-
11
- class Membrane::Schema::Bool < Membrane::Schema::Base
12
- TRUTH_VALUES = Set.new([true, false])
13
-
14
- def validate(object)
15
- if !TRUTH_VALUES.include?(object)
16
- emsg = "Expected instance of true or false, given #{object}"
17
- raise Membrane::SchemaValidationError.new(emsg)
18
- end
19
- end
20
- end
@@ -1,24 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Class < Membrane::Schema::Base
10
- attr_reader :klass
11
-
12
- def initialize(klass)
13
- @klass = klass
14
- end
15
-
16
- # Validates whether or not the supplied object is derived from klass
17
- def validate(object)
18
- if !object.kind_of?(@klass)
19
- emsg = "Expected instance of #{@klass}," \
20
- + " given an instance of #{object.class}"
21
- raise Membrane::SchemaValidationError.new(emsg)
22
- end
23
- end
24
- end
@@ -1,40 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Dictionary < Membrane::Schema::Base
10
- attr_reader :key_schema
11
- attr_reader :value_schema
12
-
13
- def initialize(key_schema, value_schema)
14
- @key_schema = key_schema
15
- @value_schema = value_schema
16
- end
17
-
18
- def validate(object)
19
- if !object.kind_of?(Hash)
20
- emsg = "Expected instance of Hash, given instance of #{object.class}."
21
- raise Membrane::SchemaValidationError.new(emsg)
22
- end
23
-
24
- errors = {}
25
-
26
- object.each do |k, v|
27
- begin
28
- @key_schema.validate(k)
29
- @value_schema.validate(v)
30
- rescue Membrane::SchemaValidationError => e
31
- errors[k] = e.to_s
32
- end
33
- end
34
-
35
- if errors.size > 0
36
- emsg = "{ " + errors.map { |k, e| "#{k} => #{e}" }.join(", ") + " }"
37
- raise Membrane::SchemaValidationError.new(emsg)
38
- end
39
- end
40
- end
@@ -1,30 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Enum < Membrane::Schema::Base
10
- attr_reader :elem_schemas
11
-
12
- def initialize(*elem_schemas)
13
- @elem_schemas = elem_schemas
14
- @elem_schema_str = elem_schemas.map { |s| s.to_s }.join(", ")
15
- end
16
-
17
- def validate(object)
18
- @elem_schemas.each do |schema|
19
- begin
20
- schema.validate(object)
21
- return nil
22
- rescue Membrane::SchemaValidationError
23
- end
24
- end
25
-
26
- emsg = "Object #{object} doesn't validate" \
27
- + " against any of #{@elem_schema_str}"
28
- raise Membrane::SchemaValidationError.new(emsg)
29
- end
30
- end
@@ -1,37 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::List < Membrane::Schema::Base
10
- attr_reader :elem_schema
11
-
12
- def initialize(elem_schema)
13
- @elem_schema = elem_schema
14
- end
15
-
16
- def validate(object)
17
- if !object.kind_of?(Array)
18
- emsg = "Expected instance of Array, given instance of #{object.class}"
19
- raise Membrane::SchemaValidationError.new(emsg)
20
- end
21
-
22
- errors = {}
23
-
24
- object.each_with_index do |elem, ii|
25
- begin
26
- @elem_schema.validate(elem)
27
- rescue Membrane::SchemaValidationError => e
28
- errors[ii] = e.to_s
29
- end
30
- end
31
-
32
- if errors.size > 0
33
- emsg = errors.map { |ii, e| "At index #{ii}: #{e}" }.join(", ")
34
- raise Membrane::SchemaValidationError.new(emsg)
35
- end
36
- end
37
- end
@@ -1,45 +0,0 @@
1
- require "set"
2
-
3
- require "membrane/errors"
4
- require "membrane/schema/base"
5
-
6
- module Membrane
7
- module Schema
8
- end
9
- end
10
-
11
- class Membrane::Schema::Record < Membrane::Schema::Base
12
- attr_reader :schemas
13
- attr_reader :optional_keys
14
-
15
- def initialize(schemas, optional_keys = [])
16
- @optional_keys = Set.new(optional_keys)
17
- @schemas = schemas
18
- end
19
-
20
- def validate(object)
21
- unless object.kind_of?(Hash)
22
- emsg = "Expected instance of Hash, given instance of #{object.class}"
23
- raise Membrane::SchemaValidationError.new(emsg)
24
- end
25
-
26
- key_errors = {}
27
-
28
- @schemas.each do |k, schema|
29
- if object.has_key?(k)
30
- begin
31
- schema.validate(object[k])
32
- rescue Membrane::SchemaValidationError => e
33
- key_errors[k] = e.to_s
34
- end
35
- elsif !@optional_keys.include?(k)
36
- key_errors[k] = "Missing key"
37
- end
38
- end
39
-
40
- if key_errors.size > 0
41
- emsg = "{ " + key_errors.map { |k, e| "#{k} => #{e}" }.join(", ") + " }"
42
- raise Membrane::SchemaValidationError.new(emsg)
43
- end
44
- end
45
- end
@@ -1,29 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Regexp < Membrane::Schema::Base
10
- attr_reader :regexp
11
-
12
- def initialize(regexp)
13
- @regexp = regexp
14
- end
15
-
16
- def validate(object)
17
- if !object.kind_of?(String)
18
- emsg = "Expected instance of String, given instance of #{object.class}"
19
- raise Membrane::SchemaValidationError.new(emsg)
20
- end
21
-
22
- if !regexp.match(object)
23
- emsg = "Value #{object} doesn't match regexp #{@regexp}"
24
- raise Membrane::SchemaValidationError.new(emsg)
25
- end
26
-
27
- nil
28
- end
29
- end
@@ -1,48 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Tuple < Membrane::Schema::Base
10
- attr_reader :elem_schemas
11
-
12
- def initialize(*elem_schemas)
13
- @elem_schemas = elem_schemas
14
- end
15
-
16
- def validate(object)
17
- if !object.kind_of?(Array)
18
- emsg = "Expected instance of Array, given instance of #{object.class}"
19
- raise Membrane::SchemaValidationError.new(emsg)
20
- end
21
-
22
- expected = @elem_schemas.length
23
- actual = object.length
24
-
25
- if actual != expected
26
- emsg = "Expected #{expected} element(s), given #{actual}"
27
- raise Membrane::SchemaValidationError.new(emsg)
28
- end
29
-
30
- errors = {}
31
-
32
- @elem_schemas.each_with_index do |schema, ii|
33
- begin
34
- schema.validate(object[ii])
35
- rescue Membrane::SchemaValidationError => e
36
- errors[ii] = e
37
- end
38
- end
39
-
40
- if errors.size > 0
41
- emsg = "There were errors at the following indices: " \
42
- + errors.map { |ii, err| "#{ii} => #{err}" }.join(", ")
43
- raise Membrane::SchemaValidationError.new(emsg)
44
- end
45
-
46
- nil
47
- end
48
- end
@@ -1,22 +0,0 @@
1
- require "membrane/errors"
2
- require "membrane/schema/base"
3
-
4
- module Membrane
5
- module Schema
6
- end
7
- end
8
-
9
- class Membrane::Schema::Value < Membrane::Schema::Base
10
- attr_reader :value
11
-
12
- def initialize(value)
13
- @value = value
14
- end
15
-
16
- def validate(object)
17
- if object != @value
18
- emsg = "Expected #{@value}, given #{object}"
19
- raise Membrane::SchemaValidationError.new(emsg)
20
- end
21
- end
22
- end
@@ -1,17 +0,0 @@
1
- require "membrane/schema/any"
2
- require "membrane/schema/base"
3
- require "membrane/schema/bool"
4
- require "membrane/schema/class"
5
- require "membrane/schema/dictionary"
6
- require "membrane/schema/enum"
7
- require "membrane/schema/list"
8
- require "membrane/schema/record"
9
- require "membrane/schema/regexp"
10
- require "membrane/schema/tuple"
11
- require "membrane/schema/value"
12
-
13
- module Membrane
14
- module Schema
15
- ANY = Membrane::Schema::Any.new
16
- end
17
- end
@@ -1,56 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Membrane::Schema::Record do
4
- describe "#validate" do
5
- it "should return an error if the validated object isn't a hash" do
6
- schema = Membrane::Schema::Record.new(nil)
7
-
8
- expect_validation_failure(schema, "test", /instance of Hash/)
9
- end
10
-
11
- it "should return an error for missing keys" do
12
- key_schemas = { "foo" => Membrane::Schema::ANY }
13
- rec_schema = Membrane::Schema::Record.new(key_schemas)
14
-
15
- expect_validation_failure(rec_schema, {}, /foo => Missing/)
16
- end
17
-
18
- it "should validate the value for each key" do
19
- data = {
20
- "foo" => 1,
21
- "bar" => 2,
22
- }
23
-
24
- key_schemas = {
25
- "foo" => mock("foo"),
26
- "bar" => mock("bar"),
27
- }
28
-
29
- key_schemas.each { |k, m| m.should_receive(:validate).with(data[k]) }
30
-
31
- rec_schema = Membrane::Schema::Record.new(key_schemas)
32
-
33
- rec_schema.validate(data)
34
- end
35
-
36
- it "should return all errors for keys or values that didn't validate" do
37
- key_schemas = {
38
- "foo" => Membrane::Schema::ANY,
39
- "bar" => Membrane::Schema::Class.new(String),
40
- }
41
-
42
- rec_schema = Membrane::Schema::Record.new(key_schemas)
43
-
44
- errors = nil
45
-
46
- begin
47
- rec_schema.validate({ "bar" => 2 })
48
- rescue Membrane::SchemaValidationError => e
49
- errors = e.to_s
50
- end
51
-
52
- errors.should match(/foo => Missing key/)
53
- errors.should match(/bar/)
54
- end
55
- end
56
- end