schemacop 3.0.0.rc2 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ task :gemspec do
18
18
  # needs access to ActiveSupport::HashWithIndifferentAccess and expects
19
19
  # behavior of that as in version 5 of ActiveSupport.
20
20
  spec.add_dependency 'activesupport', '>= 4.0'
21
+ spec.add_dependency 'ruby2_keywords', '0.0.4'
21
22
  spec.add_development_dependency 'bundler'
22
23
  spec.add_development_dependency 'rake'
23
24
  spec.add_development_dependency 'minitest'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0.rc2
1
+ 3.0.1
data/lib/schemacop.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # External dependencies
2
+ require 'ruby2_keywords'
2
3
  require 'active_support/all'
3
4
  require 'set'
4
5
 
@@ -5,6 +5,13 @@ module Schemacop
5
5
  unless Rails.env.development?
6
6
  V3::GlobalContext.eager_load!
7
7
  end
8
+
9
+ # Tell Zeitwerk to ignore the files in our load path
10
+ if defined?(Rails) && defined?(Zeitwerk)
11
+ Schemacop.load_paths.each do |load_path|
12
+ Rails.autoloaders.main.ignore(Rails.root.join(load_path))
13
+ end
14
+ end
8
15
  end
9
16
  end
10
17
  end
@@ -7,14 +7,14 @@ module Schemacop
7
7
  @prefix = prefix
8
8
  end
9
9
 
10
- def method_missing(symbol, *args, **kwargs, &block)
10
+ ruby2_keywords def method_missing(symbol, *args, &block)
11
11
  symbol = :"#{@prefix}#{symbol}" if @prefix
12
12
 
13
13
  if @methods.include?(symbol)
14
14
  if @delegation_object.respond_to?(symbol)
15
- @delegation_object.send(symbol, *args, **kwargs, &block)
15
+ @delegation_object.send(symbol, *args, &block)
16
16
  elsif @backup_binding.respond_to?(symbol)
17
- @backup_binding.send(symbol, *args, **kwargs, &block)
17
+ @backup_binding.send(symbol, *args, &block)
18
18
  else
19
19
  super
20
20
  end
data/lib/schemacop/v2.rb CHANGED
@@ -10,7 +10,6 @@ require 'schemacop/v2/node'
10
10
  require 'schemacop/v2/node_with_block'
11
11
  require 'schemacop/v2/node_supporting_type'
12
12
  require 'schemacop/v2/field_node'
13
- require 'schemacop/v2/root_node'
14
13
  require 'schemacop/v2/node_supporting_field'
15
14
  require 'schemacop/v2/caster'
16
15
  require 'schemacop/v2/dupper'
@@ -18,6 +18,7 @@ module Schemacop::V2
18
18
  @data = data
19
19
  @target_type = target_type
20
20
  @caster = nil
21
+ @value = nil
21
22
 
22
23
  if casts.is_a?(Array)
23
24
  from_types = casts
@@ -11,6 +11,8 @@ module Schemacop
11
11
 
12
12
  supports_children(name: true)
13
13
 
14
+ attr_reader :properties
15
+
14
16
  def self.allowed_options
15
17
  super + ATTRIBUTES - %i[dependencies] + %i[additional_properties]
16
18
  end
@@ -19,6 +21,14 @@ module Schemacop
19
21
  super + NodeRegistry.dsl_methods(true) + %i[dsl_dep dsl_add]
20
22
  end
21
23
 
24
+ def self.sanitize_exp(exp)
25
+ exp = exp.to_s
26
+ if exp.start_with?('(?-mix:')
27
+ exp = exp.to_s.gsub(/^\(\?-mix:/, '').gsub(/\)$/, '')
28
+ end
29
+ return exp
30
+ end
31
+
22
32
  def add_child(node)
23
33
  unless node.name
24
34
  fail Exceptions::InvalidSchemaError, 'Child nodes must have a name.'
@@ -54,11 +64,11 @@ module Schemacop
54
64
 
55
65
  json = {}
56
66
  json[:properties] = Hash[properties.values.map { |p| [p.name, p.as_json] }] if properties.any?
