class Amber::Validators::Params

Defined in:

amber/validators/params.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(raw_params : Amber::Router::Params) #

[View source]

Instance Method Detail

def add_rule(rule : BaseRule) #

Validates each field with a given set of predicates returns true if the field is valid otherwise returns false

required(:email) { |p| p.email? & p.size.between? 1..10 }

[View source]
def errors : Array(Amber::Validators::Error) #

[View source]
def params : Hash(String, String?) #

[View source]
def raw_params : Amber::Router::Params #

[View source]
def rules : Array(Amber::Validators::BaseRule) #

[View source]
def to_h : Hash(String, String?) #

[View source]
def to_unsafe_h #

[View source]
def valid? #

Returns True or false whether the validation passed

unless params.valid?
  response.puts {errors: params.errors}.to_json
  response.status_code 400
end

[View source]
def validate! #

Input must be valid otherwise raises error, if valid returns a hash of validated params Otherwise raises a Validator::ValidationFailed error messages contain errors.

user = User.new params.validate!

[View source]
def validation(&) #

Setups validation rules to be performed

params.validation do
  required(:email) { |p| p.url? }
  required(:age, UInt32)
end

[View source]

Macro Detail

macro method_missing(call) #

This will allow params to respond to HTTP::Params methods. For example: [], []?, add, delete, each, fetch, etc.


[View source]