Skip to content
This repository

Request params parser #9

Merged
merged 13 commits into from over 1 year ago

2 participants

Myron Marston proby
Myron Marston

This is a new interpol tool that does two things:

  • Validates request params according to a schema definition.
  • Parses request params based on a schema definition.

Can I get a code review, @proby? /cc @waltjones @tithonium @dudleycarr

proby proby commented on the diff September 24, 2012
lib/interpol/request_params_parser.rb
((103 lines not shown))
  103
+      def no_additional_properties?
  104
+        [
  105
+          @endpoint_definition.path_params,
  106
+          @endpoint_definition.query_params
  107
+        ].none? { |params| params['additionalProperties'] }
  108
+      end
  109
+
  110
+      STRING_EQUIVALENTS = {
  111
+        'string'  => nil,
  112
+        'integer' => { 'type' => 'string', 'pattern' => '^\-?\d+$' },
  113
+        'number'  => { 'type' => 'string', 'pattern' => '^\-?\d+(\.\d+)?$' },
  114
+        'boolean' => { 'type' => 'string', 'enum'    => %w[ true false ] },
  115
+        'null'    => { 'type' => 'string', 'enum'    => [''] }
  116
+      }
  117
+
  118
+      def adjusted_schema(schema)
2
proby Owner
proby added a note September 24, 2012

This method name + parameter seems a little clumsy. I don't have a better suggestion off the top of my head though.

Myron Marston Owner

I agree but don't have any better ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
proby proby commented on the diff September 24, 2012
lib/interpol/request_params_parser.rb
((33 lines not shown))
  33
+
  34
+    def validate!(params)
  35
+      @validator.validate!(params)
  36
+    end
  37
+
  38
+    # Private: This takes care of the validation.
  39
+    class ParamValidator
  40
+      def initialize(endpoint_definition)
  41
+        @endpoint_definition = endpoint_definition
  42
+        @params_schema = build_params_schema
  43
+      end
  44
+
  45
+      def validate_path_params_valid_for_route!
  46
+        route = @endpoint_definition.route
  47
+        invalid_params = property_defs_from(:path_params).keys.reject do |param|
  48
+          route =~ %r</:#{Regexp.escape(param)}(/|$)>
2
proby Owner
proby added a note September 24, 2012

It's initially confusing what this does.

Myron Marston Owner

This looks at your route definition and validates that every defined path_param is actually a :blah param in the route string. Maybe it's not needed but I figured it was good to verify that the path_params you have defined are in fact part of the routing path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
proby proby commented on the diff September 24, 2012
lib/interpol/request_params_parser.rb
((176 lines not shown))
  176
+        end
  177
+      end
  178
+
  179
+      NULLS = { '' => nil, nil => nil }
  180
+      def self.convert_null(value)
  181
+        NULLS.fetch(value) do
  182
+          raise ArgumentError, "#{value} is not convertable to null"
  183
+        end
  184
+      end
  185
+
  186
+      def self.convert_date(value)
  187
+        unless value =~ /\A\d{4}\-\d{2}\-\d{2}\z/
  188
+          raise ArgumentError, "Not in iso8601 format"
  189
+        end
  190
+
  191
+        Date.new(*value.split('-').map(&:to_i))
3
proby Owner
proby added a note September 24, 2012

Why did you use this instead of Date.parse(value)?

Myron Marston Owner

Historically, Date.parse has been quite slow. I think it's a lot better in 1.9.3 than prior versions of ruby (I think I heard it's in C now), but this should still be faster, I think. Date.parse handles many different date formats, and it has to be tolerant of you using any of them. Here we know that we have 3 integers, which is exactly what the normal constructor needs, so we can bypass the complicated parsing logic entirely.

Myron Marston Owner

BTW, I originally used Date.iso8601(value) but that doesn't work on 1.8.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
proby proby commented on the diff September 24, 2012
lib/interpol/sinatra/request_params_parser.rb
((49 lines not shown))
  49
+                available_versions ||= endpoint.available_versions
  50
+                interpol_config.api_version_for(env, endpoint).tap do |_version|
  51
+                  version ||= _version
  52
+                end
  53
+              end
  54
+
  55
+            if definition == DefinitionFinder::NoDefinitionFound
  56
+              interpol_config.request_version_unavailable(self, version, available_versions)
  57
+            end
  58
+
  59
+            definition
  60
+          end
  61
+        end
  62
+
  63
+        def params
  64
+          @_parsed_params || super
2
proby Owner
proby added a note September 24, 2012

Why did you add the first underscore to this variable name?

Myron Marston Owner

These instance variables live in the same space as the instance variables in an end-user's Sinatra application. I used the underscore prefix to minimize the likelihood of a naming collision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Myron Marston myronmarston merged commit f8605e9 into from September 24, 2012
Myron Marston myronmarston closed this September 24, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.