57
- json[:patternProperties] = Hash[pattern_properties.values.map { |p| [sanitize_exp(p.name), p.as_json] }] if pattern_properties.any?
67
+ json[:patternProperties] = Hash[pattern_properties.values.map { |p| [self.class.sanitize_exp(p.name), p.as_json] }] if pattern_properties.any?
58
68
 
59
69
  # In schemacop, by default, additional properties are not allowed,
60
70
  # the users explicitly need to enable additional properties
61
- if options[:additional_properties] == true
71
+ if options[:additional_properties].is_a?(TrueClass)
62
72
  json[:additionalProperties] = true
63
73
  elsif options[:additional_properties].is_a?(Node)
64
74
  json[:additionalProperties] = options[:additional_properties].as_json
@@ -75,14 +85,6 @@ module Schemacop
75
85
  return process_json(ATTRIBUTES, json)
76
86
  end
77
87
 
78
- def sanitize_exp(exp)
79
- exp = exp.to_s
80
- if exp.start_with?('(?-mix:')
81
- exp = exp.to_s.gsub(/^\(\?-mix:/, '').gsub(/\)$/, '')
82
- end
83
- return exp
84
- end
85
-
86
88
  def allowed_types
87
89
  { Hash => :object }
88
90
  end
@@ -139,7 +141,9 @@ module Schemacop
139
141
  result.error "Property name #{name.inspect} does not match #{options[:property_names].inspect}."
140
142
  end
141
143
 
142
- if options[:additional_properties].blank?
144
+ if options[:additional_properties].is_a?(TrueClass)
145
+ next
146
+ elsif options[:additional_properties].is_a?(FalseClass) || options[:additional_properties].blank?
143
147
  match = property_patterns.keys.find { |p| p.match?(name.to_s) }
144
148
  if match
145
149
  result.in_path(name) do
@@ -174,16 +178,10 @@ module Schemacop
174
178
  data ||= default
175
179
  return nil if data.nil?
176
180
 
177
- original_data_hash = data.dup
178
- data_hash = data.with_indifferent_access
179
-
180
- if original_data_hash.size != data_hash.size
181
- ambiguous_properties = original_data_hash.keys - data_hash.keys
182
-
183
- result.error "Has #{ambiguous_properties.size} ambiguous properties: #{ambiguous_properties}."
184
- end
181
+ data_hash = data.dup.with_indifferent_access
185
182
 
186
183
  property_patterns = {}
184
+ as_names = []
187
185
 
188
186
  @properties.each_value do |prop|
189
187
  if prop.name.is_a?(Regexp)
@@ -191,10 +189,18 @@ module Schemacop
191
189
  next
192
190
  end
193
191
 
194
- result[prop.name] = prop.cast(data_hash[prop.name])
192
+ as_names << prop.as&.to_s if prop.as.present?
193
+
194
+ prop_name = prop.as&.to_s || prop.name
195
195
 
196
- if result[prop.name].nil? && !data_hash.include?(prop.name)
197
- result.delete(prop.name)
196
+ casted_data = prop.cast(data_hash[prop.name])
197
+
198
+ if casted_data.present? || data_hash.include?(prop.name)
199
+ result[prop_name] = casted_data
200
+ end
201
+
202
+ if result[prop_name].nil? && !data_hash.include?(prop.name) && !as_names.include?(prop.name)
203
+ result.delete(prop_name)
198
204
  end
199
205
  end
200
206
 
@@ -211,7 +217,7 @@ module Schemacop
211
217
  end
212
218
 
213
219
  # Handle additional properties
214
- if options[:additional_properties] == true
220
+ if options[:additional_properties].is_a?(TrueClass)
215
221
  result = data_hash.merge(result)
216
222
  elsif options[:additional_properties].is_a?(Node)
217
223
  specified_properties = @properties.keys.to_set
@@ -236,6 +242,11 @@ module Schemacop
236
242
  unless @options[:additional_properties].nil? || @options[:additional_properties].is_a?(TrueClass) || @options[:additional_properties].is_a?(FalseClass)
237
243
  fail Schemacop::Exceptions::InvalidSchemaError, 'Option "additional_properties" must be a boolean value'
238
244
  end
245
+
246
+ # Default the additional_properties option to false if it's not given
247
+ if @options[:additional_properties].nil?
248
+ @options[:additional_properties] = false
249
+ end
239
250
  end
