validator_fn 0.1.2 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59d88acda61662b4adc0cf17e6abe58ccab42be28b84291191504ed2545e8d5a
4
- data.tar.gz: 0c7485fe2f3208a490ad0e4220f00e5770cad7ecb1a1798f5cae98d7bde40a0e
3
+ metadata.gz: 9a18a2dee04cebf84a277132258a1456f981fe477467d041ea6c29e564051a74
4
+ data.tar.gz: 2c82211054e4b07ee55f068dd87218b8f4d2da001889cd054d220be6346d84f0
5
5
  SHA512:
6
- metadata.gz: cea6572a3db872f38516950d3b763b67a81bd1d10dd576624662b84e9203a99e80c9fdaba10b21f6e77d56ba079d261f27d2a5a454bb825dc26b0f64be6664fa
7
- data.tar.gz: f4917cd8611691a44dc56b49fa2c36b350c30edced673ac71a4aa6b41f412ffb0e538a22d2edcc98ba02522cb2c9aabb2624fa68b43898ee0ca3b22f52113e00
6
+ metadata.gz: d027d9af4b77d44e4b0dc9271faeb9bc89dc0e617439442f1858215ff06a8f806edac282379c1e28b176cc532522b3c5258aadc29c6745649930e2bfe6ac92ef
7
+ data.tar.gz: 42385b0fbcdeb03906d81e4e4d32c29d6d19edcbb950586afc85553baba260823792e72e1f9f03fae5440e16d07160272996dacee50a2c971817dc8f68d0297e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validator_fn (0.1.2)
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.2"
2
+ VERSION = "0.1.6"
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,16 +56,17 @@ 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 {
64
63
  hash ||= {}
65
- fields.map do |(key, fn)|
66
- [key, fn.(hash[key])]
64
+ fields.reduce({}) do |memo, (key, fn)|
65
+ memo[key] ||= fn.(hash[key]) if hash[key]
66
+ memo
67
67
  rescue Error => e
68
68
  invalid.("Invalid value for #{key.inspect} key.")
69
- end.to_h
69
+ end
70
70
  }.curry
71
71
 
72
72
  @@handle_error = ->on_error, validator, value {
@@ -92,7 +92,7 @@ module ValidatorFn
92
92
  "any"
93
93
  when Hash
94
94
  inner = a.map do |k, v|
95
- %{"#{k}" => #{generate_validator.(v)}}
95
+ %{#{k.inspect} => #{generate_validator.(v)}}
96
96
  end.join(",\n ")
97
97
  "hash_of.({ #{inner} })"
98
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.2
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: 2020-12-26 00:00:00.000000000 Z
11
+ date: 2021-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fn_reader