mini_defender 0.1.0 → 0.1.2
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +143 -8
- data/lib/mini_defender/version.rb +1 -1
- data/mini_defender.gemspec +2 -3
- metadata +4 -4
- data/LICENSE.txt +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abb68f4ad7d5c250a9fea58d038b4fa51f616075e705b8ee7de1a4a61305e33f
|
4
|
+
data.tar.gz: 128aa15e2e302cf0fc20a755767df2c27d458637746c6f43f210e69c7f656f4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ed0d0ba2152f8deef6a05fd002cd4350bfbe757c82409783c369a5bc7b7e20738e5ac594a45954264baabab14a8c92a632b71baa6d75e7d7c2fc5f6d1b72a2d
|
7
|
+
data.tar.gz: 6a3a308ddc3663e83176854587361a3d72e8926df1f8d8c6750eb5732bd372471dda960f5e4ca2343ce222fd919cafb6b8637adc16405fa0d9423e9fc8ce5f59
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# MiniDefender
|
2
2
|
|
3
|
-
|
3
|
+
A pragmatic approach to validation in Rails inspired by Laravel's Validator.
|
4
4
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -14,20 +13,156 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
14
13
|
|
15
14
|
$ gem install mini_defender
|
16
15
|
|
16
|
+
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
Mini Defender allows the developer to quickly and easily validate incoming requests data as the verify first step
|
20
|
+
in the request, alongside this Mini Defender also provides the following benefits:
|
21
|
+
|
22
|
+
- Concise and easy to write validation logic
|
23
|
+
- Validate complex and nested data structures
|
24
|
+
- More than 80 validation rules available
|
25
|
+
- Custom validation rules
|
26
|
+
|
27
|
+
The easiest way to use Mini Defender is to include the concern `MiniDefender::ValidatesInput` to your
|
28
|
+
`ApplicationController` as follows:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class ApplicationController < ActionController::Base
|
32
|
+
include MiniDefender::ValidatesInput
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Now you will have access to a method called `validate!` which will accept a hash of keys and their respective rules.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# frozen_string_literal: true
|
40
|
+
class BooksController < ActionController::Base
|
41
|
+
def create
|
42
|
+
Book.create!(book_params)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def book_params
|
48
|
+
# Optional if you need easy access to rule definitions
|
49
|
+
rules = MiniDefender::Rules
|
50
|
+
|
51
|
+
validate!({
|
52
|
+
'name' => 'string|required|max:255',
|
53
|
+
'type' => ['string', rules::In.new(Book::TYPES)],
|
54
|
+
'tags' => 'array',
|
55
|
+
'tags.*' => 'required|string|max:255',
|
56
|
+
'pages' => 'required|integer',
|
57
|
+
'author' => 'required|hash',
|
58
|
+
'author.name' => 'required|string',
|
59
|
+
'author.email' => 'email',
|
60
|
+
}, true)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
The `validate!` method accepts two arguments, the first is a hash of keys and rules and the second a boolean to indicate
|
66
|
+
if you want the values to be coerced to the type indicated by rule, i.e. `integer`.
|
67
|
+
|
68
|
+
Suppose we passed the following input to our app:
|
69
|
+
|
70
|
+
```json
|
71
|
+
{
|
72
|
+
"name": "The Story of Some Guy",
|
73
|
+
"type": "Biography",
|
74
|
+
"tags": ["inspiring", "business"],
|
75
|
+
"pages": "200",
|
76
|
+
"author": {
|
77
|
+
"name": "Some Guy himself",
|
78
|
+
"random_field": "hello i can haz hakc?"
|
79
|
+
}
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
Notice that we have passed a string `"200"` to page instead of `200` and added an extra field to `author` called `random_field`.
|
84
|
+
We didn't also provide an `email` field.
|
85
|
+
|
86
|
+
The validation will pass, since `email` is not optional, and we will get the following `Hash` as a result:
|
87
|
+
|
88
|
+
```Ruby
|
89
|
+
{
|
90
|
+
'name' => 'The Story of Some Guy',
|
91
|
+
'type' => 'Biography',
|
92
|
+
'tags' => %w[inspiring business],
|
93
|
+
'pages' => 200,
|
94
|
+
'author' => {
|
95
|
+
'name' => 'Some Guy himself'
|
96
|
+
}
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
You can see that `pages` has the value converted to `200` (integer), since we chose to coerce by passing `true` to `validate!`.
|
101
|
+
You can also see that Mini Defender left out the key `random_field` as it was not a part of our validations.
|
102
|
+
|
103
|
+
|
104
|
+
## Rendering Errors
|
105
|
+
|
106
|
+
When the validation fails, an error of type `MiniDefender::ValidationError` will be raised.
|
107
|
+
|
108
|
+
You can either handle the error using `rescue` or add a global `rescue_from` to handle the errors throughout the application.
|
109
|
+
|
110
|
+
|
111
|
+
## Outside of a Controller
|
112
|
+
|
113
|
+
The `validate!` method is actually the following four lines of code:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
def validate!(rules, coerced = false)
|
117
|
+
data = params.to_unsafe_hash.deep_stringify_keys
|
118
|
+
validator = MiniDefender::Validator.new(rules, data)
|
119
|
+
validator.validate!
|
120
|
+
coerced ? validator.coerced : validator.data
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
You can use Mini Defender out side of the controller by creating a new instance of `MiniDefender::Validator` and calling
|
125
|
+
any of the methods on it, see [validator.rb](./lib/mini_defender/validator.rb) for the API.
|
126
|
+
|
127
|
+
|
128
|
+
## Rules
|
129
|
+
|
130
|
+
Mini Defender tries to implement the same set of rules provided by Laravel, you can see the available rules here at
|
131
|
+
[lib/mini_defender/rules](./lib/mini_defender/rules)
|
132
|
+
|
133
|
+
|
134
|
+
## Add a Custom Rule
|
135
|
+
|
136
|
+
To implement your custom rules, you need to create a new class that inherent from `MiniDefender::Rule` and implement
|
137
|
+
at least the following:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
class MyAwesomeRule < MiniDefender::Rule
|
141
|
+
def self.signature
|
142
|
+
'all_caps'
|
143
|
+
end
|
144
|
+
|
145
|
+
def passes?(attribute, value, validator)
|
146
|
+
value.is_a?(String) && /^[A-Z]+$/.match?(value)
|
147
|
+
end
|
148
|
+
|
149
|
+
def message(attribute, value, validator)
|
150
|
+
'ONLY CAPS ALLOWED!!!'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
```
|
20
154
|
|
21
|
-
|
155
|
+
After creating the class, you can register it as follows:
|
22
156
|
|
23
|
-
|
157
|
+
```ruby
|
158
|
+
MiniDefender::RulesFactory.register(MyAwesomeRule)
|
159
|
+
```
|
24
160
|
|
25
|
-
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).
|
26
161
|
|
27
162
|
## Contributing
|
28
163
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
164
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ahoshaiyan/mini_defender.
|
30
165
|
|
31
166
|
## License
|
32
167
|
|
33
|
-
The gem is available as open source under the terms of the [MIT License](
|
168
|
+
The gem is available as open source under the terms of the [MIT License](./LICENSE.md).
|
data/mini_defender.gemspec
CHANGED
@@ -10,15 +10,14 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = "A small and efficient validation library for Rails and anything that uses Ruby."
|
12
12
|
spec.description = spec.summary
|
13
|
-
spec.homepage = "https://
|
13
|
+
spec.homepage = "https://github.com/ahoshaiyan/mini-defender"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
16
16
|
|
17
17
|
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
18
|
|
19
19
|
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
-
|
21
|
-
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
22
21
|
|
23
22
|
# Specify which files should be added to the gem when it is released.
|
24
23
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_defender
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ali Alhoshaiyan
|
@@ -78,7 +78,6 @@ files:
|
|
78
78
|
- Gemfile
|
79
79
|
- Gemfile.lock
|
80
80
|
- LICENSE.md
|
81
|
-
- LICENSE.txt
|
82
81
|
- README.md
|
83
82
|
- RULES.md
|
84
83
|
- Rakefile
|
@@ -178,11 +177,12 @@ files:
|
|
178
177
|
- lib/mini_defender/version.rb
|
179
178
|
- mini_defender.gemspec
|
180
179
|
- sig/mini_defender.rbs
|
181
|
-
homepage: https://
|
180
|
+
homepage: https://github.com/ahoshaiyan/mini-defender
|
182
181
|
licenses:
|
183
182
|
- MIT
|
184
183
|
metadata:
|
185
|
-
homepage_uri: https://
|
184
|
+
homepage_uri: https://github.com/ahoshaiyan/mini-defender
|
185
|
+
source_code_uri: https://github.com/ahoshaiyan/mini-defender
|
186
186
|
post_install_message:
|
187
187
|
rdoc_options: []
|
188
188
|
require_paths:
|
data/LICENSE.txt
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2022 Ali Alhoshaiyan
|
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.
|