Skip to content

Commit

Permalink
Switch marks selected field as checked; included_in accepts array ins… (
Browse files Browse the repository at this point in the history
#14)

* Switch marks selected field as checked
* included_in accepts array instead of varargs
  • Loading branch information
EugZol committed Oct 9, 2023
1 parent 81ee396 commit 8043b88
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ It is currently used in production in several projects (mainly as request parame
- [`try(error_key = nil, catched_exception:) { |value| ... }`](#tryerror_key--nil-catched_exception--value--)
- [`validate(active_model_validations, name = 'Anonymous')`](#validateactive_model_validations-name--anonymous)
- [`compare(reference_value, error_key = nil)`](#comparereference_value-error_key--nil)
- [`included_in(*reference_values, error_key: nil)`](#included_inreference_values-error_key-nil)
- [`included_in(reference_values, error_key: nil)`](#included_inreference_values-error_key-nil)
- [`relate(left, op, right, error_key: nil)`](#relateleft-op-right-error_key-nil)
- [`run { |value| ... }`](#run--value--)
- [`transform { |value| ... }`](#transform--value--)
Expand Down Expand Up @@ -988,7 +988,7 @@ agreed_with_tos =

I18n keys: `error_key`, `'.compare'`, `'datacaster.errors.compare'`. Adds `reference` i18n variable, setting it to `reference_value.to_s`.

#### `included_in(*reference_values, error_key: nil)`
#### `included_in(reference_values, error_key: nil)`

Returns ValidResult if and only if `reference_values.include?` the value.

Expand Down
2 changes: 1 addition & 1 deletion lib/datacaster/predefined.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def hash_with_symbolized_keys(error_key = nil)
hash_value(error_key) & transform { |x| x.symbolize_keys }
end

def included_in(*values, error_key: nil)
def included_in(values, error_key: nil)
error_keys = ['.included_in', 'datacaster.errors.included_in']
error_keys.unshift(error_key) if error_key
check { |x| values.include?(x) }.i18n_key(*error_keys, reference: values.map(&:to_s).join(', '))
Expand Down
3 changes: 2 additions & 1 deletion lib/datacaster/switch_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ def initialize(base = nil, on_casters = [], else_caster = nil)
@base = base

if Datacaster::Utils.pickable?(@base)
@base = Datacaster::Predefined.pick(@base)
@base = Datacaster::Predefined.run { checked_key!(base) } &
Datacaster::Predefined.pick(base)
end

if !@base.nil? && !Datacaster.instance?(@base)
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.0"
VERSION = "3.2.1"
end
2 changes: 1 addition & 1 deletion spec/datacaster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
end

describe "included_in typecasting" do
subject { described_class.schema { included_in(1, '2', ['test']) } }
subject { described_class.schema { included_in([1, '2', ['test']]) } }

it "passes one of the exact values and fails on everything else" do
expect(subject.(1).to_dry_result).to eq Success(1)
Expand Down
10 changes: 10 additions & 0 deletions spec/switch_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,15 @@

expect(caster.(3).to_dry_result).to eq Failure(["is invalid"])
end

it 'marks matched-on value as checked' do
caster =
Datacaster.schema do
switch(:kind).
on(1, hash_schema(name: string))
end

expect(caster.(kind: 1, name: '1').to_dry_result).to eq Success(kind: 1, name: '1')
end
end
end

0 comments on commit 8043b88

Please sign in to comment.