typero 0.2.3 → 0.2.4

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: 041634ffb9e23cb893ef64b89769a4d7c9a6a991
4
- data.tar.gz: 71985d6075cda25963f0a55dc15ab8b8017c7a53
3
+ metadata.gz: b4f7aa7434411123ed8aad1528410d815ce2018f
4
+ data.tar.gz: e703cabe488d967b68b31f322756b8734f8a6858
5
5
  SHA512:
6
- metadata.gz: 9564d4609fb8fd8e2150046fcf63c69409c4870f68298f64800babcd1afaa75c36358984daca317726e0fe40c4e63cb039e34fa27af99a47a99f80ec992f330b
7
- data.tar.gz: 55891ab5ae341ce5b089e9b0c8b2403d36ef7de8f6a5563f9e56eff51834e996d287644356b0d0788cf460ef0f95031f3152f8b26cec3f686233103b855be779
6
+ metadata.gz: 99ab71faf7c55e56ac49231533bd0ae1132d07839b9b251dbb282127fc86825231b960e21fa023bec28eec511c4ef9e7e2e52f71c68eaa491c33bd3af256c5fe
7
+ data.tar.gz: 395435ca418457cc9833b2b3774af250b1667f377ff38fb608c639c6439889cc56415f5433fb348d0d9d041792860b0d96827252aeedef75527baa4b9c138bde
data/lib/typero.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'fast_blank'
2
+
1
3
  require_relative './typero/instance'
2
4
  require_relative './typero/schema'
3
5
  require_relative './typero/type'
@@ -66,7 +68,7 @@ module Typero
66
68
  def validate(*args)
67
69
  validate!(*args)
68
70
  true
69
- rescue
71
+ rescue TypeError
70
72
  false
71
73
  end
72
74
 
data/lib/typero/schema.rb CHANGED
@@ -51,8 +51,10 @@ module Typero
51
51
  rescue
52
52
  errors[field] = $!.message
53
53
  end
54
- elsif !Typero.validate(value, type)
55
- errors[field] = "#{field.to_s.capitalize} is #{Typero.last_error}"
54
+ else
55
+ Typero.validate!(value, type, check_hash) { |msg|
56
+ errors[field] = "#{field.to_s.capitalize}: #{msg}"
57
+ }
56
58
  end
57
59
  end
58
60
  errors.keys.length > 0 ? errors : nil
data/lib/typero/type.rb CHANGED
@@ -10,16 +10,6 @@ module Typero
10
10
 
11
11
  ### SAFE TO OVERLOAD: START
12
12
 
13
- # default min implementation
14
- def min(value, length)
15
- raise TypeError, "Min #{@type} lenght for #{@name} is #{length}, you have #{value.to_s.length}" if value.to_s.length < length
16
- end
17
-
18
- # default max implementation
19
- def max(value, length)
20
- raise TypeError, "Max #{@type} length for #{@name} is #{length}, you have #{value.to_s.length}" if value.to_s.length > length
21
- end
22
-
23
13
  # if value is not blank, it is sent to set method
24
14
  def set(value)
25
15
  value
@@ -82,9 +72,6 @@ module Typero
82
72
  raise TypeError, @opts[:uniq].class == TrueClass ? "Field #{@name} is uniqe and allready exists" : @opts[:uniq] if filter.first
83
73
  end
84
74
 
85
- min(value, @opts[:min]) if @opts[:min]
86
- max(value, @opts[:max]) if @opts[:max]
87
-
88
75
  validate(value)
89
76
  elsif @opts[:req]
90
77
  msg = @opts[:req].class == TrueClass ? "is required" : @opts[:req]
@@ -13,12 +13,10 @@ module Typero
13
13
  value
14
14
  end
15
15
 
16
- def min(value, length)
17
- raise TypeError, "Min array lenght is #{length} elements" if value.length < length
18
- end
19
-
20
- def max(value, length)
21
- raise TypeError, "Max array lenght is #{length} elements" if value.length > length
16
+ def validate(list)
17
+ raise TypeError, "Min array lenght is #{@opts[:min]} elements" if @opts[:min] && @opts[:min] < list.length
18
+ raise TypeError, "Max array lenght is #{@opts[:max]} elements" if @opts[:max] && @opts[:max] < list.length
19
+ true
22
20
  end
23
21
  end
24
22
  end
@@ -5,10 +5,10 @@ module Typero
5
5
  end
6
6
 
7
7
  def validate(value)
8
- raise TypeError, 'not having at least 8 characters' unless value.to_s.length > 7
9
- raise TypeError, 'missing @' unless value.include?('@')
10
- raise TypeError, 'in wrong domain' unless value =~ /\.\w{2,4}$/
11
- raise TypeError, 'in wrong format' unless value =~ /^[\w\-\.]+\@[\w\-\.]+$/i
8
+ raise TypeError, 'is not having at least 8 characters' unless value.to_s.length > 7
9
+ raise TypeError, 'is missing @' unless value.include?('@')
10
+ raise TypeError, 'is in wrong domain' unless value =~ /\.\w{2,4}$/
11
+ raise TypeError, 'is in wrong format' unless value =~ /^[\w\-\.]+\@[\w\-\.]+$/i
12
12
  true
13
13
  end
14
14
  end
@@ -4,12 +4,11 @@ module Typero
4
4
  value.to_f
5
5
  end
6
6
 
7
- def min(value, length)
8
- raise TypeError, "Min #{@type} lenght for #{@name} is #{length}, you have #{value}" if value.to_f < length
9
- end
10
-
11
- def max(value, length)
12
- raise TypeError, "Max #{@type} length for #{@name} is #{length}, you have #{value}" if value.to_f > length
7
+ def validate(input)
8
+ value = set(input)
9
+ raise TypeError, "min lenght is #{@opts[:min]}" if @opts[:min] && value < @opts[:min]
10
+ raise TypeError, "max lenght is #{@opts[:max]}" if @opts[:max] && value > @opts[:max]
11
+ true
13
12
  end
14
13
  end
15
14
  end
@@ -3,6 +3,15 @@ module Typero
3
3
  def set(value)
4
4
  value.to_i
5
5
  end
6
+
7
+ def validate(input)
8
+ value = set(input)
9
+
10
+ raise TypeError, "min lenght is #{@opts[:min]}, you have #{value}" if @opts[:min] && value < @opts[:min]
11
+ raise TypeError, "max lenght is #{@opts[:max]}, you have #{value}" if @opts[:max] && value > @opts[:max]
12
+
13
+ true
14
+ end
6
15
  end
7
16
  end
8
17
 
@@ -5,7 +5,7 @@ module Typero
5
5
  end
6
6
 
7
7
  def validate(value)
8
- raise TypeError, "having unallowed characters" unless value =~ /^[\w\-]+$/
8
+ raise TypeError, "Label is having unallowed characters" unless value =~ /^[\w\-]+$/
9
9
  true
10
10
  end
11
11
  end
@@ -1,7 +1,7 @@
1
1
  module Typero
2
2
  class UrlType < Typero::Type
3
3
  def validate(value)
4
- raise TypeError, 'not starting with http' unless value =~ /^https?:\/\/./
4
+ raise TypeError, 'URL is not starting with http' unless value =~ /^https?:\/\/./
5
5
  true
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module Typero
2
- VERSION = "0.2.3".freeze
2
+ VERSION = "0.2.4".freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dino Reic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-26 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fast_blank
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_support/core_ext/string
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: Simple and fast ruby type system. Enforce types as Array, Email, Boolean
14
42
  for ruby class instances
15
43
  email: reic.dino@gmail.com