240
251
 
241
252
  def validate_self
@@ -2,6 +2,7 @@ module Schemacop
2
2
  module V3
3
3
  class Node
4
4
  attr_reader :name
5
+ attr_reader :as
5
6
  attr_reader :default
6
7
  attr_reader :title
7
8
  attr_reader :description
@@ -32,8 +33,10 @@ module Schemacop
32
33
  if options.delete(:cast_str)
33
34
  format = NodeRegistry.name(klass)
34
35
  one_of_options = {
35
- required: options.delete(:required),
36
- name: options.delete(:name)
36
+ required: options.delete(:required),
37
+ name: options.delete(:name),
38
+ as: options.delete(:as),
39
+ description: options.delete(:description)
37
40
  }
38
41
  node = create(:one_of, **one_of_options) do
39
42
  self.node node
@@ -45,7 +48,7 @@ module Schemacop
45
48
  end
46
49
 
47
50
  def self.allowed_options
48
- %i[name required default description examples enum parent options cast_str title]
51
+ %i[name required default description examples enum parent options cast_str title as]
49
52
  end
50
53
 
51
54
  def self.dsl_methods
@@ -69,12 +72,13 @@ module Schemacop
69
72
  disallowed_options = options.keys - self.class.allowed_options
70
73
 
71
74
  if disallowed_options.any?
72
- fail "Options #{disallowed_options.inspect} are not allowed for this node."
75
+ fail Schemacop::Exceptions::InvalidSchemaError, "Options #{disallowed_options.inspect} are not allowed for this node."
73
76
  end
74
77
 
75
78
  # Assign attributes #
76
79
  @name = options.delete(:name)
77
80
  @name = @name.to_s unless @name.nil? || @name.is_a?(Regexp)
81
+ @as = options.delete(:as)
78
82
  @required = !!options.delete(:required)
79
83
  @default = options.delete(:default)
80
84
  @title = options.delete(:title)
@@ -91,7 +95,7 @@ module Schemacop
91
95
  # Run DSL block #
92
96
  if block_given?
93
97
  unless self.class.supports_children_options
94
- fail "Node #{self.class} does not support blocks."
98
+ fail Schemacop::Exceptions::InvalidSchemaError, "Node #{self.class} does not support blocks."
95
99
  end
96
100
 
97
101
  scope = DslScope.new(self)
@@ -40,10 +40,6 @@ module Schemacop
40
40
  def self.name(klass)
41
41
  @by_class[klass][:name]
42
42
  end
43
-
44
- def self.short_name(klass)
45
- @by_class[klass][:short_name]
46
- end
47
43
  end
48
44
  end
49
45
  end
@@ -36,7 +36,13 @@ module Schemacop
36
36
  end
37
37
 
38
38
  def used_external_schemas
39
- schemas.include?(@path) ? [] : [@path]
39
+ target_children_schema = target.used_external_schemas
40
+
41
+ if schemas.include?(@path)
42
+ return target_children_schema
43
+ else
44
+ return [@path] + target_children_schema
45
+ end
40
46
  end
41
47
 
42
48
  protected
@@ -16,6 +16,7 @@ module Schemacop
16
16
  email: URI::MailTo::EMAIL_REGEXP,
17
17
  boolean: /^(true|false)$/,
18
18
  binary: nil,
19
+ symbol: nil,
19
20
  integer: /^-?[0-9]+$/,
20
21
  number: /^-?[0-9]+(\.[0-9]+)?$/
21
22
  }.freeze
@@ -66,21 +67,31 @@ module Schemacop
66
67
  end
67
68
 
68
69
  def cast(value)
70
+ if value.present?
71
+ to_cast = value
72
+ elsif default.present?
73
+ to_cast = default
74
+ else
75
+ return nil
76
+ end
77
+
69
78
  case options[:format]
70
79
  when :boolean
71
- return value == 'true'
80
+ return to_cast == 'true'
72
81
  when :date
73
- return Date.parse(value)
82
+ return Date.parse(to_cast)
74
83
  when :'date-time'
75
- return DateTime.parse(value)
84
+ return DateTime.parse(to_cast)
76
85
  when :time
77
- Time.parse(value)
86
+ Time.parse(to_cast)
78
87
  when :integer
