formeze 2.1.0 → 4.0.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 +7 -0
- data/CHANGES.md +164 -0
- data/LICENSE.txt +4 -0
- data/README.md +69 -92
- data/formeze.gemspec +12 -7
- data/lib/formeze.rb +139 -92
- metadata +23 -53
- data/Rakefile.rb +0 -8
- data/spec/formeze_spec.rb +0 -776
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 966031b2657f2210d398a97d3c8dd6a1cc4e424e39972db1c3bad968cfe3bc8e
|
4
|
+
data.tar.gz: 140936f1897241dc7ee598fd8e63296d3d7918879dd263d825e68299897e1a1f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '092df6a16316cd7fb65f997e3e25e65622a1e8e112a5da8db6c56ca4146dc4485502fcc1ed7b9cb6a34e452878e3a6151a7c3c7c47e759ef7fe6349d35f5c1e4'
|
7
|
+
data.tar.gz: 0b4641a90d0d9e7e0cface189b699f12ebd60e94de2350a7dcbee537be6e22bd8b721587199ce3e24202ea5ccb8c1083438abd9bfa5d55538556825e08cbe0bc
|
data/CHANGES.md
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# 4.0.0
|
2
|
+
|
3
|
+
* Removed support for older rubies. **Required ruby version is now 2.4.0**
|
4
|
+
|
5
|
+
* Changed the code to use keyword arguments for options
|
6
|
+
|
7
|
+
* Renamed the `when` validation option to `if`
|
8
|
+
|
9
|
+
# 3.1.0
|
10
|
+
|
11
|
+
* Added `'commit'` to the list of Rails form keys to ignore (#4)
|
12
|
+
|
13
|
+
* Added frozen string literal comment
|
14
|
+
|
15
|
+
* Extracted private constants to reduce memory allocations
|
16
|
+
|
17
|
+
* Removed spec file from gem
|
18
|
+
|
19
|
+
# 3.0.0
|
20
|
+
|
21
|
+
* Added functionality for handling multipart form data. For example:
|
22
|
+
|
23
|
+
class ExampleForm < Formeze::Form
|
24
|
+
field :image, accept: 'image/jpg,image/png', maxsize: 1000
|
25
|
+
end
|
26
|
+
|
27
|
+
For this to work the request needs to be passed to the parse method:
|
28
|
+
|
29
|
+
ExampleForm.new.parse(request)
|
30
|
+
|
31
|
+
* Removed the deprecated parse class method
|
32
|
+
|
33
|
+
* Removed Ruby 1.8.7 compatibility
|
34
|
+
|
35
|
+
# 2.2.0
|
36
|
+
|
37
|
+
* The #fill and #parse instance methods now return self. So instead of this:
|
38
|
+
|
39
|
+
form = ExampleForm.new
|
40
|
+
form.parse(request.raw_post)
|
41
|
+
|
42
|
+
You can now do this:
|
43
|
+
|
44
|
+
form = ExampleForm.new.parse(request.raw_post)
|
45
|
+
|
46
|
+
* Deprecated the parse class method
|
47
|
+
|
48
|
+
# 2.1.1
|
49
|
+
|
50
|
+
* Fixed that custom validation should not execute for optional fields with blank values
|
51
|
+
|
52
|
+
# 2.1.0
|
53
|
+
|
54
|
+
* Fixed that custom validation should only execute when there are no existing errors on the associated field
|
55
|
+
|
56
|
+
* Removed `:word_limit` field option
|
57
|
+
|
58
|
+
# 2.0.0
|
59
|
+
|
60
|
+
* Added new custom validation functionality
|
61
|
+
|
62
|
+
* Removed existing (undocumented) custom validation functionality
|
63
|
+
|
64
|
+
* KeyError now includes an error message when raised for unexpected keys
|
65
|
+
|
66
|
+
* Added #to_h form instance method
|
67
|
+
|
68
|
+
* Removed `:char_limit` field option
|
69
|
+
|
70
|
+
* Deprecated `:word_limit` field option (use custom validation instead)
|
71
|
+
|
72
|
+
# 1.9.1
|
73
|
+
|
74
|
+
* Added `:minlength` field option
|
75
|
+
|
76
|
+
* Added `:maxlength` field option
|
77
|
+
|
78
|
+
* Deprecated `:char_limit` field option (use `:maxlength` instead)
|
79
|
+
|
80
|
+
# 1.9.0
|
81
|
+
|
82
|
+
* Added `:blank` field option for specifying a null object to be used in place of blank input
|
83
|
+
|
84
|
+
# 1.8.0
|
85
|
+
|
86
|
+
* Added #fill instance method
|
87
|
+
|
88
|
+
* Improved handling of Rails utf8/authenticity_token parameters
|
89
|
+
|
90
|
+
# 1.7.0
|
91
|
+
|
92
|
+
* Ruby 1.8.7 compatibility
|
93
|
+
|
94
|
+
* Renamed `Formeze::UserError` to `Formeze::ValidationError`
|
95
|
+
|
96
|
+
* Added #to_hash instance method
|
97
|
+
|
98
|
+
# 1.6.0
|
99
|
+
|
100
|
+
* Added #errors_on? instance method for checking if there are errors on a specific field
|
101
|
+
|
102
|
+
* Added #errors_on instance method for accessing the errors on a specific field
|
103
|
+
|
104
|
+
* Added parse class method, so instead of this:
|
105
|
+
|
106
|
+
form = ExampleForm.new
|
107
|
+
form.parse(request.raw_post)
|
108
|
+
|
109
|
+
You can now do this:
|
110
|
+
|
111
|
+
form = ExampleForm.parse(request.raw_post)
|
112
|
+
|
113
|
+
# 1.5.1
|
114
|
+
|
115
|
+
* Added `Formeze::Form` class, so forms can now be defined like this:
|
116
|
+
|
117
|
+
class ExampleForm < Formeze::Form
|
118
|
+
end
|
119
|
+
|
120
|
+
The previous style of setup is still supported:
|
121
|
+
|
122
|
+
class ExampleForm < SomeAncestorClass
|
123
|
+
Formeze.setup(self)
|
124
|
+
end
|
125
|
+
|
126
|
+
# 1.5.0
|
127
|
+
|
128
|
+
* Added #errors? instance method
|
129
|
+
|
130
|
+
* Added `Formeze.scrub` method so that the scrub methods can be re-used outside field validation
|
131
|
+
|
132
|
+
# 1.4.0
|
133
|
+
|
134
|
+
* Added `:scrub` field option for cleaning up input data before validation
|
135
|
+
|
136
|
+
# 1.3.0
|
137
|
+
|
138
|
+
* Added functionality for overriding error messages via i18n
|
139
|
+
|
140
|
+
* Added functionality for setting field labels globally via i18n
|
141
|
+
|
142
|
+
# 1.2.0
|
143
|
+
|
144
|
+
* Replaced experimental guard/halting functionality with `:defined_if` and `:defined_unless` field options
|
145
|
+
|
146
|
+
# 1.1.3
|
147
|
+
|
148
|
+
* Fixed early return from guard/halting conditions
|
149
|
+
|
150
|
+
# 1.1.2
|
151
|
+
|
152
|
+
* Fixed validation so that additional checks are skipped if the input is blank
|
153
|
+
|
154
|
+
# 1.1.1
|
155
|
+
|
156
|
+
* Added an error message for `Formeze::KeyError` exceptions
|
157
|
+
|
158
|
+
# 1.1.0
|
159
|
+
|
160
|
+
* Changed behaviour of experimental guard conditions and added halting conditions with opposite behaviour
|
161
|
+
|
162
|
+
# 1.0.0
|
163
|
+
|
164
|
+
* First version!
|
data/LICENSE.txt
ADDED
data/README.md
CHANGED
@@ -1,53 +1,62 @@
|
|
1
|
-
formeze
|
2
|
-
=======
|
1
|
+
# formeze
|
3
2
|
|
3
|
+

|
4
|
+

|
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.0'
|
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/master/CHANGES.md'
|
19
|
+
}
|
15
20
|
end
|