Module: Syntropy::RequestValidationMethods

Included in:
Request
Defined in:
lib/syntropy/request/validation.rb

Instance Method Summary collapse

Instance Method Details

#validate(value, *clauses) ⇒ any

Validates and optionally converts a value against the given clauses. If no clauses are given, verifies the parameter value is not nil. A clause can be a class, such as String, Integer, etc, in which case the value is converted into the corresponding value. A clause can also be a range, for verifying the value is within the range. A clause can also be an array of two or more clauses, at least one of which should match the value. If the validation fails, an exception is raised.

Parameters:

  • value (any)

    value

Returns:

  • (any)

    validated value

Raises:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/syntropy/request/validation.rb', line 50

def validate(value, *clauses)
  raise Syntropy::ValidationError, 'Validation error' if clauses.empty? && !value

  clauses.each do |c|
    valid = param_is_valid?(value, c)
    raise(Syntropy::ValidationError, 'Validation error') if !valid

    value = param_convert(value, c)
  end
  value
end

#validate_cache(cache_control: 'public', etag: nil, last_modified: nil) ⇒ void

This method returns an undefined value.

Validates request cache information. If the request cache information matches the given etag or last_modified values, responds with a 304 Not Modified status. Otherwise, yields to the given block for a normal response, and sets cache control headers according to the given arguments.

Parameters:

  • cache_control (String) (defaults to: 'public')

    value for Cache-Control header

  • etag (String, nil) (defaults to: nil)

    Etag header value

  • last_modified (String, nil) (defaults to: nil)

    Last-Modified header value



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/syntropy/request/validation.rb', line 71

def validate_cache(cache_control: 'public', etag: nil, last_modified: nil)
  validated = false
  if (client_etag = headers['if-none-match'])
    validated = true if client_etag == etag
  end
  if (client_mtime = headers['if-modified-since'])
    validated = true if client_mtime == last_modified
  end
  if validated
    respond(nil, ':status' => HTTP::NOT_MODIFIED)
  else
    cache_headers = {
      'Cache-Control' => cache_control
    }
    cache_headers['Etag'] = etag if etag
    cache_headers['Last-Modified'] = last_modified if last_modified
    set_response_headers(cache_headers)
    yield
  end
end

#validate_http_method(*accepted) ⇒ String

Checks the request’s HTTP method against the given accepted values. If not included in the accepted values, raises an exception. Otherwise, returns the request’s HTTP method.

Parameters:

  • accepted (Array<String>)

    list of accepted HTTP methods

Returns:

  • (String)

    request’s HTTP method



15
16
17
18
19
# File 'lib/syntropy/request/validation.rb', line 15

def validate_http_method(*accepted)
  return method if accepted.include?(method)

  raise Syntropy::Error.method_not_allowed
end

#validate_param(name, *clauses) ⇒ any

Validates and optionally converts request parameter value for the given parameter name against the given clauses. If no clauses are given, verifies the parameter value is not nil. A clause can be a class, such as String, Integer, etc, in which case the value is converted into the corresponding value. A clause can also be a range, for verifying the value is within the range. A clause can also be an array of two or more clauses, at least one of which should match the value. If the validation fails, an exception is raised. Example:

height = req.validate_param(:height, Integer, 1..100)

Parameters:

  • name (Symbol)

    parameter name

Returns:

  • (any)

    validated parameter value



35
36
37
# File 'lib/syntropy/request/validation.rb', line 35

def validate_param(name, *clauses)
  validate(query[name], *clauses)
end