Socket
Book a DemoInstallSign in
Socket

weak_parameters

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weak_parameters

0.5.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

WeakParameters Build Status

Validates params in your controller.

Installation

gem "weak_parameters"

Usage

class ApplicationController < ActionController::Base
  protect_from_forgery

  respond_to :json

  rescue_from WeakParameters::ValidationError do
    head 400
  end
end

# WeakParameters provides `validates` class method to define validations.
class RecipesController < ApplicationController
  validates :create do
    string :name, required: true, except: ["charlie", "dave"], strong: true
    integer :type, only: 1..3, strong: true
    string :quantity do |value|
      value =~ /\A\d+(?:\.\d+)g\z/
    end
    date :date, format: ["%Y/%m/%d", "%Y.%m.%d"]
    time :time, format: "%Y/%m/%d %H:%M:%S"
  end

  def create
    # pass the only parameters with strong option. like strong parameters.
    respond_with Recipe.create(permitted_params)
  end
end
irb(main):001:0> app.accept = "application/json"
=> "application/json"
irb(main):002:0> app.post "/recipes", name: "alice", type: 1
=> 201
irb(main):003:0> app.post "/recipes", name: "alice"
=> 201
irb(main):004:0> app.post "/recipes", type: 1
=> 400
irb(main):005:0> app.post "/recipes", name: "alice", type: "bob"
=> 400

Available validators

  • any
  • array
  • float
  • hash
  • integer
  • string
  • boolean (= 0, 1, false, true)
  • file
  • object
  • list
  • date (= default %Y-%m-%d)
  • time (= default %Y-%m-%d %H:%M:%S)

Available options

  • required
  • only
  • except
  • handler
  • strong
  • format (only for date and time)

Tips

WeakParameters.stats returns its validation metadata, and this is useful for auto-generating API documents. With autodoc, you can auto-generate API documents with params information.

https://github.com/r7kamura/autodoc

FAQs

Package last updated on 01 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.