Skip to content

Instantly share code, notes, and snippets.

@Ptico
Last active October 22, 2019 13:29
Show Gist options
  • Save Ptico/b8405ad7ffa2a3bea155ec46b813755a to your computer and use it in GitHub Desktop.
Save Ptico/b8405ad7ffa2a3bea155ec46b813755a to your computer and use it in GitHub Desktop.
dry-schema partial
AddressSchema = Dry::Schema.Params do
required(:street).filled(:string)
required(:city).filled(:string)
required(:zipcode).filled(:string)
required(:email).filled(:string)
end
# ...
# CREATE { street: 'Baker Street', city: 'London', zipcode: 'NW1/W1', email: '[email protected]' }
post '/addresses' do
schema = AddressSchema.(params)
DB[:addresses].insert(schema.to_h) if schema.success?
end
# UPDATE { email: '[email protected]' }
patch '/address/:id' do
schema = AddressSchema.(params) # this will obviously fail
DB[:addresses].where(id: params['id']).update(schema.to_h)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment