-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1505 from senid231/provisioning-object-validation
provisioning object validation
- Loading branch information
Showing
9 changed files
with
499 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Billing | ||
module Provisioning | ||
module Errors | ||
class Error < StandardError | ||
end | ||
|
||
class InvalidVariablesError < Error | ||
attr_reader :errors | ||
|
||
# @param errors [Hash,String] | ||
# @param msg [String,nil] | ||
def initialize(errors, msg = nil) | ||
if errors.is_a?(String) | ||
msg = errors | ||
errors = { base: [msg] } | ||
end | ||
@errors = errors | ||
super(msg || "Validation error: #{full_error_messages.join(', ')}") | ||
end | ||
|
||
def full_error_messages(errors = @errors, prefix = nil) | ||
full_messages = [] | ||
errors.each do |key, values| | ||
if values.is_a?(Array) | ||
full_messages.concat parse_errors_array(key, values, prefix) | ||
elsif values.is_a?(Hash) | ||
full_messages.concat full_error_messages(values, [prefix, key].compact.join('.')) | ||
else | ||
raise ArgumentError, "Invalid error format: #{values.inspect}\nerrors: #{errors.inspect}" | ||
end | ||
end | ||
full_messages | ||
end | ||
|
||
def parse_errors_array(key, values, prefix) | ||
values.map do |value| | ||
if prefix.nil? | ||
key == :base || key.nil? ? value : "#{prefix}.#{key} - #{value}" | ||
else | ||
key == :base || key.nil? ? "#{prefix} - #{value}" : "#{prefix}.#{key} - #{value}" | ||
end | ||
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
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
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/schema' | ||
|
||
class Dry::Schema::Macros::DSL | ||
def default(value) | ||
schema_dsl.before(:rule_applier) do |result| | ||
result.update(name => value) unless result[name] | ||
end | ||
end | ||
end |
Oops, something went wrong.