dryer_routes 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a133f0a65c2b005f047880978e21502314b61d5a2553c75fa08cebf7059a2f2
4
- data.tar.gz: 2d6c30dcaa3d7558b9544e3bea8c999f67d334f2d62c21a9d43a511747cc1dac
3
+ metadata.gz: 934861b5f0bc6948ab894e87c0e80aca12cd2400d4ad390748489dfcc140fbc4
4
+ data.tar.gz: 77e850a209bc291a1655fa1aaba2ca0e18bd23c1a0d79ada520c39b1068a50e3
5
5
  SHA512:
6
- metadata.gz: 5f31bd2c263ed221cda9edb8adda25a202c02046db8657977d96456700cb09fa3e6e383e3c3e0d15e3edd7efe5771fc3d0d6d53e9fc0b5799ae5724c7ba1292d
7
- data.tar.gz: 243599ab47690a8a38a77d70d78650a179a49b7027880dc0662999cc16e814e0206315f2c93a7f88639f07041b7fbcac5cc9ece7708aa4e05b7641133b5082fd
6
+ metadata.gz: 991fe2bfe5dbde642ef95699f7b28ba8b29f1df62a8d74911a8033dba8dbcaa6a0d2c4756bf6e0ac9a9ef394141030ec147c70d1eee90bb545c7257befaba700
7
+ data.tar.gz: 512b0911e894206551d40c48b7c82ccb4942cdbd1044089b7fdf32b05fdd8af9deab91341d01c3fb41dfedf7b111f1fb2bd785bd39a9657f00ac8a0c962242c0
data/README.md CHANGED
@@ -91,7 +91,9 @@ class ApplicationController < ActionController::Base
91
91
  def validate_request
92
92
  request_errors = RouteRegistry.validate_request(request)
93
93
  if request_errors.empty?
94
- @validated_request_body = Dry::Monads::Success(request.params)
94
+ @validated_request_body = Dry::Monads::Success(
95
+ RouteRegistry.get_validated_values(request)
96
+ )
95
97
  else
96
98
  @validated_request_body = Dry::Monads::Failure(request_errors)
97
99
  end
@@ -136,6 +138,7 @@ class UsersController < ApplicationController
136
138
  end
137
139
  end
138
140
  ```
141
+ where `body` will only contain the keys specified by the contract.
139
142
 
140
143
  ## Development
141
144
  This gem is set up to be developed using [Nix](https://nixos.org/) and
data/dryer_routes.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'dryer_routes'
3
- spec.version = "0.2.2"
3
+ spec.version = "0.3.0"
4
4
  spec.authors = ['John Bernier']
5
5
  spec.email = ['john.b.bernier@gmail.com']
6
6
  spec.summary = 'Typed routing for rails leveraging dry-validation contracts'
@@ -0,0 +1,45 @@
1
+ require "dryer_services"
2
+
3
+ module Dryer
4
+ module Routes
5
+ class ExtractValidatedKeys < Dryer::Services::SimpleService
6
+ def initialize(payload:, contract:)
7
+ @payload = payload
8
+ @contract = contract
9
+ end
10
+
11
+ def call
12
+ return {} if payload == nil || contract == nil
13
+ extract_keys(
14
+ to_symbol_keys(payload),
15
+ contract.send(:key_map)
16
+ )
17
+ end
18
+
19
+ private
20
+ def extract_keys(payload, key_map)
21
+ key_map.inject({}) do |validated_keys, key|
22
+ validated_keys[key.name.to_sym] = if key.respond_to?(:members)
23
+ extract_keys(payload[key.name.to_sym], key.members)
24
+ else
25
+ payload[key.name.to_sym]
26
+ end
27
+ validated_keys
28
+ end
29
+ end
30
+
31
+ def to_symbol_keys(hash)
32
+ hash.inject({}) do |sym_hash, (key, value)|
33
+ sym_hash[key.to_sym] = if value.is_a?(Hash)
34
+ to_symbol_keys(value)
35
+ else
36
+ value
37
+ end
38
+ sym_hash
39
+ end
40
+ end
41
+
42
+ attr_reader :payload, :contract
43
+ end
44
+ end
45
+ end
@@ -1,6 +1,7 @@
1
1
  require_relative "./build_from_resource.rb"
2
2
  require_relative "./hash_object.rb"
3
3
  require_relative "./resource_schema.rb"
4
+ require_relative "./extract_validated_keys.rb"
4
5
 
5
6
  module Dryer
6
7
  module Routes
@@ -59,6 +60,18 @@ module Dryer
59
60
  end.first
60
61
  end
61
62
 
63
+ def get_validated_values(request)
64
+ route_for(
65
+ controller: request.controller_class,
66
+ method: request.request_method_symbol
67
+ ).then do |route|
68
+ ExtractValidatedKeys.call(
69
+ payload: request.params,
70
+ contract: route.request_contract
71
+ )
72
+ end
73
+ end
74
+
62
75
  attr_reader :routes, :resources
63
76
 
64
77
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dryer_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bernier
@@ -93,6 +93,7 @@ files:
93
93
  - README.md
94
94
  - dryer_routes.gemspec
95
95
  - lib/dryer/routes/build_from_resource.rb
96
+ - lib/dryer/routes/extract_validated_keys.rb
96
97
  - lib/dryer/routes/hash_object.rb
97
98
  - lib/dryer/routes/registries/create.rb
98
99
  - lib/dryer/routes/registry.rb