formeze 2.1.1 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGES.md +168 -0
- data/LICENSE.txt +4 -0
- data/README.md +69 -92
- data/formeze.gemspec +12 -7
- data/lib/formeze.rb +133 -94
- metadata +23 -53
- data/Rakefile.rb +0 -8
- data/spec/formeze_spec.rb +0 -809
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a68f63365be4c93159701308fd264f143e508111ec6d1d860ba62bba8cfacd41
|
4
|
+
data.tar.gz: f52e50d471f7df325dbe7a6e5400375d06b988b2411888c1a2c357aec2ec24be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a08a90d2595aeeecb185c58a80b6a1fb588bee98a74002e6646a6be5d97e18effb47cf0513734de1f077a9e47501c8402fc92e1f400f2438b9f49d437ffc214c
|
7
|
+
data.tar.gz: a15f47a7e423b78c67104376f7e4ebd41bff3dc168f2703c65347bd795c0196202c1cb079b0f82d9cc8b4e68e5f8ab4374dbfb8e7d58fc9fc8ba8bb2905e7420
|
data/CHANGES.md
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
# 4.0.1
|
2
|
+
|
3
|
+
* Fixed outdated changelog_uri
|
4
|
+
|
5
|
+
# 4.0.0
|
6
|
+
|
7
|
+
* Removed support for older rubies. **Required ruby version is now 2.4.0**
|
8
|
+
|
9
|
+
* Changed the code to use keyword arguments for options
|
10
|
+
|
11
|
+
* Renamed the `when` validation option to `if`
|
12
|
+
|
13
|
+
# 3.1.0
|
14
|
+
|
15
|
+
* Added `'commit'` to the list of Rails form keys to ignore (#4)
|
16
|
+
|
17
|
+
* Added frozen string literal comment
|
18
|
+
|
19
|
+
* Extracted private constants to reduce memory allocations
|
20
|
+
|
21
|
+
* Removed spec file from gem
|
22
|
+
|
23
|
+
# 3.0.0
|
24
|
+
|
25
|
+
* Added functionality for handling multipart form data. For example:
|
26
|
+
|
27
|
+
class ExampleForm < Formeze::Form
|
28
|
+
field :image, accept: 'image/jpg,image/png', maxsize: 1000
|
29
|
+
end
|
30
|
+
|
31
|
+
For this to work the request needs to be passed to the parse method:
|
32
|
+
|
33
|
+
ExampleForm.new.parse(request)
|
34
|
+
|
35
|
+
* Removed the deprecated parse class method
|
36
|
+
|
37
|
+
* Removed Ruby 1.8.7 compatibility
|
38
|
+
|
39
|
+
# 2.2.0
|
40
|
+
|
41
|
+
* The #fill and #parse instance methods now return self. So instead of this:
|
42
|
+
|
43
|
+
form = ExampleForm.new
|
44
|
+
form.parse(request.raw_post)
|
45
|
+
|
46
|
+
You can now do this:
|
47
|
+
|
48
|
+
form = ExampleForm.new.parse(request.raw_post)
|
49
|
+
|
50
|
+
* Deprecated the parse class method
|
51
|
+
|
52
|
+
# 2.1.1
|
53
|
+
|
54
|
+
* Fixed that custom validation should not execute for optional fields with blank values
|
55
|
+
|
56
|
+
# 2.1.0
|
57
|
+
|
58
|
+
* Fixed that custom validation should only execute when there are no existing errors on the associated field
|
59
|
+
|
60
|
+
* Removed `:word_limit` field option
|
61
|
+
|
62
|
+
# 2.0.0
|
63
|
+
|
64
|
+
* Added new custom validation functionality
|
65
|
+
|
66
|
+
* Removed existing (undocumented) custom validation functionality
|
67
|
+
|
68
|
+
* KeyError now includes an error message when raised for unexpected keys
|
69
|
+
|
70
|
+
* Added #to_h form instance method
|
71
|
+
|
72
|
+
* Removed `:char_limit` field option
|
73
|
+
|
74
|
+
* Deprecated `:word_limit` field option (use custom validation instead)
|
75
|
+
|
76
|
+
# 1.9.1
|
77
|
+
|
78
|
+
* Added `:minlength` field option
|
79
|
+
|
80
|
+
* Added `:maxlength` field option
|
81
|
+
|
82
|
+
* Deprecated `:char_limit` field option (use `:maxlength` instead)
|
83
|
+
|
84
|
+
# 1.9.0
|
85
|
+
|
86
|
+
* Added `:blank` field option for specifying a null object to be used in place of blank input
|
87
|
+
|
88
|
+
# 1.8.0
|
89
|
+
|
90
|
+
* Added #fill instance method
|
91
|
+
|
92
|
+
* Improved handling of Rails utf8/authenticity_token parameters
|
93
|
+
|
94
|
+
# 1.7.0
|
95
|
+
|
96
|
+
* Ruby 1.8.7 compatibility
|
97
|
+
|
98
|
+
* Renamed `Formeze::UserError` to `Formeze::ValidationError`
|
99
|
+
|
100
|
+
* Added #to_hash instance method
|
101
|
+
|
102
|
+
# 1.6.0
|
103
|
+
|
104
|
+
* Added #errors_on? instance method for checking if there are errors on a specific field
|
105
|
+
|
106
|
+
* Added #errors_on instance method for accessing the errors on a specific field
|
107
|
+
|
108
|
+
* Added parse class method, so instead of this:
|
109
|
+
|
110
|
+
form = ExampleForm.new
|
111
|
+
form.parse(request.raw_post)
|
112
|
+
|
113
|
+
You can now do this:
|
114
|
+
|
115
|
+
form = ExampleForm.parse(request.raw_post)
|
116
|
+
|
117
|
+
# 1.5.1
|
118
|
+
|
119
|
+
* Added `Formeze::Form` class, so forms can now be defined like this:
|
120
|
+
|
121
|
+
class ExampleForm < Formeze::Form
|
122
|
+
end
|
123
|
+
|
124
|
+
The previous style of setup is still supported:
|
125
|
+
|
126
|
+
class ExampleForm < SomeAncestorClass
|
127
|
+
Formeze.setup(self)
|
128
|
+
end
|
129
|
+
|
130
|
+
# 1.5.0
|
131
|
+
|
132
|
+
* Added #errors? instance method
|
133
|
+
|
134
|
+
* Added `Formeze.scrub` method so that the scrub methods can be re-used outside field validation
|
135
|
+
|
136
|
+
# 1.4.0
|
137
|
+
|
138
|
+
* Added `:scrub` field option for cleaning up input data before validation
|
139
|
+
|
140
|
+
# 1.3.0
|
141
|
+
|
142
|
+
* Added functionality for overriding error messages via i18n
|
143
|
+
|
144
|
+
* Added functionality for setting field labels globally via i18n
|
145
|
+
|
146
|
+
# 1.2.0
|
147
|
+
|
148
|
+
* Replaced experimental guard/halting functionality with `:defined_if` and `:defined_unless` field options
|
149
|
+
|
150
|
+
# 1.1.3
|
151
|
+
|
152
|
+
* Fixed early return from guard/halting conditions
|
153
|
+
|
154
|
+
# 1.1.2
|
155
|
+
|
156
|
+
* Fixed validation so that additional checks are skipped if the input is blank
|
157
|
+
|
158
|
+
# 1.1.1
|
159
|
+
|
160
|
+
* Added an error message for `Formeze::KeyError` exceptions
|
161
|
+
|
162
|
+
# 1.1.0
|
163
|
+
|
164
|
+
* Changed behaviour of experimental guard conditions and added halting conditions with opposite behaviour
|
165
|
+
|
166
|
+
# 1.0.0
|
167
|
+
|
168
|
+
* First version!
|
data/LICENSE.txt
ADDED
data/README.md
CHANGED
@@ -1,53 +1,62 @@
|
|
1
|
-
formeze
|
2
|
-
=======
|
1
|
+
# formeze
|
3
2
|
|
3
|
+
![Gem Version](https://badge.fury.io/rb/formeze.svg)
|
4
|
+
![Build Status](https://github.com/readysteady/formeze/workflows/Test/badge.svg)
|
4
5
|
|
5
|
-
|
6
|
+
Ruby gem for validating form data.
|
6
7
|
|
7
8
|
|
8
|
-
Motivation
|
9
|
-
----------
|
9
|
+
## Motivation
|
10
10
|
|
11
11
|
Most web apps built for end users will need to process url-encoded form data.
|
12
12
|
Registration forms, profile forms, checkout forms, contact forms, and forms
|
13
|
-
for adding/editing application specific data.
|
14
|
-
process this data safely, to minimise the possibility of security holes
|
15
|
-
within our application that could be exploited. Formeze adopts the approach
|
16
|
-
of being "strict by default", forcing the application code to be explicit in
|
17
|
-
what it accepts as input.
|
13
|
+
for adding/editing application specific data.
|
18
14
|
|
15
|
+
As developers we would like to process this data safely, to minimise the
|
16
|
+
possibility of security holes within our application that could be exploited.
|
17
|
+
Formeze adopts the approach of being "strict by default", forcing the application
|
18
|
+
code to be explicit in what it accepts as input.
|
19
19
|
|
20
|
-
Installation
|
21
|
-
------------
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
## Install
|
22
|
+
|
23
|
+
Using Bundler:
|
24
|
+
|
25
|
+
$ bundle add formeze
|
26
26
|
|
27
|
+
Using RubyGems:
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
$ gem install formeze
|
30
|
+
|
31
|
+
|
32
|
+
## Usage
|
30
33
|
|
31
34
|
Here is a minimal example, which defines a form with a single field:
|
32
35
|
|
33
36
|
```ruby
|
37
|
+
require 'formeze'
|
38
|
+
|
34
39
|
class ExampleForm < Formeze::Form
|
35
40
|
field :title
|
36
41
|
end
|
37
42
|
```
|
38
43
|
|
39
|
-
|
44
|
+
You can then parse and validate form data in Rails or Sinatra like this:
|
40
45
|
|
41
46
|
```ruby
|
42
|
-
form = ExampleForm.new
|
47
|
+
form = ExampleForm.new.parse(request)
|
43
48
|
|
44
|
-
form.
|
45
|
-
|
46
|
-
|
49
|
+
if form.valid?
|
50
|
+
# do something with form data
|
51
|
+
else
|
52
|
+
# display form.errors to user
|
53
|
+
end
|
47
54
|
```
|
48
55
|
|
56
|
+
Formeze will automatically ignore the Rails "utf8" and "authenticity_token" parameters.
|
57
|
+
|
49
58
|
If you prefer not to inherit from the `Formeze::Form` class then you can
|
50
|
-
instead call the `Formeze.setup` method like this:
|
59
|
+
instead call the `Formeze.setup` method on your classes like this:
|
51
60
|
|
52
61
|
```ruby
|
53
62
|
class ExampleForm
|
@@ -62,27 +71,23 @@ methods but will otherwise leave the object untouched (i.e. you can define
|
|
62
71
|
your own initialization logic).
|
63
72
|
|
64
73
|
|
65
|
-
|
66
|
-
----------------
|
74
|
+
## Validation errors
|
67
75
|
|
68
76
|
Formeze distinguishes between validation errors (which are expected in the
|
69
77
|
normal running of your application), and key/value errors (which most likely
|
70
|
-
indicate either developer error, or form tampering).
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
the form data does not match the field definitions.
|
78
|
+
indicate either developer error, or form tampering). For the latter case,
|
79
|
+
the `parse` method that formeze provides will raise a `Formeze::KeyError`
|
80
|
+
or a `Formeze::ValueError` exception if the structure of the form data
|
81
|
+
does not match the field definitions.
|
75
82
|
|
76
83
|
After calling `parse` you can check that the form is valid by calling the
|
77
|
-
|
78
|
-
return an array of error messages to display to the end user.
|
79
|
-
|
80
|
-
You can also use `errors_on?` and `errors_on` to check for and select error
|
84
|
+
`valid?` method. If it isn't you can call the `errors` method which will
|
85
|
+
return an array of error messages to display to the end user. You can also
|
86
|
+
use the `errors_on?` and `errors_on` methods to check for and select error
|
81
87
|
messages specific to a single field.
|
82
88
|
|
83
89
|
|
84
|
-
Field options
|
85
|
-
-------------
|
90
|
+
## Field options
|
86
91
|
|
87
92
|
By default fields cannot be blank, they are limited to 64 characters,
|
88
93
|
and they cannot contain newlines. These restrictions can be overridden
|
@@ -106,9 +111,9 @@ is not required, i.e. the value of the field can be blank/empty. For example:
|
|
106
111
|
field :title, required: false
|
107
112
|
```
|
108
113
|
|
109
|
-
|
110
|
-
|
111
|
-
|
114
|
+
You might want to return a different value for blank fields, such as nil,
|
115
|
+
zero, or a "null" object. Use the `blank` option to specify this behaviour.
|
116
|
+
For example:
|
112
117
|
|
113
118
|
```ruby
|
114
119
|
field :title, required: false, blank: nil
|
@@ -154,18 +159,14 @@ option to handle the case where the checkbox is unchecked. For example:
|
|
154
159
|
field :accept_terms, values: %w(true), key_required: false
|
155
160
|
```
|
156
161
|
|
157
|
-
Sometimes you'll have a field with multiple values
|
158
|
-
a set of checkboxes. For this case you can specify the `multiple`
|
159
|
-
|
162
|
+
Sometimes you'll have a field with multiple values, such as a multiple select
|
163
|
+
input, or a set of checkboxes. For this case you can specify the `multiple`
|
164
|
+
option, for example:
|
160
165
|
|
161
166
|
```ruby
|
162
167
|
field :colour, multiple: true, values: Colour.keys
|
163
168
|
```
|
164
169
|
|
165
|
-
Note that unlike all the other examples so far, reading the attribute
|
166
|
-
that corresponds to this field will return an array of strings instead
|
167
|
-
of a single string.
|
168
|
-
|
169
170
|
Sometimes you'll only want the field to be defined if some condition is true.
|
170
171
|
The condition may depend on the state of other form fields, or some external
|
171
172
|
state accessible from the form object. You can do this by specifying either
|
@@ -210,8 +211,22 @@ Custom scrub methods can be defined by adding a symbol/proc entry to the
|
|
210
211
|
`Formeze.scrub_methods` hash.
|
211
212
|
|
212
213
|
|
213
|
-
|
214
|
-
|
214
|
+
## Multipart form data
|
215
|
+
|
216
|
+
For file fields you can specify the `accept` and `maxsize` options, for example:
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
class ExampleForm < Formeze::Form
|
220
|
+
field :image, accept: 'image/jpg,image/png', maxsize: 1000
|
221
|
+
end
|
222
|
+
```
|
223
|
+
|
224
|
+
For this to work you need to make sure your application includes the
|
225
|
+
[mime-types gem](https://rubygems.org/gems/mime-types), and that the
|
226
|
+
form is submitted with the multipart/form-data mime type.
|
227
|
+
|
228
|
+
|
229
|
+
## Custom validation
|
215
230
|
|
216
231
|
You may need additional validation logic beyond what the field options
|
217
232
|
described above provide, such as validating the format of a field without
|
@@ -243,16 +258,16 @@ class ExampleForm < Formeze::Form
|
|
243
258
|
end
|
244
259
|
```
|
245
260
|
|
246
|
-
Specify the `
|
261
|
+
Specify the `if` option with a proc to peform the validation conditionally.
|
247
262
|
Similar to the `defined_if` and `defined_unless` field options, the proc is
|
248
263
|
evaluated in the scope of the form instance. For example:
|
249
264
|
|
250
265
|
```ruby
|
251
266
|
class ExampleForm < Formeze::Form
|
252
|
-
field :business_name, :
|
253
|
-
field :vat_number, :
|
267
|
+
field :business_name, defined_if: :business_account?
|
268
|
+
field :vat_number, defined_if: :business_account?
|
254
269
|
|
255
|
-
validates :vat_number, :
|
270
|
+
validates :vat_number, if: :business_account? do
|
256
271
|
# ...
|
257
272
|
end
|
258
273
|
|
@@ -279,7 +294,7 @@ class ExampleForm < Formeze::Form
|
|
279
294
|
|
280
295
|
validates :email, &EmailAddress.method(:valid?)
|
281
296
|
|
282
|
-
validates :password_confirmation, :
|
297
|
+
validates :password_confirmation, error: :does_not_match do
|
283
298
|
password_confirmation == password
|
284
299
|
end
|
285
300
|
end
|
@@ -291,49 +306,11 @@ key does not exist. The error for the password_confirmation field validation
|
|
291
306
|
would include the value of the `formeze.errors.does_not_match` I18n key.
|
292
307
|
|
293
308
|
|
294
|
-
|
295
|
-
-----------
|
296
|
-
|
297
|
-
This is the basic pattern for using a formeze form in a Rails controller:
|
298
|
-
|
299
|
-
```ruby
|
300
|
-
form = SomeForm.parse(request.raw_post)
|
301
|
-
|
302
|
-
if form.valid?
|
303
|
-
# do something with form data
|
304
|
-
else
|
305
|
-
# display form.errors to user
|
306
|
-
end
|
307
|
-
```
|
308
|
-
|
309
|
-
Formeze will automatically ignore the "utf8" and "authenticity_token"
|
310
|
-
parameters that Rails uses.
|
311
|
-
|
312
|
-
|
313
|
-
Sinatra usage
|
314
|
-
-------------
|
315
|
-
|
316
|
-
Using formeze with sinatra is similar, the only difference is that there is
|
317
|
-
no raw_post method on the request object so the body has to be read directly:
|
318
|
-
|
319
|
-
```ruby
|
320
|
-
form = SomeForm.parse(request.body.read)
|
321
|
-
|
322
|
-
if form.valid?
|
323
|
-
# do something with form data
|
324
|
-
else
|
325
|
-
# display form.errors to user
|
326
|
-
end
|
327
|
-
```
|
328
|
-
|
329
|
-
|
330
|
-
Integration with I18n
|
331
|
-
---------------------
|
309
|
+
## I18n integration
|
332
310
|
|
333
311
|
Formeze integrates with [I18n](http://edgeguides.rubyonrails.org/i18n.html)
|
334
312
|
so that you can define custom error messages and field labels within your
|
335
313
|
locales (useful both for localization, and when working with designers).
|
336
|
-
|
337
314
|
For example, here is how you would change the "required" error message
|
338
315
|
(which defaults to "is required"):
|
339
316
|
|
data/formeze.gemspec
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'formeze'
|
3
|
-
s.version = '
|
3
|
+
s.version = '4.0.1'
|
4
|
+
s.license = 'LGPL-3.0'
|
4
5
|
s.platform = Gem::Platform::RUBY
|
5
6
|
s.authors = ['Tim Craft']
|
6
7
|
s.email = ['mail@timcraft.com']
|
7
|
-
s.homepage = '
|
8
|
-
s.description = '
|
8
|
+
s.homepage = 'https://github.com/readysteady/formeze'
|
9
|
+
s.description = 'Ruby gem for validating form data'
|
9
10
|
s.summary = 'See description'
|
10
|
-
s.files = Dir.glob('
|
11
|
-
s.
|
12
|
-
s.add_development_dependency('i18n', ['~> 0.6.0'])
|
13
|
-
s.add_development_dependency('minitest', ['>= 4.2.0']) if RUBY_VERSION == '1.8.7'
|
11
|
+
s.files = Dir.glob('lib/**/*.rb') + %w(CHANGES.md LICENSE.txt README.md formeze.gemspec)
|
12
|
+
s.required_ruby_version = '>= 2.4.0'
|
14
13
|
s.require_path = 'lib'
|
14
|
+
s.metadata = {
|
15
|
+
'homepage' => 'https://github.com/readysteady/formeze',
|
16
|
+
'source_code_uri' => 'https://github.com/readysteady/formeze',
|
17
|
+
'bug_tracker_uri' => 'https://github.com/readysteady/formeze/issues',
|
18
|
+
'changelog_uri' => 'https://github.com/readysteady/formeze/blob/main/CHANGES.md'
|
19
|
+
}
|
15
20
|
end
|