sekisyo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dc34b962bd5613306a875e2125fc79a9537574410838db07c71f5ff11ca9cdb3
4
+ data.tar.gz: dd7fe339e6c2540cea6fc7d6027d4f1e784f4379dafef843d54f73bfb5c9e9cc
5
+ SHA512:
6
+ metadata.gz: 213ea0a699bdccffff19e309b650dde6de60b3613af1ae5fd55d031baa0dca5803d09ae7cfe4fb0afa882cc63d80cd537df9955f4afd9b22f23a2e00f92cc20a
7
+ data.tar.gz: f0dc2a463412d3cbe04ae1d05592728378ee57339b335f9358661da2abd3da59e8606e162a4bb07487650716aad1a4378877a890524e9da92bd064f0a7ffc05a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - 'spec/**/*_spec.rb'
7
+
8
+ Style/PreferredHashMethods:
9
+ EnforcedStyle: verbose
10
+
11
+ Layout/LineLength:
12
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-03
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ride.poke@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'rack-test', '~> 1.1'
8
+ gem 'rspec', '~> 3.0'
9
+ gem 'rubocop', '~> 1.21'
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sekisyo (0.1.0)
5
+ hashie (~> 5.0)
6
+ rake (~> 13.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.0)
13
+ hashie (5.0.0)
14
+ parallel (1.21.0)
15
+ parser (3.1.1.0)
16
+ ast (~> 2.4.1)
17
+ rack (2.2.3)
18
+ rack-test (1.1.0)
19
+ rack (>= 1.0, < 3)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.2.1)
23
+ rexml (3.2.5)
24
+ rspec (3.11.0)
25
+ rspec-core (~> 3.11.0)
26
+ rspec-expectations (~> 3.11.0)
27
+ rspec-mocks (~> 3.11.0)
28
+ rspec-core (3.11.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-expectations (3.11.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-mocks (3.11.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-support (3.11.0)
37
+ rubocop (1.25.1)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.1.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml
43
+ rubocop-ast (>= 1.15.1, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 3.0)
46
+ rubocop-ast (1.16.0)
47
+ parser (>= 3.1.1.0)
48
+ ruby-progressbar (1.11.0)
49
+ unicode-display_width (2.1.0)
50
+
51
+ PLATFORMS
52
+ x86_64-darwin-19
53
+ x86_64-linux
54
+
55
+ DEPENDENCIES
56
+ rack-test (~> 1.1)
57
+ rspec (~> 3.0)
58
+ rubocop (~> 1.21)
59
+ sekisyo!
60
+
61
+ BUNDLED WITH
62
+ 2.3.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 rhiroe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,209 @@
1
+ # Sekisyo
2
+
3
+ [![Test](https://github.com/rhiroe/sekisyo/actions/workflows/main.yml/badge.svg)](https://github.com/rhiroe/sekisyo/actions/workflows/main.yml/badge.svg)
4
+
5
+ Sekisyo is rack middleware for input validation.
6
+
7
+ By writing a parameter whitelist in YAML files, requests that do not comply with it are rejected and returns status 400(Bad request).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'sekisyo'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install sekisyo
24
+
25
+ ## Usage
26
+
27
+ Use rack middleware in your application.
28
+
29
+ ```ruby
30
+ middleware.use Sekisyo::Middleware
31
+ ```
32
+
33
+ And write whitelist in YAML files.
34
+
35
+ ```yaml
36
+ paths:
37
+ '/pets':
38
+ get:
39
+ required []
40
+ properties: {}
41
+ ```
42
+
43
+ ## Configuration
44
+
45
+ - `file_paths` e.g. `['custom/whitelist.yml']`
46
+
47
+ The path to the yaml file is `sekisyo.yml` by default, but you can specify a custom path.
48
+
49
+ ```ruby
50
+ Sekisyo::Middleware.configure do |config|
51
+ config.file_paths = ['custom/whitelist.yml']
52
+ end
53
+ ```
54
+
55
+ - `undefined_request`
56
+
57
+ Can specify the behavior when a request is sent to a path that is not defined in the whitelist.
58
+
59
+ ```ruby
60
+ Sekisyo::Middleware.configure do |config|
61
+ config.undefined_request = :failure
62
+ end
63
+ ```
64
+
65
+ - `logger`
66
+
67
+ Can specify the log class.
68
+
69
+ ```ruby
70
+ Sekisyo::Middleware.configure do |config|
71
+ config.logger = Logger.new($stdout)
72
+ end
73
+ ```
74
+
75
+ - `allow_keys`
76
+
77
+ Can specifies the top-level key of the parameter to be exempt from validation.
78
+
79
+ ```ruby
80
+ Sekisyo::Middleware.configure do |config|
81
+ config.allow_keys = ['utf8', 'authenticity_token']
82
+ end
83
+ ```
84
+
85
+ ## Whitelist
86
+
87
+ Path can be specified as a dynamic URL. The path parameter can be enclosed in `{}`.
88
+
89
+ ```yaml
90
+ paths:
91
+ '/pets/{id}':
92
+ get:
93
+ required: []
94
+ properties:
95
+ id:
96
+ type: 'integer'
97
+ ```
98
+
99
+ Keys that must be included in the parameters can be specified in `required`.
100
+
101
+ ```yaml
102
+ paths:
103
+ '/pets':
104
+ post:
105
+ required:
106
+ - 'name'
107
+ properties:
108
+ name:
109
+ type: 'string'
110
+ ```
111
+
112
+ Parameters to be accepted are specified in `properties`.
113
+
114
+ ```yaml
115
+ paths:
116
+ '/pets/{id}':
117
+ put:
118
+ required: ['id']
119
+ properties:
120
+ id:
121
+ type: 'integer'
122
+ name:
123
+ type: 'string'
124
+ category:
125
+ type: 'object'
126
+ id:
127
+ type: 'integer'
128
+ ```
129
+
130
+ Can specify validation options that match the type.
131
+
132
+ ```yaml
133
+ paths:
134
+ '/pets':
135
+ get:
136
+ required []
137
+ properties:
138
+ status:
139
+ type: 'string'
140
+ enum:
141
+ - 'available'
142
+ - 'pending'
143
+ - 'sold'
144
+ ```
145
+
146
+ The type that can be specified are:
147
+
148
+ - `'string'`
149
+ - `'integer'`
150
+ - `'float'`
151
+ - `'numeric'`
152
+ - `'boolean'`
153
+ - `'array'`
154
+ - `'file'`
155
+ - `'object'`
156
+ - `'any'`
157
+
158
+ The options that can be specified are:
159
+
160
+ - string
161
+ - `presence` e.g. `true`
162
+ - `max_bytesize` e.g. `128`
163
+ - `enum` e.g. `['A', 'B']`
164
+ - `mutch` e.g. `'^\d{3}-\d{4}-\d{4}$'`
165
+ - integer
166
+ - `presence` e.g. `true`
167
+ - `max_bytesize` e.g. `128`
168
+ - `enum` e.g. `['1', '2']`
169
+ - float
170
+ - `presence` e.g. `true`
171
+ - `max_bytesize` e.g. `128`
172
+ - `enum` e.g. `['0.1', '0.2']`
173
+ - numeric
174
+ - `presence` e.g. `true`
175
+ - `max_bytesize` e.g. `128`
176
+ - `enum` e.g. `['0.1', '1']`
177
+ - boolean
178
+ - `presence` e.g. `true`
179
+ - array
180
+ - `presence` e.g. `true`
181
+ - `max_bytesize` e.g. `128`
182
+ - `min_size` e.g. `1`
183
+ - `max_size` e.g. `10`
184
+ - `items` e.g. `{ type: 'string', presence: true }`
185
+ - file
186
+ - `allow_file_types` e.g. `['image/jpeg', 'image/png', 'image/gif']`
187
+ - `presence` e.g. `true`
188
+ - `max_bytesize` e.g. `10000`
189
+ - any
190
+ - `presence` e.g. `true`
191
+ - `max_bytesize` e.g. `128`
192
+
193
+ ## Development
194
+
195
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
196
+
197
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
198
+
199
+ ## Contributing
200
+
201
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rhiroe/sekisyo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sekisyo/blob/master/CODE_OF_CONDUCT.md).
202
+
203
+ ## License
204
+
205
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
206
+
207
+ ## Code of Conduct
208
+
209
+ Everyone interacting in the Sekisyo project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sekisyo/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sekisyo
4
+ #
5
+ # Sekisyo Configuration is a wrapper class that transfers some methods to Hashie::Mash. Available methods are
6
+ #
7
+ # * <tt>deep_merge!</tt>
8
+ # * <tt>method_missing</tt>
9
+ # * <tt>[]</tt>
10
+ # * <tt>[]=</tt>
11
+ #
12
+ class Configuration
13
+ extend Forwardable
14
+ delegate :deep_merge! => :@options,
15
+ :method_missing => :@options,
16
+ :[] => :@options,
17
+ :[]= => :@options
18
+
19
+ def initialize
20
+ @options = Hashie::Mash.new
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sekisyo
4
+ #
5
+ # Sekisyo Middleware is rack middleware for input validation.
6
+ #
7
+ class Middleware
8
+ class << self
9
+ #
10
+ # @return [Sekisyo::Configuration] Configuration object, wrapper for Hashie::Mash.
11
+ #
12
+ def configuration
13
+ @configuration ||= Sekisyo::Configuration.new
14
+ end
15
+
16
+ #
17
+ # @return [Sekisyo::Whitelist] Whitelist of parameters to be received per request
18
+ #
19
+ def whitelist
20
+ @whitelist ||= Sekisyo::Whitelist.new.parse(*configuration.file_paths)
21
+ end
22
+
23
+ #
24
+ # @param [Hash] options Override configuration with this value.
25
+ #
26
+ # ==== Options
27
+ # * <tt>:file_paths</tt>(Array<String>) --- Whitelist yml file paths.
28
+ # * <tt>:undefined_request</tt>(Symbol, nil) --- Specifies what to do with the validation results
29
+ # if whitelist does not contain a description. Available values are nil(default), :warning, :failure
30
+ # * <tt>:logger</tt>(Logger) --- Specify log class instances.
31
+ # * <tt>:allow_keys</tt>(Array<Symbol, String>) --- Specifies the top-level key
32
+ # of the parameter to be exempt from validation.
33
+ #
34
+ # ==== Yield
35
+ # * (Sekisyo::Configuration)
36
+ #
37
+ # @return [void]
38
+ #
39
+ def configure(**options)
40
+ if block_given?
41
+ yield configuration
42
+ else
43
+ configuration.deep_merge!(options)
44
+ end
45
+ end
46
+
47
+ #
48
+ # @param [Symbol] name Configuration key
49
+ # @param [Any] value Configuration value
50
+ #
51
+ # @return [void]
52
+ #
53
+ def option(name, value = nil)
54
+ configuration[name] = value
55
+ end
56
+ end
57
+
58
+ def initialize(app)
59
+ @app = app
60
+ @configuration = self.class.configuration.dup
61
+ @whitelist = self.class.whitelist.dup
62
+ @logger = @configuration.logger
63
+ @allow_keys = @configuration.allow_keys.map(&:to_s)
64
+ end
65
+
66
+ option :file_paths, ['sekisyo.yml']
67
+ option :undefined_request, nil
68
+ option :logger, Logger.new($stdout)
69
+ option :allow_keys, []
70
+
71
+ def call(env)
72
+ return @app.call(env) if valid?(env)
73
+
74
+ code = 400
75
+ body = ['Bad request']
76
+ header = { 'Content-Type' => 'text/html;charset=utf-8',
77
+ 'Content-Length' => body.sum(&:bytesize).to_s,
78
+ 'X-XSS-Protection' => '1;mode=block',
79
+ 'X-Content-Type-Options' => 'nosniff',
80
+ 'X-Frame-Options' => 'SAMEORIGIN' }
81
+ [code, header, body]
82
+ end
83
+
84
+ private
85
+
86
+ #
87
+ # ==== Variable
88
+ # * <tt>white_uri</tt>(Sekisyo::WhitelistDetails::Path)
89
+ # * <tt>white_properties</tt>(Sekisyo::WhitelistDetails::Method)
90
+ # * <tt>params</tt>(Hash) --- Integrated request parameters and path parameters.
91
+ #
92
+ # @return [true, false]
93
+ #
94
+ def valid?(env)
95
+ white_uri = @whitelist.find(env['PATH_INFO'])
96
+ return undefined_request(env) if white_uri.nil?
97
+
98
+ white_properties = white_uri[env['REQUEST_METHOD']]
99
+ return undefined_request(env) if white_properties.nil?
100
+
101
+ params = Rack::Request.new(env).params.except(*@allow_keys)
102
+ params.merge!(white_uri.path_params(env['PATH_INFO']))
103
+ white_properties.valid?(params)
104
+ end
105
+
106
+ #
107
+ # Processes whether or not to allow a request if there is no whitelist definition for the request.
108
+ #
109
+ # @return [true, false]
110
+ #
111
+ def undefined_request(env)
112
+ case @configuration.undefined_request&.to_sym
113
+ when :warning
114
+ @logger.warn "Undefined white list #{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
115
+ true
116
+ when :failure
117
+ false
118
+ else
119
+ true
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'validators/any_validator'
4
+ require_relative 'validators/array_validator'
5
+ require_relative 'validators/boolean_validator'
6
+ require_relative 'validators/file_validator'
7
+ require_relative 'validators/float_validator'
8
+ require_relative 'validators/integer_validator'
9
+ require_relative 'validators/numeric_validator'
10
+ require_relative 'validators/object_validator'
11
+ require_relative 'validators/string_validator'
12
+
13
+ module Sekisyo
14
+ #
15
+ # Sekisyo Validator is manages each validator in one place.
16
+ #
17
+ class Validator
18
+ VALIDATORS = {
19
+ 'any' => Sekisyo::Validators::AnyValidator,
20
+ 'array' => Sekisyo::Validators::ArrayValidator,
21
+ 'boolean' => Sekisyo::Validators::BooleanValidator,
22
+ 'file' => Sekisyo::Validators::FileValidator,
23
+ 'float' => Sekisyo::Validators::FloatValidator,
24
+ 'integer' => Sekisyo::Validators::IntegerValidator,
25
+ 'numeric' => Sekisyo::Validators::NumericValidator,
26
+ 'object' => Sekisyo::Validators::ObjectValidator,
27
+ 'string' => Sekisyo::Validators::StringValidator
28
+ }.freeze
29
+
30
+ extend Forwardable
31
+ delegate valid?: :@validator, key: :@validator
32
+
33
+ def initialize(key, options = {})
34
+ validator_class = VALIDATORS[options.delete('type')]
35
+ validator_class ||= Sekisyo::Validators::AnyValidator
36
+ @validator = validator_class.new(key, options)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sekisyo
4
+ module Validators
5
+ #
6
+ # Sekisyo Validators AnyValidator is a validator that can come in any value.
7
+ #
8
+ class AnyValidator
9
+ def initialize(key, options = {})
10
+ @key = key
11
+ @options = options
12
+ end
13
+
14
+ attr_reader :key
15
+
16
+ def valid?(value)
17
+ presence_validate(value) &&
18
+ bytesize_validate(value)
19
+ end
20
+
21
+ private
22
+
23
+ def presence_validate(value)
24
+ !@options['presence'] || !(value.respond_to?(:empty?) ? !!value.empty? : !value)
25
+ end
26
+
27
+ def bytesize_validate(value)
28
+ !@options['max_bytesize'] || value.to_s.bytesize <= @options['max_bytesize'].to_i
29
+ end
30
+ end
31
+ end
32
+ end