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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 860e9db64270b3cb2520b593d0ad026e1653497889cb5cf0a3e8a884f9da7f31
4
- data.tar.gz: fa55b8d02579d6d38f3dd2f3222a6a0a3574f4de9d9efc71696605d02572bec0
3
+ metadata.gz: 9a18a2dee04cebf84a277132258a1456f981fe477467d041ea6c29e564051a74
4
+ data.tar.gz: 2c82211054e4b07ee55f068dd87218b8f4d2da001889cd054d220be6346d84f0
5
5
  SHA512:
6
- metadata.gz: becdc4ee94dfa39c885257213f04927b6dcb160421ba80a328b0405d5f51f1c674975e0898056765f927ca4e0ad4062658a03400e149f554c693a120c3ecef01
7
- data.tar.gz: a98a27f35f8aa7fd2b0832b931bd5b876d0af2d89d9f5f8b153d49afebdb691dbcc6675bf7abfe75fba20e5f5dce210837398d878442148657620977de7c625e
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.5)
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.5"
2
+ VERSION = "0.1.6"
3
3
  end
data/lib/validator_fn.rb CHANGED
@@ -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.5
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-01-29 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