request_validator 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/request_validator.rb +84 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 64ff8578ea1c2078a0cd4c9fb88291179aa66638
4
+ data.tar.gz: 0c78b7df9c96ce730abe965b0545493b45f091f3
5
+ SHA512:
6
+ metadata.gz: 28037cd73e9f255926145573191ce3712e3c85ccb7fd59649939d4cec2500a3e6054dcd4606d7790d9983d1066ed6253c4781e7cbb28f001c80b11606c283c3d
7
+ data.tar.gz: f3d6db8750b8a11f588f15b7e611b51f1f5ab6a621f9d55183719017edc4646b1456ae02ac89176f4e2f8f530fdbd6a687dc64ecb212d4ab5e5986b97d8029b2
@@ -0,0 +1,84 @@
1
+ class Validator
2
+
3
+ @@response = {
4
+ has_errors: false,
5
+ errors: []
6
+ }
7
+ class Configuration
8
+ attr_accessor :skip_main_object_empty, :error_mode
9
+
10
+ def initialize
11
+ @skip_main_object_empty = false
12
+ end
13
+
14
+ def [](value)
15
+ self.public_send(value)
16
+ end
17
+ end
18
+
19
+ def self.configure
20
+ @config ||= Configuration.new
21
+ yield(@config) if block_given?
22
+ @config
23
+ end
24
+
25
+ def self.config
26
+ @config || configure
27
+ end
28
+
29
+ def self.validate(object, checks)
30
+ raise ArgumentError, 'Object cannnot be empty' unless !object.empty? || config.skip_main_object_empty
31
+ checks.each_pair do |key, againsts|
32
+ res = @@functions_hash[againsts].call(object[key])
33
+ if !res[:valid]
34
+ @@response[:errors] << res[:error]
35
+ end
36
+ end
37
+ @@response[:has_errors] = @@response[:errors].length > 0 ? true : false
38
+ return @@response
39
+ end
40
+
41
+ # Structure
42
+ # *********************************************
43
+ # Returns
44
+ # {
45
+ # valid: Boolean,
46
+ # error: String,
47
+ # checked: String
48
+ # }
49
+
50
+ check_string = lambda { | value |
51
+ check = (value =~ /[^a-zA-Z0-9]/).nil?
52
+ return {
53
+ valid: check,
54
+ error: check ? nil : "#{value} is not a valid string",
55
+ checked: value
56
+ }
57
+ }
58
+
59
+ check_email = lambda { | value |
60
+ # http://www.rubydoc.info/stdlib/uri/URI/MailTo
61
+ check = (value =~ /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/).nil?
62
+ return {
63
+ valid: check,
64
+ error: check ? nil : "#{value} is not a valid email address",
65
+ checked: value
66
+ }
67
+ }
68
+
69
+ check_number = lambda { | value |
70
+ check = (value =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/).nil?
71
+ return {
72
+ valid: check,
73
+ error: check ? nil : "#{value} is not a valid number",
74
+ checked: value
75
+ }
76
+ }
77
+
78
+ @@functions_hash = {
79
+ 'string' => check_string,
80
+ 'email' => check_email,
81
+ 'number' => check_number
82
+ }
83
+
84
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: request_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Williams Isaac
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Gem to validate requests before hitting your models, why do you need
14
+ to hit active record before you validate your params ?. This will help do that.
15
+ email: williamscalg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/request_validator.rb
21
+ homepage: http://rubygems.org/gems/request_validator
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.14.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Rails request validator!
45
+ test_files: []