dzl 1.0.0.rc11 → 1.0.0.rc12

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,7 +23,7 @@ class Dzl::DSLSubjects::ParameterBlock < Dzl::DSLSubject
23
23
  Dzl::ValueOrError.new(e: param.opts[:header] ? :missing_required_header : :missing_required_param)
24
24
  elsif parandidate.nil?
25
25
  # TODO HashValidator with default values
26
- Dzl::ValueOrError.new(v: :__no_value__)
26
+ Dzl::ValueOrError.new(v: param.default_value)
27
27
  else
28
28
  unless parandidate.is_a?(Hash)
29
29
  if param.opts[:format] == :json
@@ -57,4 +57,17 @@ class Dzl::Examples::FunWithHashes < Dzl::Examples::Base
57
57
  optional(:id) { type Fixnum }
58
58
  end
59
59
  end
60
+
61
+ post '/hash_with_default' do
62
+ optional :hash do
63
+ type Hash
64
+ default({
65
+ default: 'v1'
66
+ })
67
+
68
+ required :default do
69
+ allowed_values %w{v1 v2 v3}
70
+ end
71
+ end
72
+ end
60
73
  end
@@ -1,3 +1,3 @@
1
1
  module Dzl
2
- VERSION = "1.0.0.rc11"
2
+ VERSION = "1.0.0.rc12"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  class HashValidator
2
2
  attr_reader :dsl_proxy
3
+ attr_accessor :default_value
3
4
 
4
5
  class << self
5
6
  alias_method :orig_new, :new
@@ -18,6 +19,7 @@ class HashValidator
18
19
 
19
20
  @key_stack = []
20
21
  @dsl_proxy = DSLProxy.new(self)
22
+ @default_value = :__no_value__
21
23
  end
22
24
 
23
25
  def opts
@@ -146,5 +148,10 @@ class HashValidator
146
148
  def forbidden_values(ary)
147
149
  @subject.add_option(:forbidden_values, ary)
148
150
  end
151
+
152
+ def default(hsh)
153
+ raise ArgumentError unless hsh.is_a?(Hash)
154
+ @subject.default_value = hsh
155
+ end
149
156
  end
150
157
  end
@@ -188,4 +188,27 @@ describe 'hash parameters' do
188
188
  end
189
189
  end
190
190
  end
191
+
192
+ context 'default values' do
193
+ specify 'are not used when the parameter is present' do
194
+ post('/hash_with_default', {hash: {default: 'v2'}.to_json}) do |response|
195
+ response.status.should == 200
196
+ JSON.parse(response.body)['params']['hash']['default'].should == 'v2'
197
+ end
198
+ end
199
+
200
+ specify 'are used when the parameter is omitted' do
201
+ post('/hash_with_default') do |response|
202
+ response.status.should == 200
203
+ JSON.parse(response.body)['params']['hash']['default'].should == 'v1'
204
+ end
205
+ end
206
+
207
+ specify 'validations on provided parameter still work as expected' do
208
+ post('/hash_with_default', {hash: {default: 'bad'}.to_json}) do |response|
209
+ response.status.should == 404
210
+ JSON.parse(response.body)['errors']['/hash_with_default']['hash'].should == 'hash_validation_failed'
211
+ end
212
+ end
213
+ end
191
214
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dzl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc11
4
+ version: 1.0.0.rc12
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-05-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
17
- requirement: &70317751225820 !ruby/object:Gem::Requirement
17
+ requirement: &70160812208580 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.4.1
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70317751225820
25
+ version_requirements: *70160812208580
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &70317751225320 !ruby/object:Gem::Requirement
28
+ requirement: &70160812208080 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 3.2.2
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70317751225320
36
+ version_requirements: *70160812208080
37
37
  description: Small, fast racktivesupport web framework with handy DSL and explicit
38
38
  parameter validation.
39
39
  email: