schemacop 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +11 -6
  4. data/VERSION +1 -1
  5. data/doc/Schemacop.html +1 -1
  6. data/doc/Schemacop/ArrayValidator.html +1 -1
  7. data/doc/Schemacop/BooleanValidator.html +1 -1
  8. data/doc/Schemacop/Collector.html +206 -30
  9. data/doc/Schemacop/Exceptions.html +1 -1
  10. data/doc/Schemacop/Exceptions/InvalidSchemaError.html +1 -1
  11. data/doc/Schemacop/Exceptions/ValidationError.html +1 -1
  12. data/doc/Schemacop/FieldNode.html +1 -1
  13. data/doc/Schemacop/FloatValidator.html +1 -1
  14. data/doc/Schemacop/HashValidator.html +1 -1
  15. data/doc/Schemacop/IntegerValidator.html +1 -1
  16. data/doc/Schemacop/NilValidator.html +1 -1
  17. data/doc/Schemacop/Node.html +1 -1
  18. data/doc/Schemacop/NodeResolver.html +1 -1
  19. data/doc/Schemacop/NodeSupportingField.html +1 -1
  20. data/doc/Schemacop/NodeSupportingType.html +1 -1
  21. data/doc/Schemacop/NodeWithBlock.html +1 -1
  22. data/doc/Schemacop/NumberValidator.html +1 -1
  23. data/doc/Schemacop/ObjectValidator.html +18 -8
  24. data/doc/Schemacop/RootNode.html +1 -1
  25. data/doc/Schemacop/Schema.html +2 -2
  26. data/doc/Schemacop/StringValidator.html +1 -1
  27. data/doc/Schemacop/SymbolValidator.html +1 -1
  28. data/doc/ScopedEnv.html +1 -1
  29. data/doc/_index.html +1 -1
  30. data/doc/file.README.html +25 -18
  31. data/doc/index.html +25 -18
  32. data/doc/method_list.html +19 -3
  33. data/doc/top-level-namespace.html +1 -1
  34. data/lib/schemacop/collector.rb +21 -2
  35. data/lib/schemacop/node_supporting_field.rb +0 -2
  36. data/lib/schemacop/schema.rb +1 -1
  37. data/lib/schemacop/validator/object_validator.rb +7 -1
  38. data/schemacop.gemspec +5 -5
  39. data/test/collector_test.rb +45 -0
  40. data/test/short_forms_test.rb +15 -0
  41. data/test/validator_object_test.rb +18 -0
  42. metadata +5 -3
@@ -1,21 +1,31 @@
1
1
  module Schemacop
2
2
  class Collector
3
3
  attr_reader :current_path
4
+ attr_reader :exceptions
4
5
 
5
6
  def initialize
6
7
  @exceptions = []
7
8
  @current_path = []
9
+ @ignore_next_segment = false
8
10
  end
9
11
 
10
12
  def valid?
11
13
  @exceptions.empty?
12
14
  end
13
15
 
16
+ # Construct the current path
14
17
  def path(segment)
15
- @current_path << segment
18
+ ignore_this_segment = false
19
+ if @ignore_next_segment
20
+ ignore_this_segment = true
21
+ @ignore_next_segment = false
22
+ end
23
+
24
+ @current_path << segment unless ignore_this_segment
25
+
16
26
  yield
17
27
  ensure
18
- @current_path.pop
28
+ @current_path.pop unless ignore_this_segment
19
29
  end
20
30
 
21
31
  def exception_message
@@ -30,5 +40,14 @@ module Schemacop
30
40
  message: error_msg
31
41
  }
32
42
  end
43
+
44
+ # Does not include the path segment next time {Schemacop::Collector.path} is
45
+ # called.
46
+ #
47
+ # @return [Schemacop::Collector]
48
+ def ignore_next_segment
49
+ @ignore_next_segment = true
50
+ return self
51
+ end
33
52
  end
34
53
  end
@@ -38,8 +38,6 @@ module Schemacop
38
38
  protected
39
39
 
40
40
  def field(*args, required:, allow_nil:, &block)
41
- # name = args.shift
42
- # options = args.last.is_a?(Hash) ? args.pop : {}
43
41
  name = args.shift
44
42
 
45
43
  if @fields[name]
@@ -38,7 +38,7 @@ module Schemacop
38
38
  # throughout the validation.
39
39
  def validate(data)
40
40
  collector = Collector.new
41
- @root.fields[:root].validate({ root: data }, collector)
41
+ @root.fields[:root].validate({ root: data }, collector.ignore_next_segment)
42
42
  return collector
43
43
  end
44
44
 
@@ -3,13 +3,19 @@ module Schemacop
3
3
  register symbols: :object, klasses: Object
4
4
 
5
5
  option :classes
6
+ option :strict
6
7
 
7
8
  def type_label
