validator_fn 0.1.3 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a79bc916ca1dba284aa215464f30a5734be7384e74153e704e57cade0bd4856
4
- data.tar.gz: 1e4ae24e5abd2cff941117b3558fe39fe9c0c2efdf1ef13f87806dc07902e341
3
+ metadata.gz: f3c19ca1d1f502bed0157cfafa1f2a1b895ada518a58770f22f7ea3eeb159fb8
4
+ data.tar.gz: e7d7cb6dd656953bded2541458316019a1578913907b473499520f86b59fda8a
5
5
  SHA512:
6
- metadata.gz: abb3834b1c0f4a93770ec973f39fd65f44f1e63bc7cf30beeadbf69e6fa3bddbb48ca13d77f33ca76426bd1d7b4fe60253c680d038c01680b399871dd7fdf8db
7
- data.tar.gz: fe879dc647418bbd36a31784e740b08a52e4ed63774b9e5e1c40b785cd303e69dfd0c94e921c47f37ccc9466f61b51f3dde7ee5c01696eef4ddcdaec7a94d0af
6
+ metadata.gz: c03edea5b8e2fecf7fb892ca22a4157ebfbe0dd8e717578cc5d38e3280c99ada72e72a18b6ee7c661e1d533261560037c47ff6c18065c28af7eb5478fc5c9f4e
7
+ data.tar.gz: 382a11b7ea12ebda356c03039b3e5b2228d1dcc0d7e38ac95da7419f36b69ab932f14054fb44029cf6dfed670a6257f6bb270d2cd5abae98be54e004badc6af7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validator_fn (0.1.3)
4
+ validator_fn (0.1.6)
5
5
  fn_reader
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # ValidatorFn
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/validator_fn`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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).
@@ -1,3 +1,3 @@
1
1
  module ValidatorFn
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.7"
3
3
  end
data/lib/validator_fn.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "validator_fn/version"
2
2
  require "fn_reader"
3
- require "pry-nav"
4
3
 
5
4
  module ValidatorFn
6
5
  class Error < StandardError; end
@@ -57,7 +56,7 @@ module ValidatorFn
57
56
  end
58
57
  }.curry
59
58
  @@any = ->a { a }
60
- @@is_nil = ->a { invalid.("Should be nil") unless a.nil?; a }
59
+ @@is_nil = ->a { invalid.("Should be nil but was #{a}") unless a.nil?; a }
61
60
  @@maybe = either.(is_nil)
62
61
  @@is_a = ->klass, a { invalid.("Expected type #{klass}, got #{a.inspect}") unless a.is_a?(klass); a }.curry
63
62
  @@hash_of = ->fields, hash {
@@ -93,7 +92,7 @@ module ValidatorFn
93
92
  "any"
94
93
  when Hash
95
94
  inner = a.map do |k, v|
96
- %{"#{k}" => #{generate_validator.(v)}}
95
+ %{#{k.inspect} => #{generate_validator.(v)}}
97
96
  end.join(",\n ")
98
97
  "hash_of.({ #{inner} })"
99
98
  when Array
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.3
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Chabot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-29 00:00:00.000000000 Z
11
+ date: 2021-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fn_reader