schemacop 3.0.0 → 3.0.5

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
  SHA256:
3
- metadata.gz: eb25d83997e507cf7bf5e905143584e01b543c296f74e190eae487fe1a8bfdc0
4
- data.tar.gz: 896f9bc4e14ac0e59831eacd83ae2e5a691a53e377f0b4895790b3686c24c2db
3
+ metadata.gz: 27400e248bfdb65fc7cc229fbbb625cfa68a1421996fc5cbbb3283eb82d28a1e
4
+ data.tar.gz: 4b435374b7d2c0ce05e4410235995983dc0f96c8d6c21b69d88e80abdae7aed0
5
5
  SHA512:
6
- metadata.gz: 461b762ba18edeac632010db52d368978ead449c6de8a88f6b6883d0109cfa3ff3e8b9bf4c8f2780023b60624bfc80f6456a421a8044fdf4ed7b565d8779db5c
7
- data.tar.gz: 693beb806323f0695fce4183ad65d65670d5cab8b76692d74b4a467e396a52540bbb4fa9b9c03bef9940a35c1f62a32fcd0e51ea435f450bc134b502ca5839df
6
+ metadata.gz: 194c2f72fcadad2e311e1c96ca9bfd004e5f27c5d591bd1188a1fb177d56a75aa2670b50fddc07e7afaee99fd48f98195156a8f98a250485fd6a75a0ac57bda8
7
+ data.tar.gz: 798a3dfaf53b6c2996e2799cc841bdf2172ef4ebd89c022c67a2d21424add61f1c0ef0c686e11c17b589b417961343f603012395e5510284573cb8a7e78a3468
data/CHANGELOG.md CHANGED
@@ -10,6 +10,34 @@
10
10
  ### Changes
11
11
  -->
12
12
 
