validator_fn 0.3.9 → 0.5.0

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: ee9bfac9a9c6e88cd08fc270860a60a919c2389a390fb7086e8767048542e0c7
4
- data.tar.gz: 2168dc33c3a7bc6fba8f47f3832c744a2ec9514b0f9b752aa35fe2ce93d92c63
3
+ metadata.gz: 1748730b9f3adb3a7388282d193b000ba67f97f5f418aa306c699eb546bcfa03
4
+ data.tar.gz: a3625b480793914edb326fadd3640ec2e607dd3dc6e443f4ec3460bdb2c39a36
5
5
  SHA512:
6
- metadata.gz: 944e615250789bb463461c42aab479685ca1dba5e386550d1d54ae08234e1a7142d21c78680b0aab1071fdcbc89666e9ba6be8ed68674cbe3fe31d9677ef48a7
7
- data.tar.gz: d4f6d2389ecfef0b4b0e12305097b3890b80577d57318a9b8cfa99ca7e0029f2eacb68fddfce295fb59b818a3f670ac68f0dbef955125253d9a0690bb870b61a
6
+ metadata.gz: d8a1749eb560ed5935b879faac3437912bb7ef618b35bc5ea54d1f15664ec7eba6006b6c7a10099eae0014dc84844b433e648d7dece870cadca44122db03760c
7
+ data.tar.gz: fa5a2b780de8ea3ef0982c3f5107ddabc194d71d058ca24025bb2d62b3c9fdbe9fe8b4089d7711007ec41e5f3445b67255b718677788c589f9bedcd4d5d55ae9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validator_fn (0.3.9)
4
+ validator_fn (0.4.0)
5
5
  fn_reader
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -62,7 +62,7 @@ user = hash_of.({name: is_a.(String), age: to_int})
62
62
  user.({name: "", age: "234"})
63
63
  ```
64
64
 
65
- Since we are using curried lambdas, you can compose validators as you which.
65
+ Since we are using curried lambdas, you can compose validators as you wish.
66
66
 
67
67
  ```ruby
68
68
  postal_code = -> a {
@@ -97,7 +97,7 @@ This makes it very useful for processing JSON payloads.
97
97
  You can generate validator code from existing structure:
98
98
 
99
99
  ```ruby
100
- require 'openuri'
100
+ require 'open-uri'
101
101
  require "json"
102
102
  struct = JSON.parse(URI.open("https://api.github.com/users/defunkt").read)
103
103
 
@@ -109,6 +109,9 @@ code = ValidatorFn.generate_validator.(struct)
109
109
  # You can reformat it using a code formatter
110
110
  require "rufo"
111
111
  puts Rufo.format(code)
112
+ ```
113
+ The output
114
+ ```
112
115
  hash_of.({ "login" => is_a.(String),
113
116
  "id" => is_a.(Integer),
114
117
  "node_id" => is_a.(String),
@@ -23,7 +23,7 @@ module ValidatorFn
23
23
 
24
24
  class MissingKey < Error
25
25
  def initialize(key)
26
- super("Missing field #{key}")
26
+ super("Missing field #{key.inspect}")
27
27
  end
28
28
  end
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatorFn
2
- VERSION = "0.3.9"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/validator_fn.rb CHANGED
@@ -4,7 +4,7 @@ require "fn_reader"
4
4
 
5
5
  module ValidatorFn
6
6
  fn_reader :something, :matches, :either, :array_of, :any, :is_nil,
7
- :maybe, :present, :is_a, :is_a_bool, :int, :hash_of, :invalid, :generate_validator, :handle_error, :error_str, :apply
7
+ :maybe, :present, :is_a, :is_a_bool, :int, :hash_of, :invalid, :generate_validator, :handle_error, :error_str, :apply, :enum_of
8
8
 
9
9
  @@apply = ->fn, a {
10
10
  begin
@@ -24,6 +24,10 @@ module ValidatorFn
24
24
  end
25
25
  value
26
26
  }.curry
27
+ @@enum_of = ->enum, a {
28
+ raise Error.new("Should be in #{enum.join(",")}") unless enum.include?(a)
29
+ a
30
+ }.curry
27
31
  @@array_of = ->fn, array {
28
32
  is_a.(Array).(array)
29
33
  array.each_with_index.map do |a, idx|
@@ -42,10 +46,13 @@ module ValidatorFn
42
46
  @@hash_of = ->fields, hash {
43
47
  hash ||= {}
44
48
  fields.reduce({}) do |memo, (key, fn)|
45
- memo[key] = fn.(hash[key])
49
+ value = hash.fetch(key) { raise MissingKey.new(key) }
50
+ memo[key] = fn.(value)
46
51
  memo
47
52
  rescue Error => e
48
53
  invalid.("Invalid value for #{key.inspect} key:")
54
+ rescue MissingKey => e
55
+ invalid.(e.message)
49
56
  end
50
57
  }.curry
51
58
 
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.3.9
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Chabot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-23 00:00:00.000000000 Z
11
+ date: 2025-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fn_reader
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  requirements: []
70
- rubygems_version: 3.4.1
70
+ rubygems_version: 3.3.22
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: Series of lambdas for validating Ruby structures