validaty 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.yml +4 -5
- data/Makefile +12 -0
- data/README.md +7 -36
- data/lib/validators/cnpj_validator.rb +1 -1
- data/lib/validators/cpf_validator.rb +1 -1
- data/lib/validators/date_validator.rb +3 -1
- data/lib/validators/email_validator.rb +1 -1
- data/lib/validators/phone_validator.rb +10 -3
- data/lib/validators/url_validator.rb +5 -3
- data/lib/validators/uuid_validator.rb +1 -1
- data/lib/validators/word_count_validator.rb +23 -0
- data/lib/validaty/base.rb +3 -1
- data/lib/validaty/locales/en.yml +15 -0
- data/lib/validaty/locales/pt-BR.yml +15 -0
- data/lib/validaty/version.rb +1 -1
- data/lib/validaty.rb +7 -0
- data/validaty.gemspec +2 -0
- metadata +20 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c94f22233330f514a413262ffea2a2f611b6a5fec8fb500fe3dc8b5615b543ae
|
|
4
|
+
data.tar.gz: fa246c30efb3390a4d14aa3b4b2e3b874165d74a1f92d915ddc928fca6defe2c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 42c89ff164a4e3ec740cd5a4e5dd2d3eb937be51e0a1cf84112ed4964475c2fe73e4ac29511a917ab63eba63803b0e390f893c5b77e999cc565d3c6f9d1a3aac
|
|
7
|
+
data.tar.gz: c3942fa5ec96764027dbce2ba8201d4215b35dd78f0dc3214f6973df5ef75727d386d50ddc5cecabd7e82cdcd40cc0d52fe56790731bc416277bd96d1b43de3d
|
data/.rubocop.yml
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
require:
|
|
2
|
-
- rubocop-rails
|
|
3
|
-
- rubocop-factory_bot
|
|
4
|
-
- rubocop-capybara
|
|
5
2
|
- rubocop-rspec
|
|
6
3
|
- rubocop-rake
|
|
7
4
|
|
|
@@ -10,5 +7,7 @@ inherit_gem:
|
|
|
10
7
|
|
|
11
8
|
AllCops:
|
|
12
9
|
NewCops: enable
|
|
13
|
-
TargetRubyVersion: 3.
|
|
14
|
-
|
|
10
|
+
TargetRubyVersion: 3.4
|
|
11
|
+
|
|
12
|
+
RSpec/MultipleExpectations:
|
|
13
|
+
Max: 2
|
data/Makefile
CHANGED
data/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Validaty has basic validations for Rails application
|
|
|
7
7
|
Add this line to your application's Gemfile:
|
|
8
8
|
|
|
9
9
|
```ru
|
|
10
|
-
gem "validaty", "~> 0.0.
|
|
10
|
+
gem "validaty", "~> 0.0.5"
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
And then execute:
|
|
@@ -28,7 +28,8 @@ Imagine this schema:
|
|
|
28
28
|
|
|
29
29
|
```rb
|
|
30
30
|
create_table "pixes", force: :cascade do |t|
|
|
31
|
-
t.uuid "
|
|
31
|
+
t.uuid "pid", null: false
|
|
32
|
+
t.string "name", null: false
|
|
32
33
|
t.integer "kind"
|
|
33
34
|
t.string "key", null: false
|
|
34
35
|
t.string "url", null: false
|
|
@@ -45,12 +46,13 @@ You should validate this way:
|
|
|
45
46
|
class Pix < ApplicationRecord
|
|
46
47
|
enum :kind, {cpf: 0, cnpj: 1, email: 2, phone: 3, evp: 4}
|
|
47
48
|
|
|
48
|
-
validates :
|
|
49
|
+
validates :pid, uuid: true
|
|
50
|
+
validates :name, presence: true, word_count: {min: 3, max: 10}
|
|
49
51
|
validates :kind, presence: true
|
|
50
52
|
validates :key, cpf: true, if: :cpf?
|
|
51
53
|
validates :key, cnpj: true, if: :cnpj?
|
|
52
54
|
validates :key, email: true, if: :email?
|
|
53
|
-
validates :key, phone: {
|
|
55
|
+
validates :key, phone: {country_calling_code: :calling_code}, if: :phone?
|
|
54
56
|
validates :key, uuid: true, if: :evp?
|
|
55
57
|
validates :accepted, boolean: true
|
|
56
58
|
validates :schedule_date, date: true
|
|
@@ -59,41 +61,10 @@ class Pix < ApplicationRecord
|
|
|
59
61
|
# OR
|
|
60
62
|
validates :url, presence: true, url: {domain: "domain.com"}
|
|
61
63
|
# OR
|
|
62
|
-
validates :url, presence: true, url: {
|
|
64
|
+
validates :url, presence: true, url: {start_with: "https://domain.com/path"}
|
|
63
65
|
end
|
|
64
66
|
```
|
|
65
67
|
|
|
66
|
-
## Translations
|
|
67
|
-
|
|
68
|
-
If you using url validation with options `:domain` or `:starts_with`, you must add in your locales:
|
|
69
|
-
|
|
70
|
-
```yaml
|
|
71
|
-
pt-BR:
|
|
72
|
-
...
|
|
73
|
-
errors:
|
|
74
|
-
messages:
|
|
75
|
-
domain: "precisa ser do domínio %{domain}"
|
|
76
|
-
starts_with: "precisa começar com %{start}"
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
## Troubleshooting
|
|
80
|
-
|
|
81
|
-
If you need use CPF or CNPJ validation you need add 2 acronym to `config/initializers/inflections.rb`
|
|
82
|
-
|
|
83
|
-
```rb
|
|
84
|
-
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
|
85
|
-
inflect.acronym "CPF"
|
|
86
|
-
inflect.acronym "CNPJ"
|
|
87
|
-
end
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
If you can't add inflections in your application, the suggestion is create an initializer `config/initializers/validaty.rb` with this content:
|
|
91
|
-
|
|
92
|
-
```rb
|
|
93
|
-
CnpjValidator = CNPJValidator
|
|
94
|
-
CpfValidator = CPFValidator
|
|
95
|
-
```
|
|
96
|
-
|
|
97
68
|
## Contributing
|
|
98
69
|
|
|
99
70
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validaty.
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
require "phone"
|
|
2
2
|
|
|
3
3
|
class PhoneValidator < Validaty::AllowBlankBase
|
|
4
|
+
DEFAULT_COUNTRY_CALLING_CODE = "55"
|
|
5
|
+
|
|
4
6
|
private
|
|
5
7
|
|
|
6
8
|
def valid_value?(value)
|
|
@@ -8,14 +10,19 @@ class PhoneValidator < Validaty::AllowBlankBase
|
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
def prepare_params(resource, attribute, value)
|
|
11
|
-
|
|
13
|
+
country_calling_code =
|
|
14
|
+
resource
|
|
15
|
+
.send(options[:country_calling_code])
|
|
16
|
+
.to_s
|
|
17
|
+
.gsub(/[^0-9]/, "")
|
|
18
|
+
.presence || DEFAULT_COUNTRY_CALLING_CODE
|
|
12
19
|
|
|
13
|
-
value = ["+",
|
|
20
|
+
value = ["+", country_calling_code, value.to_s.gsub(/[^0-9]/, "")].join
|
|
14
21
|
|
|
15
22
|
[resource, attribute, value]
|
|
16
23
|
end
|
|
17
24
|
|
|
18
25
|
def default_message_error
|
|
19
|
-
:
|
|
26
|
+
:invalid_phone
|
|
20
27
|
end
|
|
21
28
|
end
|
|
@@ -6,8 +6,10 @@ class UrlValidator < Validaty::AllowBlankBase
|
|
|
6
6
|
resource.errors.add(attribute, :domain, domain: options[:domain])
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
start = options[:start_with].presence || options[:starts_with]
|
|
10
|
+
|
|
11
|
+
if start.present? && !value.to_s.start_with?(start)
|
|
12
|
+
resource.errors.add(attribute, :start_with, start:)
|
|
11
13
|
end
|
|
12
14
|
|
|
13
15
|
super
|
|
@@ -23,6 +25,6 @@ class UrlValidator < Validaty::AllowBlankBase
|
|
|
23
25
|
end
|
|
24
26
|
|
|
25
27
|
def default_message_error
|
|
26
|
-
:
|
|
28
|
+
:invalid_url
|
|
27
29
|
end
|
|
28
30
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class WordCountValidator < Validaty::Base
|
|
2
|
+
def validate_each(resource, attribute, value)
|
|
3
|
+
return if value.blank?
|
|
4
|
+
|
|
5
|
+
words = value.to_s.split.size
|
|
6
|
+
min = options[:min]
|
|
7
|
+
max = options[:max]
|
|
8
|
+
|
|
9
|
+
if min && words < min
|
|
10
|
+
resource.errors.add(attribute, :too_few_words, count: min)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
if max && words > max
|
|
14
|
+
resource.errors.add(attribute, :too_many_words, count: max)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def valid_value?(value)
|
|
21
|
+
value.to_s.split.count >= WORDS_QUANTITY
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/validaty/base.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "active_model"
|
|
2
|
+
|
|
1
3
|
module Validaty
|
|
2
4
|
class Base < ActiveModel::EachValidator
|
|
3
5
|
def validate_each(resource, attribute, value)
|
|
@@ -5,7 +7,7 @@ module Validaty
|
|
|
5
7
|
|
|
6
8
|
return if valid_value?(value)
|
|
7
9
|
|
|
8
|
-
resource.errors.add(attribute,
|
|
10
|
+
resource.errors.add(attribute, options[:message] || default_message_error)
|
|
9
11
|
end
|
|
10
12
|
|
|
11
13
|
private
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
en:
|
|
2
|
+
errors:
|
|
3
|
+
messages:
|
|
4
|
+
invalid_url: "is not a valid URL"
|
|
5
|
+
invalid_uuid: "is not a valid UUID"
|
|
6
|
+
invalid_phone: "is not a valid phone number"
|
|
7
|
+
invalid_email: "is not a valid email"
|
|
8
|
+
invalid_date: "is not a valid date"
|
|
9
|
+
invalid_cnpj: "is not a valid CNPJ"
|
|
10
|
+
invalid_cpf: "is not a valid CPF"
|
|
11
|
+
boolean: "must be true or false"
|
|
12
|
+
domain: "must be from the %{domain} domain"
|
|
13
|
+
start_with: "must start with '%{start}'"
|
|
14
|
+
too_few_words: "must have at least %{count} words"
|
|
15
|
+
too_many_words: "must have at most %{count} words"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
pt-BR:
|
|
2
|
+
errors:
|
|
3
|
+
messages:
|
|
4
|
+
invalid_url: "não é uma URL válida"
|
|
5
|
+
invalid_uuid: "não é um UUID válido"
|
|
6
|
+
invalid_phone: "não é um telefone válido"
|
|
7
|
+
invalid_email: "não é um email válido"
|
|
8
|
+
invalid_date: "não é uma data válida"
|
|
9
|
+
invalid_cnpj: "não é um CNPJ válido"
|
|
10
|
+
invalid_cpf: "não é um CPF válido"
|
|
11
|
+
boolean: "deve ser verdadeiro ou falso"
|
|
12
|
+
domain: "precisa ser do domínio %{domain}"
|
|
13
|
+
start_with: "precisa começar com '%{start}'"
|
|
14
|
+
too_few_words: "deve ter no mínimo %{count} palavras"
|
|
15
|
+
too_many_words: "deve ter no máximo %{count} palavras"
|
data/lib/validaty/version.rb
CHANGED
data/lib/validaty.rb
CHANGED
|
@@ -10,7 +10,14 @@ require_relative "validators/email_validator"
|
|
|
10
10
|
require_relative "validators/phone_validator"
|
|
11
11
|
require_relative "validators/url_validator"
|
|
12
12
|
require_relative "validators/uuid_validator"
|
|
13
|
+
require_relative "validators/word_count_validator"
|
|
13
14
|
|
|
14
15
|
module Validaty
|
|
16
|
+
I18n.load_path += Dir[File.join(__dir__, "validaty/locales/*.yml")]
|
|
17
|
+
|
|
15
18
|
class Error < StandardError; end
|
|
16
19
|
end
|
|
20
|
+
|
|
21
|
+
CpfValidator = CPFValidator unless defined?(CpfValidator)
|
|
22
|
+
CnpjValidator = CNPJValidator unless defined?(CnpjValidator)
|
|
23
|
+
UuidValidator = UUIDValidator unless defined?(UuidValidator)
|
data/validaty.gemspec
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: validaty
|
|
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
|
- Lavenda Software
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: cpf_cnpj
|
|
@@ -37,6 +37,20 @@ dependencies:
|
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: activemodel
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
40
54
|
email:
|
|
41
55
|
- lavenda@lavenda.com.br
|
|
42
56
|
executables: []
|
|
@@ -58,9 +72,12 @@ files:
|
|
|
58
72
|
- lib/validators/phone_validator.rb
|
|
59
73
|
- lib/validators/url_validator.rb
|
|
60
74
|
- lib/validators/uuid_validator.rb
|
|
75
|
+
- lib/validators/word_count_validator.rb
|
|
61
76
|
- lib/validaty.rb
|
|
62
77
|
- lib/validaty/allow_blank_base.rb
|
|
63
78
|
- lib/validaty/base.rb
|
|
79
|
+
- lib/validaty/locales/en.yml
|
|
80
|
+
- lib/validaty/locales/pt-BR.yml
|
|
64
81
|
- lib/validaty/version.rb
|
|
65
82
|
- sig/validaty/spec.rbs
|
|
66
83
|
- validaty.gemspec
|
|
@@ -86,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
86
103
|
- !ruby/object:Gem::Version
|
|
87
104
|
version: '0'
|
|
88
105
|
requirements: []
|
|
89
|
-
rubygems_version:
|
|
106
|
+
rubygems_version: 4.0.3
|
|
90
107
|
specification_version: 4
|
|
91
108
|
summary: Validaty has basic validations for Rails application
|
|
92
109
|
test_files: []
|