mini_defender 0.1.0 → 0.1.1

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: 1fa43ad18c9eec0f5536856021d4ad91124919b193ebf402b063db104c2423bc
4
- data.tar.gz: '09550f63e65490c59b43cf50b6f3454d4385655090dbe880e6408c10c50375da'
3
+ metadata.gz: 6442a233854e38c0ac2a50d040d132c927be919fbba2c1a0dd8a303501c53fb4
4
+ data.tar.gz: 28b2336b4e20828a3a3e92027c9c501902426e1ef8319b88f84d92c7589ab92b
5
5
  SHA512:
6
- metadata.gz: aa5d6f0399d1fb8df563f5b86830b0e058f7343360258dceff43dc34d9e6ce381250a9c3ef5291133c3ca639681770583c2e7418d3d177c8202cd89777952ec4
7
- data.tar.gz: 5dc095ea1c58329e1fe04f4977f53f08c832b51e61e4e6bb6c97ac71783793049e6ab360eac3053a0441c049b6d590b320db976f355e5727d568b09a5389b8c4
6
+ metadata.gz: 3ab48998b4c80d478ae46fcab7d161d8c4f2fee7c7e43c0066c9d46cf5188db1b02cb3ef578f4b4db58a48add2fbad048d443f1606a4156bfa9f724e70832a62
7
+ data.tar.gz: 18f49db6546dca476afbbd137e28a0f4c6e0760ee7cd9aeb083624ff0da5fbdfd6484af4a44bbfdd7a5a961935a3000cdb7662b5fa83765d78f706531fb8f1ad
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # MiniDefender
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mini_defender`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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
- ## Development
155
+ After creating the class, you can register it as follows:
22
156
 
23
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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/[USERNAME]/mini_defender.
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](https://opensource.org/licenses/MIT).
168
+ The gem is available as open source under the terms of the [MIT License](./LICENSE.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniDefender
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
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.0
4
+ version: 0.1.1
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
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.