Skip to content

Commit

Permalink
Merge branch 'main' into release-1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Sep 13, 2022
2 parents 342cf08 + c0faeb3 commit 0df19c0
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 32 deletions.
22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
<!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->

## 1.10.4 2022-10-13


### Fixed

- Once again reverting zeitwerk related changes that were included in 1.10.3 by an accident :( (@solnic)


[Compare v1.10.3...v1.10.4](https://github.com/dry-rb/dry-schema/compare/v1.10.3...v1.10.4)

## 1.10.3 2022-10-10


### Fixed

- Addressed regressions causing issues with handling sum types (see #419 and #423 fixed via #425) (@robhanlon22)


[Compare v1.10.2...v1.10.3](https://github.com/dry-rb/dry-schema/compare/v1.10.2...v1.10.3)

## 1.10.2 2022-08-23


Expand Down Expand Up @@ -204,7 +224,7 @@ This release ships with a bunch of internal refactorings that should improve per

[Compare v1.5.4...v1.5.5](https://github.com/dry-rb/dry-schema/compare/v1.5.4...v1.5.5)

## 1.5.4
## 1.5.4

2020-09-03

Expand Down
14 changes: 12 additions & 2 deletions changelog.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
---
- version: 1.10.4
date: '2022-10-13'
fixed:
- 'Once again reverting zeitwerk related changes that were included in 1.10.3 by an accident :( (@solnic)'
- version: 1.10.3
summary:
date: '2022-10-10'
fixed:
- 'Addressed regressions causing issues with handling sum types (see #419 and #423 fixed via #425) (@robhanlon22)'
added:
changed:
- version: 1.10.2
summary:
date: '2022-08-23'
Expand All @@ -9,8 +20,7 @@
- version: 1.10.1
date: '2022-08-22'
changed:
- Reverted zeitwerk-related changes that were included in 1.10.0 by an accident
(@solnic)
- Reverted zeitwerk-related changes that were included in 1.10.0 by an accident (@solnic)
- version: 1.10.0
summary:
date: '2022-08-16'
Expand Down
2 changes: 1 addition & 1 deletion dry-schema.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "dry-configurable", "~> 0.13", ">= 0.13.0"
spec.add_runtime_dependency "dry-core", "~> 0.5", ">= 0.5"
spec.add_runtime_dependency "dry-initializer", "~> 3.0"
spec.add_runtime_dependency "dry-logic", "~> 1.0"
spec.add_runtime_dependency "dry-logic", "~> 1.2"
spec.add_runtime_dependency "dry-types", "~> 1.5"

spec.add_development_dependency "bundler"
Expand Down
18 changes: 18 additions & 0 deletions lib/dry/schema/macros/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ def filled(*args, **opts, &block)
end
end

# Set type specification and predicates for a maybe value
#
# @example
# required(:name).maybe(:string)
#
# @see Macros::Key#value
#
# @return [Macros::Key]
#
# @api public
def maybe(*args, **opts, &block)
extract_type_spec(args, nullable: true) do |*predicates, type_spec:, type_rule:|
append_macro(Macros::Maybe) do |macro|
macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
end
end
end

# Specify a nested hash without enforced `hash?` type-check
#
# This is a simpler building block than `hash` macro, use it
Expand Down
15 changes: 9 additions & 6 deletions lib/dry/schema/macros/filled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ class Filled < Value
def call(*predicates, **opts, &block)
ensure_valid_predicates(predicates)

if opts[:type_spec] && !filter_empty_string?
value(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts, &block)
elsif opts[:type_rule]
value(:filled?).value(*predicates, **opts, &block)
else
value(:filled?, *predicates, **opts, &block)
append_macro(Macros::Value) do |macro|
if opts[:type_spec] && !filter_empty_string?
macro.call(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts,
&block)
elsif opts[:type_rule]
macro.call(:filled?).value(*predicates, **opts, &block)
else
macro.call(:filled?, *predicates, **opts, &block)
end
end
end

Expand Down
18 changes: 0 additions & 18 deletions lib/dry/schema/macros/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,6 @@ def filter(...)
end
ruby2_keywords(:filter) if respond_to?(:ruby2_keywords, true)

# Set type specification and predicates for a maybe value
#
# @example
# required(:name).maybe(:string)
#
# @see Macros::Key#value
#
# @return [Macros::Key]
#
# @api public
def maybe(*args, **opts, &block)
extract_type_spec(args, nullable: true) do |*predicates, type_spec:, type_rule:|
append_macro(Macros::Maybe) do |macro|
macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block)
end
end
end

# Coerce macro to a rule
#
# @return [Dry::Logic::Rule]
Expand Down
4 changes: 3 additions & 1 deletion lib/dry/schema/macros/maybe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def call(*args, **opts, &block)
raise ::Dry::Schema::InvalidSchemaError, "Using maybe with nil? predicate is redundant"
end

value(*args, **opts, &block)
append_macro(Macros::Value) do |macro|
macro.call(*args, **opts, &block)
end

self
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/schema/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Dry
module Schema
VERSION = "1.10.2"
VERSION = "1.10.4"
end
end
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ gemspec:
- [dry-configurable, "~> 0.13", ">= 0.13.0"]
- [dry-core, "~> 0.5", ">= 0.5"]
- [dry-initializer, "~> 3.0"]
- [dry-logic, "~> 1.0"]
- [dry-logic, "~> 1.2"]
- [dry-types, "~> 1.5"]
82 changes: 82 additions & 0 deletions spec/integration/schema/custom_types_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ class Test::CustomTypeSchema < Dry::Schema::Params
expect(result[:age]).to eql("i am not that old")
end
end

context "maybe decimal" do
let(:klass) do
class Test::CustomTypeSchema < Dry::Schema::JSON
define do
required(:number).maybe(Types::JSON::Decimal | Types::Params::Nil)
end
end
end

let(:params) do
{number: "19.3"}
end

it "coerces the type" do
expect(result[:number]).to eql(BigDecimal("19.3"))
end
end

context "filled string" do
let(:klass) do
class Test::CustomTypeSchema < Dry::Schema::JSON
define do
required(:string).filled(
Types::Strict::String.constrained(format: /foo/) |
Types::Strict::String.constrained(format: /bar/)
)
end
end
end

let(:params) do
{string: "foo"}
end

it "coerces the type" do
expect(result[:string]).to eql("foo")
end
end
end

context "DSL-based definition" do
Expand Down Expand Up @@ -123,6 +162,49 @@ class Test::CustomTypeSchema < Dry::Schema::Params
expect(result[:user][:age]).to eql("i am not that old")
end
end

context "custom constructor" do
subject(:schema) do
Dry::Schema.Params do
config.types = ContainerWithTypes
optional(:date).maybe(:calendar_day)
end
end

let(:calendar_date) do
Class.new(::Date) do
def to_json(*args)
strftime("--%m-%d").to_json(*args)
end

def self.parse(date)
mon, mday = Date._iso8601(date).values_at(:mon, :mday)
raise ArgumentError, "invalid ISO8601 calendar day string, expected format \"--MM-DD\"" unless mon && mday

new(2000, mon, mday)
end
end
end

before do
stub_const("CalendarDate", calendar_date)

container_with_types.register(
:calendar_day,
Types::Strict(CalendarDate).constructor(CalendarDate.method(:parse))
)
end

let(:params) do
{"date" => "--02-09"}
end

specify do
expect(result).to be_success
expect(result[:date]).to be_a(CalendarDate)
expect(result[:date].to_json).to eq('"--02-09"')
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/schema/macros/filled_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
Dry::Schema.define do
required(:foo).filled(:array).each do
filled(:array).each do
filled(:string)
filled(Types::Strict::String)
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/integration/schema/macros/maybe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,29 @@
expect(schema.(users: [{name: 1}]).errors.to_h).to eq(users: {0 => {name: ["must be a string"]}})
end
end

describe "nested into further DSLs" do
subject(:schema) do
Dry::Schema.define do
required(:foo).maybe(:array).each do
maybe(:array).each do
maybe(Types::Strict::String)
end
end
end
end

it "passes when valid" do
expect(schema.call(foo: [["bar"]])).to be_success
expect(schema.call(foo: [])).to be_success
expect(schema.call(foo: [nil])).to be_success
expect(schema.call(foo: [[nil]])).to be_success
end

it "fails when invalid" do
expect(schema.call(foo: 1).messages).to eql(foo: ["must be an array"])
expect(schema.call(foo: [1]).messages).to eql(foo: {0 => ["must be an array"]})
expect(schema.call(foo: [[1]]).messages).to eql(foo: {0 => {0 => ["must be a string"]}})
end
end
end

0 comments on commit 0df19c0

Please sign in to comment.