Skip to content

Commit

Permalink
Schema cleaner fix (#15)
Browse files Browse the repository at this point in the history
* Schema cleaner fix
* Bumped gem version
  • Loading branch information
EugZol committed Oct 9, 2023
1 parent 8043b88 commit 694c939
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/datacaster/context_nodes/structure_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def create_runtime(parent)
def transform_result(result)
return result unless result.valid?
result = cast_success(result)
if @runtime.instance_variable_get(:@parent).respond_to?(:checked_schema=)
@runtime.instance_variable_get(:@parent).checked_schema = @runtime.checked_schema
parent_runtime = @runtime.instance_variable_get(:@parent)
if parent_runtime.respond_to?(:checked_schema!)
parent_runtime.checked_schema!(@runtime.checked_schema)
end
result
end
Expand Down
4 changes: 4 additions & 0 deletions lib/datacaster/runtimes/structure_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def will_check!
@should_check_stack[-1] = true
end

def checked_schema!(schema)
@pointer_stack[-1].merge!(schema)
end

def ignore_checks!(&block)
@ignore = true
result = yield
Expand Down
2 changes: 1 addition & 1 deletion lib/datacaster/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Datacaster
VERSION = "3.2.1"
VERSION = "3.2.2"
end
26 changes: 24 additions & 2 deletions spec/datacaster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1542,12 +1542,34 @@ def amount
describe "cleaning nested schemas" do
it "doesn't complain on keys checked in nested contex node" do
schema = Datacaster.schema do
Datacaster.schema do
schema(
hash_schema(a: integer, b: integer)
end & hash_schema(b: integer)
) & hash_schema(b: integer)
end

expect(schema.(a: 1, b: 2).to_dry_result).to eq Success(a: 1, b: 2)
end

it "doesn't complain on nested keys in nested context node" do
caster = Datacaster.schema do
nested = schema(
hash_schema(a: integer, b: integer)
)

hash_schema(nested: nested)
end

expect(caster.(nested: {a: 1, b: 2}).to_dry_result).to eq Success(nested: {a: 1, b: 2})
end

it "doesn't complain on keys in deeply nested strict schemas" do
schema = Datacaster.schema do
hash_schema(
test: schema(hash_schema(a: integer, b: integer))
)
end

expect(schema.(test: {a: 1, b: 2}).to_dry_result).to eq Success(test: {a: 1, b: 2})
end
end
end

0 comments on commit 694c939

Please sign in to comment.