Coder Social home page Coder Social logo

validator's Introduction

Validator

Build Status

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.

  object = OpenStruct.new(:email => '[email protected]', :password => 'foobar')
  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, you can create a custom rule class for it:

  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

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. Must be an underscored_version of the class name
  • 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

If you add your custom rule class to the Validation::Rule namespace, you can reference it using a symbol:

  validator.rule(:field, :my_custom_rule)  # resolves to Validation::Rule::MyCustomRule
  validator.rule(:field, :my_custom_rule => { :param => :value })

Otherwise, just pass in the rule class itself:

  validator.rule(:field, MyProject::CustomRule)
  validator.rule(:field, MyProject::CustomRule => { :param => :value })

Writing self-contained validators

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

  require 'validation'
  require 'validation/rule/not_empty'

  class MyFormValidator < Validation::Validator
    include Validation

    rule :email, :not_empty
  end

Now you can use this anywhere in your code:

  form_validator = MyFormValidator.new(OpenStruct.new(params))
  form_validator.valid?

Semantic Versioning

This project conforms to semver.

Contributing

Have an improvement? Have an awesome rule you want included? Simple!

  1. Fork the repository
  2. Create a branch off of the master branch
  3. Write specs for the change
  4. Add your change
  5. Submit a pull request to merge against the master branch

Don't change any version files or gemspec files in your change.

validator's People

Contributors

amcoder avatar cbandy avatar mushishi78 avatar olivierdagenais avatar raven24 avatar solymosi avatar supertux88 avatar zombor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.