Module: ValidateMyRoutes::Validatable
- Defined in:
- lib/validate_my_routes/validatable.rb
Overview
Route parameters validation extension To start using it, the extension needs to be registered
register ValidateMyRoutes::Validatable
Registering Validatable extension adds two conditions:
- validate_all_params - a list of rules that need to be applied to all parameters together
- validate_params - a hash with parameter names as keys and rules with extra information
in values
Class Method Summary collapse
Instance Method Summary collapse
-
#all_params_validation(rule) ⇒ Object
Define all parameters validation for a single route.
-
#param_validation(name, rule) ⇒ Object
Define path parameter with validation for all routes (including nested routes).
-
#route(verb, route_pattern, conditions = {}, &block) ⇒ Object
Hook into .route Sinatra method to add validation for parameters.
- #route_path_parameters(route_pattern) ⇒ Object
Class Method Details
.registered(app) ⇒ Object
13 14 15 16 |
# File 'lib/validate_my_routes/validatable.rb', line 13 def registered(app) add_validate_params_condition_to app add_validate_all_params_condition_to app end |
Instance Method Details
#all_params_validation(rule) ⇒ Object
Define all parameters validation for a single route
all_params_validation at_least_one_of(%i[version class status owner])
get '/' do
# params contain at least one of :version, :class, :status or :owner parameter
end
61 62 63 64 |
# File 'lib/validate_my_routes/validatable.rb', line 61 def all_params_validation(rule) Validate::Rules.validate_all_params_rule! rule (@all_params_validation ||= []) << rule end |
#param_validation(name, rule) ⇒ Object
Define path parameter with validation for all routes (including nested routes)
param_validation :service_id, from_enum(%w[a b c])
50 51 52 53 |
# File 'lib/validate_my_routes/validatable.rb', line 50 def param_validation(name, rule) Validate::Rules.validate_single_param_rule! rule (@param_validations ||= {})[name.to_sym] = rule end |
#route(verb, route_pattern, conditions = {}, &block) ⇒ Object
Hook into .route Sinatra method to add validation for parameters
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/validate_my_routes/validatable.rb', line 67 def route(verb, route_pattern, conditions = {}, &block) route_path_parameters(route_pattern).each do |name| next unless param_validations.key? name rule = param_validations[name] # Add path parameter validation if it was specified (conditions[:validate_params] ||= {})[name] ||= { path_param: true, rule: rule } end # Add all params validation if it was specified conditions[:validate_all_params] = @all_params_validation if @all_params_validation @all_params_validation = nil # remove params validation as it is defined on per-route bases super(verb, route_pattern, conditions, &block) end |
#route_path_parameters(route_pattern) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/validate_my_routes/validatable.rb', line 83 def route_path_parameters(route_pattern) path_parameters = route_pattern.split('/').map do |part| part.start_with?(':') ? part[1..-1].to_sym : nil end path_parameters.flatten.compact.uniq.map(&:to_sym) end |