simple-validations-rails 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
+ SHA256:
3
+ metadata.gz: 7fe0d2424b33137751db9f16d5cc995984829081e0ab4b44139761ba21726498
4
+ data.tar.gz: 20b9237bfa143cff85c13b420799fd725f48613107437ae930f7630fcc2a11c8
5
+ SHA512:
6
+ metadata.gz: 1e874285fa557eb3d8166e5a8e6434515bf17708268c3b40afe60a4319f01b6da27c6a0c2a340b6cc05ffc6aa36a0873783abd83d8e0611a9ea916b75141bbbb
7
+ data.tar.gz: 1c13c8623e24c6ddb7e966c76dba7300c73cbe67a4a360b84ff5225c50bb9766eed6d5f231aacdbdf10767970a96841581778b29579da593f6ef4328087738bd
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # simple-validations-rails
2
+ A simple set of validations I got tired of writing over and over and over again. Includes boolean, locale, and email validators.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'simple-validations-rails'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install simple-validations-rails
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ It's a set of validators...and is therefore used like other [validators](https://guides.rubyonrails.org/active_record_validations.html).
24
+
25
+ ### Boolean Validator
26
+
27
+ Validates that the value is boolean in nature.
28
+ ```ruby
29
+ # Allows only non-null booleanish values (most values that aren't nil are true - but in this case, nil is NOT false, and thus fails).
30
+ validates :allow_something, boolean: true
31
+
32
+ # Allows basically anything. Probably this is useless...
33
+ validates :preference_that_can_be_null, boolean: {allow_nil: true}
34
+
35
+ # Allows ONLY true or false.
36
+ validates :strict_preference, boolean: {strict: true}
37
+
38
+ # Allows ONLY true, false, or nil.
39
+ validates :strict_preference_that_can_be_null, boolean: {strict: true, allow_nil: true}
40
+ ```
41
+
42
+ ### Locale Validator
43
+
44
+ Validates that the value is within the allowed locales for the application.
45
+ ```ruby
46
+ # Allows strings/symbols, checks against available locales.
47
+ validates locale, locale: true
48
+
49
+ # Same as above, but nil is valid.
50
+ validates locale, locale: {allow_nil: true}
51
+ ```
52
+
53
+ ### Email Validator
54
+
55
+ TODO: Documentation
56
+
57
+ ## License
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ module SimpleValidationsRails
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleValidationsRails
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'simple_validations_rails/railtie'
2
+ require 'validators/boolean_validator'
3
+ require 'validators/email_validator'
4
+ require 'validators/locale_validator'
@@ -0,0 +1,5 @@
1
+ class BooleanValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ record.errors.add attribute, 'is not a valid boolean' unless (!options[:strict] && value.present?) || [true, false].include?(value) || (options[:allow_nil] && value.nil?)
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ class EmailValidator < ActiveModel::EachValidator
2
+ MAX_EMAIL_LENGTH = 254
3
+
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add attribute, 'is not a valid locale' unless (options[:allow_nil] && value.nil?) || ( asdfasdfasdfasdfadfa && value.legnth <= MAX_EMAIL_LENGTH || options[:skip_length])
6
+ end
7
+
8
+ protected
9
+
10
+ def self.regex(options = {})
11
+ options = {mode: :normal}.merge options
12
+
13
+ case options[:mode]
14
+ when :loose
15
+ /\A[^\s]@[^\s]\z/
16
+ when :strict
17
+ # TODO: make a better validator here.
18
+ raise ArgumentError.new("Strict mode isn't yet implimented.")
19
+ when :rfc
20
+ # TODO: Add a pure RFC5322 regex...just in case someone actually wants to use something that terrible...
21
+ raise ArgumentError.new("RFC mode isn't yet implimented.")
22
+ else
23
+ /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ class LocaleValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ record.errors.add attribute, 'is not a valid locale' unless I18n.locale_available?(value) || (options[:allow_nil] && value.nil?)
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-validations-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Schultz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A simple set of validations I got tired of writing over and over and
42
+ over again.
43
+ email:
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/simple_validations_rails.rb
50
+ - lib/simple_validations_rails/railtie.rb
51
+ - lib/simple_validations_rails/version.rb
52
+ - lib/validators/boolean_validator.rb
53
+ - lib/validators/email_validator.rb
54
+ - lib/validators/locale_validator.rb
55
+ homepage: https://github.com/MatthewSchultz/simple-validations-rails
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ source_code_uri: https://github.com/MatthewSchultz/simple-validations-rails
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.0.4
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: A simple set of validations I got tired of writing over and over and over
79
+ again.
80
+ test_files: []