Code Monkey home page Code Monkey logo

validate_params's Introduction

ValidateParams

Params validations for Ruby on Rails.

Build Status

Installation

Add this line to your application's Gemfile:

gem 'validate_params'

And then execute:

$ bundle

Usage

In your controller:

class ExamplesController < ApplicationController
  include ValidateParams

  def create
    validate_params! :example
    # [...]
  end
end

You can rescue from exception like this:

class ApplicationController < ActionController::Base
  rescue_from InvalidParamsException do |exception|
    respond_to do |format|
      format.json { render json: { error: exception.to_s }, status: 400 }
      format.html { raise ActionController::RoutingError.new('Not found') }
    end

  end
end

Validate presence

validate_params! :user
validate_params! :user, :token

Validate type and size

validate_params! user: Array
validate_params! user: { type: Array }
validate_params! user: Array, token: String
validate_params! example: { type: Array, size: 2 }

Validate that value is included in a range

validate_params! number: { range: 0..10 }

Validate inclusion

validate_params! example: { in: %w(value1 value2) }

Validate regex

validate_params! example: { regex: /myregex/ }

Validation from custom Proc

validate_params! example: { custom: Proc.new { |value| value == 'a' } }

Set default value

validate_params! example: { default: 'abc' }

Cast

# :Integer :Float or :Array
validate_params! example: { cast: :Integer }

Todo

  • Add a validate_params method (without !) that returns false
  • Config custom exception class

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rdelandesen/validate_params.

License

The gem is available as open source under the terms of the MIT License.

validate_params's People

Contributors

rdelandesen 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.