79
- return Integer(value)
88
+ return Integer(to_cast)
80
89
  when :number
81
- return Float(value)
90
+ return Float(to_cast)
91
+ when :symbol
92
+ return to_cast.to_sym
82
93
  else
83
- return value || default
94
+ return to_cast
84
95
  end
85
96
  end
86
97
 
data/schemacop.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 3.0.0.rc2 ruby lib
2
+ # stub: schemacop 3.0.1 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "3.0.0.rc2"
6
+ s.version = "3.0.1"
7
7
 
8
- s.required_rubygems_version = Gem::Requirement.new("> 1.3.1".freeze) if s.respond_to? :required_rubygems_version=
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 = "2021-01-28"
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, "README_V2.md".freeze, "README_V3.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/base_schema.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/railtie.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/schema2.rb".freeze, "lib/schemacop/schema3.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/v2.rb".freeze, "lib/schemacop/v2/caster.rb".freeze, "lib/schemacop/v2/collector.rb".freeze, "lib/schemacop/v2/dupper.rb".freeze, "lib/schemacop/v2/field_node.rb".freeze, "lib/schemacop/v2/node.rb".freeze, "lib/schemacop/v2/node_resolver.rb".freeze, "lib/schemacop/v2/node_supporting_field.rb".freeze, "lib/schemacop/v2/node_supporting_type.rb".freeze, "lib/schemacop/v2/node_with_block.rb".freeze, "lib/schemacop/v2/root_node.rb".freeze, "lib/schemacop/v2/validator/array_validator.rb".freeze, "lib/schemacop/v2/validator/boolean_validator.rb".freeze, "lib/schemacop/v2/validator/float_validator.rb".freeze, "lib/schemacop/v2/validator/hash_validator.rb".freeze, "lib/schemacop/v2/validator/integer_validator.rb".freeze, "lib/schemacop/v2/validator/nil_validator.rb".freeze, "lib/schemacop/v2/validator/number_validator.rb".freeze, "lib/schemacop/v2/validator/object_validator.rb".freeze, "lib/schemacop/v2/validator/string_validator.rb".freeze, "lib/schemacop/v2/validator/symbol_validator.rb".freeze, "lib/schemacop/v3.rb".freeze, "lib/schemacop/v3/all_of_node.rb".freeze, "lib/schemacop/v3/any_of_node.rb".freeze, "lib/schemacop/v3/array_node.rb".freeze, "lib/schemacop/v3/boolean_node.rb".freeze, "lib/schemacop/v3/combination_node.rb".freeze, "lib/schemacop/v3/context.rb".freeze, "lib/schemacop/v3/dsl_scope.rb".freeze, "lib/schemacop/v3/global_context.rb".freeze, "lib/schemacop/v3/hash_node.rb".freeze, "lib/schemacop/v3/integer_node.rb".freeze, "lib/schemacop/v3/is_not_node.rb".freeze, "lib/schemacop/v3/node.rb".freeze, "lib/schemacop/v3/node_registry.rb".freeze, "lib/schemacop/v3/number_node.rb".freeze, "lib/schemacop/v3/numeric_node.rb".freeze, "lib/schemacop/v3/object_node.rb".freeze, "lib/schemacop/v3/one_of_node.rb".freeze, "lib/schemacop/v3/reference_node.rb".freeze, "lib/schemacop/v3/result.rb".freeze, "lib/schemacop/v3/string_node.rb".freeze, "lib/schemacop/v3/symbol_node.rb".freeze, "schemacop.gemspec".freeze, "test/lib/test_helper.rb".freeze, "test/schemas/nested/group.rb".freeze, "test/schemas/user.rb".freeze, "test/unit/schemacop/v2/casting_test.rb".freeze, "test/unit/schemacop/v2/collector_test.rb".freeze, "test/unit/schemacop/v2/custom_check_test.rb".freeze, "test/unit/schemacop/v2/custom_if_test.rb".freeze, "test/unit/schemacop/v2/defaults_test.rb".freeze, "test/unit/schemacop/v2/empty_test.rb".freeze, "test/unit/schemacop/v2/nil_dis_allow_test.rb".freeze, "test/unit/schemacop/v2/node_resolver_test.rb".freeze, "test/unit/schemacop/v2/short_forms_test.rb".freeze, "test/unit/schemacop/v2/types_test.rb".freeze, "test/unit/schemacop/v2/validator_array_test.rb".freeze, "test/unit/schemacop/v2/validator_boolean_test.rb".freeze, "test/unit/schemacop/v2/validator_float_test.rb".freeze, "test/unit/schemacop/v2/validator_hash_test.rb".freeze, "test/unit/schemacop/v2/validator_integer_test.rb".freeze, "test/unit/schemacop/v2/validator_nil_test.rb".freeze, "test/unit/schemacop/v2/validator_number_test.rb".freeze, "test/unit/schemacop/v2/validator_object_test.rb".freeze, "test/unit/schemacop/v2/validator_string_test.rb".freeze, "test/unit/schemacop/v2/validator_symbol_test.rb".freeze, "test/unit/schemacop/v3/all_of_node_test.rb".freeze, "test/unit/schemacop/v3/any_of_node_test.rb".freeze, "test/unit/schemacop/v3/array_node_test.rb".freeze, "test/unit/schemacop/v3/boolean_node_test.rb".freeze, "test/unit/schemacop/v3/global_context_test.rb".freeze, "test/unit/schemacop/v3/hash_node_test.rb".freeze, "test/unit/schemacop/v3/integer_node_test.rb".freeze, "test/unit/schemacop/v3/is_not_node_test.rb".freeze, "test/unit/schemacop/v3/node_test.rb".freeze, "test/unit/schemacop/v3/number_node_test.rb".freeze, "test/unit/schemacop/v3/object_node_test.rb".freeze, "test/unit/schemacop/v3/one_of_node_test.rb".freeze, "test/unit/schemacop/v3/reference_node_test.rb".freeze, "test/unit/schemacop/v3/string_node_test.rb".freeze, "test/unit/schemacop/v3/symbol_node_test.rb".freeze]
11
+ s.date = "2021-02-11"
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, "README_V2.md".freeze, "README_V3.md".freeze, "RUBY_VERSION".freeze, "Rakefile".freeze, "VERSION".freeze, "lib/schemacop.rb".freeze, "lib/schemacop/base_schema.rb".freeze, "lib/schemacop/exceptions.rb".freeze, "lib/schemacop/railtie.rb".freeze, "lib/schemacop/schema.rb".freeze, "lib/schemacop/schema2.rb".freeze, "lib/schemacop/schema3.rb".freeze, "lib/schemacop/scoped_env.rb".freeze, "lib/schemacop/v2.rb".freeze, "lib/schemacop/v2/caster.rb".freeze, "lib/schemacop/v2/collector.rb".freeze, "lib/schemacop/v2/dupper.rb".freeze, "lib/schemacop/v2/field_node.rb".freeze, "lib/schemacop/v2/node.rb".freeze, "lib/schemacop/v2/node_resolver.rb".freeze, "lib/schemacop/v2/node_supporting_field.rb".freeze, "lib/schemacop/v2/node_supporting_type.rb".freeze, "lib/schemacop/v2/node_with_block.rb".freeze, "lib/schemacop/v2/validator/array_validator.rb".freeze, "lib/schemacop/v2/validator/boolean_validator.rb".freeze, "lib/schemacop/v2/validator/float_validator.rb".freeze, "lib/schemacop/v2/validator/hash_validator.rb".freeze, "lib/schemacop/v2/validator/integer_validator.rb".freeze, "lib/schemacop/v2/validator/nil_validator.rb".freeze, "lib/schemacop/v2/validator/number_validator.rb".freeze, "lib/schemacop/v2/validator/object_validator.rb".freeze, "lib/schemacop/v2/validator/string_validator.rb".freeze, "lib/schemacop/v2/validator/symbol_validator.rb".freeze, "lib/schemacop/v3.rb".freeze, "lib/schemacop/v3/all_of_node.rb".freeze, "lib/schemacop/v3/any_of_node.rb".freeze, "lib/schemacop/v3/array_node.rb".freeze, "lib/schemacop/v3/boolean_node.rb".freeze, "lib/schemacop/v3/combination_node.rb".freeze, "lib/schemacop/v3/context.rb".freeze, "lib/schemacop/v3/dsl_scope.rb".freeze, "lib/schemacop/v3/global_context.rb".freeze, "lib/schemacop/v3/hash_node.rb".freeze, "lib/schemacop/v3/integer_node.rb".freeze, "lib/schemacop/v3/is_not_node.rb".freeze, "lib/schemacop/v3/node.rb".freeze, "lib/schemacop/v3/node_registry.rb".freeze, "lib/schemacop/v3/number_node.rb".freeze, "lib/schemacop/v3/numeric_node.rb".freeze, "lib/schemacop/v3/object_node.rb".freeze, "lib/schemacop/v3/one_of_node.rb".freeze, "lib/schemacop/v3/reference_node.rb".freeze, "lib/schemacop/v3/result.rb".freeze, "lib/schemacop/v3/string_node.rb".freeze, "lib/schemacop/v3/symbol_node.rb".freeze, "schemacop.gemspec".freeze, "test/lib/test_helper.rb".freeze, "test/schemas/nested/group.rb".freeze, "test/schemas/user.rb".freeze, "test/unit/schemacop/v2/casting_test.rb".freeze, "test/unit/schemacop/v2/collector_test.rb".freeze, "test/unit/schemacop/v2/custom_check_test.rb".freeze, "test/unit/schemacop/v2/custom_if_test.rb".freeze, "test/unit/schemacop/v2/defaults_test.rb".freeze, "test/unit/schemacop/v2/empty_test.rb".freeze, "test/unit/schemacop/v2/nil_dis_allow_test.rb".freeze, "test/unit/schemacop/v2/node_resolver_test.rb".freeze, "test/unit/schemacop/v2/short_forms_test.rb".freeze, "test/unit/schemacop/v2/types_test.rb".freeze, "test/unit/schemacop/v2/validator_array_test.rb".freeze, "test/unit/schemacop/v2/validator_boolean_test.rb".freeze, "test/unit/schemacop/v2/validator_float_test.rb".freeze, "test/unit/schemacop/v2/validator_hash_test.rb".freeze, "test/unit/schemacop/v2/validator_integer_test.rb".freeze, "test/unit/schemacop/v2/validator_nil_test.rb".freeze, "test/unit/schemacop/v2/validator_number_test.rb".freeze, "test/unit/schemacop/v2/validator_object_test.rb".freeze, "test/unit/schemacop/v2/validator_string_test.rb".freeze, "test/unit/schemacop/v2/validator_symbol_test.rb".freeze, "test/unit/schemacop/v3/all_of_node_test.rb".freeze, "test/unit/schemacop/v3/any_of_node_test.rb".freeze, "test/unit/schemacop/v3/array_node_test.rb".freeze, "test/unit/schemacop/v3/boolean_node_test.rb".freeze, "test/unit/schemacop/v3/global_context_test.rb".freeze, "test/unit/schemacop/v3/hash_node_test.rb".freeze, "test/unit/schemacop/v3/integer_node_test.rb".freeze, "test/unit/schemacop/v3/is_not_node_test.rb".freeze, "test/unit/schemacop/v3/node_test.rb".freeze, "test/unit/schemacop/v3/number_node_test.rb".freeze, "test/unit/schemacop/v3/object_node_test.rb".freeze, "test/unit/schemacop/v3/one_of_node_test.rb".freeze, "test/unit/schemacop/v3/reference_node_test.rb".freeze, "test/unit/schemacop/v3/string_node_test.rb".freeze, "test/unit/schemacop/v3/symbol_node_test.rb".freeze]
13
13
  s.homepage = "https://github.com/sitrox/schemacop".freeze
