forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Grape::Util::Registry to Grape::Validations
ContractScope validator has been moved to validations/validators and renamed properly
- Loading branch information
1 parent
81e68f1
commit 8aee8f9
Showing
8 changed files
with
117 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/grape/validations/validators/contract_scope_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
module Validations | ||
module Validators | ||
class ContractScopeValidator < Base | ||
attr_reader :schema | ||
|
||
def initialize(_attrs, _options, _required, _scope, opts) | ||
super | ||
@schema = opts.fetch(:schema) | ||
end | ||
|
||
# Validates a given request. | ||
# @param request [Grape::Request] the request currently being handled | ||
# @raise [Grape::Exceptions::ValidationArrayErrors] if validation failed | ||
# @return [void] | ||
def validate(request) | ||
res = schema.call(request.params) | ||
|
||
if res.success? | ||
request.params.deep_merge!(res.to_h) | ||
return | ||
end | ||
|
||
raise Grape::Exceptions::ValidationArrayErrors.new(build_errors_from_messages(res.errors.messages)) | ||
end | ||
|
||
private | ||
|
||
def build_errors_from_messages(messages) | ||
messages.map do |message| | ||
full_name = message.path.first.to_s | ||
full_name << "[#{message.path[1..].join('][')}]" if message.path.size > 1 | ||
Grape::Exceptions::Validation.new(params: [full_name], message: message.text) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
spec/grape/validations/validators/contract_scope_validator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Grape::Validations::Validators::ContractScopeValidator do | ||
describe '.inherits' do | ||
subject { described_class } | ||
|
||
it { is_expected.to be < Grape::Validations::Validators::Base } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters