# A validation question (trying to make a censorship function)

**URL:** <https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582>\
**Category:** Rails\
**Created:** [December 30, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582 "2007-12-30T15:42:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 30, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/1 "2007-12-30T15:42:23Z")

</div>

I’m trying to make a rails forum application, and of course, I want to  
censor any bad words people may post in my text models. That said, I  
wondered if, in Rails 1.2.5, there were any validations method that  
could do this easily.

I’m thinking of using “validates\_format\_of :text, :with =\> #some  
regex, :message =\> ‘bad word’”, but I don’t remember if ruby has a  
negation regex (that is, when it finds a pattern defined in regexp, it  
returns false).

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 30, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/2 "2007-12-30T15:53:34Z")

</div>

Actually, in correspondence to this, is it possible to make a model’s  
module? Since several models are going to be sharing the same  
validation censor function, I wondered if I can make them all  
implement the same module or object (preferably module, since it’s  
only a function, and maybe a constant array).

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 30, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/3 "2007-12-30T18:59:29Z")

</div>

On Dec 30, 2007 3:42 PM, Taro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m thinking of using “validates\_format\_of :text, :with =\> #some  
> regex, :message =\> ‘bad word’”, but I don’t remember if ruby has a  
> negation regex (that is, when it finds a pattern defined in regexp, it  
> returns false).

Something like the following in your model:

protected  
def validate  
errors.add(:some\_field, “contains disallowed words”) if  
some\_field.match /bad word/  
end

Break out the condition above into a separate private method and  
iterate over a list of bad words or something…  
(I’m not sure how expensive the regex operation is, maybe it’s not a  
great idea to iterate over a long list. OTOH, maybe you don’t expect  
validations to run that often?)

- Martin

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 31, 2007, 1:18am UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/4 "2007-12-31T01:18:40Z")

</div>

Hey,  
there’s a _ton_ of different ways to implement this.

Here’s one idea that (imho) results in reasonably easy-to-read  
validation  
declarations, keeps the logic in one place, and doesn’t require you to  
delve  
into creating mixins or custom validation methods:

# lib/bad\_words.rb

class BadWords  
DISALLOWED = [  
/bunny/i,  
/carrot/i,  
/lettuce/i  
]

def self.include?(text)  
DISALLOWED.any? {|disallowed| disallowed.match(text)}  
end  
end

# app/models/post.rb

class Post \< ActiveRecord::Base  
validates\_exclusion\_of :message, :in =\> BadWords, :message =\>  
‘contains  
bad words’  
end

Most examples for validates\_exclusion\_of (and validates\_inclusion\_of)  
only  
show the usage where you supply an array for the :in parameter.

However, you can actually supply _any_ object for :in, as long as the  
object  
responds to include?() because that’s the method that  
validates\_exclusion\_of  
calls when performing the validation.

That’s what I’ve done in the above example. The BadWords class will  
respond  
to include?() by running through its array of disallowed regular  
expressions  
and return true if a match is found.

HTH,  
Trevor

On 12/30/07, Taro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > censor any bad words people may post in my text models. That said, I  
> > wondered if, in Rails 1.2.5, there were any validations method that  
> > could do this easily.
> > 
> > I’m thinking of using “validates\_format\_of :text, :with =\> #some  
> > regex, :message =\> ‘bad word’”, but I don’t remember if ruby has a  
> > negation regex (that is, when it finds a pattern defined in regexp, it  
> > returns false).

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 31, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/5 "2007-12-31T15:37:52Z")

</div>

Thanks a lot, Trevor and Martin. I like Trevor’s solution for its  
versatility, so I’ll use that.

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 31, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/6 "2007-12-31T16:08:56Z")

</div>

By the way, I noticed you can call objects defined from the lib  
directory. Does this apply to ALL ruby programs in rails, or only the  
models?

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [December 31, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/a-validation-question-trying-to-make-a-censorship-function/124582/7 "2007-12-31T19:55:24Z")

</div>

Hey,  
rails will autoload from the lib directory. So as long as you follow  
rails’  
file\_name.rb to FileName conventions then you can directly refer to lib  
classes/modules from anywhere in your rails app without first having to  
require the file.

Trevor

On 12/31/07, Taro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> By the way, I noticed you can call objects defined from the lib  
> directory. Does this apply to ALL ruby programs in rails, or only the  
> models?
> 
> >

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