14
14
  s.licenses = ["MIT".freeze]
15
15
  s.rubygems_version = "3.0.3".freeze
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
 
22
22
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
23
23
  s.add_runtime_dependency(%q<activesupport>.freeze, [">= 4.0"])
24
+ s.add_runtime_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
24
25
  s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
25
26
  s.add_development_dependency(%q<rake>.freeze, [">= 0"])
26
27
  s.add_development_dependency(%q<minitest>.freeze, [">= 0"])
@@ -32,6 +33,7 @@ Gem::Specification.new do |s|
32
33
  s.add_development_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
33
34
  else
34
35
  s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
36
+ s.add_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
35
37
  s.add_dependency(%q<bundler>.freeze, [">= 0"])
36
38
  s.add_dependency(%q<rake>.freeze, [">= 0"])
37
39
  s.add_dependency(%q<minitest>.freeze, [">= 0"])
@@ -44,6 +46,7 @@ Gem::Specification.new do |s|
44
46
  end
45
47
  else
46
48
  s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
49
+ s.add_dependency(%q<ruby2_keywords>.freeze, ["= 0.0.4"])
47
50
  s.add_dependency(%q<bundler>.freeze, [">= 0"])
48
51
  s.add_dependency(%q<rake>.freeze, [">= 0"])
