Giter Site home page Giter Site logo

request_validator's Introduction

N|Solid

Request Validator

Request validator is a gem thats allows you do run validation in your controller rather than your models. Why do you need validates :question, presence: true in model when v.check('param').notEmpty() solves it

Installation

No dependency!

$ gem install request_validator

Configuration

Validator.configure do |config|
	config.skip_main_object_empty = true
	config.error_mode = 'surpressed'
end

Config

Dillinger is currently extended with the following plugins. Instructions on how to use them in your own application are linked below.

Option Description
skip_main_object_empty Allows your to skip error when the main object is empty. Default is false
error_mode silences errors and move on to next step

Usage

This gem can be used in two ways and both returns same response in same format.

We will be using this params throughout

{
	"name": "User 1",
	"phone": "789",
	"role" : "admin",
	"email": "[email protected]",
	"website": "https://google.com",
	"actions": ["jsjjd","skskjd"],
}

The first method allows you to match params to methods without explicitly specifying the methods.

Validator.new.validate(params, {
	'name' => 'string',
	'phone' => 'number',
	'email' => 'email',
})

If thers are any errors, response object containing property error as an array containing the error messages is returned

It skips other params if they are not specified...

Method Chaining

Example

v = Validator.new(params)
v.check('email').notEmpty().isEmail()
v.check('name').isString().notEmpty()
v.check('role').isIn(['admin', 'user']).notEmpty()
v.check('actions').isArray()
v.check('website').validUrl()
v.result()

Using this method, it allows changing of methods together. The method check must be specified a parameter is passed into it which is the property to check for.

The params body must me passed into the new method of the validator.

Example response

{
    "error": [
        "phone is not a valid number"
    ]
}

Todos

  • Write tests (but its working right now)
  • Include more methods
  • More documentation

License

MIT

Free Software, Hell Yeah!

request_validator's People

Contributors

wilforlan avatar

Stargazers

 avatar

Watchers

James Cloos 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.