|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Committee |
| 4 | + module Middleware |
| 5 | + module Options |
| 6 | + class RequestValidation < Base |
| 7 | + # RequestValidation specific options |
| 8 | + attr_reader :strict |
| 9 | + attr_reader :coerce_date_times |
| 10 | + attr_reader :coerce_recursive |
| 11 | + attr_reader :coerce_form_params |
| 12 | + attr_reader :coerce_path_params |
| 13 | + attr_reader :coerce_query_params |
| 14 | + attr_reader :allow_form_params |
| 15 | + attr_reader :allow_query_params |
| 16 | + attr_reader :allow_get_body |
| 17 | + attr_reader :allow_non_get_query_params |
| 18 | + attr_reader :check_content_type |
| 19 | + attr_reader :check_header |
| 20 | + attr_reader :optimistic_json |
| 21 | + attr_reader :request_body_hash_key |
| 22 | + attr_reader :query_hash_key |
| 23 | + attr_reader :path_hash_key |
| 24 | + attr_reader :headers_key |
| 25 | + attr_reader :params_key |
| 26 | + attr_reader :allow_blank_structures |
| 27 | + attr_reader :allow_empty_date_and_datetime |
| 28 | + attr_reader :parameter_overwrite_by_rails_rule |
| 29 | + attr_reader :deserialize_parameters |
| 30 | + |
| 31 | + # Default values |
| 32 | + DEFAULTS = { strict: false, coerce_recursive: true, allow_get_body: false, check_content_type: true, check_header: true, optimistic_json: false, allow_form_params: true, allow_query_params: true, allow_non_get_query_params: false, allow_blank_structures: false, allow_empty_date_and_datetime: false, parameter_overwrite_by_rails_rule: true, request_body_hash_key: "committee.request_body_hash", query_hash_key: "committee.query_hash", path_hash_key: "committee.path_hash", headers_key: "committee.headers", params_key: "committee.params" }.freeze |
| 33 | + |
| 34 | + def initialize(options = {}) |
| 35 | + super(options) |
| 36 | + |
| 37 | + # Validation related |
| 38 | + @strict = options.fetch(:strict, DEFAULTS[:strict]) |
| 39 | + @check_content_type = options.fetch(:check_content_type, DEFAULTS[:check_content_type]) |
| 40 | + @check_header = options.fetch(:check_header, DEFAULTS[:check_header]) |
| 41 | + @optimistic_json = options.fetch(:optimistic_json, DEFAULTS[:optimistic_json]) |
| 42 | + |
| 43 | + # Type coercion related |
| 44 | + @coerce_date_times = options[:coerce_date_times] |
| 45 | + @coerce_recursive = options.fetch(:coerce_recursive, DEFAULTS[:coerce_recursive]) |
| 46 | + @coerce_form_params = options[:coerce_form_params] |
| 47 | + @coerce_path_params = options[:coerce_path_params] |
| 48 | + @coerce_query_params = options[:coerce_query_params] |
| 49 | + |
| 50 | + # Parameter permission related |
| 51 | + @allow_form_params = options.fetch(:allow_form_params, DEFAULTS[:allow_form_params]) |
| 52 | + @allow_query_params = options.fetch(:allow_query_params, DEFAULTS[:allow_query_params]) |
| 53 | + @allow_get_body = options.fetch(:allow_get_body, DEFAULTS[:allow_get_body]) |
| 54 | + @allow_non_get_query_params = options.fetch(:allow_non_get_query_params, DEFAULTS[:allow_non_get_query_params]) |
| 55 | + @allow_blank_structures = options.fetch(:allow_blank_structures, DEFAULTS[:allow_blank_structures]) |
| 56 | + @allow_empty_date_and_datetime = options.fetch(:allow_empty_date_and_datetime, DEFAULTS[:allow_empty_date_and_datetime]) |
| 57 | + |
| 58 | + # Rails compatibility |
| 59 | + @parameter_overwrite_by_rails_rule = options.fetch(:parameter_overwrite_by_rails_rule, DEFAULTS[:parameter_overwrite_by_rails_rule]) |
| 60 | + |
| 61 | + # Deserialization |
| 62 | + @deserialize_parameters = options[:deserialize_parameters] |
| 63 | + |
| 64 | + # Hash keys |
| 65 | + @request_body_hash_key = options.fetch(:request_body_hash_key, DEFAULTS[:request_body_hash_key]) |
| 66 | + @query_hash_key = options.fetch(:query_hash_key, DEFAULTS[:query_hash_key]) |
| 67 | + @path_hash_key = options.fetch(:path_hash_key, DEFAULTS[:path_hash_key]) |
| 68 | + @headers_key = options.fetch(:headers_key, DEFAULTS[:headers_key]) |
| 69 | + @params_key = options.fetch(:params_key, DEFAULTS[:params_key]) |
| 70 | + end |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def build_hash |
| 75 | + super.merge(strict: @strict, coerce_date_times: @coerce_date_times, coerce_recursive: @coerce_recursive, coerce_form_params: @coerce_form_params, coerce_path_params: @coerce_path_params, coerce_query_params: @coerce_query_params, allow_form_params: @allow_form_params, allow_query_params: @allow_query_params, allow_get_body: @allow_get_body, allow_non_get_query_params: @allow_non_get_query_params, allow_blank_structures: @allow_blank_structures, allow_empty_date_and_datetime: @allow_empty_date_and_datetime, parameter_overwrite_by_rails_rule: @parameter_overwrite_by_rails_rule, deserialize_parameters: @deserialize_parameters, check_content_type: @check_content_type, check_header: @check_header, optimistic_json: @optimistic_json, request_body_hash_key: @request_body_hash_key, query_hash_key: @query_hash_key, path_hash_key: @path_hash_key, headers_key: @headers_key, params_key: @params_key) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments