Next-gen ruby libs! » github.com/dry-rb » website: https://dry-rb.org » forum: https://discourse.dry-rb.org
rule(parse_request_id: %i[parse_request_id]) do |parse_request_id|
parse_request_id.filled? > parse_request_repository.find_by(id: value(parse_request_id)).filled?
end
validate(exists?: :parse_request_id) do |parse_request_id|
if parse_request.nil?
true
else
parse_request_repository.find_by(id: parse_request_id).present?
end
end
rule
block. What I think you want to do is make a cusastom predicate called parse_request_exists?
which takes the ID and returns true/false if it exists
parse_request_id.filled? > parse_request_id.parse_request_exists?
record.class.where
-> it does not work for multiple checks in one schema
request_id.filled? > request_id.record_exists?(:parse_request)
TargetRequest::CreateSchema.with(record: customer_repository.new).call(input).to_monad hints: false
:parse_request
name inside that predicate method to work out which repo to use or whatever
configure do
def exists?(klass, value)
!!klass.find(value).empty?
end
end
optional(: parse_request_id) {
filled? > exists(ParseRequest)?
}
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})