standalone_class_instance_validator 1.0.0
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 +7 -0
- data/lib/standalone_class_instance_validator.rb +105 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5e4747f9f8c285b7b8ea04b5d3045a92993d26ced64ee09cf20d7de464bfbe0d
|
4
|
+
data.tar.gz: ffeb5f11bc7606cdcc5d2d8017a46a3491eea845a3596271f41b84e3e76456c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a99011adcafd6d787a4419b2cd8a2852063b1e0a6148b1e4dc77e6df64227c9b7df4a7569e8dc7db9d705ff207522577538dad6e94a1f57f8902aeeb733438b5
|
7
|
+
data.tar.gz: a5c6883912c20823f640f41d290db450ec0ca4baaafc9eaf1af72bde1a7c912730307d92f69f9248e53dc1c76d195a10d33488d47b00e8f177c351c2cf8b2efb
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module StandaloneClassInstanceValidator
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def initialize(params = {})
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
def class_validate(attribute_name, validations_hash = { })
|
12
|
+
@validated_attributes ||= { }
|
13
|
+
@validated_attributes[attribute_name] ||= { }
|
14
|
+
|
15
|
+
validations_hash.each do |validation_name, validation_comparison_value|
|
16
|
+
@validated_attributes[attribute_name][validation_name] ||= validation_comparison_value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def validated_attributes
|
21
|
+
@validated_attributes ||= { }
|
22
|
+
end
|
23
|
+
|
24
|
+
def attr_accessor(*vars)
|
25
|
+
@attributes ||= []
|
26
|
+
@attributes.concat vars
|
27
|
+
super(*vars)
|
28
|
+
end
|
29
|
+
|
30
|
+
def attributes
|
31
|
+
@attributes
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def validated_attributes
|
37
|
+
self.class.validated_attributes
|
38
|
+
end
|
39
|
+
|
40
|
+
def attributes
|
41
|
+
self.class.attributes
|
42
|
+
end
|
43
|
+
|
44
|
+
def class_valid?
|
45
|
+
validate_everything.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
def class_validate!
|
49
|
+
errors = validate_everything
|
50
|
+
raise errors.join(',') unless errors.empty?
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
# todo: consider showing errors separately for each attribute
|
56
|
+
# like this: { parameter_name: [ error1, error2 ], parameter_two: [ error1 ] }
|
57
|
+
|
58
|
+
def validate_everything
|
59
|
+
@errors = []
|
60
|
+
validated_attributes.keys.each do |parameter|
|
61
|
+
tested_value = send(parameter)
|
62
|
+
full_validation_info = validated_attributes[parameter]
|
63
|
+
full_validation_info.each do |key, value|
|
64
|
+
class_exception_validate!(parameter, key, value, tested_value)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
@errors
|
68
|
+
end
|
69
|
+
|
70
|
+
def class_exception_validate!(parameter, validation_key, validation_value, tested_value)
|
71
|
+
case validation_key
|
72
|
+
when :presence
|
73
|
+
@errors << 'attribute can not be nil' unless instance_parameter_value_present?(parameter)
|
74
|
+
when :format
|
75
|
+
@errors << 'invalid format' unless consider_format_valid?(parameter)
|
76
|
+
when :type
|
77
|
+
@errors << 'invalid type' unless consider_type_valid?(parameter)
|
78
|
+
else
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def instance_parameter_value_present?(parameter)
|
83
|
+
!send(parameter).nil?
|
84
|
+
end
|
85
|
+
|
86
|
+
def instance_parameter_matches_format(parameter)
|
87
|
+
send(parameter).match?(validated_attributes[parameter][:format])
|
88
|
+
end
|
89
|
+
|
90
|
+
def instance_parameter_type_matches_type(parameter)
|
91
|
+
send(parameter).kind_of?(validated_attributes[parameter][:type]) # todo: test for bad user input (e.g. if user enters smth else instead of a class)
|
92
|
+
end
|
93
|
+
|
94
|
+
def instance_parameter_is_a_string(parameter)
|
95
|
+
send(parameter).kind_of?(String) # todo: test for bad user input (e.g. if user enters smth else instead of a class)
|
96
|
+
end
|
97
|
+
|
98
|
+
def consider_format_valid?(parameter)
|
99
|
+
instance_parameter_value_present?(parameter) && instance_parameter_is_a_string(parameter) && instance_parameter_matches_format(parameter)
|
100
|
+
end
|
101
|
+
|
102
|
+
def consider_type_valid?(parameter)
|
103
|
+
instance_parameter_value_present?(parameter) && instance_parameter_type_matches_type(parameter)
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: standalone_class_instance_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Serge Vinogradoff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: An independent validator that validates attribute presence, string format
|
14
|
+
and attribute type.
|
15
|
+
email: zoopyserg@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/standalone_class_instance_validator.rb
|
21
|
+
homepage: https://rubygems.org/gems/standalone_class_instance_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
|
+
rubygems_version: 3.2.15
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Standalone Class Instance Validator!
|
44
|
+
test_files: []
|