validator_fn 0.1.8 → 0.2.8
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/README.md +17 -20
- data/lib/validator_fn/error.rb +6 -0
- data/lib/validator_fn/version.rb +1 -1
- data/lib/validator_fn.rb +8 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cecd9880750c518fcd26fd705b76ffeaf4604fb90af4bd60719ecf37a387d8ee
|
4
|
+
data.tar.gz: d04d0ef810ab4ac5ea7b8914690c1995ffdf913c8ad4a40c5bfa85ce5ce8a817
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52647b99fdac1ff9dcf399b96d4b5a5becac6748270795cb47f0caa11e1735217239108450418781bf933dabdc0accf3ef3eaf7c9b8feb9fc1bd5f3deabe34d9
|
7
|
+
data.tar.gz: 5c16a63c7a8f261cf8cf76af633dbc448975dd77cbe08074abf2dd8f77cca95b42baa3ce876c08d2f3186d52eee68edd1a94abea6b33440263e617364732d6a3
|
data/README.md
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# ValidatorFn
|
2
2
|
|
3
3
|
ValidatorFn is a collection of very simple lambdas that can be used for validating/transforming
|
4
|
-
data structures. It makes use of currying
|
5
|
-
DSL. To help you understand the concepts
|
4
|
+
data structures. It makes use of currying to provide a very composable
|
5
|
+
DSL. To help you understand the concepts, I strongly advise reading [this blog post.](http://blog.martinosis.com/blog/simple-functional-strong-params-in-ruby/).
|
6
6
|
|
7
|
-
It can be very useful for validating structures that
|
8
|
-
JSON apis, test result.
|
7
|
+
It can be very useful for validating structures that come from input, such as configuration files, JSON APIs, test results.
|
9
8
|
|
10
9
|
## Installation
|
11
10
|
|
@@ -25,30 +24,30 @@ Or install it yourself as:
|
|
25
24
|
|
26
25
|
## Usage
|
27
26
|
|
28
|
-
```
|
27
|
+
```ruby
|
29
28
|
require 'validator_fn'
|
30
29
|
include ValidatorFn
|
31
30
|
```
|
32
31
|
|
33
|
-
You can start validating type of an object.
|
32
|
+
You can start validating the type of an object.
|
34
33
|
|
35
|
-
```
|
34
|
+
```ruby
|
36
35
|
is_a.(String).(3)
|
37
36
|
```
|
38
37
|
|
39
|
-
Note that parameters are curried. The first param of `is_a` is the Ruby class
|
40
|
-
In the previous example,
|
38
|
+
Note that parameters are curried. The first param of `is_a` is the Ruby class you want to check the last parameter against.
|
39
|
+
In the previous example, a `ValidatorFn::Error`exception will be raised.
|
41
40
|
|
42
|
-
However
|
41
|
+
However, setting a valid entry as the second parameter will return that last parameter.
|
43
42
|
|
44
|
-
```
|
43
|
+
```ruby
|
45
44
|
is_a.(String).("Joe")
|
46
45
|
# => "Joe"
|
47
46
|
```
|
48
47
|
|
49
48
|
This allows chaining lambdas:
|
50
49
|
|
51
|
-
```
|
50
|
+
```ruby
|
52
51
|
to_int = is_a.(String) >> -> a { Integer(a) }
|
53
52
|
|
54
53
|
to_int.("12")
|
@@ -58,14 +57,14 @@ to_int.("asdf") # will raise an exception
|
|
58
57
|
|
59
58
|
You can validate a hash:
|
60
59
|
|
61
|
-
```
|
60
|
+
```ruby
|
62
61
|
user = hash_of.({name: is_a.(String), age: to_int})
|
63
62
|
user.({name: "", age: "234"})
|
64
63
|
```
|
65
64
|
|
66
65
|
Since we are using curried lambdas, you can compose validators as you which.
|
67
66
|
|
68
|
-
```
|
67
|
+
```ruby
|
69
68
|
postal_code = -> a {
|
70
69
|
invalid.("Invalid postal code: #{a}") unless a =~ /[a-z]\d[a-z]\s*\d[a-z]\d/i
|
71
70
|
a
|
@@ -75,8 +74,7 @@ contact = hash_of.(user: user,
|
|
75
74
|
postal_code: postal_code}))
|
76
75
|
```
|
77
76
|
|
78
|
-
This
|
79
|
-
creating your own lambdas.
|
77
|
+
This very simple library (100 lines of code) can be extended easily by creating new lambdas.
|
80
78
|
|
81
79
|
## Casting
|
82
80
|
|
@@ -92,18 +90,18 @@ user_validator.(user)
|
|
92
90
|
# => {:name=>"Joe", :created_at=>#<Date: 2020-01-03 ((2458852j,0s,0n),+0s,2299161j)>}
|
93
91
|
```
|
94
92
|
|
95
|
-
This makes it very useful processing JSON payloads.
|
93
|
+
This makes it very useful for processing JSON payloads.
|
96
94
|
|
97
95
|
## Code generation
|
98
96
|
|
99
97
|
You can generate validator code from existing structure:
|
100
98
|
|
101
|
-
```
|
99
|
+
```ruby
|
102
100
|
require 'openuri'
|
103
101
|
require "json"
|
104
102
|
struct = JSON.parse(URI.open("https://api.github.com/users/defunkt").read)
|
105
103
|
|
106
|
-
|
104
|
+
require "validator_fn"
|
107
105
|
|
108
106
|
# Generate unformatted code
|
109
107
|
code = ValidatorFn.generate_validator.(struct)
|
@@ -162,4 +160,3 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
162
160
|
## Code of Conduct
|
163
161
|
|
164
162
|
Everyone interacting in the ValidatorFn project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/validator_fn/blob/master/CODE_OF_CONDUCT.md).
|
165
|
-
Everyone interacting in the ValidatorFn project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/validator_fn/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/validator_fn/error.rb
CHANGED
data/lib/validator_fn/version.rb
CHANGED
data/lib/validator_fn.rb
CHANGED
@@ -40,8 +40,15 @@ module ValidatorFn
|
|
40
40
|
@@hash_of = ->fields, hash {
|
41
41
|
hash ||= {}
|
42
42
|
fields.reduce({}) do |memo, (key, fn)|
|
43
|
-
|
43
|
+
value = hash.fetch(key, :missing_field)
|
44
|
+
if value == :missing_field
|
45
|
+
raise MissingKey.new(key)
|
46
|
+
else
|
47
|
+
memo[key] = fn.(hash[key]) if hash[key]
|
48
|
+
end
|
44
49
|
memo
|
50
|
+
rescue MissingKey => e
|
51
|
+
raise
|
45
52
|
rescue Error => e
|
46
53
|
invalid.("Invalid value for #{key.inspect} key.")
|
47
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validator_fn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Chabot
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fn_reader
|