8
9
  "#{super} (#{classes.join(', ')})"
9
10
  end
10
11
 
11
12
  def type_matches?(data)
12
- super && (classes.empty? || classes.include?(data.class)) && !data.nil?
13
+ if option(:strict).is_a?(FalseClass)
14
+ sub_or_class = classes.map { |klass| data.class <= klass }.include?(true)
15
+ super && (classes.empty? || sub_or_class) && !data.nil?
16
+ else
17
+ super && (classes.empty? || classes.include?(data.class)) && !data.nil?
18
+ end
13
19
  end
14
20
 
15
21
  private
@@ -1,20 +1,20 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 2.2.0 ruby lib
2
+ # stub: schemacop 2.3.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "2.2.0"
6
+ s.version = "2.3.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Sitrox".freeze]
11
- s.date = "2017-05-17"
12
- s.files = [".gitignore".freeze, ".releaser_config".freeze, ".rubocop.yml".freeze, ".travis.yml".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "doc/Schemacop.html".freeze, "doc/Schemacop/ArrayValidator.html".freeze, "doc/Schemacop/BooleanValidator.html".freeze, "doc/Schemacop/Collector.html".freeze, "doc/Schemacop/Exceptions.html".freeze, "doc/Schemacop/Exceptions/InvalidSchemaError.html".freeze, "doc/Schemacop/Exceptions/ValidationError.html".freeze, "doc/Schemacop/FieldNode.html".freeze, "doc/Schemacop/FloatValidator.html".freeze, "doc/Schemacop/HashValidator.html".freeze, "doc/Schemacop/IntegerValidator.html".freeze, "doc/Schemacop/NilValidator.html".freeze, "doc/Schemacop/Node.html".freeze, "doc/Schemacop/NodeResolver.html".freeze, "doc/Schemacop/NodeSupportingField.html".freeze, "doc/Schemacop/NodeSupportingType.html".freeze, "doc/Schemacop/NodeWithBlock.html".freeze, "doc/Schemacop/NumberValidator.html".freeze, "doc/Schemacop/ObjectValidator.html".freeze, "doc/Schemacop/RootNode.html".freeze, "doc/Schemacop/Schema.html".freeze, "doc/Schemacop/StringValidator.html".freeze, "doc/Schemacop/SymbolValidator.html".freeze, "doc/ScopedEnv.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/inheritance.graphml".freeze, "doc/inheritance.pdf".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/collector.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/field_node.rb".freeze, "lib/schemacop/node.rb".freeze, "lib/schemacop/node_resolver.rb".freeze, "lib/schemacop/node_supporting_field.rb".freeze, "lib/schemacop/node_supporting_type.rb".freeze, "lib/schemacop/node_with_block.rb".freeze, "lib/schemacop/root_node.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/validator/array_validator.rb".freeze, "lib/schemacop/validator/boolean_validator.rb".freeze, "lib/schemacop/validator/float_validator.rb".freeze, "lib/schemacop/validator/hash_validator.rb".freeze, "lib/schemacop/validator/integer_validator.rb".freeze, "lib/schemacop/validator/nil_validator.rb".freeze, "lib/schemacop/validator/number_validator.rb".freeze, "lib/schemacop/validator/object_validator.rb".freeze, "lib/schemacop/validator/string_validator.rb".freeze, "lib/schemacop/validator/symbol_validator.rb".freeze, "schemacop.gemspec".freeze, "test/custom_check_test.rb".freeze, "test/custom_if_test.rb".freeze, "test/nil_dis_allow_test.rb".freeze, "test/short_forms_test.rb".freeze, "test/test_helper.rb".freeze, "test/types_test.rb".freeze, "test/validator_array_test.rb".freeze, "test/validator_boolean_test.rb".freeze, "test/validator_float_test.rb".freeze, "test/validator_hash_test.rb".freeze, "test/validator_integer_test.rb".freeze, "test/validator_nil_test.rb".freeze, "test/validator_number_test.rb".freeze, "test/validator_object_test.rb".freeze, "test/validator_string_test.rb".freeze, "test/validator_symbol_test.rb".freeze]
11
+ s.date = "2017-05-18"
12
+ s.files = [".gitignore".freeze, ".releaser_config".freeze, ".rubocop.yml".freeze, ".travis.yml".freeze, ".yardopts".freeze, "CHANGELOG.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "doc/Schemacop.html".freeze, "doc/Schemacop/ArrayValidator.html".freeze, "doc/Schemacop/BooleanValidator.html".freeze, "doc/Schemacop/Collector.html".freeze, "doc/Schemacop/Exceptions.html".freeze, "doc/Schemacop/Exceptions/InvalidSchemaError.html".freeze, "doc/Schemacop/Exceptions/ValidationError.html".freeze, "doc/Schemacop/FieldNode.html".freeze, "doc/Schemacop/FloatValidator.html".freeze, "doc/Schemacop/HashValidator.html".freeze, "doc/Schemacop/IntegerValidator.html".freeze, "doc/Schemacop/NilValidator.html".freeze, "doc/Schemacop/Node.html".freeze, "doc/Schemacop/NodeResolver.html".freeze, "doc/Schemacop/NodeSupportingField.html".freeze, "doc/Schemacop/NodeSupportingType.html".freeze, "doc/Schemacop/NodeWithBlock.html".freeze, "doc/Schemacop/NumberValidator.html".freeze, "doc/Schemacop/ObjectValidator.html".freeze, "doc/Schemacop/RootNode.html".freeze, "doc/Schemacop/Schema.html".freeze, "doc/Schemacop/StringValidator.html".freeze, "doc/Schemacop/SymbolValidator.html".freeze, "doc/ScopedEnv.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/inheritance.graphml".freeze, "doc/inheritance.pdf".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/collector.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/field_node.rb".freeze, "lib/schemacop/node.rb".freeze, "lib/schemacop/node_resolver.rb".freeze, "lib/schemacop/node_supporting_field.rb".freeze, "lib/schemacop/node_supporting_type.rb".freeze, "lib/schemacop/node_with_block.rb".freeze, "lib/schemacop/root_node.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/validator/array_validator.rb".freeze, "lib/schemacop/validator/boolean_validator.rb".freeze, "lib/schemacop/validator/float_validator.rb".freeze, "lib/schemacop/validator/hash_validator.rb".freeze, "lib/schemacop/validator/integer_validator.rb".freeze, "lib/schemacop/validator/nil_validator.rb".freeze, "lib/schemacop/validator/number_validator.rb".freeze, "lib/schemacop/validator/object_validator.rb".freeze, "lib/schemacop/validator/string_validator.rb".freeze, "lib/schemacop/validator/symbol_validator.rb".freeze, "schemacop.gemspec".freeze, "test/collector_test.rb".freeze, "test/custom_check_test.rb".freeze, "test/custom_if_test.rb".freeze, "test/nil_dis_allow_test.rb".freeze, "test/short_forms_test.rb".freeze, "test/test_helper.rb".freeze, "test/types_test.rb".freeze, "test/validator_array_test.rb".freeze, "test/validator_boolean_test.rb".freeze, "test/validator_float_test.rb".freeze, "test/validator_hash_test.rb".freeze, "test/validator_integer_test.rb".freeze, "test/validator_nil_test.rb".freeze, "test/validator_number_test.rb".freeze, "test/validator_object_test.rb".freeze, "test/validator_string_test.rb".freeze, "test/validator_symbol_test.rb".freeze]
13
13
  s.homepage = "https://github.com/sitrox/schemacop".freeze
14
14
  s.licenses = ["MIT".freeze]
15
15
  s.rubygems_version = "2.6.6".freeze
16
16
  s.summary = "Schemacop validates ruby structures consisting of nested hashes and arrays against simple schema definitions.".freeze
17
- s.test_files = ["test/custom_check_test.rb".freeze, "test/custom_if_test.rb".freeze, "test/nil_dis_allow_test.rb".freeze, "test/short_forms_test.rb".freeze, "test/test_helper.rb".freeze, "test/types_test.rb".freeze, "test/validator_array_test.rb".freeze, "test/validator_boolean_test.rb".freeze, "test/validator_float_test.rb".freeze, "test/validator_hash_test.rb".freeze, "test/validator_integer_test.rb".freeze, "test/validator_nil_test.rb".freeze, "test/validator_number_test.rb".freeze, "test/validator_object_test.rb".freeze, "test/validator_string_test.rb".freeze, "test/validator_symbol_test.rb".freeze]
17
+ s.test_files = ["test/collector_test.rb".freeze, "test/custom_check_test.rb".freeze, "test/custom_if_test.rb".freeze, "test/nil_dis_allow_test.rb".freeze, "test/short_forms_test.rb".freeze, "test/test_helper.rb".freeze, "test/types_test.rb".freeze, "test/validator_array_test.rb".freeze, "test/validator_boolean_test.rb".freeze, "test/validator_float_test.rb".freeze, "test/validator_hash_test.rb".freeze, "test/validator_integer_test.rb".freeze, "test/validator_nil_test.rb".freeze, "test/validator_number_test.rb".freeze, "test/validator_object_test.rb".freeze, "test/validator_string_test.rb".freeze, "test/validator_symbol_test.rb".freeze]
18
18
 
19
19
  if s.respond_to? :specification_version then
20
20
  s.specification_version = 4
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ module Schemacop
4
+ class CollectorTest < Minitest::Test
5
+ def test_no_root_node
6
+ s = Schema.new do
7
+ req :a, :string
8
+ end
9
+
10
+ col = s.validate(a: 0)
11
+ assert col.exceptions.first[:path].first !~ %r{^/root}, 'Root node is present in the path.'
12
+ end
13
+
14
+ def test_correct_path
15
+ s = Schema.new do
16
+ req :long_symbol, :string
17
+ req 'long_string', :string
18
+ req 123, :string
19
+ end
20
+
21
+ col = s.validate('long_string' => 0, long_symbol: 0, 123 => 0)
22
+
23
+ symbol = col.exceptions[0]
24
+ string = col.exceptions[1]
25
+ number = col.exceptions[2]
26
+
27
+ assert symbol[:path].first =~ %r{^/long_symbol}
28
+ assert string[:path].first =~ %r{^/long_string}
29
+ assert number[:path].first =~ %r{^/123}
30
+ end
31
+
32
+ def test_nested_paths
33
+ s = Schema.new do
34
+ req :one do
35
+ req :two, :string
36
+ end
37
+ req :three, :string
38
+ end
39
+
40
+ col = s.validate(one: { two: 0 }, three: 0)
41
+ assert_equal 2, col.exceptions[0][:path].length
42
+ assert_equal 1, col.exceptions[1][:path].length
43
+ end
44
+ end
45
+ end
@@ -2,6 +2,9 @@ require 'test_helper'
2
2
 
3
3
  module Schemacop
4
4
  class ShortFormsTest < Minitest::Test
5
+ class User; end
6
+ class Group; end
7
+
5
8
  def test_constructor_defaults_to_hash
6
9
  s = Schema.new do
7
10
  req! :r do
@@ -142,6 +145,7 @@ module Schemacop
142
145
  end
143
146
  req :name, :integer, min: 5, max: 7
144
147
  req :id, [:integer, :string]
148
+ req :callback, :symbol
145
149
  req :attrs do
146
150
  req :color do
147
151
  type :integer
@@ -161,6 +165,7 @@ module Schemacop
161
165
  foo: { bar: nil },
162
166
  attrs: { color: 5 },
163
167
  id: 'hallo',
168
+ callback: :funky_function,
164
169
  colors: [5, 'sdf'],
165
170
  cars: [
166
171
  {
@@ -312,5 +317,15 @@ module Schemacop
312
317
  assert_verr { s.validate!('123') }
313
318
  assert_verr { s.validate!(string: '1234') }
314
319
  end
320
+
321
+ def test_inline_objects
322
+ s = Schema.new do
323
+ req :user, User
324
+ req :group, Group
325
+ end
326
+
327
+ assert_nil s.validate!(user: User.new, group: Group.new)
328
+ assert_verr { s.validate!(user: Group.new, group: User.new) }
329
+ end
315
330
  end
316
331
  end
@@ -83,5 +83,23 @@ module Schemacop
83
83
  assert_verr { s.validate!(fld: nil) }
84
84
  assert_verr { s.validate!({}) }
85
85
  end
86
+
87
+ def test_strict_option
88
+ s = Schema.new do
89
+ req :o_strict do
90
+ type :object, classes: User, strict: true
91
+ end
92
+ opt :o_ns do
93
+ type :object, classes: User, strict: false
94
+ end
95
+ end
96
+
97
+ assert_nil s.validate!(o_strict: User.new, o_ns: User.new)
98
+ assert_nil s.validate!(o_strict: User.new, o_ns: SubUser.new)
99
+ assert_nil s.validate!(o_strict: User.new)
100
+ assert_verr { s.validate!(o_strict: SubUser.new) }
101
+ assert_verr { s.validate!(o_strict: User.new, o_ns: AdminUser.new) }
102
+ assert_verr { s.validate!(o_strict: SubUser.new, o_ns: User.new) }
103
+ end
86
104
  end
87
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemacop
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-17 00:00:00.000000000 Z
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -223,6 +223,7 @@ files:
223
223
  - lib/schemacop/validator/string_validator.rb
224
224
  - lib/schemacop/validator/symbol_validator.rb
225
225
  - schemacop.gemspec
226
+ - test/collector_test.rb
226
227
  - test/custom_check_test.rb
227
228
  - test/custom_if_test.rb
228
229
  - test/nil_dis_allow_test.rb
@@ -259,12 +260,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
259
260
  version: '0'
260
261
  requirements: []
261
262
  rubyforge_project:
262
- rubygems_version: 2.6.8
263
+ rubygems_version: 2.6.11
263
264
  signing_key:
264
265
  specification_version: 4
265
266
  summary: Schemacop validates ruby structures consisting of nested hashes and arrays
266
267
  against simple schema definitions.
267
268
  test_files:
269
+ - test/collector_test.rb
268
270
  - test/custom_check_test.rb
269
271
  - test/custom_if_test.rb
270
272
  - test/nil_dis_allow_test.rb