schemacop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/schemacop.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: schemacop 0.0.1 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "schemacop"
6
+ s.version = "0.0.1"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["Sitrox"]
10
+ s.date = "2016-06-06"
11
+ s.files = [".gitignore", ".rubocop.yml", ".travis.yml", ".yardopts", "Gemfile", "LICENSE", "README.md", "RUBY_VERSION", "Rakefile", "VERSION", "doc/Schemacop.html", "doc/Schemacop/Exceptions.html", "doc/Schemacop/Exceptions/Base.html", "doc/Schemacop/Exceptions/InvalidSchema.html", "doc/Schemacop/Exceptions/Validation.html", "doc/Schemacop/Validator.html", "doc/_index.html", "doc/class_list.html", "doc/css/common.css", "doc/css/full_list.css", "doc/css/style.css", "doc/file.README.html", "doc/file_list.html", "doc/frames.html", "doc/index.html", "doc/js/app.js", "doc/js/full_list.js", "doc/js/jquery.js", "doc/method_list.html", "doc/top-level-namespace.html", "lib/schemacop.rb", "lib/schemacop/exceptions.rb", "lib/schemacop/validator.rb", "schemacop.gemspec", "test/schemacop_validator_test.rb"]
12
+ s.require_paths = ["lib"]
13
+ s.rubygems_version = "2.1.11"
14
+ s.summary = "Schemacop validates ruby structures consisting of nested hashes and arrays against simple schema definitions."
15
+ s.test_files = ["test/schemacop_validator_test.rb"]
16
+
17
+ if s.respond_to? :specification_version then
18
+ s.specification_version = 4
19
+
20
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
21
+ s.add_development_dependency(%q<bundler>, ["~> 1.3"])
22
+ s.add_development_dependency(%q<rake>, [">= 0"])
23
+ s.add_development_dependency(%q<ci_reporter>, ["~> 2.0"])
24
+ s.add_development_dependency(%q<ci_reporter_minitest>, [">= 0"])
25
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
26
+ s.add_development_dependency(%q<haml>, [">= 0"])
27
+ s.add_development_dependency(%q<yard>, [">= 0"])
28
+ s.add_development_dependency(%q<redcarpet>, [">= 0"])
29
+ else
30
+ s.add_dependency(%q<bundler>, ["~> 1.3"])
31
+ s.add_dependency(%q<rake>, [">= 0"])
32
+ s.add_dependency(%q<ci_reporter>, ["~> 2.0"])
33
+ s.add_dependency(%q<ci_reporter_minitest>, [">= 0"])
34
+ s.add_dependency(%q<activesupport>, [">= 0"])
35
+ s.add_dependency(%q<haml>, [">= 0"])
36
+ s.add_dependency(%q<yard>, [">= 0"])
37
+ s.add_dependency(%q<redcarpet>, [">= 0"])
38
+ end
39
+ else
40
+ s.add_dependency(%q<bundler>, ["~> 1.3"])
41
+ s.add_dependency(%q<rake>, [">= 0"])
42
+ s.add_dependency(%q<ci_reporter>, ["~> 2.0"])
43
+ s.add_dependency(%q<ci_reporter_minitest>, [">= 0"])
44
+ s.add_dependency(%q<activesupport>, [">= 0"])
45
+ s.add_dependency(%q<haml>, [">= 0"])
46
+ s.add_dependency(%q<yard>, [">= 0"])
47
+ s.add_dependency(%q<redcarpet>, [">= 0"])
48
+ end
49
+ end
@@ -0,0 +1,197 @@
1
+ require 'minitest/autorun'
2
+ require 'schemacop'
3
+
4
+ # rubocop: disable Metrics/ClassLength
5
+ class SchemacopValidatorTest < Minitest::Test
6
+ def test_top_level_schema
7
+ assert_schema({ type: :integer }, 1)
8
+ assert_schema({ type: :hash, null: true }, nil)
9
+ end
10
+
11
+ def test_short_type_schema
12
+ assert_schema({ type: :array, array: :string }, %w(Hello world))
13
+ assert_schema({ array: :string }, %w(Hello world))
14
+ assert_schema({ hash: { first_name: :string, last_name: :string } }, first_name: 'John', last_name: 'Doe')
15
+
16
+ assert_schema_failure({ type: :array }, %w(Hello world))
17
+ end
18
+
19
+ def test_many_types_schema
20
+ assert_schema({ type: [String, :integer] }, 'Hello world')
21
+ assert_schema({ type: [:string, 'integer'] }, 123)
22
+ assert_schema({ type: String }, 'Hello world')
23
+ assert_schema({ type: :string }, 'Hello world')
24
+ assert_schema({ type: :array, array: :string }, %w(John Doe))
25
+ assert_schema({ type: :hash, hash: { name: :string } }, name: 'John Doe')
26
+ assert_schema({ type: [:array, :hash], array: { type: :string }, hash: { name: :string } }, name: 'John Doe')
27
+ assert_schema({ type: [:array, :hash], array: { type: :string }, hash: { name: :string } }, %w(John Doe))
28
+ end
29
+
30
+ def test_unify_schema
31
+ schema_rules = {
32
+ hash: {
33
+ id: [Integer, String],
34
+ name: :string,
35
+ meta: {
36
+ hash: {
37
+ groups: { array: :integer },
38
+ birthday: Date,
39
+ comment: {
40
+ type: :string,
41
+ required: false,
42
+ null: true
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+
49
+ assert_schema(schema_rules, id: 123, name: 'John Doe', meta: { groups: [1, 2, 3], birthday: Date.today, comment: 'Hello world' })
50
+ assert_schema(schema_rules, id: 'XYZ', name: 'John Doe', meta: { groups: [1, 2, 3], birthday: Date.today })
51
+ end
52
+
53
+ def test_nested_hash_schema
54
+ schema_rules = {
55
+ type: :hash,
56
+ hash: {
57
+ name: {
58
+ type: :hash,
59
+ hash: {
60
+ first_name: { type: :string },
61
+ last_name: { type: :string }
62
+ }
63
+ }
64
+ }
65
+ }
66
+
67
+ assert_schema(schema_rules, name: { first_name: 'John', last_name: 'Doe' })
68
+ end
69
+
70
+ def test_flat_hash_old_schema
71
+ schema_rules = {
72
+ type: :hash,
73
+ fields: {
74
+ name: {
75
+ type: :string, null: true
76
+ }
77
+ }
78
+ }
79
+
80
+ assert_schema schema_rules, name: 'John Doe'
81
+ assert_schema schema_rules, name: ''
82
+ assert_schema schema_rules, name: nil
83
+
84
+ assert_schema_failure schema_rules, name: 123
85
+ assert_schema_failure schema_rules, name: true
86
+ assert_schema_failure schema_rules, name: []
87
+ end
88
+
89
+ def test_array_hash_old_schema
90
+ schema_rules = {
91
+ type: :array,
92
+ array: {
93
+ type: :hash,
94
+ fields: {
95
+ required: { type: :boolean },
96
+ name: { type: :string }
97
+ }
98
+ }
99
+ }
100
+
101
+ assert_schema schema_rules, [{ name: 'John', required: true }, { name: 'Alex', required: false }]
102
+
103
+ assert_schema_failure schema_rules, name: 'John Doe'
104
+ assert_schema_failure schema_rules, [{ name: 'John Doe' }]
105
+ assert_schema_failure schema_rules, [{ required: true }]
106
+ assert_schema_failure schema_rules, [{ name: 0, required: true }]
107
+ assert_schema_failure schema_rules, [{ name: 'John Doe', required: 'string' }]
108
+ end
109
+
110
+ def test_fields_array_old_schema
111
+ schema_rules = {
112
+ type: :hash,
113
+ fields: {
114
+ ids: {
115
+ type: :array,
116
+ array: {
117
+ type: :integer
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ assert_schema schema_rules, ids: []
124
+ assert_schema schema_rules, ids: [1, 2, 3]
125
+
126
+ assert_schema_failure schema_rules, ids: [1, '2']
127
+ assert_schema_failure schema_rules, ids: '3'
128
+ assert_schema_failure schema_rules, ids: nil
129
+ end
130
+
131
+ def test_validate_fields_old_schema
132
+ schema_rules = {
133
+ type: :hash,
134
+ fields: {
135
+ name: { type: :string, null: true, required: true },
136
+ age: { type: :integer, required: false, null: true },
137
+ id: { type: :integer, null: false },
138
+ ids: { type: :array, array: { type: :integer }, null: true },
139
+ currency: { type: :string, allowed_values: %w(CHF USD EUR) }
140
+ }
141
+ }
142
+
143
+ assert_schema schema_rules, name: 'John', id: 2, ids: [1, 2, 3], currency: 'CHF'
144
+ assert_schema schema_rules, name: nil, id: 2, ids: [1, 2, 3], currency: 'CHF'
145
+ assert_schema schema_rules, name: nil, age: 2, id: 2, ids: nil, currency: 'CHF'
146
+ assert_schema schema_rules, name: nil, id: 2, ids: nil, currency: 'CHF'
147
+
148
+ assert_schema_failure schema_rules, id: 2, ids: [1, 2, 3], currency: 'CHF'
149
+ assert_schema_failure schema_rules, name: nil, id: nil, ids: [1, 2, 3], currency: 'CHF'
150
+ assert_schema_failure schema_rules, name: nil, id: 2, ids: [1, 2, 3], currency: 'JPN'
151
+ end
152
+
153
+ def test_nested_old_schema
154
+ schema_rules = {
155
+ type: :array,
156
+ array: {
157
+ type: :hash,
158
+ fields: {
159
+ id: { type: :integer },
160
+ hosts: {
161
+ type: :array,
162
+ array: {
163
+ type: :hash,
164
+ fields: {
165
+ hostname: { type: :string }
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+
173
+ assert_schema schema_rules, [
174
+ { id: 1, hosts: [{ hostname: 'localhost' }, { hostname: '127.0.0.1' }] },
175
+ { id: 2, hosts: [{ hostname: '192.168.0.1' }, { hostname: 'somedomain.com' }] }
176
+ ]
177
+ end
178
+
179
+ private
180
+
181
+ def assert_schema_failure(schema, data)
182
+ assert_raises Schemacop::Exceptions::Validation do
183
+ Schemacop.validate!(schema, data)
184
+ end
185
+ end
186
+
187
+ def assert_schema(schema, data)
188
+ begin
189
+ Schemacop.validate!(schema, data)
190
+ success = true
191
+ rescue
192
+ success = false
193
+ end
194
+
195
+ assert success
196
+ end
197
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schemacop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sitrox
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ci_reporter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ci_reporter_minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: haml
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redcarpet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".gitignore"
132
+ - ".rubocop.yml"
133
+ - ".travis.yml"
134
+ - ".yardopts"
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - RUBY_VERSION
139
+ - Rakefile
140
+ - VERSION
141
+ - doc/Schemacop.html
142
+ - doc/Schemacop/Exceptions.html
143
+ - doc/Schemacop/Exceptions/Base.html
144
+ - doc/Schemacop/Exceptions/InvalidSchema.html
145
+ - doc/Schemacop/Exceptions/Validation.html
146
+ - doc/Schemacop/Validator.html
147
+ - doc/_index.html
148
+ - doc/class_list.html
149
+ - doc/css/common.css
150
+ - doc/css/full_list.css
151
+ - doc/css/style.css
152
+ - doc/file.README.html
153
+ - doc/file_list.html
154
+ - doc/frames.html
155
+ - doc/index.html
156
+ - doc/js/app.js
157
+ - doc/js/full_list.js
158
+ - doc/js/jquery.js
159
+ - doc/method_list.html
160
+ - doc/top-level-namespace.html
161
+ - lib/schemacop.rb
162
+ - lib/schemacop/exceptions.rb
163
+ - lib/schemacop/validator.rb
164
+ - schemacop.gemspec
165
+ - test/schemacop_validator_test.rb
166
+ homepage:
167
+ licenses: []
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.4.5.1
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Schemacop validates ruby structures consisting of nested hashes and arrays
189
+ against simple schema definitions.
190
+ test_files:
191
+ - test/schemacop_validator_test.rb
192
+ has_rdoc: