Skip to content

Instantly share code, notes, and snippets.

@gotar
Created December 13, 2019 12:32
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 gotar/181e2027e763fc6e6cc197d080dbd9ba to your computer and use it in GitHub Desktop.
Save gotar/181e2027e763fc6e6cc197d080dbd9ba to your computer and use it in GitHub Desktop.
ROM (SQLite) + DRY-V example
#!/usr/bin/env ruby
require 'bundler/inline'
DB_URL = "postgres://localhost/gotar"
gemfile(true) do
source 'https://rubygems.org'
gem 'rom', '~> 5.1'
gem 'rom-sql', '~> 3.0'
gem 'sqlite3'
gem "dry-validation", "~> 1.4"
gem "pry"
end
require 'rom'
require 'rom-sql'
require 'dry-validation'
require 'logger'
config = ROM::Configuration.new(:sql, 'sqlite::memory')
config.gateways[:default].use_logger(Logger.new($stdout))
conn = config.gateways[:default].connection
conn.create_table(:users) do
primary_key :id
column :name, String
end
config.relation(:users) do
schema(infer: true)
end
$rom = ROM.container(config)
class Schema < Dry::Validation::Contract
option :id
params do
required(:name).filled
end
rule(:name) do
key.failure("must be unique") if $rom.relations[:users].where(name: value).where{ id.not(id) }.exist?
end
end
validation = Schema.new(id: 1).call({name: "foo"})
validation.success?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment