forked from dry-rb/dry-schema
-
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.
It makes message compiler a specific case of a generic error compiler, just like new ast error compiler. References dry-rb/dry-validation#604
- Loading branch information
1 parent
c3fa27d
commit e2a58f8
Showing
13 changed files
with
265 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/schema/error_compiler' | ||
require 'dry/schema/ast_error_set' | ||
|
||
module Dry | ||
module Schema | ||
# Compiles rule results AST into machine-readable format | ||
# | ||
# @api private | ||
class AstErrorCompiler < ErrorCompiler | ||
# @api private | ||
def call(ast) | ||
AstErrorSet[ast] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/schema/error_set' | ||
|
||
module Dry | ||
module Schema | ||
# A set of AST errors used to generate machine-readable errors | ||
# | ||
# @see Result#message_set | ||
# | ||
# @api public | ||
class AstErrorSet < ErrorSet | ||
private | ||
|
||
# @api private | ||
def errors_map(errors = self.errors) | ||
errors | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dry | ||
module Schema | ||
# Compiles rule results AST into some type of errors | ||
# | ||
# @api private | ||
class ErrorCompiler | ||
# @api private | ||
def call(_ast) | ||
raise NotImplementedError | ||
end | ||
|
||
# @api private | ||
def with(*args) | ||
self | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/equalizer' | ||
|
||
module Dry | ||
module Schema | ||
# A set of generic errors | ||
# | ||
# @see Result#message_set | ||
# | ||
# @api public | ||
class ErrorSet | ||
include Enumerable | ||
include Dry::Equalizer(:errors, :options) | ||
|
||
# A list of compiled errors | ||
# | ||
# @return [Array<Any>] | ||
attr_reader :errors | ||
|
||
# Options hash | ||
# | ||
# @return [Hash] | ||
attr_reader :options | ||
|
||
# @api private | ||
def self.[](errors, options = EMPTY_HASH) | ||
new(errors, options) | ||
end | ||
|
||
# @api private | ||
def initialize(errors, options = EMPTY_HASH) | ||
@errors = errors | ||
@options = options | ||
end | ||
|
||
# Iterate over errors | ||
# | ||
# @example | ||
# result.errors.each do |message| | ||
# puts message.text | ||
# end | ||
# | ||
# @return [Array] | ||
# | ||
# @api public | ||
def each(&block) | ||
return self if empty? | ||
return to_enum unless block | ||
|
||
errors.each(&block) | ||
end | ||
|
||
# Dump message set to a hash | ||
# | ||
# @return [Hash<Symbol=>Array<String>>] | ||
# | ||
# @api public | ||
def to_h | ||
@to_h ||= errors_map | ||
end | ||
alias_method :to_hash, :to_h | ||
|
||
# Get a list of errors for the given key | ||
# | ||
# @param [Symbol] key | ||
# | ||
# @return [Array<String>] | ||
# | ||
# @api public | ||
def [](key) | ||
to_h[key] | ||
end | ||
|
||
# Get a list of errors for the given key | ||
# | ||
# @param [Symbol] key | ||
# | ||
# @return [Array<String>] | ||
# | ||
# @raise KeyError | ||
# | ||
# @api public | ||
def fetch(key) | ||
self[key] || raise(KeyError, "+#{key}+ error was not found") | ||
end | ||
|
||
# Check if an error set is empty | ||
# | ||
# @return [Boolean] | ||
# | ||
# @api public | ||
def empty? | ||
@empty ||= errors.empty? | ||
end | ||
|
||
# @api private | ||
def freeze | ||
to_h | ||
empty? | ||
super | ||
end | ||
|
||
# @api private | ||
def errors_map(_errors) | ||
raise NotImplementedError | ||
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
Oops, something went wrong.