49
52
  s.add_dependency(%q<minitest>.freeze, [">= 0"])
@@ -1,5 +1,15 @@
1
1
  require 'simplecov'
2
- SimpleCov.start
2
+ SimpleCov.start do
3
+ add_group 'v3', 'lib/schemacop/v3'
4
+ add_group 'v2', 'lib/schemacop/v2'
5
+
6
+ # We don't care about the test coverage for the
7
+ # tests themselves
8
+ add_filter 'test'
9
+
10
+ # Also enable branch coverage reporting
11
+ enable_coverage :branch
12
+ end
3
13
 
4
14
  # TODO: Move to more sensible location
5
15
  def assert_verr(&block)
@@ -141,7 +151,12 @@ class V3Test < SchemacopTest
141
151
  input_data_was = Marshal.load(Marshal.dump(input_data))
142
152
  result = @schema.validate(input_data)
143
153
  assert_empty result.errors
144
- assert_equal expected_data, result.data, 'Unexpected result data.'
154
+
155
+ if expected_data.nil?
156
+ assert_nil result.data, 'Unexpected result data.'
157
+ else
158
+ assert_equal expected_data, result.data, 'Unexpected result data.'
159
+ end
145
160
 
146
161
  if input_data.nil?
147
162
  assert_nil input_data_was, 'Expected input_data to stay the same.'
