ruby-hashvalidator 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e14b70d5bd817cf445ab0306768ea8cbac4a8e2
4
+ data.tar.gz: 9bae3c37387c163c67cb5038c612f479abab3023
5
+ SHA512:
6
+ metadata.gz: 5d4d2a33123a687b784806de2b78f71d19694e532d8950a96958c736f8bddf61fe2ab6a9b2ed5f8bd35f5783691a2160b052847d642a2beb45dad60743c7ed18
7
+ data.tar.gz: 51b7863fa82b882fd8892884c1019a4c930c618d96a662bc9e776e7da4b917ae3f81063d98992a1fa3c2ebc4e0966548a29e5ede988ccb399aa1cda4bb968133
@@ -0,0 +1,11 @@
1
+ module Validation
2
+ module Rules
3
+ def validates_required(data, field)
4
+ !([nil, '', [], {}].include? data[field])
5
+ end
6
+
7
+ def validates_match(data, field, regex)
8
+ data[field] =~ Regexp.new(regex)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path('../validator', __FILE__)
2
+
3
+ module Validation
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../rules', __FILE__)
2
+
3
+ module Validation
4
+ class Validator
5
+ include Rules
6
+
7
+ attr_reader :rules
8
+ attr_reader :errors
9
+
10
+ def initialize
11
+ @rules = {}
12
+ @errors = {}
13
+ end
14
+
15
+ def add_rule(field, rule, message)
16
+ @rules[field] ||= []
17
+ @rules[field].push({'rule' => rule, 'message' => message})
18
+ end
19
+
20
+ def validate(params)
21
+ reset_errors
22
+ @rules.each do |field, rules|
23
+ rules.each do |rule|
24
+ method, *args = rule['rule'].split(',').map(&:strip)
25
+ args = [params, field] + args
26
+ unless send("validates_#{method}", *args)
27
+ @errors[field] ||= []
28
+ @errors[field].push(rule['message'])
29
+ end
30
+ end
31
+ end
32
+ valid?
33
+ end
34
+
35
+ def valid?
36
+ @errors.empty?
37
+ end
38
+
39
+ def reset_errors
40
+ @errors = {}
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-hashvalidator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sirfilip
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Provides hash validation with pure text configuration that can be stored
14
+ in db
15
+ email: github.sirfilip@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/validation/rules.rb
21
+ - lib/validation/validation.rb
22
+ - lib/validation/validator.rb
23
+ homepage: https://github.com/sirfilip/ruby-hashvalidator
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.5.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Simple hash validator
47
+ test_files: []