compel 0.4.2 → 0.4.3

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
  SHA1:
3
- metadata.gz: 97cfa78c4892af6b318eb14948f7b0defccc5863
4
- data.tar.gz: d30bc04383159e49098a7a861995ee61f1d1e7f0
3
+ metadata.gz: a0f3b93d575f3944b1468d63d8e23ee2ecfca78c
4
+ data.tar.gz: 4dc2e41aec14acc27928fa800bf16831f67829f7
5
5
  SHA512:
6
- metadata.gz: 90ee036e1d29802696e42c90c2c2a8b179de2ccc609b71d597e75fce23389273ebbf39fed245e1dd6811ece2f7b7eb74e2f52faa3bee2abb51025e0640dfe988
7
- data.tar.gz: 8848f0337a8c43b8b012e70b6c117c7352e2c5418dc7c8891063e93d6468825e4b323ccb367e9873960e7b188af4454dfde1b129978e04f18015877d3dd4e012
6
+ metadata.gz: e5c4eaebc721edf7480a06044ccd09f0e73773b0e7d4dcea66a3af2740258014e24a8f02e2f4523ca9f3851d59f4f9da30fdc25600f8501721fc53a1f6434b71
7
+ data.tar.gz: 2a72784727bf7a24a7bed5a18958db7248358962d737c8ad1f052e763c88d932bafa70d4a58bfa581685ff7d9ce4c6c33bd7fc526826aa9fc403500381fe6552
data/README.md CHANGED
@@ -95,6 +95,10 @@ Methods `length`, `min_length` and `max_length` turn the object to validate into
95
95
  - `length(``integer``)`
96
96
  - `min_length(``integer``)`
97
97
  - `max_length(``integer``)`
98
+ - `if`
99
+ - `if(->(value){ value == 1 })`
100
+ - `if{|value| value == 1 }`
101
+ - `if{:custom_validation} # Check the specs for now, I'm rewriting the docs ;)`
98
102
 
99
103
  ==========================
100
104
 
@@ -291,7 +295,7 @@ end
291
295
 
292
296
  Add this line to your application's Gemfile:
293
297
 
294
- gem 'compel', '~> 0.3.7'
298
+ gem 'compel', '~> 0.4.3'
295
299
 
296
300
  And then execute:
297
301
 
@@ -27,6 +27,24 @@ module Compel
27
27
  build_option :max_length, Coercion.coerce!(value, Coercion::Integer), options
28
28
  end
29
29
 
30
+ def if(lambda = nil, options = {}, &block)
31
+ build_option :if, coerce_if_proc(lambda || block), options
32
+
33
+ rescue
34
+ raise Compel::TypeError, 'invalid proc for if'
35
+ end
36
+
37
+ # this is lovely, refactor later
38
+ def coerce_if_proc(proc)
39
+ if proc && proc.is_a?(Proc) &&
40
+ (proc.arity == 1 || proc.arity == 0 &&
41
+ (proc.call.is_a?(::Symbol) || proc.call.is_a?(::String)))
42
+ proc
43
+ else
44
+ fail
45
+ end
46
+ end
47
+
30
48
  end
31
49
 
32
50
  end
@@ -29,7 +29,13 @@ module Compel
29
29
  def validate_value_with_error_message
30
30
  error_message = validate_value
31
31
 
32
- options[:message] || error_message
32
+ if error_message
33
+ error_message_with_value(options[:message] || error_message)
34
+ end
35
+ end
36
+
37
+ def error_message_with_value(message)
38
+ message.gsub(/\{\{value\}\}/, "#{value}")
33
39
  end
34
40
 
35
41
  end
@@ -0,0 +1,25 @@
1
+ module Compel
2
+ module Validation
3
+
4
+ class If < Condition
5
+
6
+ def validate_value
7
+ unless valid?
8
+ "'#{value}' is invalid"
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def valid?
15
+ if option_value.arity == 1
16
+ option_value.call(value)
17
+ else
18
+ eval("#{option_value.call}(#{value})", option_value.binding)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -7,6 +7,7 @@ require 'compel/validation/conditions/format'
7
7
  require 'compel/validation/conditions/length'
8
8
  require 'compel/validation/conditions/min_length'
9
9
  require 'compel/validation/conditions/max_length'