@@ -115,6 +115,43 @@ module Schemacop
115
115
  assert_equal expected_float, s.validate!(float_field: '')
116
116
  assert_equal expected_float, s.validate!(float_field: ' ')
117
117
  end
118
+
119
+ def test_float_to_integer
120
+ s = Schema.new do
121
+ req :foo, :integer, cast: [Float]
122
+ end
123
+
124
+ assert_equal({ foo: 42 }, s.validate!(foo: 42.0))
125
+ assert_equal({ foo: 42 }, s.validate!(foo: Float(42)))
126
+ end
127
+
128
+ def test_integer_to_float
129
+ s = Schema.new do
130
+ req :foo, :float, cast: [Integer]
131
+ end
132
+
133
+ assert_equal({ foo: 42.0 }, s.validate!(foo: 42))
134
+ assert_equal({ foo: 42.0 }, s.validate!(foo: Integer(42)))
135
+ end
136
+
137
+ def test_invalid_cast_option
138
+ s = Schema.new do
139
+ req :foo, :integer, cast: true
140
+ end
141
+
142
+ assert_raises Schemacop::Exceptions::InvalidSchemaError do
143
+ s.validate!({ foo: '42' })
144
+ end
145
+ end
146
+
147
+ def test_impossible_cast
148
+ s = Schema.new do
149
+ req :foo, :integer, cast: [String]
150
+ end
151
+
152
+ assert_equal({ foo: 42 }, s.validate!(foo: '42'))
153
+ assert_verr { s.validate!(foo: 'foo') }
154
+ end
118
155
  end
119
156
  end
120
157
  end
@@ -90,6 +90,17 @@ module Schemacop
90
90
  assert_verr { s.validate!(o: true) }
91
91
  assert_verr { s.validate!(r: nil, r_nil: false, o: true) }
92
92
  end
93
+
94
+ def test_invalid_hash
95
+ s = Schema.new do
96
+ type :hash do
97
+ req :foo
98
+ end
99
+ end
100
+
101
+ assert_verr { s.validate!({ foo: 42, 'foo' => 42 }) }
102
+ assert_nothing_raised { s.validate!({ foo: 42 }.with_indifferent_access) }
103
+ end
93
104
  end
94
105
  end
95
106
  end