13
+ ## 3.0.5 (2021-02-14)
14
+
15
+ * Allow option `pattern` to be a `Regexp` for `string` (`str`) nodes
16
+
17
+ * Remove `examples_keyword` from context again
18
+
19
+ * Add option `json_format` to {Schemacop::Schema3.as_json as_json}. This allows
20
+ generating a JSON schema that is [specific to
21
+ swagger](https://swagger.io/docs/specification/data-models/keywords/) by
22
+ passing `:swagger` to it.
23
+
24
+ ## 3.0.4 (2021-02-15)
25
+
26
+ * Add `examples_keyword` to context which allows to customize the name of the
27
+ `examples` attribute in JSON output
28
+
29
+ ## 3.0.3 (2021-02-15)
30
+
31
+ * Fix boolean node casting
32
+
33
+ ## 3.0.2 (2021-02-14)
34
+
35
+ * Fix #15 Code to ignore Zeitwerk fails when Zeitwerk is disabled
36
+
37
+ ## 3.0.1 (2021-02-11)
38
+
39
+ * Add format `symbol` to strings
40
+
13
41
  ## 3.0.0 (2021-02-08)
14
42
 
15
43
  * Setup Zeitwerk ignores for Rails applications
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020 Sitrox
3
+ Copyright © 2016 - 2021 Sitrox
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -16,7 +16,7 @@ Schemacop is tested with the following ruby versions:
16
16
  * 2.7.1
17
17
  * 3.0.0
18
18
 
19
- For these versions, the automated CI tests are ran on travis. Other ruby versions might work, but stick to these versions for best results.
19
+ Other ruby versions might work but are not covered by our Travis tests.
20
20
 
21
21
  ## Basic example
22
22
 
data/README_V3.md CHANGED
@@ -241,6 +241,9 @@ transformed into various types.
241
241
  * `number`
242
242
  The string must be a number and will be casted to a ruby `Float` object.
243
243
 
244
+ * `symbol`
245
+ The string can be anything and will be casted to a ruby `Symbol` object.
246
+
244
247
  #### Examples
245
248
 
246
249
  ```ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.0.5
@@ -7,7 +7,7 @@ module Schemacop
7
7
  end
8
8
 
9
9
  # Tell Zeitwerk to ignore the files in our load path
10
- if defined?(Rails) && defined?(Zeitwerk)
10
+ if defined?(Rails) && defined?(Zeitwerk) && Rails.autoloaders.zeitwerk_enabled?
11
11
  Schemacop.load_paths.each do |load_path|
12
12
  Rails.autoloaders.main.ignore(Rails.root.join(load_path))
13
13
  end
@@ -14,8 +14,8 @@ module Schemacop
14
14
  root.validate(data)
15
15
  end
16
16
 
17
- def as_json
18
- root.as_json
17
+ def as_json(json_format: V3::Context::DEFAULT_JSON_FORMAT)
18
+ Schemacop.context.spawn_with(json_format: json_format) { root.as_json }
19
19
  end
20
20
  end
21
21
  end
data/lib/schemacop/v3.rb CHANGED
@@ -3,6 +3,24 @@ module Schemacop
3
3
  def self.register(*args)
4
4
  NodeRegistry.register(*args)
5
5
  end
6
+
7
+ # @private
8
+ def self.sanitize_exp(exp)
9
+ return exp if exp.is_a?(String)
10
+
11
+ _start_slash, caret, exp, dollar, _end_slash, flags = exp.inspect.match(%r{^(/?)(\^)?(.*?)(\$)?(/?)([ixm]*)?$}).captures
12
+ flags = flags.split('')
13
+
14
+ if flags.delete('i')
15
+ exp = "(?i)(#{exp})"
16
+ end
17
+
18
+ if flags.any?
19
+ fail "Flags #{flags.inspect} are not supported by Schemacop."
20
+ end
21
+
22
+ return "#{caret}#{exp}#{dollar}"
23
+ end
6
24
  end
7
25
  end
8
26
 
@@ -11,6 +11,14 @@ module Schemacop
11
11
  FalseClass => :boolean
12
12
  }
13
13
  end
14
+
15
+ def cast(value)
16
+ if value.is_a?(TrueClass) || value.is_a?(FalseClass)
17
+ value
18
+ else
19
+ default
20
+ end
21
+ end
14
22
  end
15
23
  end
16
24
  end
@@ -2,9 +2,13 @@ module Schemacop
2
2
  module V3
3
3
  class Context
4
4
  attr_accessor :schemas
5
+ attr_accessor :json_format
5
6
 
6
- def initialize
7
+ DEFAULT_JSON_FORMAT = :default
8
+
9
+ def initialize(json_format: DEFAULT_JSON_FORMAT)
7
10
  @schemas = {}.with_indifferent_access.freeze
11
+ @json_format = json_format
8
12
  end
9
13
 
10
14
  def schema(name, type = :hash, **options, &block)
@@ -12,6 +16,18 @@ module Schemacop
12
16
  name => Node.create(type, **options, &block)
13
17
  ).freeze
14
18
  end
19
+
20
+ def spawn_with(json_format: @json_format)
21
+ prev_json_format = @json_format
22
+ @json_format = json_format
23
+ return yield
24
+ ensure
25
+ @json_format = prev_json_format
26
+ end
27
+
28
+ def swagger_json?
29
+ @json_format == :swagger
30
+ end
15
31
  end
16
32
  end
17
33
  end
@@ -21,14 +21,6 @@ module Schemacop
21
21
  super + NodeRegistry.dsl_methods(true) + %i[dsl_dep dsl_add]
22
22
  end
23
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
-
32
24
  def add_child(node)
33
25
  unless node.name
34
26
  fail Exceptions::InvalidSchemaError, 'Child nodes must have a name.'
@@ -64,7 +56,7 @@ module Schemacop
64
56
 
65
57
  json = {}
66
58
  json[:properties] = Hash[properties.values.map { |p| [p.name, p.as_json] }] if properties.any?
67
- json[:patternProperties] = Hash[pattern_properties.values.map { |p| [self.class.sanitize_exp(p.name), p.as_json] }] if pattern_properties.any?
59
+ json[:patternProperties] = Hash[pattern_properties.values.map { |p| [V3.sanitize_exp(p.name), p.as_json] }] if pattern_properties.any?
68
60
 
69
61
  # In schemacop, by default, additional properties are not allowed,
70
62
  # the users explicitly need to enable additional properties
@@ -150,6 +150,10 @@ module Schemacop
150
150
 
151
151
  protected
152
152
 
153
+ def context
154
+ Schemacop.context
155
+ end
156
+
153
157
  def item_matches?(item, data)
154
158
  item_result = Result.new(self)
155
159
  item._validate(data, result: item_result)
@@ -170,7 +174,7 @@ module Schemacop
170
174
  end
171
175
 
172
176
  json[:title] = @title if @title
173
- json[:examples] = @examples if @examples
177
+ json[context.swagger_json? ? :example : :examples] = @examples if @examples
174
178
  json[:description] = @description if @description
175
179
  json[:default] = @default if @default
176
180
  json[:enum] = @enum.to_a if @enum
@@ -4,16 +4,34 @@ module Schemacop
4
4
  class NumericNode < Node
5
5
  ATTRIBUTES = %i[
6
6
  minimum
7
- exclusive_minimum
8
7
  maximum
9
- exclusive_maximum
10
8
  multiple_of
9
+ exclusive_minimum
10
+ exclusive_maximum
11
11
  ].freeze
12
12
 
13
13
  def self.allowed_options
14
14
  super + ATTRIBUTES
15
15
  end
16
16
 
17
+ def process_json(attrs, json)
18
+ if context.swagger_json?
19
+ if options[:exclusive_minimum]
20
+ json[:minimum] = options[:exclusive_minimum]
21
+ json[:exclusiveMinimum] = true
22
+ end
23
+
24
+ if options[:exclusive_maximum]
25
+ json[:maximum] = options[:exclusive_maximum]
26
+ json[:exclusiveMaximum] = true
27
+ end
28
+
29
+ attrs -= %i[exclusive_minimum exclusive_maximum]
30
+ end
31
+
32
+ super attrs, json
33
+ end
34
+
17
35
  def _validate(data, result:)
18
36
  super_data = super
19
37
  return if super_data.nil?
@@ -4,7 +4,6 @@ module Schemacop
4
4
  ATTRIBUTES = %i[
5
5
  min_length
6
6
  max_length
7
- pattern
8
7
  format
9
8
  ].freeze
10
9
 
@@ -16,13 +15,14 @@ module Schemacop
16
15
  email: URI::MailTo::EMAIL_REGEXP,
17
16
  boolean: /^(true|false)$/,
18
17
  binary: nil,
18
+ symbol: nil,
19
19
  integer: /^-?[0-9]+$/,
20
20
  number: /^-?[0-9]+(\.[0-9]+)?$/
21
21
  }.freeze
22
22
  # rubocop:enable Layout/LineLength
23
23
 
24
24
  def self.allowed_options
25
- super + ATTRIBUTES - %i[cast_str] + %i[format_options]
25
+ super + ATTRIBUTES - %i[cast_str] + %i[format_options pattern]
26
26
  end
27
27
 
28
28
  def allowed_types
@@ -30,7 +30,11 @@ module Schemacop
30
30
  end
31
31
 
32
32
  def as_json
33
- process_json(ATTRIBUTES, type: :string)
33
+ json = { type: :string }
34
+ if options[:pattern]
35
+ json[:pattern] = V3.sanitize_exp(Regexp.compile(options[:pattern]))
36
+ end
37
+ process_json(ATTRIBUTES, json)
34
38
  end
35
39
 
36
40
  def _validate(data, result:)
@@ -49,8 +53,14 @@ module Schemacop
49
53
  end
50
54
 
51
55
  # Validate pattern #
52
- if options[:pattern] && !super_data.match?(Regexp.compile(options[:pattern]))
53
- result.error "String does not match pattern #{options[:pattern].inspect}."
56
+ if (pattern = options[:pattern])
57
+ unless options[:pattern].is_a?(Regexp)
58
+ pattern = Regexp.compile(pattern)
59
+ end
60
+
61
+ unless super_data.match?(pattern)
62
+ result.error "String does not match pattern #{V3.sanitize_exp(pattern).inspect}."
63
+ end
54
64
  end
55
65
 
56
66
  # Validate format #
@@ -87,6 +97,8 @@ module Schemacop
87
97
  return Integer(to_cast)
88
98
  when :number
89
99
  return Float(to_cast)
100
+ when :symbol
101
+ return to_cast.to_sym
90
102
  else
91
103
  return to_cast
92
104
  end
@@ -118,7 +130,9 @@ module Schemacop
118
130
  end
119
131
 
120
132
  if options[:pattern]
121
- fail 'Option "pattern" must be a string.' unless options[:pattern].is_a?(String)
133
+ unless options[:pattern].is_a?(String) || options[:pattern].is_a?(Regexp)
134
+ fail 'Option "pattern" must be a string or Regexp.'
135
+ end
122
136
 
123
137
  begin
124
138
  Regexp.compile(options[:pattern])
data/schemacop.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: schemacop 3.0.0 ruby lib
2
+ # stub: schemacop 3.0.5 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "schemacop".freeze
6
- s.version = "3.0.0"
6
+ s.version = "3.0.5"
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 = "2021-02-08"
11
+ s.date = "2021-02-16"
12
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]
@@ -142,6 +142,11 @@ class V3Test < SchemacopTest
142
142
  assert_equal expected_json.as_json, @schema.as_json.as_json
143
143
  end
144
144
 
145
+ def assert_swagger_json(expected_json)
146
+ # TODO: Double "as_json" should not be necessary
147
+ assert_equal expected_json.as_json, @schema.as_json(json_format: :swagger).as_json
148
+ end
149
+
145
150
  def assert_match_any(array, exp)
146
151
  assert array.any? { |element| element.match?(exp) },
147
152
  "Expected any of #{array.inspect} to match #{exp}."
@@ -120,6 +120,22 @@ module Schemacop
120
120
  ]
121
121
  })
122
122
  end
123
+
124
+ def test_cast
125
+ schema :boolean
126
+
127
+ assert_cast(true, true)
128
+ assert_cast(false, false)
129
+ assert_cast(nil, nil)
130
+ end
131
+
132
+ def test_cast_default
133
+ schema :boolean, default: true
134
+
135
+ assert_cast(true, true)
136
+ assert_cast(false, false)
137
+ assert_cast(nil, true)
138
+ end
123
139
  end
124
140
  end
125
141
  end
@@ -125,6 +125,12 @@ module Schemacop
125
125
  exclusiveMinimum: 0
126
126
  )
127
127
 
128
+ assert_swagger_json(
129
+ type: :integer,
130
+ minimum: 0,
131
+ exclusiveMinimum: true
132
+ )
133
+
128
134
  assert_validation 5
129
135
  assert_validation 1
130
136
  assert_validation(0) do
@@ -158,6 +164,12 @@ module Schemacop
158
164
  exclusiveMaximum: 5
159
165
  )
160
166
 
167
+ assert_swagger_json(
168
+ type: :integer,
169
+ maximum: 5,
170
+ exclusiveMaximum: true
171
+ )
172
+
161
173
  assert_validation 4
162
174
  assert_validation 1
163
175
  assert_validation(5) do
@@ -63,6 +63,20 @@ module Schemacop
63
63
  })
64
64
  end
65
65
 
66
+ def test_swagger_example
67
+ schema :string, examples: ['Foo', 'Foo bar']
68
+
69
+ assert_json(
70
+ type: :string,
71
+ examples: ['Foo', 'Foo bar']
72
+ )
73
+
74
+ assert_swagger_json(
75
+ type: :string,
76
+ example: ['Foo', 'Foo bar']
77
+ )
78
+ end
79
+
66
80
  def test_cast_in_root
67
81
  schema :integer, cast_str: true, required: true
68
82
 
@@ -101,6 +101,12 @@ module Schemacop
101
101
  exclusiveMinimum: 0
102
102
  )
103
103
 
104
+ assert_swagger_json(
105
+ type: :number,
106
+ minimum: 0,
107
+ exclusiveMinimum: true
108
+ )
109
+
104
110
  assert_validation 5
105
111
  assert_validation 1
106
112
  assert_validation(0) do
@@ -134,6 +140,12 @@ module Schemacop
134
140
  exclusiveMaximum: 5
135
141
  )
136
142
 
143
+ assert_swagger_json(
144
+ type: :number,
145
+ maximum: 5,
146
+ exclusiveMaximum: true
147
+ )
148
+
137
149
  assert_validation 4
138
150
  assert_validation 1
139
151
  assert_validation(5) do
@@ -80,7 +80,7 @@ module Schemacop
80
80
  end
81
81
  end
82
82
 
83
- def test_pattern
83
+ def test_pattern_as_string
84
84
  schema :string, pattern: '^a_.*_z$'
85
85
 
86
86
  assert_json(type: :string, pattern: '^a_.*_z$')
@@ -95,6 +95,22 @@ module Schemacop
95
95
  end
96
96
  end
97
97
 
98
+ def test_pattern_as_regexp
99
+ schema :string, pattern: /^a_.*_z$/i
100
+
101
+ assert_json(type: :string, pattern: '^(?i)(a_.*_z)$')
102
+
103
+ assert_validation 'a__z'
104
+ assert_validation 'a__Z'
105
+ assert_validation 'a_ foo bar _Z'
106
+ assert_validation '' do
107
+ error '/', 'String does not match pattern "^(?i)(a_.*_z)$".'
108
+ end
109
+ assert_validation 'a_ _zfoo' do
110
+ error '/', 'String does not match pattern "^(?i)(a_.*_z)$".'
111
+ end
112
+ end
113
+
98
114
  def test_format_date
99
115
  schema :string, format: :date
100
116
 
@@ -165,6 +181,23 @@ module Schemacop
165
181
  assert_cast('john.doe@example.com', 'john.doe@example.com')
166
182
  end
167
183
 
184
+ def test_format_symbol
185
+ schema :string, format: :symbol
186
+
187
+ assert_json(type: :string, format: :symbol)
188
+
189
+ assert_validation 'foobar'
190
+ assert_validation ''
191
+
192
+ assert_validation 234 do
193
+ error '/', 'Invalid type, expected "string".'
194
+ end
195
+
196
+ assert_cast(nil, nil)
197
+ assert_cast('foobar', :foobar)
198
+ assert_cast('039n23$g- sfk3/', :'039n23$g- sfk3/')
199
+ end
200
+
168
201
  def test_enum
169
202
  schema :string, enum: ['foo', 'some string', 'some other string', 42]
170
203
 
@@ -297,8 +330,8 @@ module Schemacop
297
330
  end
298
331
 
299
332
  assert_raises_with_message Exceptions::InvalidSchemaError,
300
- 'Option "pattern" must be a string.' do
301
- schema :string, pattern: //
333
+ 'Option "pattern" must be a string or Regexp.' do
334
+ schema :string, pattern: 42
302
335
  end
303
336
 
304
337
  assert_raises_with_message Exceptions::InvalidSchemaError,
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: 3.0.0
4
+ version: 3.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sitrox
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-08 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -164,8 +164,8 @@ dependencies:
164
164
  - - '='
165
165
  - !ruby/object:Gem::Version
166
166
  version: 0.21.2
167
- description:
168
- email:
167
+ description:
168
+ email:
169
169
  executables: []
170
170
  extensions: []
171
171
  extra_rdoc_files: []
@@ -277,7 +277,7 @@ homepage: https://github.com/sitrox/schemacop
277
277
  licenses:
278
278
  - MIT
279
279
  metadata: {}
280
- post_install_message:
280
+ post_install_message:
281
281
  rdoc_options: []
282
282
  require_paths:
283
283
  - lib
@@ -292,8 +292,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
292
292
  - !ruby/object:Gem::Version
293
293
  version: '0'
294
294
  requirements: []
295
- rubygems_version: 3.0.3
296
- signing_key:
295
+ rubygems_version: 3.1.4
296
+ signing_key:
297
297
  specification_version: 4
298
298
  summary: Schemacop validates ruby structures consisting of nested hashes and arrays
299
299
  against simple schema definitions.