10
+ require 'compel/validation/conditions/if'
10
11
 
11
12
  require 'compel/validation/result'
12
13
 
@@ -24,6 +25,7 @@ module Compel
24
25
  length: Validation::Length,
25
26
  min_length: Validation::MinLength,
26
27
  max_length: Validation::MaxLength,
28
+ if: Validation::If,
27
29
  }
28
30
 
29
31
  def validate(value, type, options)
@@ -1,3 +1,3 @@
1
1
  module Compel
2
- VERSION = '0.4.2'
2
+ VERSION = '0.4.3'
3
3
  end
@@ -217,7 +217,7 @@ describe Compel::Builder do
217
217
 
218
218
  subject(:builder) { Compel.string }
219
219
 
220
- context 'in' do
220
+ context '#in' do
221
221
 
222
222
  it 'should set in value' do
223
223
  builder.in(['a', 'b'])
@@ -366,6 +366,45 @@ describe Compel::Builder do
366
366
 
367
367
  end
368
368
 
369
+ context 'Any' do
370
+
371
+ context '#if' do
372
+
373
+ it 'should have a proc' do
374
+ _proc = Proc.new {|value| value == 1 }
375
+
376
+ schema = Compel.any.if(_proc)
377
+
378
+ expect(schema.options[:if][:value]).to eq _proc
379
+ end
380
+
381
+ it 'should have a block with string or symbol value' do
382
+ schema = Compel.any.if{:is_valid_one}
383
+ expect(schema.options[:if][:value].call).to eq :is_valid_one
384
+
385
+ schema = Compel.any.if{'is_valid_one'}
386
+ expect(schema.options[:if][:value].call).to eq 'is_valid_one'
387
+ end
388
+
389
+ it 'should raise_error for missing value' do
390
+ expect{ Compel.any.if() }.to \
391
+ raise_error Compel::TypeError, 'invalid proc for if'
392
+ end
393
+
394
+ it 'should raise_error for invalid proc' do
395
+ expect{ Compel.any.if('proc') }.to \
396
+ raise_error Compel::TypeError, 'invalid proc for if'
397
+ end
398
+
399
+ it 'should raise_error for invalid proc arity' do
400
+ expect{ Compel.any.if(Proc.new {|a, b| a == b }) }.to \
401
+ raise_error Compel::TypeError, 'invalid proc for if'
402
+ end
403
+
404
+ end
405
+
406
+ end
407
+
369
408
  end
370
409
 
371
410
  context 'Validate' do
@@ -526,10 +565,10 @@ describe Compel::Builder do
526
565
  end
527
566
 
528
567
  it 'should use custom error message' do
529
- schema = Compel.any.length(2, message: 'not the same size')
568
+ schema = Compel.any.length(2, message: '({{value}}) does not have the right size')
530
569
 
531
- expect(schema.validate(12).errors).to \
532
- include('not the same size')
570
+ expect(schema.validate(123).errors).to \
571
+ include('(123) does not have the right size')
533
572
  end
534
573
 
535
574
  end
@@ -552,6 +591,12 @@ describe Compel::Builder do
552
591
  expect(result.valid?).to be true
553
592
  end
554
593
 
594
+ it 'should validate a constant object' do
595
+ schema = Compel.any.length(2)
596
+
597
+ expect(schema.validate(12).valid?).to eq(true)
598
+ end
599
+
555
600
  end
556
601
 
557
602
  end
@@ -586,6 +631,135 @@ describe Compel::Builder do
586
631
 
587
632
  end
588
633
 
