<commit_before># Validator

[![Build Status](https://secure.travis-ci.org/zombor/Validator.png)](http://travis-ci.org/zombor/Validator)

Validator is a simple ruby validation class. You don't use it directly inside your classes like just about every other ruby validation class out there. I chose to implement it in this way so I didn't automatically pollute the namespace of the objects I wanted to validate.

This also solves the problem of validating forms very nicely. Frequently you will have a form that represents many different data objects in your system, and you can pre-validate everything before doing any saving.

## Usage

Validator is useful for validating the state of any existing ruby object.

```ruby
  object = OpenStruct.new(:email => '<EMAIL>', :password => '<PASSWORD>')
  validator = Validation::Validator.new(object)
  validator.rule(:email, [:email, :not_empty]) # multiple rules in one line
  validator.rule(:password, :not_empty) # a single rule on a line
  validator.rule(:password, :length => {:minimum => 3}) # a rule that takes parameters

  if validator.valid?
    # save the data somewhere
  else
    @errors = validator.errors
  end
```

The first paramater can be any message that the object responds to.

### Writing your own rules

If you have a custom rule you need to write, just put it inside the `Validation::Rule` namespace:

```ruby
  module Validation
    module Rule
      class MyCustomRule
        def error_key
          :my_custom_rule
        end

        def valid_value?(value)
          # Logic for determining the validity of the value
        end

        def params
          {}
        end
      end
    end
  end
```

A rule class should have the following methods on it:

  - `error_key` a symbol to represent the error. This shows up in the errors hash
  - `valid_values?(value)` the beef of the rule. This is where you determine if the value is valid or not
  - `params` the params hash that was passed into the constructor

### Writing self-contained validators

You can also create self-contained validation classes if you don't like the dynamic creation approach:

```ruby
  require 'validation'
  require 'validation/rule/not_empty'<commit_msg>Fix typo in valid_value? method name<commit_after># Validator

[![Build Status](https://secure.travis-ci.org/zombor/Validator.png)](http://travis-ci.org/zombor/Validator)

Validator is a simple ruby validation class. You don't use it directly inside your classes like just about every other ruby validation class out there. I chose to implement it in this way so I didn't automatically pollute the namespace of the objects I wanted to validate.

This also solves the problem of validating forms very nicely. Frequently you will have a form that represents many different data objects in your system, and you can pre-validate everything before doing any saving.

## Usage

Validator is useful for validating the state of any existing ruby object.

```ruby
  object = OpenStruct.new(:email => '<EMAIL>', :password => '<PASSWORD>')
  validator = Validation::Validator.new(object)
  validator.rule(:email, [:email, :not_empty]) # multiple rules in one line
  validator.rule(:password, :not_empty) # a single rule on a line
  validator.rule(:password, :length => {:minimum => 3}) # a rule that takes parameters

  if validator.valid?
    # save the data somewhere
  else
    @errors = validator.errors
  end
```

The first paramater can be any message that the object responds to.

### Writing your own rules

If you have a custom rule you need to write, just put it inside the `Validation::Rule` namespace:

```ruby
  module Validation
    module Rule
      class MyCustomRule
        def error_key
          :my_custom_rule
        end

        def valid_value?(value)
          # Logic for determining the validity of the value
        end

        def params
          {}
        end
      end
    end
  end
```

A rule class should have the following methods on it:

  - `error_key` a symbol to represent the error. This shows up in the errors hash
  - `valid_value?(value)` the beef of the rule. This is where you determine if the value is valid or not
  - `params` the params hash that was passed into the constructor

### Writing self-contained validators

You can also create self-contained validation classes if you don't like the dynamic creation approach:

```ruby
  require 'validation'
  require 'validation/rule/not_empty'
{'fraction_non_alphanumeric': 0.07406573615488518, 'fraction_numerical': 0.00045024763619990995, 'mean_word_length': 3.5152439024390243, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5460546', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1155, 'n_words': 558}