Skip to content

Instantly share code, notes, and snippets.

@x2es
Last active December 13, 2023 19:16
Show Gist options
  • Save x2es/ed6d401bd02698f48e9bf927120e0eb5 to your computer and use it in GitHub Desktop.
Save x2es/ed6d401bd02698f48e9bf927120e0eb5 to your computer and use it in GitHub Desktop.
# https://github.com/dry-rb/dry-schema/issues/448
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem "dry-schema"
gem "awesome_print"
gem "minitest"
end
require 'minitest/autorun'
DocumentSchema = Dry::Schema.JSON do
optional(:date).filter(format?: /^\d{4}-\d{2}-\d{2}$/).filled(:date)
end
DocumentsSchema = Dry::Schema.JSON do
required(:documents).array(DocumentSchema)
end
module Sample
VALID_DOCUMENT = { date: "2023-01-01" }
INVALID_FORMAT = { date: "20239-01-01" }
INVALID_DATE = { date: "2023-38-01" }
FULL_LIST = {
documents: [
VALID_DOCUMENT, # 0
INVALID_FORMAT, # 1
INVALID_DATE # 2
]
}
FORMAT_LIST = {
documents: [
VALID_DOCUMENT, # 0
INVALID_FORMAT, # 1
]
}
DATE_LIST = {
documents: [
VALID_DOCUMENT, # 0
INVALID_DATE, # 1
]
}
end
class DateTest < Minitest::Test
def test_format
expected = {:date=>["is in invalid format"]}
assert_equal expected, DocumentSchema.call(Sample::INVALID_FORMAT).errors.to_h
end
def test_date
expected = {:date=>["must be a date"]}
assert_equal expected, DocumentSchema.call(Sample::INVALID_DATE).errors.to_h
end
# UNEXPECTED BEHAVIOUR
# and inconsistent with `DocumentSchema.call(Sample::INVALID_FORMAT)`
# 2) Failure:
# DateTest#test_format_list [dry_schema_array_hash_filter_issue.rb:68]:
# --- expected
# +++ actual
# @@ -1 +1 @@
# -{:documents=>{1=>{:date=>["is in invalid format"]}}}
# +{}
def test_format_list
expected = {:documents=>{1=>{:date=>["is in invalid format"]}}}
assert_equal expected, DocumentsSchema.call(Sample::FORMAT_LIST).errors.to_h
end
def test_date_list
expected = {:documents=>{1=>{:date=>["must be a date"]}}}
assert_equal expected, DocumentsSchema.call(Sample::DATE_LIST).errors.to_h
end
# 1) Failure:
# DateTest#test_full_list [dry_schema_array_hash_filter_issue.rb:83]:
# --- expected
# +++ actual
# @@ -1 +1 @@
# -{:documents=>{1=>{:date=>["is in invalid format"]}, 2=>{:date=>["must be a date"]}}}
# +{:documents=>{1=>{:date=>["must be a date"]}}}
def test_full_list
expected = {
:documents=>{
1=>{:date=>["is in invalid format"]},
2=>{:date=>["must be a date"]}
}
}
assert_equal(expected, DocumentsSchema.call(Sample::DATE_LIST).errors.to_h)
end
end
# {
# :full_list => #<Dry::Schema::Result{:documents=>[{:date=>#<Date: 2023-01-01 ((2459946j,0s,0n),+0s,2299161j)>}, # {:date=>#<Date: 20239-01-01 ((9113203j,0s,0n),+0s,2299161j)>}, {:date=>"2023-38-01"}]} errors={:documents=>{2=># {:date=>["must be a date"]}}} path=[]>,
# :format_list => #<Dry::Schema::Result{:documents=>[{:date=>#<Date: 2023-01-01 ((2459946j,0s,0n),+0s,2299161j)>}, {:date=>#<Date: 20239-01-01 ((9113203j,0s,0n),+0s,2299161j)>}]} errors={} path=# []>,
# :date_list => #<Dry::Schema::Result{:documents=>[{:date=>#<Date: 2023-01-01 ((2459946j,0s,0n),+0s,2299161j)>}, {:date=>"2023-38-01"}]} errors={:documents=>{1=>{:date=>["must be a date"]}}} path=[]>,
# :format => #<Dry::Schema::Result{:date=>"20239-01-01"} errors={:date=>["is in invalid format"]} path=[]>,
# :date => #<Dry::Schema::Result{:date=>"2023-38-01"} errors={:date=>["must be a date"]} path=[]>
# }
ap ({
full_list: DocumentsSchema.call(Sample::FULL_LIST),
format_list: DocumentsSchema.call(Sample::FORMAT_LIST),
date_list: DocumentsSchema.call(Sample::DATE_LIST),
format: DocumentSchema.call(Sample::INVALID_FORMAT),
date: DocumentSchema.call(Sample::INVALID_DATE)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment