Is there a way to define a schema as a constant by itself like this:
AddressSchema = Dry::Schema.Params do
required(:street).filled(:string)
required(:city).filled(:string)
required(:zipcode).filled(:string)
end
And use it in a Dry::Struct and a Dry::Validation::Contract subclasses like this:
class AddressStruct < Dry::Struct
include AddresSchema
end
class AddressContract < Dry::Validation::Contract
include AddressSchema
# Validation rules below
end
EDIT: Turns out I was wrong
It seems odd to just write out the whole schema again when it’s the same thing for the validator and the struct.
I played around with it and it turns out you can just do
class AddressStruct < Dry::Struct
schema(AddresSchema)
end
class AddressContract < Dry::Validation::Contract
schema(AddressSchema)
# Validation rules below
end
What I end up having to do is duplicate the schema as attributes in the Struct
class Address < Dry::Struct
attribute :street, Types::String
attribute :city, Types::String
attribute :zipcode, Types::String
end