string_length_conformable 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9d964ace0305d966851b56fbe9cf38d88fb370f4b35b452705cf6609ad9dd37
4
- data.tar.gz: 9017049d48b51d9db9ef431fc387caf21ad1a2f89dbe1b52daac4fd29d3f412c
3
+ metadata.gz: 43e42b3c80a5a1b3bcbc2e9aa67516f657eee4d5eb0bd1a5e429b78ef3604d63
4
+ data.tar.gz: 182aaa4a7586cb855e6ae431061827f2a77c52ddfbfa5b2e65ef86213851e094
5
5
  SHA512:
6
- metadata.gz: a1871ac7599f6dc6da4b83f4e945a9af523f8a9fa44b6305ebe83e05e389f428b578d8eb52c6095cc6eee26574704e3acc4e19f9c17e5ccb037e7d3d38d1f445
7
- data.tar.gz: cbf5e0eb93cb1ff22ea511425a71b6c3cff62cc2f2ced99a97e52074c6d499bb5fad3284ee039bfab92869960d5ffb29a5f740632f315d8cf0fbf6ebb16adf18
6
+ metadata.gz: 7d0e73b0541248586789d1c6a54f1c4478375348e25e85e3bcd7f7365a417671dae0575823551eddd300e6699bdccca5ff5c34dd9d39112c87fc65a1034fe715
7
+ data.tar.gz: 28a0a824dfae6b01cf3eb413407a6292c0bd5e5aa7c8974e03afe02590e1d5988e473aecb5c580df6ab1a479dd4c6f8ff4c441b51b02f1378bd20e97c52fc22c
data/README.md CHANGED
@@ -1,13 +1,21 @@
1
1
  # StringLengthConformable
2
2
 
