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
SettingsSchema = Dry::Schema.JSON do | |
optional(:custom_object).hash do | |
required(:active).filled(:bool) | |
optional(:entity_name).maybe(:string) | |
end | |
end | |
module Macros | |
def self.included(validator) | |
validator.register_macro(:custom_object_entity_name) do | |
next unless key? && key?(:active) | |
active, entity_name = value.values_at(:active, :entity_name) | |
if active && entity_name.blank? | |
key("settings.custom_object.entity_name").failure("must be provided when custom_object is active") | |
end | |
end | |
end | |
end | |
class CreateContract < Dry::Validation::Contract | |
include Macros | |
params do | |
required(:owner).filled(type?: User) | |
# ... | |
optional(:settings).hash(Integrations::Bullhorn::Schemas::Settings) | |
end | |
rule("settings.custom_object").validate(:custom_object_entity_name) | |
end | |
class UpdateContract < Dry::Validation::Contract | |
include Macros | |
params do | |
required(:my_object).filled(type?: MyObject) | |
# ... | |
optional(:settings).hash(Integrations::Bullhorn::Schemas::Settings) | |
end | |
rule("settings.custom_object").validate(:custom_object_entity_name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment