rulix 0.7.0 → 0.8.2

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
  SHA1:
3
- metadata.gz: 3844bac00c8405d5f9b563f4ad3fffc7745c8295
4
- data.tar.gz: 786e4e7088079f2124e2ec8cabcdd022119d9e7f
3
+ metadata.gz: d239e13723c2772e811664e150bd906d55809794
4
+ data.tar.gz: 25d0a4bd9361533cf142c6ba0ffc8689f38f94e9
5
5
  SHA512:
6
- metadata.gz: 26a4ac9b1353fb14d1e0177af5e7089bc729b1bd3b1f5d44f390afbb1854041bd92d8af3f4a0b9bcfffba4aa89e0b7d3efef6cae2506359ecfaebd3f8b0761d2
7
- data.tar.gz: 1c3eb703a716b2664f2799e32a4f82ef3eaf4ff1cdee24615f22f5ab953f23c791987eddb5b12c56cc715855e79812c572835be545608e683379db200a5eb6e7
6
+ metadata.gz: 8d37da7824088a88ca88b748ef38972e3514db928bf59bac6bf211984c2611bc7abb7f48695dfa40ae5c0f9ee1fcfd06295db448801b3f631368b21987b597a6
7
+ data.tar.gz: 5434eb4aeef2241513ee23d580cc3202d843a1e18fef7e248038f4316192bfd5c525bfb54f27f96175f4af28ab729a7eb49a7912493ebaf5715cb1d9d76c374c
data/README.md CHANGED
@@ -184,7 +184,7 @@ See the wiki for a [list of included mutator functions](https://github.com/blars
184
184
 
185
185
  ## Validation
186
186
 
187
- Use `Rulix::Validator` to validate a dataset. You can test if a dataset is valid with `Rulix::Validator.valid?(dataset, ruleset)`. If your dataset fails validation, you can extract errors with `Rulix::Validator.errors(dataset, ruleset)`
187
+ Use `Rulix::Validator` to validate a dataset. `Rulix::Validator.run` will return a `Rulix::Validation` object, which is just hash with a few extra helpful methods (like `valid?` and `error_messages`).
188
188
 
189
189
  ```ruby
190
190
  dataset = {
@@ -199,10 +199,14 @@ ruleset = {
199
199
  }
200
200
  }
201
201
 
202
- Rulix::Validator.valid? dataset, ruleset
202
+ result = Rulix::Validator.run dataset, ruleset
203
+
204
+ result.valid?
203
205
  #=> false
204
- Rulix::Validator.errors dataset, ruleset
206
+ result
205
207
  #=> { phone: { number: ['does not match format'] } }
208
+ result.error_messages
209
+ #=> ['Number does not match format']
206
210
  ```
207
211
 
208
212
  See the wiki for a [list of included validation functions](https://github.com/blarshk/rulix/wiki/List-of-Included-Validation-Functions). If you have a validation function that you like that you think should be included in Rulix's base set, submit a pull request!
@@ -272,6 +276,25 @@ Rulix::Validator.run data, rules
272
276
  #=> { my_field: ['is not a number', 'ends in oo'] }
273
277
  ```
274
278
 
279
+ The `error_message` compontent of your validations can be anything you want; the results will be concatenated into a single array.
280
+
281
+ ```ruby
282
+ Rulix::Validator.register :has_a_digit do |val|
283
+ /\d/.match(val) ? true : [false, { code: '001', message: 'The value supplied does not contain a digit!' }]
284
+ end
285
+
286
+ data = {
287
+ my_field: 'bar'
288
+ }
289
+
290
+ rules = {
291
+ my_field: [:has_a_digit]
292
+ }
293
+
294
+ Rulix::Validator.run data, rules
295
+ #=> { my_field: [{ code: '001', message: 'The value supplied does not contain a digit!' }] }
296
+ ```
297
+
275
298
  ## Contributing
276
299
 
277
300
  1. Fork it ( https://github.com/blarshk/rulix/fork )
@@ -1,5 +1,8 @@
1
+ require 'delegate'
2
+
1
3
  require_relative './rulix/version'
2
4
  require_relative './rulix/errors'
5
+ require_relative './rulix/validation'
3
6
  require_relative './rulix/registry'
4
7
  require_relative './rulix/base'
5
8
  require_relative './rulix/mutator'
@@ -9,12 +9,12 @@ module Rulix
9
9
  end
10
10
 
11
11
  def validate record
12
- if Rulix::Validator.valid? record, ruleset
12
+ result = Rulix::Validator.run record, ruleset
13
+
14
+ if result.valid?
13
15
  true
14
16
  else
15
- errors = Rulix::Validator.errors record, ruleset
16
-
17
- reduce_errors_into record, errors
17
+ reduce_errors_into record, result
18
18
 
19
19
  false
20
20
  end
@@ -2,20 +2,18 @@ module Rulix
2
2
  class Base
3
3
  include Rulix::Registry
4
4
 
5
+ # This will ALWAYS return a hash with symbolized keys
6
+ # This was a design decision to reduce the potential for dependencies
7
+ # by introducing HashWithIndifferentAccess or edge cases in rule
8
+ # registration and such
5
9
  def self.run dataset, ruleset
6
10
  return to_enum(__callee__) unless block_given?
7
11
 
8
- coercion = -> (result) { result }
9
-
10
- if dataset.is_a?(Hash)
11
- coercion = -> (result) { result.deep_stringify_keys } if String === dataset.keys.first
12
-
13
- dataset = dataset.deep_symbolize_keys
14
- end
12
+ dataset = dataset.deep_symbolize_keys if dataset.is_a?(Hash)
15
13
 
16
14
  dataset = data_for_ruleset dataset, ruleset
17
15
 
18
- result = dataset.deep_merge ruleset do |key, data_obj, operation_obj|
16
+ dataset.deep_merge ruleset do |key, data_obj, operation_obj|
19
17
  if (data_obj.is_a?(Array) && data_obj.all? { |o| o.respond_to?(:deep_merge) })
20
18
  data_obj.map do |data|
21
19
  data.deep_merge operation_obj.first do |k, d, o|
@@ -30,8 +28,6 @@ module Rulix
30
28
  yield *handle_merge(data_obj, operation_obj)
31
29
  end
32
30
  end
33
-
34
- coercion.call(result)
35
31
  end
36
32
 
37
33
  def self.handle_merge data_obj, operation_obj
@@ -0,0 +1,43 @@
1
+ module Rulix
2
+ class Validation < SimpleDelegator
3
+ def valid?
4
+ deep_compact.empty?
5
+ end
6
+
7
+ def error_messages
8
+ extract_error_messages self
9
+ end
10
+
11
+ def extract_error_messages hash
12
+ hash.map do |key, val|
13
+ if data_remaining?(val)
14
+ extract_error_messages val
15
+ else
16
+ format_val(key, val)
17
+ end
18
+ end.flatten
19
+ end
20
+
21
+ def data_remaining? val
22
+ # If val is a hash and any of its children are hashes,
23
+ # or all of them are arrays
24
+ # we assume that we haven't yet reached the deepest point
25
+ # of the result set
26
+ val.is_a?(Hash) &&
27
+ (
28
+ val.values.any? { |v| v.is_a?(Hash)} ||
29
+ val.values.all? { |v| v.is_a?(Array)}
30
+ )
31
+ end
32
+
33
+ def format_val key, val
34
+ if val.is_a?(Array) && val.all? { |v| v.is_a?(String) }
35
+ val.map do |v|
36
+ "#{key.to_s.capitalize} #{v}"
37
+ end
38
+ else
39
+ val
40
+ end
41
+ end
42
+ end
43
+ end
@@ -2,34 +2,32 @@ module Rulix
2
2
  class Validator < Rulix::Base
3
3
  include Rulix::Registry
4
4
 
5
- def self.run dataset, ruleset
6
- super dataset, ruleset do |value, operations|
7
- begin
8
- success, errors = operations.reduce([true, []]) do |result, op|
9
- success, errors = result
10
-
11
- new_success, *new_errors = op.call(value)
12
-
13
- [success && new_success, errors.concat(new_errors)]
5
+ class << self
6
+ def run dataset, ruleset
7
+ result = super dataset, ruleset do |value, operations|
8
+ begin
9
+ success, errors = operations.reduce([true, []]) do |result, op|
10
+ success, errors = result
11
+
12
+ new_success, *new_errors = op.call(value)
13
+
14
+ [success && new_success, errors.concat(new_errors)]
15
+ end
16
+ rescue AllowableNil
17
+ errors = []
14
18
  end
15
- rescue AllowableNil
16
- errors = []
19
+
20
+ errors
17
21
  end
18
22
 
19
- errors
23
+ Rulix::Validation.new(result.deep_compact)
20
24
  end
21
- end
22
25
 
23
- def self.valid? dataset, ruleset
24
- run = run dataset, ruleset
26
+ def valid? dataset, ruleset
27
+ run = run dataset, ruleset
25
28
 
26
- run.deep_compact.empty?
27
- end
28
-
29
- def self.errors dataset, ruleset
30
- run = run dataset, ruleset
31
-
32
- run.deep_compact
29
+ run.empty?
30
+ end
33
31
  end
34
32
  end
35
33
  end
@@ -1,3 +1,3 @@
1
1
  module Rulix
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rulix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitch Monsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-30 00:00:00.000000000 Z
11
+ date: 2016-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -79,6 +79,7 @@ files:
79
79
  - lib/rulix/mutators/strip.rb
80
80
  - lib/rulix/mutators/truncate.rb
81
81
  - lib/rulix/registry.rb
82
+ - lib/rulix/validation.rb
82
83
  - lib/rulix/validator.rb
83
84
  - lib/rulix/validators/allow_nil_validator.rb
84
85
  - lib/rulix/validators/alpha_numeric_validator.rb