3
- # USE WITH MySQL
3
+ [![Gem Version](https://badge.fury.io/rb/string_length_conformable.svg)](https://badge.fury.io/rb/string_length_conformable)
4
4
 
5
- If you have some attributes in your models with type string (varchar(255) by default).
6
- And you haven't validate length of it, you can face once with a problem when user will
7
- pass in form field more characters. Then, your database(MySQL), most probably will throw an error 500.
8
- To avoid such kind a problems I've createt this gem.
5
+ # Description
9
6
 
10
- This gem will throw a human readable exception when there are not validations applied for string, and user tries to pass longer string, then is specified in DB.
7
+ ### This gem resolves basically two problems.
8
+ ---
9
+ 1. MySQL for strings(VARCHAR(255)) by default has limit 255 characters. And when developer left this attribute without any length validation, then it's possible to face with situation when user unintentionally or intentionally will pass in text field more characters. So, then, probably you will get 500...
10
+ ---
11
+ 2. PostgreSQL. The maximum number of characters for variable unlimited length types (text, varchar) is undefined. There is a limit of size in bytes for all string types: In any case, the longest possible character string that can be stored is about 1 GB.
12
+ And when developer left this attribute without any length validation, then it's possible to face with situation when user unintentionally or intentionally will try to full up your database with lots of GB of 'important' info.
13
+ ---
14
+
15
+ Both of this cases, I guess, are not very pleasant.
16
+
17
+ This gem adds default length validation for all string attributes.
18
+ Except those which are already vlidated in standart rails way.
11
19
 
12
20
  ## Installation
13
21
 
@@ -46,6 +54,39 @@ end
46
54
 
47
55
  ```
48
56
 
57
+ ## Example
58
+
59
+ ```ruby
60
+ #schema.rb
61
+ create_table "users", force: :cascade do |t|
62
+ t.string "name"
63
+ t.string "description"
64
+ t.datetime "created_at", null: false
65
+ t.datetime "updated_at", null: false
66
+ end
67
+ ```
68
+
69
+ ```ruby
70
+ # user.rb
71
+ class User < ApplicationRecord
72
+ acts_as_string_length_conformable
73
+ validates :description, length: { maximum: 800 }
74
+ end
75
+ ```
76
+ `example for MySQL(VARCHAR(255))`
77
+ ```ruby
78
+ john = User.new
79
+ john.name = 'n'*500 # exception "is too long, 255 characters is the maximum allowed"
80
+ john.description = 'd'*799 # ok
81
+ ```
82
+ `example for PostgreSQL(1gb)`
83
+ ```ruby
84
+ john = User.new
85
+ john.name = 'n'*100.000.000.000 # exception "is too long, 1000 characters is the maximum allowed"
86
+ john.description = 'd'*799 #
87
+ ```
88
+ ___
89
+
49
90
  ## License
50
91
 
51
92
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,35 +1,24 @@
1
- require "string_length_conformable/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'string_length_conformable/version'
4
+ require 'string_length_conformable/mysql_length_of_string_validator'
5
+ require 'string_length_conformable/postgresql_length_of_string_validator'
2
6
 
3
7
  ActiveSupport.on_load(:active_record) do
4
8
  class ActiveRecord::Base
5
- class NotMysqlDb < StandardError; end
9
+ class NotImplementedForDb < StandardError; end
6
10
  def self.acts_as_string_length_conformable
7
-
8
- unless ActiveRecord::Base.connection.adapter_name == 'MySQL'
9
- raise NotMysqlDb, 'Please use this gem with mysql. But better use PostgreSQL and be happy!'
10
- end
11
-
12
- validates_with LengthOfStringValidator
13
- end
14
- class LengthOfStringValidator < ActiveModel::Validator
15
- def validate(record)
16
- record.attribute_names.each do |attr_name|
17
-
18
- next if record.instance_eval(attr_name).nil?
19
-
20
- next unless record.column_for_attribute(attr_name).type == :string
21
-
22
- symbol_attr = attr_name.parameterize.underscore.to_sym
23
-
24
- next if record.class.validators_on(symbol_attr).select { |v| v.class == ActiveRecord::Validations::LengthValidator}.any?
25
-
26
- permited_length = record.class.columns_hash[attr_name].limit
27
- new_length = record.instance_eval(attr_name).length
28
-
29
- if new_length > permited_length.to_i
30
- record.errors.add attr_name, "too long, #{permited_length.to_i} characters is the maximum allowed"
31
- end
32
- end
11
+ if ActiveRecord::Base.connection.adapter_name == 'MySQL'
12
+ validates_with MysqlLengthOfStringValidator
13
+ elsif ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
14
+ validates_with PostgresqlLengthOfStringValidator
15
+ else
16
+ text = <<-TEXT
17
+ Unfortunately #{ActiveRecord::Base.connection.adapter_name} is not supported yet,
18
+ please create an issue https://github.com/Yaponcik/string_length_conformable/issues,
19
+ or pull request!
20
+ TEXT
21
+ railse NotImplementedForDb, text
33
22
  end
34
23
  end
35
24
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logging
4
+ def log
5
+ text = <<-TEXT
6
+ \n
7
+ #{'*' * 100}
8
+ String is validated by gem string_length_conformable.
9
+ for more info visit https://github.com/Yaponcik/string_length_conformable
10
+ #{'*' * 100}
11
+ TEXT
12
+ Logger.new(STDOUT).info text
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logging'
4
+
5
+ class MysqlLengthOfStringValidator < ActiveModel::Validator
6
+ include Logging
7
+
8
+ def validate(record)
9
+ record.attribute_names.each do |attr_name|
10
+ next if record.instance_eval(attr_name).nil?
11
+
12
+ next unless record.column_for_attribute(attr_name).type == :string
13
+
14
+ symbol_attr = attr_name.parameterize.underscore.to_sym
15
+
16
+ next if record.class.validators_on(symbol_attr).select { |v| v.is_a? ActiveRecord::Validations::LengthValidator }.any?
17
+
18
+ permited_length = record.class.columns_hash[attr_name].limit
19
+ new_length = record.instance_eval(attr_name).length
20
+
21
+ if new_length > permited_length.to_i
22
+ record.errors.add attr_name, "is too long, #{permited_length.to_i} characters is the maximum allowed"
23
+ log
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logging'
4
+
5
+ class PostgresqlLengthOfStringValidator < ActiveModel::Validator
6
+ include Logging
7
+ DEFAULT_STRING_LENGTH = 1000
8
+
9
+ def validate(record)
10
+ record.attribute_names.each do |attr_name|
11
+ next if record.instance_eval(attr_name).nil?
12
+
13
+ next unless record.column_for_attribute(attr_name).type == :string
14
+
15
+ symbol_attr = attr_name.parameterize.underscore.to_sym
16
+
17
+ next if record.class.validators_on(symbol_attr).select { |v| v.is_a? ActiveRecord::Validations::LengthValidator }.any?
18
+
19
+ permited_length = record.class.columns_hash[attr_name].limit || DEFAULT_STRING_LENGTH
20
+ new_length = record.instance_eval(attr_name).length
21
+
22
+ if new_length > permited_length.to_i
23
+ record.errors.add attr_name, "is too long, #{permited_length.to_i} characters is the maximum allowed"
24
+ log
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module StringLengthConformable
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.1'.freeze
3
3
  end
@@ -1,35 +1,43 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "string_length_conformable/version"
3
+ require 'string_length_conformable/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "string_length_conformable"
6
+ spec.name = 'string_length_conformable'
8
7
  spec.version = StringLengthConformable::VERSION
9
- spec.authors = ["Vladimir"]
10
- spec.email = ["raileanv@gmail.com"]
8
+ spec.authors = ['Vladimir']
9
+ spec.email = ['raileanv@gmail.com']
11
10
 
12
- spec.summary = %q{Gem for validate string length}
11
+ spec.summary = 'Gem for validate string length'
13
12
  spec.description = <<-TEXT
14
- If you have some attributes in your models with type string (varchar(255) by default).
15
- And you haven't validate length of it, you can face once with a problem when user will
16
- pass in form field more characters. Then, your database, most probably will throw an error 500.
17
- To avoid such kind a problems I've createt this gem.
13
+ This gem resolves basically two problems.
14
+ ---
15
+ 1. MySQL for strings(VARCHAR(255)) by default has limit 255 characters. And when developer left this attribute without any length validation, then it's possible to face with situation when user unintentionally or intentionally will pass in text field more characters. So, then, probably you will get 500...
16
+ ---
17
+ 2. PostgreSQL. The maximum number of characters for variable unlimited length types (text, varchar) is undefined. There is a limit of size in bytes for all string types: In any case, the longest possible character string that can be stored is about 1 GB.
18
+ And when developer left this attribute without any length validation, then it's possible to face with situation when user unintentionally or intentionally will try to full up your database with lots of GB of 'important' info.
19
+ ---
20
+
21
+ Both of this cases, I guess, are not very pleasant.
22
+
23
+ This gem adds default length validation for all string attributes.
24
+ Except those which are already vlidated in standart rails way.
25
+
18
26
  TEXT
19
-
20
- spec.homepage = "https://github.com/Yaponcik/string_length_conformable"
21
- spec.license = "MIT"
27
+
28
+ spec.homepage = 'https://github.com/Yaponcik/string_length_conformable'
29
+ spec.license = 'MIT'
22
30
 
23
31
  # Specify which files should be added to the gem when it is released.
24
32
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
34
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
35
  end
28
- spec.bindir = "exe"
36
+ spec.bindir = 'exe'
29
37
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
38
+ spec.require_paths = ['lib']
31
39
 
32
- spec.add_development_dependency "bundler", "~> 1.16"
33
- spec.add_development_dependency "rake", "~> 10.0"
34
- spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency 'bundler', '~> 1.16'
41
+ spec.add_development_dependency 'rake', '~> 10.0'
42
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_length_conformable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-19 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,11 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: |2
56
- If you have some attributes in your models with type string (varchar(255) by default).
57
- And you haven't validate length of it, you can face once with a problem when user will
58
- pass in form field more characters. Then, your database, most probably will throw an error 500.
59
- To avoid such kind a problems I've createt this gem.
55
+ description: |2+
56
+ This gem resolves basically two problems.
57
+ ---
58
+ 1. MySQL for strings(VARCHAR(255)) by default has limit 255 characters. And when developer left this attribute without any length validation, then it's possible to face with situation when user unintentionally or intentionally will pass in text field more characters. So, then, probably you will get 500...
59
+ ---
60
+ 2. PostgreSQL. The maximum number of characters for variable unlimited length types (text, varchar) is undefined. There is a limit of size in bytes for all string types: In any case, the longest possible character string that can be stored is about 1 GB.
61
+ And when developer left this attribute without any length validation, then it's possible to face with situation when user unintentionally or intentionally will try to full up your database with lots of GB of 'important' info.
62
+ ---
63
+
64
+ Both of this cases, I guess, are not very pleasant.
65
+
66
+ This gem adds default length validation for all string attributes.
67
+ Except those which are already vlidated in standart rails way.
68
+
60
69
  email:
61
70
  - raileanv@gmail.com
62
71
  executables: []
@@ -71,6 +80,9 @@ files:
71
80
  - bin/console
72
81
  - bin/setup
73
82
  - lib/string_length_conformable.rb
83
+ - lib/string_length_conformable/logging.rb
84
+ - lib/string_length_conformable/mysql_length_of_string_validator.rb
85
+ - lib/string_length_conformable/postgresql_length_of_string_validator.rb
74
86
  - lib/string_length_conformable/version.rb
75
87
  - string_length_conformable.gemspec
76
88
  homepage: https://github.com/Yaponcik/string_length_conformable