634
+ context '#if' do
635
+
636
+ class CustomValidationsKlass
637
+
638
+ attr_reader :value
639
+
640
+ def initialize(value)
641
+ @value = value
642
+ end
643
+
644
+ def validate
645
+ Compel.any.if{:is_custom_valid?}.validate(value)
646
+ end
647
+
648
+ def validate_with_lambda(lambda)
649
+ Compel.any.if(lambda).validate(value)
650
+ end
651
+
652
+ private
653
+
654
+ def is_custom_valid?(value)
655
+ value == assert_value
656
+ end
657
+
658
+ def assert_value
659
+ [1, 2, 3]
660
+ end
661
+
662
+ end
663
+
664
+ context 'invalid' do
665
+
666
+ it 'should validate with custom method' do
667
+ def is_valid_one(value)
668
+ value == 1
669
+ end
670
+
671
+ result = Compel.any.if{:is_valid_one}.validate(2)
672
+
673
+ expect(result.valid?).to eq(false)
674
+ expect(result.errors).to include("'2' is invalid")
675
+ end
676
+
677
+ it 'should validate with lambda' do
678
+ result = Compel.any.if(Proc.new {|value| value == 2 }).validate(1)
679
+
680
+ expect(result.valid?).to eq(false)
681
+ expect(result.errors).to include("'1' is invalid")
682
+ end
683
+
684
+ it 'should validate within an instance method' do
685
+ result = CustomValidationsKlass.new(1).validate
686
+
687
+ expect(result.valid?).to eq(false)
688
+ expect(result.errors).to include("'1' is invalid")
689
+ end
690
+
691
+ it 'should validate within an instance method 1' do
692
+ result = \
693
+ CustomValidationsKlass.new('two')
694
+ .validate_with_lambda(Proc.new {|value| value == [1, 2, 3] })
695
+
696
+ expect(result.valid?).to eq(false)
697
+ expect(result.errors).to include("'two' is invalid")
698
+ end
699
+
700
+ it 'should use custom message with parsed value' do
701
+ schema = \
702
+ Compel.any.if(Proc.new {|value| value == 2 }, message: 'give me a {{value}}!')
703
+
704
+ result = schema.validate(1)
705
+
706
+ expect(result.valid?).to eq(false)
707
+ expect(result.errors).to include('give me a 1!')
708
+ end
709
+
710
+ end
711
+
712
+ context 'valid' do
713
+
714
+ it 'should validate with custom method' do
715
+ def is_valid_one(value)
716
+ value == 1
717
+ end
718
+
719
+ result = Compel.any.if{:is_valid_one}.validate(1)
720
+
721
+ expect(result.valid?).to eq(true)
722
+ end
723
+
724
+ it 'should validate with custom method 1' do
725
+ def is_valid_one(value)
726
+ value == 1
727
+ end
728
+
729
+ result = Compel.any.if{|value| value == 1 }.validate(1)
730
+
731
+ expect(result.valid?).to eq(true)
732
+ end
733
+
734
+ it 'should validate with lambda' do
735
+ result = Compel.any.if(Proc.new {|value| value == 2 }).validate(2)
736
+
737
+ expect(result.valid?).to eq(true)
738
+ end
739
+
740
+ it 'should validate within an instance method' do
741
+ result = CustomValidationsKlass.new([1, 2, 3]).validate
742
+
743
+ expect(result.valid?).to eq(true)
744
+ end
745
+
746
+ it 'should validate within an instance method' do
747
+ result = CustomValidationsKlass.new([1, 2, 3]).validate
748
+ expect(result.valid?).to eq(true)
749
+ end
750
+
751
+ it 'should validate within an instance method 1' do
752
+ result = \
753
+ CustomValidationsKlass.new([1, 2, 3])
754
+ .validate_with_lambda(Proc.new {|value| value == [1, 2, 3] })
755
+
756
+ expect(result.valid?).to eq(true)
757
+ end
758
+
759
+ end
760
+
761
+ end
762
+
589
763
  end
590
764
 
591
765
  context 'Hash' do
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'simplecov'
2
2
  require 'codeclimate-test-reporter'
3
3
 
4
4
  SimpleCov.start do
5
- formatter SimpleCov::Formatter::MultiFormatter[
5
+ formatter SimpleCov::Formatter::MultiFormatter.new [
6
6
  SimpleCov::Formatter::HTMLFormatter,
7
7
  CodeClimate::TestReporter::Formatter
8
8
  ]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joaquim Adráz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-11 00:00:00.000000000 Z
11
+ date: 2016-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -107,6 +107,7 @@ files:
107
107
  - lib/compel/result.rb
108
108
  - lib/compel/validation/conditions/condition.rb
109
109
  - lib/compel/validation/conditions/format.rb
110
+ - lib/compel/validation/conditions/if.rb
110
111
  - lib/compel/validation/conditions/in.rb
111
112
  - lib/compel/validation/conditions/is.rb
112
113
  - lib/compel/validation/conditions/length.rb