trek / strongest_params

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stronger Parameters

Stronger Parameters is a library intended to repalce Rails strong_paramters. strong_paramters has a nice chaninable API for validating input in code at the place the code lives:

params.require(:person).permit(:name, :age)

but it isn't terribly composable if you want to store validation rules elsewhere.

Stronger Parameters uses ActiveModel validations internally rather than inventing a new API for data validations. Some possible uses:

Data shape validations for functions, procs, commands, etc (a la Schema for Clojure):

class Rule
  class Schema < StrongerParameters
    validates :name, :age, :color, presence: true
    validates :coolness, allowed: true
  end

  def call(parameters)
    raise ArgumentError unless Schema.new(parameters).valid?
    # continue exection
  end
end

Rails parameters validations at the controller layer:

class FoosController < ApplicationController
  class Validations < StrongerParameters
    validates :name, :age, :color, presence: true, on: [:create, :update]
    validates_nested :author do |a|
      a.validates :name, length: {minimum: 12}, on: [:create]
    end
  end
end

Available validations

Allowed

Specifies that a parameter is allowed. By defalt, paramters must be whitelisted. Passing unknown paramters will fail.

validates :name, allowed: true

A parameter can be allowed but not required:

validates :name, allowed: true, presence: false

Presence

Specifies that a parameter's presence is required. Parameters will be considred invalid if a required key is missing:

validates :name, presence: true

Inclusion

Specifies that a parameter's value must be included in a supplied array. Values other than those supplied in the array will not valid.

validates :name, inclusion: ['a', 'b']

Exclusion

Specifies that a parameter's value cannot be included in a supplied array. All values are considered valid except those in the array.

validates :name, exclusion: ['a', 'b']

Length

Specifies that a parameter's value must be a certain length. See ActiveModel length validation for all the options.

validates :name, length: {minimum: 12}

Nested

Valiadtions rules for a nested key. These rules will only apply if the key is present.

validates_nested :page, presence: false do |page|
  page.validates :name, presence: true
end

Adding validations

see http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations

Available options for validations

see http://guides.rubyonrails.org/active_record_validations.html#conditional-validation and http://guides.rubyonrails.org/active_record_validations.html#common-validation-options

About

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Ruby 100.0%