validator_fn 0.1.8 → 0.2.8

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: dbf0665436aeda62b487528bb8f8f85777c45a357c4cf34884600a14273f289d
4
- data.tar.gz: 0ba29a6cf3b5d453f3561f8e9c3f205c060d088226c83f494ff7a269dc672734
3
+ metadata.gz: cecd9880750c518fcd26fd705b76ffeaf4604fb90af4bd60719ecf37a387d8ee
4
+ data.tar.gz: d04d0ef810ab4ac5ea7b8914690c1995ffdf913c8ad4a40c5bfa85ce5ce8a817
5
5
  SHA512:
6
- metadata.gz: de5d94c98746fd64ecb55f05e658ec434f57c8e518d16bb817ef39e4c56b53cd7130c52ab3ee69dddd5d8b853830dd271374d29c4ac6573576b9cdaf9287c80b
7
- data.tar.gz: cfd9b2aab1a51bae15f335fc74e0970bf406d16f0e522ce3eb239176e0814a59bf0be37f13c4753bb0e2193c828634c7e6de7aeda7f323eb92eede177c3ad8ce
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 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
+ 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 comes from input, such as configuration files,
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 that you want to check the last parameter against.
40
- In the previous example, aValidatorFn::Error`exception will be raised.
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 if you send a valid entry as the second parameter, it will return that last parameter.
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 is a very simple library (100 lines of code) that can be extended as you which by
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
- require_relative "validator_fn"
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).
@@ -20,4 +20,10 @@ module ValidatorFn
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ class MissingKey < Error
25
+ def initialize(key)
26
+ super("Missing field #{key}")
27
+ end
28
+ end
23
29
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatorFn
2
- VERSION = "0.1.8"
2
+ VERSION = "0.2.8"
3
3
  end
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
- memo[key] ||= fn.(hash[key]) if hash[key]
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.1.8
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-02-13 00:00:00.000000000 Z
11
+ date: 2022-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fn_reader