has_validated_attributes 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/has_validated_attributes.rb +10 -1
- data/lib/version.rb +1 -1
- data/spec/db/schema.rb +1 -0
- data/spec/db/test.sqlite3 +0 -0
- data/spec/has_validate_fields_spec.rb +34 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ae3da84725736fe09eb9151a3c819c19b32cd3c
|
4
|
+
data.tar.gz: 354508adbc4b167363194956aa50b4fc98ede9e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c56000936872f767087128b3f9d348582a4ead07480da32fdcf5dbab3577055ef74b333366e427ace9c0b9ef83137755dab008b5d62191edf8851311d8378b39
|
7
|
+
data.tar.gz: 1f81240a106e9b231aa3b74f6839c46febe2a86a9b8cfc42fc67f2f7fbe41721a9a5e6fa049565023b37dbe933b815ecf7730eb66e6a143cf0cea0f1547b9414
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module HasValidatedAttributes
|
4
4
|
extend ActiveSupport::Concern
|
5
|
+
NO_CONTROL_CHARS_REGEX = /\A[^[:cntrl:]]*\Z/
|
6
|
+
NO_CONTROL_CHARS_ERROR_MSG = "avoid non-printing characters"
|
5
7
|
|
6
8
|
#instance methods
|
7
9
|
def self.validations(*args)
|
@@ -20,8 +22,15 @@ module HasValidatedAttributes
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
25
|
+
class SafeTextValidator < ::ActiveModel::EachValidator
|
26
|
+
def validate_each(record, attribute, value)
|
27
|
+
record.errors[attribute] << NO_CONTROL_CHARS_ERROR_MSG unless NO_CONTROL_CHARS_REGEX =~ value.to_s.gsub(/[\n\r\t]/, '')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
23
31
|
#loading all methods dynamically
|
24
|
-
validations :name => {:format => {:with =>
|
32
|
+
validations :name => { :format => { :with => NO_CONTROL_CHARS_REGEX, :message => NO_CONTROL_CHARS_ERROR_MSG }, :length => {:maximum => 63}, :has_if? => true},
|
33
|
+
:safe_text => { :safe_text => true, :has_if? => true},
|
25
34
|
:username => {:length => {:within => 5..127}, :format => {:with => /\A\w[\w\.\-_@]+\z/, :message => "use only letters, numbers, and .-_@ please."}, :uniqueness => true},
|
26
35
|
:rails_name => {:format => {:with => /\A[a-zA-Z\_]*?\z/u, :message => "should only include underscores and letters."}},
|
27
36
|
:email => {:length => {:maximum => 63}, :format => {:with => /\A[\w\.%\+\-’']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|pro|mobi|name|aero|jobs|museum)\z/i, :message => "should look like an email address."}},
|
data/lib/version.rb
CHANGED
data/spec/db/schema.rb
CHANGED
data/spec/db/test.sqlite3
CHANGED
Binary file
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
class Resource < ActiveRecord::Base
|
6
|
-
has_validated_attributes :name_attr => {:format => :name, :maximum_length => 10}, :username_attr => {:format => :username}, :email_attr => {:format => :email},
|
6
|
+
has_validated_attributes :name_attr => {:format => :name, :maximum_length => 10}, :safe_text_attr => { :format => :safe_text }, :username_attr => {:format => :username}, :email_attr => {:format => :email},
|
7
7
|
:phone_number_attr => {:format => :phone_number}, :phone_extension_attr => {:format => :phone_extension},
|
8
8
|
:domain_attr => {:format => :domain}, :zipcode_attr => {:format => :zipcode},
|
9
9
|
:middle_initial_attr => {:format => :middle_initial}, :dollar_attr => {:format => :dollar},
|
@@ -51,24 +51,48 @@ describe "HasValidatedAttributes" do
|
|
51
51
|
end
|
52
52
|
|
53
53
|
describe "#name" do
|
54
|
-
|
55
|
-
|
54
|
+
[">*", "< test"].each do |value|
|
55
|
+
it "should return error setting name to \"#{ value }\"" do
|
56
56
|
@resource.name_attr = value
|
57
|
-
@resource.valid
|
58
|
-
|
57
|
+
expect(@resource.valid?).to be_truthy
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
["Hello\nWorld", "\eHey", "Oh\cFNo, it's a control char!"].each do |value|
|
62
|
+
it "should return error setting name to \"#{ value }\"" do
|
63
|
+
@resource.name_attr = value
|
64
|
+
expect(@resource.valid?).to be_falsey
|
65
|
+
expect(@resource.errors.full_messages).to include("Name attr avoid non-printing characters")
|
59
66
|
end
|
60
67
|
end
|
61
68
|
|
62
69
|
it "should return error with more than 10 chars" do
|
63
70
|
@resource.name_attr = "test" * 6
|
64
|
-
@resource.valid
|
65
|
-
@resource.errors.full_messages.
|
71
|
+
expect(@resource.valid?).to be_falsey
|
72
|
+
expect(@resource.errors.full_messages).to include("Name attr is too long (maximum is 10 characters)")
|
66
73
|
end
|
67
74
|
|
68
|
-
|
69
|
-
|
75
|
+
["k c", "- H-", " t", "& u", "21 ", "brok", nil].each do |value|
|
76
|
+
it "should return ok setting name to \"#{ value }\"" do
|
70
77
|
@resource.name_attr = value
|
71
|
-
@resource.valid
|
78
|
+
expect(@resource.valid?).to be_truthy
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#safe_text" do
|
84
|
+
[">*", "< test", "Hey\tWorld", "new\nline", "new\r\nline"].each do |value|
|
85
|
+
it "should allow value to be set to \"#{ value.gsub("\r", "\\r").gsub("\n", "\\n") }\"" do
|
86
|
+
@resource.safe_text_attr = value
|
87
|
+
expect(@resource.valid?).to be_truthy
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
["\eHey", "Oh\cFNo, it's a control char!"].each do |value|
|
92
|
+
it "should return error setting value to \"#{ value }\"" do
|
93
|
+
@resource.safe_text_attr = value
|
94
|
+
expect(@resource.valid?).to be_falsey
|
95
|
+
expect(@resource.errors.full_messages).to include("Safe text attr avoid non-printing characters")
|
72
96
|
end
|
73
97
|
end
|
74
98
|
end
|