tram-validators 0.0.4 → 0.0.5
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 +4 -4
- data/.rubocop_todo.yml +12 -0
- data/CHANGELOG.md +7 -0
- data/lib/tram/locales/en.yml +12 -0
- data/lib/tram/locales/ru.yml +13 -0
- data/lib/tram/validators.rb +4 -0
- data/lib/tram/validators/consistency_validator.rb +4 -1
- data/lib/tram/validators/outcome_validator.rb +7 -13
- data/lib/tram/validators/reference_validator.rb +6 -1
- data/lib/tram/validators/size_validator.rb +7 -3
- data/lib/tram/validators/validity_validator.rb +4 -3
- data/tram-validators.gemspec +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f890e67e5985736d2fb52569af92743acd02f021
|
4
|
+
data.tar.gz: '0952d74dcd81759ce8b04118b053758452f86f8f'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8a72c30378c53d742caa9069adc4b77fab8c9bd144bd81d6649f4073000bc8041b8e51a65e8bba50ccbc5123572de02bedf13e82ad660855114f7350b0460e7
|
7
|
+
data.tar.gz: cb5e2e9ff8a40adef4f072a558e2b6e187124e4b128bd12f85d9e8bfde74ccf13599a403c3ccc1ccfd7b572767894227101bd88c99dd21ab90cc1022483940c3
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-02-06 13:19:54 +0300 using RuboCop version 0.47.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
# Configuration parameters: CountComments.
|
11
|
+
Metrics/MethodLength:
|
12
|
+
Max: 12
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
---
|
2
|
+
en:
|
3
|
+
errors:
|
4
|
+
messages:
|
5
|
+
not_validatable: "cannot be validated"
|
6
|
+
not_found: "refers to no record of %{klass} by %{key}: %{value}"
|
7
|
+
size_equal_to: "has size %{size} other than %{limit}"
|
8
|
+
size_other_than: "has size equal to %{limit}"
|
9
|
+
size_greater_than: "has size %{size} which isn't greater than %{limit}"
|
10
|
+
size_greater_than_or_equal_to: "has size %{size} which is less than %{limit}"
|
11
|
+
size_less_than: "has size %{size} which isn't less than %{limit}"
|
12
|
+
size_less_than_or_equal_to: "has size %{size} which is greater than %{limit}"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
---
|
2
|
+
ru:
|
3
|
+
errors:
|
4
|
+
messages:
|
5
|
+
invalid: "имеет некорректное значение"
|
6
|
+
not_validatable: "не может быть проверен"
|
7
|
+
not_found: "не соответствует объекту класса %{klass} с идентификатором %{key}: %{value}"
|
8
|
+
size_equal_to: "имеет размер %{size} отличный от %{limit}"
|
9
|
+
size_other_than: "имеет размер равный %{limit}"
|
10
|
+
size_greater_than: "имеет размер %{size} не превышающий %{limit}"
|
11
|
+
size_greater_than_or_equal_to: "имеет размер %{size} меньший %{limit}"
|
12
|
+
size_less_than: "имеет размер %{size} не меньший %{limit}"
|
13
|
+
size_less_than_or_equal_to: "имеет размер %{size} превышающий %{limit}"
|
data/lib/tram/validators.rb
CHANGED
@@ -21,6 +21,9 @@ class ConsistencyValidator < ActiveModel::EachValidator
|
|
21
21
|
return if value && other_value && yield(value, other_value)
|
22
22
|
|
23
23
|
error_name = [condition, chain.to_s.split(".")].flatten.join("_").to_sym
|
24
|
-
record.errors.add attribute, error_name,
|
24
|
+
record.errors.add attribute, error_name, model: record,
|
25
|
+
attribute: attribute,
|
26
|
+
value: value,
|
27
|
+
other: other_value
|
25
28
|
end
|
26
29
|
end
|
@@ -11,6 +11,7 @@
|
|
11
11
|
# # user_id.user_name_presence
|
12
12
|
#
|
13
13
|
class OutcomeValidator < ActiveModel::EachValidator
|
14
|
+
# rubocop: disable Metrics/MethodLength
|
14
15
|
def validate_each(record, attribute, value)
|
15
16
|
dependency = options[:value].to_s.gsub(".", "_")
|
16
17
|
dependent = Tram::Validators.chained_value(record, options[:value])
|
@@ -20,11 +21,14 @@ class OutcomeValidator < ActiveModel::EachValidator
|
|
20
21
|
validators.each do |condition, validator|
|
21
22
|
next if valid_in_sandbox(sandbox, attribute, dependent, validator)
|
22
23
|
|
23
|
-
key
|
24
|
-
|
25
|
-
|
24
|
+
key = message_key(dependency, condition)
|
25
|
+
record.errors.add attribute, key, model: record,
|
26
|
+
attribute: attribute,
|
27
|
+
value: value,
|
28
|
+
delependent: dependent
|
26
29
|
end
|
27
30
|
end
|
31
|
+
# rubocop: enable Metrics/MethodLength
|
28
32
|
|
29
33
|
private
|
30
34
|
|
@@ -37,14 +41,4 @@ class OutcomeValidator < ActiveModel::EachValidator
|
|
37
41
|
def message_key(dependency, condition)
|
38
42
|
[dependency, condition.to_s].compact.join("_").to_sym
|
39
43
|
end
|
40
|
-
|
41
|
-
def message(record, attribute, value, dependent, message_key)
|
42
|
-
model = record.class.name.underscore
|
43
|
-
scope = %W(active_model errors models #{model} attributes #{attribute})
|
44
|
-
I18n.t message_key, record: record,
|
45
|
-
attribute: attribute,
|
46
|
-
value: value,
|
47
|
-
delependent: dependent,
|
48
|
-
scope: scope
|
49
|
-
end
|
50
44
|
end
|
@@ -6,9 +6,14 @@
|
|
6
6
|
class ReferenceValidator < ActiveModel::EachValidator
|
7
7
|
def validate_each(record, attribute, value)
|
8
8
|
model = options.fetch(:model) { raise "You should define :model option" }
|
9
|
+
model = model.constantize if model.is_a? String
|
9
10
|
key = options.fetch(:find_by, :id)
|
10
11
|
|
11
12
|
return if model.find_by(key => value)
|
12
|
-
record.errors.add attribute, :not_found
|
13
|
+
record.errors.add attribute, :not_found, model: record,
|
14
|
+
attribute: attribute,
|
15
|
+
value: value,
|
16
|
+
klass: model.name,
|
17
|
+
key: key
|
13
18
|
end
|
14
19
|
end
|
@@ -8,19 +8,23 @@ class SizeValidator < ActiveModel::EachValidator
|
|
8
8
|
def validate_each(record, attribute, value)
|
9
9
|
size = value.size if value.respond_to? :size
|
10
10
|
Tram::Validators::CONDITIONS.each do |key, block|
|
11
|
-
check(key, record, attribute, size, &block)
|
11
|
+
check(key, record, attribute, value, size, &block)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
-
def check(condition, record, attribute, size)
|
17
|
+
def check(condition, record, attribute, value, size)
|
18
18
|
return unless options.key? condition
|
19
19
|
|
20
20
|
limit = extract_limit(record, condition)
|
21
21
|
return if size && limit && yield(size, limit.to_i)
|
22
22
|
|
23
|
-
record.errors.add attribute, error_key(condition),
|
23
|
+
record.errors.add attribute, error_key(condition), model: record,
|
24
|
+
attribute: attribute,
|
25
|
+
value: value,
|
26
|
+
size: size,
|
27
|
+
limit: limit
|
24
28
|
end
|
25
29
|
|
26
30
|
def extract_limit(record, condition)
|
@@ -14,10 +14,11 @@
|
|
14
14
|
class ValidityValidator < ActiveModel::EachValidator
|
15
15
|
def validate_each(record, key, value)
|
16
16
|
if !value.respond_to? :valid?
|
17
|
-
record.errors
|
18
|
-
|
17
|
+
record.errors.add attribute, :not_validatable, model: record,
|
18
|
+
attribute: key,
|
19
|
+
value: value
|
19
20
|
elsif !value.valid?
|
20
|
-
Tram::Validators.copy_errors(value, record, key, :
|
21
|
+
Tram::Validators.copy_errors(value, record, key, :invalid, value, options)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
data/tram-validators.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tram-validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kozin (nepalez)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- ".gitignore"
|
80
80
|
- ".rspec"
|
81
81
|
- ".rubocop.yml"
|
82
|
+
- ".rubocop_todo.yml"
|
82
83
|
- ".travis.yml"
|
83
84
|
- CHANGELOG.md
|
84
85
|
- Gemfile
|
@@ -86,6 +87,8 @@ files:
|
|
86
87
|
- README.md
|
87
88
|
- Rakefile
|
88
89
|
- lib/tram-validators.rb
|
90
|
+
- lib/tram/locales/en.yml
|
91
|
+
- lib/tram/locales/ru.yml
|
89
92
|
- lib/tram/validators.rb
|
90
93
|
- lib/tram/validators/consistency_validator.rb
|
91
94
|
- lib/tram/validators/contract_validator.rb
|