Class: Respect::Rails::RequestSchema
- Inherits:
-
Object
- Object
- Respect::Rails::RequestSchema
- Includes:
- HeadersSimplifier
- Defined in:
- lib/respect/rails/request_schema.rb
Instance Attribute Summary (collapse)
-
- (Object) action
readonly
Returns the value of attribute action.
-
- (Object) body_parameters
Returns the value of attribute body_parameters.
-
- (Object) controller
readonly
Returns the value of attribute controller.
-
- (Object) default_path_parameters
readonly
Returns the value of attribute default_path_parameters.
-
- (Object) headers
Returns the value of attribute headers.
-
- (Object) last_error
readonly
Return the last validation error that happens during the validation process.
-
- (Object) path_parameters
Returns the value of attribute path_parameters.
-
- (Object) query_parameters
Returns the value of attribute query_parameters.
-
- (Object) sanitized_params
readonly
Returns the value of attribute sanitized_params.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (RequestSchema) initialize(controller, action)
constructor
A new instance of RequestSchema.
-
- (Object) sanitize!(request)
Sanitize all the request's parameters (path, query and body) in-place.
-
- (Object) validate(request)
Validate the given
request
. -
- (Object) validate!(request)
Validate the request and sanitize its parameters if the validation succeed.
- - (Boolean) validate?(request)
Methods included from HeadersSimplifier
Constructor Details
- (RequestSchema) initialize(controller, action)
A new instance of RequestSchema
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/respect/rails/request_schema.rb', line 12 def initialize(controller, action) @controller = controller @action = action @default_path_parameters = HashSchema.define do |s| s.string "controller", equal_to: @controller.to_s, doc: false s.string "action", equal_to: @action.to_s, doc: false end @path_parameters = @default_path_parameters.dup @body_parameters = HashSchema.new @query_parameters = HashSchema.new @headers = HashSchema.new end |
Instance Attribute Details
- (Object) action (readonly)
Returns the value of attribute action
25 26 27 |
# File 'lib/respect/rails/request_schema.rb', line 25 def action @action end |
- (Object) body_parameters
Returns the value of attribute body_parameters
29 30 31 |
# File 'lib/respect/rails/request_schema.rb', line 29 def body_parameters @body_parameters end |
- (Object) controller (readonly)
Returns the value of attribute controller
25 26 27 |
# File 'lib/respect/rails/request_schema.rb', line 25 def controller @controller end |
- (Object) default_path_parameters (readonly)
Returns the value of attribute default_path_parameters
31 32 33 |
# File 'lib/respect/rails/request_schema.rb', line 31 def default_path_parameters @default_path_parameters end |
- (Object) headers
Returns the value of attribute headers
27 28 29 |
# File 'lib/respect/rails/request_schema.rb', line 27 def headers @headers end |
- (Object) last_error (readonly)
Return the last validation error that happens during the validation process. (set by #validate?) Reset each time #validate? is called.
80 81 82 |
# File 'lib/respect/rails/request_schema.rb', line 80 def last_error @last_error end |
- (Object) path_parameters
Returns the value of attribute path_parameters
31 32 33 |
# File 'lib/respect/rails/request_schema.rb', line 31 def path_parameters @path_parameters end |
- (Object) query_parameters
Returns the value of attribute query_parameters
29 30 31 |
# File 'lib/respect/rails/request_schema.rb', line 29 def query_parameters @query_parameters end |
- (Object) sanitized_params (readonly)
Returns the value of attribute sanitized_params
38 39 40 |
# File 'lib/respect/rails/request_schema.rb', line 38 def sanitized_params @sanitized_params end |
Class Method Details
+ (Object) define(*args, &block)
7 8 9 |
# File 'lib/respect/rails/request_schema.rb', line 7 def define(*args, &block) RequestDef.eval(*args, &block) end |
Instance Method Details
- (Object) sanitize!(request)
Sanitize all the request's parameters (path, query and body) in-place.
94 95 96 97 98 |
# File 'lib/respect/rails/request_schema.rb', line 94 def sanitize!(request) [ :path, :query, :body ].each do |name| send("#{name}_parameters").sanitize_object!(request.params) end end |
- (Object) validate(request)
Validate the given request
. Raise a Respect::Rails::RequestValidationError
if an error occur. Returns true
on success.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/respect/rails/request_schema.rb', line 43 def validate(request) # Validate requests. unless Respect::Rails::Engine.disable_request_headers_validation begin headers.validate(request.headers) rescue Respect::ValidationError => e raise RequestValidationError.new(e, :headers, simplify_headers(request.headers)) end end [ :path, :query, :body ].each do |name| begin send("#{name}_parameters").validate(request.params) rescue Respect::ValidationError => e raise RequestValidationError.new(e, name, request.params) end end # Build sane parameters. @sanitized_params = {} [ :path, :query, :body ].each do |name| @sanitized_params.merge!(send("#{name}_parameters").sanitized_object) end true end |
- (Object) validate!(request)
Validate the request and sanitize its parameters if the validation succeed. You can disable the sanitization with Engine.sanitize_request_parameters.
85 86 87 88 89 90 91 |
# File 'lib/respect/rails/request_schema.rb', line 85 def validate!(request) valid = validate?(request) if valid sanitize!(request) end valid end |
- (Boolean) validate?(request)
67 68 69 70 71 72 73 74 75 |
# File 'lib/respect/rails/request_schema.rb', line 67 def validate?(request) begin validate(request) true rescue RequestValidationError => e @last_error = e false end end |