@@ -283,16 +283,15 @@ def validates(attrs, validations)
283
283
284
284
doc_attrs [ :documentation ] = validations . delete ( :documentation ) if validations . key? ( :documentation )
285
285
286
- full_attrs = attrs . collect { |name | { name : name , full_name : full_name ( name ) } }
287
- @api . document_attribute ( full_attrs , doc_attrs )
286
+ document_attribute ( attrs , doc_attrs )
288
287
289
288
opts = derive_validator_options ( validations )
290
289
290
+ order_specific_validations = Set [ :as ]
291
+
291
292
# Validate for presence before any other validators
292
- if validations . key? ( :presence ) && validations [ :presence ]
293
- validate ( 'presence' , validations [ :presence ] , attrs , doc_attrs , opts )
294
- validations . delete ( :presence )
295
- validations . delete ( :message ) if validations . key? ( :message )
293
+ validates_presence ( validations , attrs , doc_attrs , opts ) do |validation_type |
294
+ order_specific_validations << validation_type
296
295
end
297
296
298
297
# Before we run the rest of the validators, let's handle
@@ -301,8 +300,13 @@ def validates(attrs, validations)
301
300
coerce_type validations , attrs , doc_attrs , opts
302
301
303
302
validations . each do |type , options |
303
+ next if order_specific_validations . include? ( type )
304
304
validate ( type , options , attrs , doc_attrs , opts )
305
305
end
306
+
307
+ # Apply as validator last so other validations are applied to
308
+ # renamed param
309
+ validate ( :as , validations [ :as ] , attrs , doc_attrs , opts ) if validations . key? ( :as )
306
310
end
307
311
308
312
# Validate and comprehend the +:type+, +:types+, and +:coerce_with+
@@ -464,6 +468,18 @@ def derive_validator_options(validations)
464
468
fail_fast : validations . delete ( :fail_fast ) || false
465
469
}
466
470
end
471
+
472
+ def validates_presence ( validations , attrs , doc_attrs , opts )
473
+ return unless validations . key? ( :presence ) && validations [ :presence ]
474
+ validate ( :presence , validations [ :presence ] , attrs , doc_attrs , opts )
475
+ yield :presence
476
+ yield :message if validations . key? ( :message )
477
+ end
478
+
479
+ def document_attribute ( attrs , doc_attrs )
480
+ full_attrs = attrs . collect { |name | { name : name , full_name : full_name ( name ) } }
481
+ @api . document_attribute ( full_attrs , doc_attrs )
482
+ end
467
483
end
468
484
end
469
485
end
0 commit comments