validator_fn 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +58 -3
- data/lib/validator_fn/version.rb +1 -1
- data/lib/validator_fn.rb +1 -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: 9a18a2dee04cebf84a277132258a1456f981fe477467d041ea6c29e564051a74
|
4
|
+
data.tar.gz: 2c82211054e4b07ee55f068dd87218b8f4d2da001889cd054d220be6346d84f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d027d9af4b77d44e4b0dc9271faeb9bc89dc0e617439442f1858215ff06a8f806edac282379c1e28b176cc532522b3c5258aadc29c6745649930e2bfe6ac92ef
|
7
|
+
data.tar.gz: 42385b0fbcdeb03906d81e4e4d32c29d6d19edcbb950586afc85553baba260823792e72e1f9f03fae5440e16d07160272996dacee50a2c971817dc8f68d0297e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
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 in order to provide a very composable
|
5
|
+
DSL. To help you understand the concepts it is stongly advised to read [this blog post.](http://blog.martinosis.com/blog/simple-functional-strong-params-in-ruby/).
|
4
6
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -22,7 +23,58 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
## Usage
|
24
25
|
|
25
|
-
|
26
|
+
```
|
27
|
+
require 'validator_fn'
|
28
|
+
include ValidatorFn
|
29
|
+
```
|
30
|
+
|
31
|
+
You can start validating type of an object.
|
32
|
+
|
33
|
+
```
|
34
|
+
is_a.(String).(3)
|
35
|
+
```
|
36
|
+
|
37
|
+
Note that parameters are curried. The first param of `is_a` is the Ruby class that you want to check the last parameter against.
|
38
|
+
In the previous example, aValidatorFn::Error`exception will be raised.
|
39
|
+
|
40
|
+
However if you send a valid entry as the second parameter, it will return that last parameter.
|
41
|
+
|
42
|
+
```
|
43
|
+
is_a.(String).("Joe")
|
44
|
+
# => "Joe"
|
45
|
+
```
|
46
|
+
|
47
|
+
This allows chaining lambdas:
|
48
|
+
|
49
|
+
```
|
50
|
+
to_int = is_a.(String) >> -> a { Integer(a) }
|
51
|
+
|
52
|
+
to_int.("12")
|
53
|
+
# => 12
|
54
|
+
to_int.("asdf") # will raise an exception
|
55
|
+
```
|
56
|
+
|
57
|
+
You can validate a hash:
|
58
|
+
|
59
|
+
```
|
60
|
+
user = hash_of.({name: is_a.(String), age: to_int})
|
61
|
+
user.({name: "", age: "234"})
|
62
|
+
```
|
63
|
+
|
64
|
+
Since we are using curried lambdas, you can compose validators as you which.
|
65
|
+
|
66
|
+
```
|
67
|
+
postal_code = -> a {
|
68
|
+
invalid.("Invalid postal code: #{a}") unless a =~ /[a-z]\d[a-z]\s*\d[a-z]\d/i
|
69
|
+
a
|
70
|
+
}
|
71
|
+
contact = hash_of.(user: user,
|
72
|
+
address: hash_of.({ street_number: is_a.(Integer),
|
73
|
+
postal_code: postal_code}))
|
74
|
+
```
|
75
|
+
|
76
|
+
This is a very simple library (100 lines of code) that can be extended as you which by
|
77
|
+
creating your own lambdas.
|
26
78
|
|
27
79
|
## Development
|
28
80
|
|
@@ -42,3 +94,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
42
94
|
## Code of Conduct
|
43
95
|
|
44
96
|
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).
|
97
|
+
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).
|
98
|
+
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).
|
99
|
+
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/version.rb
CHANGED
data/lib/validator_fn.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Chabot
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fn_reader
|