Created
September 12, 2022 15:14
-
-
Save paul/32151fc1bf49874de644966444427f10 to your computer and use it in GitHub Desktop.
This file contains hidden or 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