Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looser params #2

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source "https://rubygems.org"
# Specify your gem's dependencies in scheemer.gemspec
gemspec

gem "pry-nav"
gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ PATH
specs:
scheemer (3.0.0)
dry-schema (~> 1.13)
json-schema (~> 4.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
coderay (1.1.3)
concurrent-ruby (1.2.2)
diff-lcs (1.5.0)
dry-configurable (1.0.1)
Expand Down Expand Up @@ -37,9 +41,18 @@ GEM
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
json (2.6.3)
json-schema (4.0.0)
addressable (>= 2.8)
method_source (1.0.0)
parallel (1.23.0)
parser (3.2.2.1)
ast (~> 2.4.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-nav (1.0.0)
pry (>= 0.9.10, < 0.15)
public_suffix (5.0.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.0)
Expand Down Expand Up @@ -79,6 +92,7 @@ PLATFORMS
x86_64-linux-musl

DEPENDENCIES
pry-nav
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)
Expand Down
5 changes: 5 additions & 0 deletions lib/scheemer/schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "dry-schema"
require "json-schema"

Dry::Schema.load_extensions(:hints, :json_schema)

Expand Down Expand Up @@ -40,6 +41,8 @@ def check_schema_exists!

def initialize(&)
@definitions = ::Dry::Schema.Params do
config.validate_keys = false

instance_eval(&)
end
end
Expand All @@ -49,6 +52,8 @@ def validate(params)
end

def validate!(params)
return params if JSON::Validator.validate(json_schema, params)

validate(params).tap do |result|
next if result.success?

Expand Down
1 change: 1 addition & 0 deletions scheemer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|

# Uncomment to register a new dependency of your gem
spec.add_dependency "dry-schema", "~> 1.13"
spec.add_dependency "json-schema", "~> 4.0"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
4 changes: 2 additions & 2 deletions spec/scheemer/params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

describe ".on_missing" do
context "with a single level key" do
context "with a shallow key" do
let(:klass) do
Class.new do
extend Scheemer::Params::DSL
Expand All @@ -35,7 +35,7 @@

subject(:record) { klass.new({ someValue: "testing" }) }

it "allows access to fields using underscored accessors" do
it "fills in the missing missing" do
expect(record.content).to eql({ fall: "back" })
expect(record.someValue).to eql("testing")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/scheemer/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe ".validate!" do
subject(:schema) do
described_class.new do
required(:test)
required(:test).filled(:string)
end
end

Expand Down
40 changes: 39 additions & 1 deletion spec/scheemer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
end
end

subject(:record) { klass.new({ root: { someValue: "testing" } }) }
subject(:record) do
klass.new({ root: { someValue: "testing" } })
end

it "allows access to fields using underscored accessors" do
expect(record.some_value).to eql("testing")
Expand Down Expand Up @@ -58,5 +60,41 @@

it { expect { klass.new({}) }.to raise_error(NotImplementedError) }
end

context "when the extra fields are specified" do
let(:klass) do
Class.new do
extend Scheemer::DSL

schema do
required(:item).hash do
required(:content).hash do
required(:name).filled(:string)
optional(:address).filled(:string)
end
end
end
end
end
let(:data) do
{
item: {
content: {
name: "John",
age: "9999",
address: "John's Street 69"
}
}
}
end

subject(:record) { klass.new(data) }

it "it allows all fields through" do
expect(record.content.dig(:name)).to eql "John"
expect(record.content.dig(:age)).to eql "9999"
expect(record.content.dig(:address)).to eql "John's Street 69"
end
end
end
end