Module: RazorRisk::Sinatra::Helpers::ValidateQueryParametersHelper

Includes:
Pantheios, Xqsr3::Quality::ParameterChecking
Defined in:
lib/razor_risk/sinatra/helpers/validate_query_parameters_helper.rb

Overview

########################################################################## ValidateQueryParametersHelper

Defined Under Namespace

Modules: ValidateQueryParametersHelper_Constants

Instance Method Summary collapse

Instance Method Details

#validate_query_parameters(*args, **options) ⇒ Object

module ValidateQueryParametersHelper_Constants



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/razor_risk/sinatra/helpers/validate_query_parameters_helper.rb', line 55

def validate_query_parameters *args, **options

    trace ParamNames[ :args, :options], args, options


    params  =   args.shift
    spec    =   args.shift
    rt_vars =   args.shift || []

    case spec
    when nil

        params
    when ::Array

        # interpreted as a complete list of optional parameters


        unrecognised    =   params.keys - spec

        unrecognised    =   unrecognised - rt_vars

        unrecognised    =   unrecognised - ValidateQueryParametersHelper_Constants::SINATRA_SPECIAL_PARAMS

        unless unrecognised.empty?

            halt(*[ 422, {}, "One or more parameters - '#{unrecognised}' - are not supported on this route" ])
        end

        params
    when ::Hash

        # interpreted as a map of { query-parameter => [ 0+

        # characteristics-symbols] }, where recognised characteristics are:

        #

        # - :allow_empty - means that the parameter value may be empty

        #   (subject to :strip);

        # - :required - means that the parameter must be present;

        # - :strip - causes the parameter to be (converted to string

        #   form and then) stripped;

        #

        # - :integral - causes the parameter to be converted to string;

        #   halts(422) if the conversion fails;

        # - :negative - requires the value to be <0; operative only if

        #   :integral is specified;

        # - :positive - requires the value to be >0; operative only if

        #   :integral is specified;

        # - :non_negative - requires the value to be >=0; operative only

        #   if :integral is specified;

        # - :non_positive - requires the value to be <=0; operative only

        #   if :integral is specified;


        warn "validating query parameters from a #{::Hash} spec is not supported in the current version"

        unrecognised    =   params.keys - spec.keys

        unrecognised    =   unrecognised - rt_vars

        unrecognised    =   unrecognised - ValidateQueryParametersHelper_Constants::SINATRA_SPECIAL_PARAMS

        unless unrecognised.empty?

            halt(*[ 422, {}, "One or more parameters - '#{unrecognised}' - are not supported on this route" ])
        end

        validated_params = params.dup

        spec.each do |k, characteristics|

            next if ValidateQueryParametersHelper_Constants::SINATRA_SPECIAL_PARAMS.include?(k)

            next if rt_vars.include?(k)

            case v
            when nil

                next
            when ::Array

                ;
            else

                characteristics = [ characteristics ]
            end

            if validated_params.has_key? k

                value   =   validated_params[k]

                value   =   value.to_s.strip if characteristics.include? :strip

                unless characteristics.include? :allow_empty

                    if value.to_s.empty?

                        halt(*[ 422, {}, "Query parameter '#{k}' has missing value in URI" ])
                    end
                end

                if characteristics.include? :integral

                    # TODO : implement integral conversion and

                    # range-checking

                end

                validated_params[k] = value
            else

                if characteristics.include? :required

                    halt(*[ 422, {}, "Required query parameter '#{k}' not present in URI" ])
                end
            end
        end

        validated_params
    else



        warn "#{self.class}##{__method__}() : invalid spec type - '#{spec.class}' - given - ignoring"
    end
end