repobot-file-sync[bot] on main
Update dry-types.gemspec Sourc… (compare)
repobot-file-sync[bot] on main
Update README.md Source file h… (compare)
repobot-file-sync[bot] on main
Update .repobot.yml Source fil… (compare)
flash-gordon on main
Fix version specs (compare)
repobot-file-sync[bot] on main
Update dry-system.gemspec Sour… (compare)
repobot-file-sync[bot] on main
Update README.md Source file h… (compare)
flash-gordon on main
Fix version specs (compare)
flash-gordon on main
Add meaningful source location … (compare)
module TargetRequests
CreateSchema = Dry::Validation.Schema(ApplicationSchema) do
required(:customer_id).filled { valid_db_id? & record_exists?(Customer) }
required(:parse_request_id).filled { valid_db_id? & record_exists?(ParseRequest) }
optional(:webhook_id).filled { valid_db_id? & record_exists?(Webhook) }
optional(:target_email_id).filled { valid_db_id? & record_exists?(TargetEmail) }
rule(target: %i[webhook_id target_email_id]) do |webhook_id, target_email_id|
webhook_id.filled? | target_email_id.filled?
end
end
end
class ApplicationSchema < Dry::Validation::Schema
configure do |config|
option :record
option :user_repository, User
option :customer_repository, Customer
option :document_repository, Document
option :document_content_repository, DocumentContent
option :vendor_repository, Vendor
option :template_repository, Template
option :source_email_repository, SourceEmail
option :parse_request_repository, ParseRequest
option :target_email_repository, TargetEmail
option :webhook_repository, Webhook
config.messages_file = 'config/locales/validations.yml'
end
...
end
MySchema.call({i_dont_want_you: 'xxx', customer_id: 1, parse_request_id: "0266fa8b-6878-44aa-ad2e-a1d6e9dffa9d", webhook_id: 1}).to_monad
=> Success({:i_dont_want_you=>"xxx", :customer_id=>1, :parse_request_id=>"0266fa8b-6878-44aa-ad2e-a1d6e9dffa9d", :webhook_id=>1})
config.type_specs = true
) does that. see: https://dry-rb.org/gems/dry-validation/input-preprocessing/
class DependencyContainer
extend Dry::Container::Mixin
register("dependency") { Object }
end
Injector = Dry::AutoInject(DependencyContainer)
class MyClass
include Injector["dependency"]
def initialize(name:)
@name = name
@object = dependency.new
end
end
MyClass.new(name: "Foo")
# ArgumentError: unknown keyword: dependency
# from (pry):10:in `initialize'
class MyClass2
include Injector["dependency"]
def initialize(name:, **params)
@name = name
@object = dependency.new
end
end
MyClass2.new(name: "Foo")
# NoMethodError: undefined method `new' for nil:NilClass
# from (pry):21:in `initialize'
def initialize(name:, **deps)
super(deps)
@name = name
@object = dependency.new
end
dry-initializer
in compare with dry-struct
?
dry-struct
is ADT in ruby, basically
Hi, I've been thinking about opening a pull request for dry-struct
to make it quack a bit more like a hash in order to enable validating it with dry-validation
. Nowadays an attempt to call a schema with a struct object throws an error. I don't understand if this is a conscious design decision because it seems like a useful use case.
What I want to enable is this:
schema = Dry::Validation.Schema { required(:age).filled(gteq?: 18) }
User.new(age: 17)
schema.(user)
#<Dry::Validation::Result output=#<User age=17> errors={:age=>["must be greater than or equal to 18"]}>