schemacop 2.4.5 → 3.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +25 -1
- data/.travis.yml +3 -1
- data/CHANGELOG.md +32 -1
- data/README.md +53 -710
- data/README_V2.md +775 -0
- data/README_V3.md +1195 -0
- data/Rakefile +8 -12
- data/VERSION +1 -1
- data/lib/schemacop.rb +35 -36
- data/lib/schemacop/base_schema.rb +37 -0
- data/lib/schemacop/railtie.rb +10 -0
- data/lib/schemacop/schema.rb +1 -60
- data/lib/schemacop/schema2.rb +22 -0
- data/lib/schemacop/schema3.rb +21 -0
- data/lib/schemacop/scoped_env.rb +25 -13
- data/lib/schemacop/v2.rb +26 -0
- data/lib/schemacop/{caster.rb → v2/caster.rb} +16 -2
- data/lib/schemacop/{collector.rb → v2/collector.rb} +5 -2
- data/lib/schemacop/{dupper.rb → v2/dupper.rb} +1 -1
- data/lib/schemacop/{field_node.rb → v2/field_node.rb} +4 -3
- data/lib/schemacop/v2/node.rb +142 -0
- data/lib/schemacop/{node_resolver.rb → v2/node_resolver.rb} +1 -1
- data/lib/schemacop/v2/node_supporting_field.rb +70 -0
- data/lib/schemacop/{node_supporting_type.rb → v2/node_supporting_type.rb} +14 -11
- data/lib/schemacop/{node_with_block.rb → v2/node_with_block.rb} +3 -2
- data/lib/schemacop/v2/root_node.rb +6 -0
- data/lib/schemacop/v2/validator/array_validator.rb +32 -0
- data/lib/schemacop/{validator → v2/validator}/boolean_validator.rb +1 -1
- data/lib/schemacop/v2/validator/float_validator.rb +7 -0
- data/lib/schemacop/v2/validator/hash_validator.rb +37 -0
- data/lib/schemacop/v2/validator/integer_validator.rb +7 -0
- data/lib/schemacop/{validator → v2/validator}/nil_validator.rb +1 -1
- data/lib/schemacop/v2/validator/number_validator.rb +21 -0
- data/lib/schemacop/v2/validator/object_validator.rb +29 -0
- data/lib/schemacop/v2/validator/string_validator.rb +39 -0
- data/lib/schemacop/{validator → v2/validator}/symbol_validator.rb +1 -1
- data/lib/schemacop/v3.rb +45 -0
- data/lib/schemacop/v3/all_of_node.rb +27 -0
- data/lib/schemacop/v3/any_of_node.rb +28 -0
- data/lib/schemacop/v3/array_node.rb +218 -0
- data/lib/schemacop/v3/boolean_node.rb +16 -0
- data/lib/schemacop/v3/combination_node.rb +45 -0
- data/lib/schemacop/v3/context.rb +17 -0
- data/lib/schemacop/v3/dsl_scope.rb +46 -0
- data/lib/schemacop/v3/global_context.rb +114 -0
- data/lib/schemacop/v3/hash_node.rb +256 -0
- data/lib/schemacop/v3/integer_node.rb +13 -0
- data/lib/schemacop/v3/is_not_node.rb +32 -0
- data/lib/schemacop/v3/node.rb +215 -0
- data/lib/schemacop/v3/node_registry.rb +49 -0
- data/lib/schemacop/v3/number_node.rb +18 -0
- data/lib/schemacop/v3/numeric_node.rb +76 -0
- data/lib/schemacop/v3/object_node.rb +40 -0
- data/lib/schemacop/v3/one_of_node.rb +28 -0
- data/lib/schemacop/v3/reference_node.rb +49 -0
- data/lib/schemacop/v3/result.rb +58 -0
- data/lib/schemacop/v3/string_node.rb +124 -0
- data/lib/schemacop/v3/symbol_node.rb +13 -0
- data/schemacop.gemspec +24 -27
- data/test/lib/test_helper.rb +152 -0
- data/test/schemas/nested/group.rb +6 -0
- data/test/schemas/user.rb +7 -0
- data/test/unit/schemacop/v2/casting_test.rb +120 -0
- data/test/unit/schemacop/v2/collector_test.rb +47 -0
- data/test/unit/schemacop/v2/custom_check_test.rb +95 -0
- data/test/unit/schemacop/v2/custom_if_test.rb +97 -0
- data/test/unit/schemacop/v2/defaults_test.rb +95 -0
- data/test/unit/schemacop/v2/empty_test.rb +16 -0
- data/test/unit/schemacop/v2/nil_dis_allow_test.rb +43 -0
- data/test/unit/schemacop/v2/node_resolver_test.rb +28 -0
- data/test/unit/schemacop/v2/short_forms_test.rb +351 -0
- data/test/unit/schemacop/v2/types_test.rb +88 -0
- data/test/unit/schemacop/v2/validator_array_test.rb +99 -0
- data/test/unit/schemacop/v2/validator_boolean_test.rb +17 -0
- data/test/unit/schemacop/v2/validator_float_test.rb +59 -0
- data/test/unit/schemacop/v2/validator_hash_test.rb +95 -0
- data/test/unit/schemacop/v2/validator_integer_test.rb +48 -0
- data/test/unit/schemacop/v2/validator_nil_test.rb +15 -0
- data/test/unit/schemacop/v2/validator_number_test.rb +62 -0
- data/test/unit/schemacop/v2/validator_object_test.rb +141 -0
- data/test/unit/schemacop/v2/validator_string_test.rb +78 -0
- data/test/unit/schemacop/v2/validator_symbol_test.rb +18 -0
- data/test/unit/schemacop/v3/all_of_node_test.rb +198 -0
- data/test/unit/schemacop/v3/any_of_node_test.rb +218 -0
- data/test/unit/schemacop/v3/array_node_test.rb +815 -0
- data/test/unit/schemacop/v3/boolean_node_test.rb +126 -0
- data/test/unit/schemacop/v3/global_context_test.rb +164 -0
- data/test/unit/schemacop/v3/hash_node_test.rb +884 -0
- data/test/unit/schemacop/v3/integer_node_test.rb +323 -0
- data/test/unit/schemacop/v3/is_not_node_test.rb +173 -0
- data/test/unit/schemacop/v3/node_test.rb +148 -0
- data/test/unit/schemacop/v3/number_node_test.rb +292 -0
- data/test/unit/schemacop/v3/object_node_test.rb +170 -0
- data/test/unit/schemacop/v3/one_of_node_test.rb +187 -0
- data/test/unit/schemacop/v3/reference_node_test.rb +351 -0
- data/test/unit/schemacop/v3/string_node_test.rb +334 -0
- data/test/unit/schemacop/v3/symbol_node_test.rb +75 -0
- metadata +152 -145
- data/doc/Schemacop.html +0 -146
- data/doc/Schemacop/ArrayValidator.html +0 -329
- data/doc/Schemacop/BooleanValidator.html +0 -145
- data/doc/Schemacop/Caster.html +0 -379
- data/doc/Schemacop/Collector.html +0 -787
- data/doc/Schemacop/Dupper.html +0 -214
- data/doc/Schemacop/Exceptions.html +0 -115
- data/doc/Schemacop/Exceptions/InvalidSchemaError.html +0 -124
- data/doc/Schemacop/Exceptions/ValidationError.html +0 -124
- data/doc/Schemacop/FieldNode.html +0 -421
- data/doc/Schemacop/FloatValidator.html +0 -158
- data/doc/Schemacop/HashValidator.html +0 -293
- data/doc/Schemacop/IntegerValidator.html +0 -158
- data/doc/Schemacop/NilValidator.html +0 -145
- data/doc/Schemacop/Node.html +0 -1438
- data/doc/Schemacop/NodeResolver.html +0 -258
- data/doc/Schemacop/NodeSupportingField.html +0 -590
- data/doc/Schemacop/NodeSupportingType.html +0 -612
- data/doc/Schemacop/NodeWithBlock.html +0 -289
- data/doc/Schemacop/NumberValidator.html +0 -232
- data/doc/Schemacop/ObjectValidator.html +0 -298
- data/doc/Schemacop/RootNode.html +0 -171
- data/doc/Schemacop/Schema.html +0 -699
- data/doc/Schemacop/StringValidator.html +0 -295
- data/doc/Schemacop/SymbolValidator.html +0 -145
- data/doc/ScopedEnv.html +0 -351
- data/doc/_index.html +0 -379
- data/doc/class_list.html +0 -51
- data/doc/css/common.css +0 -1
- data/doc/css/full_list.css +0 -58
- data/doc/css/style.css +0 -496
- data/doc/file.README.html +0 -833
- data/doc/file_list.html +0 -56
- data/doc/frames.html +0 -17
- data/doc/index.html +0 -833
- data/doc/inheritance.graphml +0 -524
- data/doc/inheritance.pdf +0 -825
- data/doc/js/app.js +0 -303
- data/doc/js/full_list.js +0 -216
- data/doc/js/jquery.js +0 -4
- data/doc/method_list.html +0 -587
- data/doc/top-level-namespace.html +0 -112
- data/lib/schemacop/node.rb +0 -139
- data/lib/schemacop/node_supporting_field.rb +0 -58
- data/lib/schemacop/root_node.rb +0 -4
- data/lib/schemacop/validator/array_validator.rb +0 -30
- data/lib/schemacop/validator/float_validator.rb +0 -5
- data/lib/schemacop/validator/hash_validator.rb +0 -35
- data/lib/schemacop/validator/integer_validator.rb +0 -5
- data/lib/schemacop/validator/number_validator.rb +0 -19
- data/lib/schemacop/validator/object_validator.rb +0 -27
- data/lib/schemacop/validator/string_validator.rb +0 -37
- data/test/casting_test.rb +0 -90
- data/test/collector_test.rb +0 -45
- data/test/custom_check_test.rb +0 -93
- data/test/custom_if_test.rb +0 -95
- data/test/defaults_test.rb +0 -93
- data/test/empty_test.rb +0 -14
- data/test/nil_dis_allow_test.rb +0 -41
- data/test/node_resolver_test.rb +0 -26
- data/test/short_forms_test.rb +0 -349
- data/test/test_helper.rb +0 -13
- data/test/types_test.rb +0 -84
- data/test/validator_array_test.rb +0 -97
- data/test/validator_boolean_test.rb +0 -15
- data/test/validator_float_test.rb +0 -57
- data/test/validator_hash_test.rb +0 -93
- data/test/validator_integer_test.rb +0 -46
- data/test/validator_nil_test.rb +0 -13
- data/test/validator_number_test.rb +0 -60
- data/test/validator_object_test.rb +0 -139
- data/test/validator_string_test.rb +0 -76
- data/test/validator_symbol_test.rb +0 -16
@@ -0,0 +1,58 @@
|
|
1
|
+
module Schemacop
|
2
|
+
class Result
|
3
|
+
attr_reader :current_path
|
4
|
+
attr_reader :errors
|
5
|
+
|
6
|
+
def initialize(root = nil, original_data = nil)
|
7
|
+
@current_path = []
|
8
|
+
@errors = {}
|
9
|
+
@root = root
|
10
|
+
@original_data = original_data
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
errors.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
def data
|
18
|
+
if errors.any?
|
19
|
+
return nil
|
20
|
+
else
|
21
|
+
return @data ||= @root.cast(@original_data)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def error(message)
|
26
|
+
@errors[current_path] ||= []
|
27
|
+
@errors[current_path] << message
|
28
|
+
end
|
29
|
+
|
30
|
+
def messages_by_path
|
31
|
+
@errors.transform_keys { |k| "/#{k.join('/')}" }
|
32
|
+
end
|
33
|
+
|
34
|
+
def exception_message
|
35
|
+
messages.join("\n")
|
36
|
+
end
|
37
|
+
|
38
|
+
def messages
|
39
|
+
messages = []
|
40
|
+
|
41
|
+
@errors.each do |path, path_messages|
|
42
|
+
messages += path_messages.map do |path_message|
|
43
|
+
"/#{path.join('/')}: #{path_message}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
return messages
|
48
|
+
end
|
49
|
+
|
50
|
+
def in_path(segment)
|
51
|
+
prev_path = @current_path
|
52
|
+
@current_path += [segment]
|
53
|
+
yield
|
54
|
+
ensure
|
55
|
+
@current_path = prev_path
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
module Schemacop
|
2
|
+
module V3
|
3
|
+
class StringNode < Node
|
4
|
+
ATTRIBUTES = %i[
|
5
|
+
min_length
|
6
|
+
max_length
|
7
|
+
pattern
|
8
|
+
format
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
# rubocop:disable Layout/LineLength
|
12
|
+
FORMAT_PATTERNS = {
|
13
|
+
date: /^([0-9]{4})-?(1[0-2]|0[1-9])-?(3[01]|0[1-9]|[12][0-9])$/,
|
14
|
+
'date-time': /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/,
|
15
|
+
time: /^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/,
|
16
|
+
email: URI::MailTo::EMAIL_REGEXP,
|
17
|
+
boolean: /^(true|false)$/,
|
18
|
+
binary: nil,
|
19
|
+
integer: /^-?[0-9]+$/,
|
20
|
+
number: /^-?[0-9]+(\.[0-9]+)?$/
|
21
|
+
}.freeze
|
22
|
+
# rubocop:enable Layout/LineLength
|
23
|
+
|
24
|
+
def self.allowed_options
|
25
|
+
super + ATTRIBUTES - %i[cast_str] + %i[format_options]
|
26
|
+
end
|
27
|
+
|
28
|
+
def allowed_types
|
29
|
+
{ String => :string }
|
30
|
+
end
|
31
|
+
|
32
|
+
def as_json
|
33
|
+
process_json(ATTRIBUTES, type: :string)
|
34
|
+
end
|
35
|
+
|
36
|
+
def _validate(data, result:)
|
37
|
+
super_data = super
|
38
|
+
return if super_data.nil?
|
39
|
+
|
40
|
+
# Validate length #
|
41
|
+
length = super_data.size
|
42
|
+
|
43
|
+
if options[:min_length] && length < options[:min_length]
|
44
|
+
result.error "String is #{length} characters long but must be at least #{options[:min_length]}."
|
45
|
+
end
|
46
|
+
|
47
|
+
if options[:max_length] && length > options[:max_length]
|
48
|
+
result.error "String is #{length} characters long but must be at most #{options[:max_length]}."
|
49
|
+
end
|
50
|
+
|
51
|
+
# Validate pattern #
|
52
|
+
if options[:pattern] && !super_data.match?(Regexp.compile(options[:pattern]))
|
53
|
+
result.error "String does not match pattern #{options[:pattern].inspect}."
|
54
|
+
end
|
55
|
+
|
56
|
+
# Validate format #
|
57
|
+
if options[:format] && FORMAT_PATTERNS.include?(options[:format])
|
58
|
+
pattern = FORMAT_PATTERNS[options[:format]]
|
59
|
+
if pattern && !super_data.match?(pattern)
|
60
|
+
result.error "String does not match format #{options[:format].to_s.inspect}."
|
61
|
+
elsif options[:format_options] && Node.resolve_class(options[:format])
|
62
|
+
node = create(options[:format], **options[:format_options])
|
63
|
+
node._validate(cast(super_data), result: result)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def cast(value)
|
69
|
+
case options[:format]
|
70
|
+
when :boolean
|
71
|
+
return value == 'true'
|
72
|
+
when :date
|
73
|
+
return Date.parse(value)
|
74
|
+
when :'date-time'
|
75
|
+
return DateTime.parse(value)
|
76
|
+
when :time
|
77
|
+
Time.parse(value)
|
78
|
+
when :integer
|
79
|
+
return Integer(value)
|
80
|
+
when :number
|
81
|
+
return Float(value)
|
82
|
+
else
|
83
|
+
return value || default
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
protected
|
88
|
+
|
89
|
+
def init
|
90
|
+
if options.include?(:format)
|
91
|
+
options[:format] = options[:format].to_s.dasherize.to_sym
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def validate_self
|
96
|
+
if options.include?(:format) && !FORMAT_PATTERNS.include?(options[:format])
|
97
|
+
fail "Format #{options[:format].to_s.inspect} is not supported."
|
98
|
+
end
|
99
|
+
|
100
|
+
unless options[:min_length].nil? || options[:min_length].is_a?(Integer)
|
101
|
+
fail 'Option "min_length" must be an "integer"'
|
102
|
+
end
|
103
|
+
|
104
|
+
unless options[:max_length].nil? || options[:max_length].is_a?(Integer)
|
105
|
+
fail 'Option "max_length" must be an "integer"'
|
106
|
+
end
|
107
|
+
|
108
|
+
if options[:min_length] && options[:max_length] && options[:min_length] > options[:max_length]
|
109
|
+
fail 'Option "min_length" can\'t be greater than "max_length".'
|
110
|
+
end
|
111
|
+
|
112
|
+
if options[:pattern]
|
113
|
+
fail 'Option "pattern" must be a string.' unless options[:pattern].is_a?(String)
|
114
|
+
|
115
|
+
begin
|
116
|
+
Regexp.compile(options[:pattern])
|
117
|
+
rescue RegexpError => e
|
118
|
+
fail "Option \"pattern\" can't be parsed: #{e.message}."
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
data/schemacop.gemspec
CHANGED
@@ -1,60 +1,57 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: schemacop
|
2
|
+
# stub: schemacop 3.0.0.rc2 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "schemacop".freeze
|
6
|
-
s.version = "
|
6
|
+
s.version = "3.0.0.rc2"
|
7
7
|
|
8
|
-
s.required_rubygems_version = Gem::Requirement.new("
|
8
|
+
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1".freeze) if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib".freeze]
|
10
10
|
s.authors = ["Sitrox".freeze]
|
11
|
-
s.date = "
|
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, "
|
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]
|
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
|
16
16
|
s.summary = "Schemacop validates ruby structures consisting of nested hashes and arrays against simple schema definitions.".freeze
|
17
|
-
s.test_files = ["test/
|
17
|
+
s.test_files = ["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]
|
18
18
|
|
19
19
|
if s.respond_to? :specification_version then
|
20
20
|
s.specification_version = 4
|
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_development_dependency(%q<bundler>.freeze, ["
|
24
|
+
s.add_development_dependency(%q<bundler>.freeze, [">= 0"])
|
25
25
|
s.add_development_dependency(%q<rake>.freeze, [">= 0"])
|
26
|
-
s.add_development_dependency(%q<
|
27
|
-
s.add_development_dependency(%q<
|
28
|
-
s.add_development_dependency(%q<haml>.freeze, [">= 0"])
|
26
|
+
s.add_development_dependency(%q<minitest>.freeze, [">= 0"])
|
27
|
+
s.add_development_dependency(%q<minitest-reporters>.freeze, [">= 0"])
|
29
28
|
s.add_development_dependency(%q<colorize>.freeze, [">= 0"])
|
30
|
-
s.add_development_dependency(%q<
|
31
|
-
s.add_development_dependency(%q<rubocop>.freeze, ["= 0.35.1"])
|
32
|
-
s.add_development_dependency(%q<redcarpet>.freeze, [">= 0"])
|
29
|
+
s.add_development_dependency(%q<rubocop>.freeze, ["= 0.92.0"])
|
33
30
|
s.add_development_dependency(%q<pry>.freeze, [">= 0"])
|
31
|
+
s.add_development_dependency(%q<byebug>.freeze, [">= 0"])
|
32
|
+
s.add_development_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
|
34
33
|
else
|
35
34
|
s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
|
36
|
-
s.add_dependency(%q<bundler>.freeze, ["
|
35
|
+
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
37
36
|
s.add_dependency(%q<rake>.freeze, [">= 0"])
|
38
|
-
s.add_dependency(%q<
|
39
|
-
s.add_dependency(%q<
|
40
|
-
s.add_dependency(%q<haml>.freeze, [">= 0"])
|
37
|
+
s.add_dependency(%q<minitest>.freeze, [">= 0"])
|
38
|
+
s.add_dependency(%q<minitest-reporters>.freeze, [">= 0"])
|
41
39
|
s.add_dependency(%q<colorize>.freeze, [">= 0"])
|
42
|
-
s.add_dependency(%q<
|
43
|
-
s.add_dependency(%q<rubocop>.freeze, ["= 0.35.1"])
|
44
|
-
s.add_dependency(%q<redcarpet>.freeze, [">= 0"])
|
40
|
+
s.add_dependency(%q<rubocop>.freeze, ["= 0.92.0"])
|
45
41
|
s.add_dependency(%q<pry>.freeze, [">= 0"])
|
42
|
+
s.add_dependency(%q<byebug>.freeze, [">= 0"])
|
43
|
+
s.add_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
|
46
44
|
end
|
47
45
|
else
|
48
46
|
s.add_dependency(%q<activesupport>.freeze, [">= 4.0"])
|
49
|
-
s.add_dependency(%q<bundler>.freeze, ["
|
47
|
+
s.add_dependency(%q<bundler>.freeze, [">= 0"])
|
50
48
|
s.add_dependency(%q<rake>.freeze, [">= 0"])
|
51
|
-
s.add_dependency(%q<
|
52
|
-
s.add_dependency(%q<
|
53
|
-
s.add_dependency(%q<haml>.freeze, [">= 0"])
|
49
|
+
s.add_dependency(%q<minitest>.freeze, [">= 0"])
|
50
|
+
s.add_dependency(%q<minitest-reporters>.freeze, [">= 0"])
|
54
51
|
s.add_dependency(%q<colorize>.freeze, [">= 0"])
|
55
|
-
s.add_dependency(%q<
|
56
|
-
s.add_dependency(%q<rubocop>.freeze, ["= 0.35.1"])
|
57
|
-
s.add_dependency(%q<redcarpet>.freeze, [">= 0"])
|
52
|
+
s.add_dependency(%q<rubocop>.freeze, ["= 0.92.0"])
|
58
53
|
s.add_dependency(%q<pry>.freeze, [">= 0"])
|
54
|
+
s.add_dependency(%q<byebug>.freeze, [">= 0"])
|
55
|
+
s.add_dependency(%q<simplecov>.freeze, ["= 0.21.2"])
|
59
56
|
end
|
60
57
|
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
# TODO: Move to more sensible location
|
5
|
+
def assert_verr(&block)
|
6
|
+
assert_raises(Schemacop::V2::Exceptions::ValidationError, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
# TODO: Move to more sensible location
|
10
|
+
def assert_nothing_raised(&_block)
|
11
|
+
yield
|
12
|
+
assert true
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'minitest/autorun'
|
16
|
+
require 'minitest/reporters'
|
17
|
+
require 'schemacop'
|
18
|
+
require 'pry'
|
19
|
+
require 'colorize'
|
20
|
+
require 'byebug'
|
21
|
+
|
22
|
+
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]
|
23
|
+
|
24
|
+
class SchemacopTest < Minitest::Test
|
25
|
+
def assert_is_a(klass, object)
|
26
|
+
assert object.is_a?(klass),
|
27
|
+
"Expected object with class #{object.class.inspect} to be a #{klass.inspect}."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class V2Test < SchemacopTest
|
32
|
+
def setup
|
33
|
+
Schemacop.default_schema_version = 2
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class V3Test < SchemacopTest
|
38
|
+
class ValidationAssertion
|
39
|
+
attr_reader :errors
|
40
|
+
|
41
|
+
def initialize(&block)
|
42
|
+
@errors = {}
|
43
|
+
instance_exec(&block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def error(path, exp)
|
47
|
+
@errors[path] ||= []
|
48
|
+
@errors[path] << exp
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def setup
|
53
|
+
Schemacop.default_schema_version = 3
|
54
|
+
end
|
55
|
+
|
56
|
+
# Help test raise errors message
|
57
|
+
# https://ruby-doc.org/stdlib-2.1.5/libdoc/test/unit/rdoc/Test/Unit/Assertions.html#method-i-assert_raise
|
58
|
+
def assert_raises_with_message(exception, expected, msg = nil, &block)
|
59
|
+
case expected
|
60
|
+
when String
|
61
|
+
assert = :assert_equal
|
62
|
+
when Regexp
|
63
|
+
assert = :assert_match
|
64
|
+
else
|
65
|
+
fail TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
|
66
|
+
end
|
67
|
+
|
68
|
+
ex = assert_raises(exception, *msg, &block)
|
69
|
+
msg = message(msg, '') { "Expected Exception(#{exception}) was raised, but the message doesn't match" }
|
70
|
+
|
71
|
+
if assert == :assert_equal
|
72
|
+
assert_equal(expected, ex.message, msg)
|
73
|
+
else
|
74
|
+
msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp ex.message}" }
|
75
|
+
assert expected =~ ex.message, msg
|
76
|
+
block.binding.eval('proc{|_|$~=_}').call($LAST_MATCH_INFO)
|
77
|
+
end
|
78
|
+
return ex
|
79
|
+
end
|
80
|
+
|
81
|
+
def schema(type = :hash, **options, &block)
|
82
|
+
@schema = Schemacop::Schema3.new(type, **options, &block)
|
83
|
+
end
|
84
|
+
|
85
|
+
def with_context(context, &block)
|
86
|
+
Schemacop.with_context(context, &block)
|
87
|
+
end
|
88
|
+
|
89
|
+
def assert_validation(data, &block)
|
90
|
+
result = @schema.validate(data)
|
91
|
+
|
92
|
+
if block_given?
|
93
|
+
assertion = ValidationAssertion.new(&block)
|
94
|
+
|
95
|
+
assert_equal assertion.errors.keys.sort,
|
96
|
+
result.messages_by_path.keys.sort,
|
97
|
+
'Unexpected validation error paths'
|
98
|
+
|
99
|
+
assertion.errors.each do |path, expected_errors|
|
100
|
+
actual_errors = result.messages_by_path[path].dup
|
101
|
+
|
102
|
+
assert_equal expected_errors.size,
|
103
|
+
actual_errors.size,
|
104
|
+
"Unexpected number of messages for path #{path}"
|
105
|
+
|
106
|
+
expected_errors.each do |expected_error|
|
107
|
+
match = actual_errors.find do |ae|
|
108
|
+
if expected_error.is_a?(Regexp)
|
109
|
+
expected_error.match?(ae)
|
110
|
+
else
|
111
|
+
expected_error == ae
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
if match
|
116
|
+
actual_errors.delete(match)
|
117
|
+
else
|
118
|
+
assert match,
|
119
|
+
"Did not find error #{expected_error.inspect} in path #{path}, errors: " \
|
120
|
+
+ result.messages_by_path[path].inspect
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
else
|
125
|
+
assert result.errors.empty?,
|
126
|
+
"Expected data #{data.inspect} to match schema #{@schema.as_json}, but got errors: #{result.messages}."
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def assert_json(expected_json)
|
131
|
+
# TODO: Double "as_json" should not be necessary
|
132
|
+
assert_equal expected_json.as_json, @schema.as_json.as_json
|
133
|
+
end
|
134
|
+
|
135
|
+
def assert_match_any(array, exp)
|
136
|
+
assert array.any? { |element| element.match?(exp) },
|
137
|
+
"Expected any of #{array.inspect} to match #{exp}."
|
138
|
+
end
|
139
|
+
|
140
|
+
def assert_cast(input_data, expected_data)
|
141
|
+
input_data_was = Marshal.load(Marshal.dump(input_data))
|
142
|
+
result = @schema.validate(input_data)
|
143
|
+
assert_empty result.errors
|
144
|
+
assert_equal expected_data, result.data, 'Unexpected result data.'
|
145
|
+
|
146
|
+
if input_data.nil?
|
147
|
+
assert_nil input_data_was, 'Expected input_data to stay the same.'
|
148
|
+
else
|
149
|
+
assert_equal input_data, input_data_was, 'Expected input_data to stay the same.'
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|