Skip to content

Instantly share code, notes, and snippets.

@paul
Created September 12, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul/32151fc1bf49874de644966444427f10 to your computer and use it in GitHub Desktop.
Save paul/32151fc1bf49874de644966444427f10 to your computer and use it in GitHub Desktop.
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