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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c9b904fc55cbad4d43f33eef17eaea5c5da5624
4
- data.tar.gz: 8ac267e89e79e4377d26059fea2560d1fd7ff3a9
3
+ metadata.gz: 4ae3da84725736fe09eb9151a3c819c19b32cd3c
4
+ data.tar.gz: 354508adbc4b167363194956aa50b4fc98ede9e9
5
5
  SHA512:
6
- metadata.gz: bc21794016ba13c880ad91c55957fa5e950a216d5503fb6300def08df9260435e87ce31c54e0ede1b4ffc187a4299867178234193c7b82106df6e321ca676a7f
7
- data.tar.gz: 83075d95872d904788dca6db2d0632e4e2a37f1aee6ecfed20ceb083d71dc69e3db21cedae9fffffae4c1ab911c8ef1a550b989ffffb6652d023f8045b3355ea
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 => /\A[^[:cntrl:]\\<>]*\z/, :message => "avoid non-printing characters and \\&gt;&lt;/ please."}, :length => {:maximum => 63}, :has_if? => true},
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
@@ -1,3 +1,3 @@
1
1
  module HasValidatedAttributes
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
data/spec/db/schema.rb CHANGED
@@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version => 0) do
3
3
  create_table "resources", :force => true do |t|
4
4
  t.string "username_attr"
5
5
  t.string "name_attr"
6
+ t.string "safe_text_attr"
6
7
  t.string "domain_attr"
7
8
  t.string "email_attr"
8
9
  t.string "phone_number_attr"
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
- it "should return error" do
55
- [">*", "< test"].each do |value|
54
+ [">*", "< test"].each do |value|
55
+ it "should return error setting name to \"#{ value }\"" do
56
56
  @resource.name_attr = value
57
- @resource.valid?.should be_falsey
58
- @resource.errors.full_messages.should == ["Name attr avoid non-printing characters and \\&gt;&lt;/ please."]
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?.should be_falsey
65
- @resource.errors.full_messages.should == ["Name attr is too long (maximum is 10 characters)"]
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
- it "should return ok" do
69
- ["k c", "- H-", " t", "& u", "21 ", "brok", nil].each do |value|
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?.should be_truthy
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_validated_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Ginavan