Module: WeakParameters
- Defined in:
- lib/weak_parameters.rb,
lib/weak_parameters/version.rb,
lib/weak_parameters/validator.rb,
lib/weak_parameters/controller.rb,
lib/weak_parameters/base_validator.rb,
lib/weak_parameters/hash_validator.rb,
lib/weak_parameters/array_validator.rb,
lib/weak_parameters/string_validator.rb,
lib/weak_parameters/validation_error.rb,
lib/weak_parameters/boolean_validator.rb,
lib/weak_parameters/integer_validator.rb
Overview
Provides ‘validates` DSL to controllers to validate params.
Examples
# POST /recipes { name: "alice" } #=> 201
# POST /recipes { name: "alice", type: 1 } #=> 201
# POST /recipes { name: "alice", type: "bob" } #=> 400
# POST /recipes { type: 1 } #=> 400
class RecipesController < ApplicationController
rescue_from WeakParameters::ValidationError do |exception|
render text: exception., status: 400
end
validates :create do
string :name, required: true
integer :type
end
def create
respond_with Recipe.create(params.slice(:name, :type))
end
end
# WeakParameters.stats has its definitions.
WeakParameters::stats[:recipes][:create].validators[0].key #=> :name
WeakParameters::stats[:recipes][:create].validators[0].required? #=> true
WeakParameters::stats[:recipes][:create].validators[1].key #=> :type
WeakParameters::stats[:recipes][:create].validators[1].required? #=> false
Defined Under Namespace
Modules: Controller Classes: ArrayValidator, BaseValidator, BooleanValidator, HashValidator, IntegerValidator, StringValidator, ValidationError, Validator
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
Class Method Details
.stats ⇒ Object
45 46 47 48 49 |
# File 'lib/weak_parameters.rb', line 45 def self.stats @stats ||= ActiveSupport::HashWithIndifferentAccess.new do |hash, key| hash[key] = ActiveSupport::HashWithIndifferentAccess.new end end |