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

Fallback to use result of first branch when decoding :or and :orn #946

Merged
merged 7 commits into from
Sep 5, 2023

Conversation

ikitommi
Copy link
Member

@ikitommi ikitommi commented Sep 3, 2023

from #910:

I see. Here's one repro:

(m/decode [:int {:decode/math str}] 1 (mt/transformer {:name :math})) ; => "1"
(m/decode [:and [:int {:decode/math str}]] 1 (mt/transformer {:name :math})) ; => "1"
(m/decode [:or [:int {:decode/math str}]] 1 (mt/transformer {:name :math})) ; => 1

so, options being:

  1. do nothing: it's a feature
  2. use first branch in case none is valid after decode
  3. use first branch where value is changed in decode

Not sure what is the most correct, 1 or 2 here 🤔

this PR implements option 2.

This is a BREAKING change as the behavior changes. But, I think the current implementation is wrong and this is (more) correct.

src/malli/core.cljc Outdated Show resolved Hide resolved
(let [x* (transformer x)]
(if ((nth validators i) x*)
(reduced x*)
(or acc x*))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about when the first transformer returns a nil? Could that happen?

I'm thinking of something like (m/decode [:maybe :int] false false-to-nil-transformer) ==> nil

Or perhaps even just (m/decode [:or nil :int] nil trans)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it wouldn't matter, as [:or nil :int] with nil value succeeds and doesn't use the fallback but here's a case where is fails:

(m/decode
 [:or
  [:string {:decode/remove-secrets (constantly nil)}]
  :keyword]
 "SECRET!"
 (mt/transformer {:name :remove-secrets}))
; => "SECRET!"

Fixed it so that value of the first branch is always used as fallback, regardless of value.

Copy link
Member

@opqdonut opqdonut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change looks good, I like the tests. one question though.

@ikitommi ikitommi merged commit aa49c3e into master Sep 5, 2023
6 checks passed
@ikitommi ikitommi changed the title Fallback to use first branch result with :or and :orn when decoding. Fallback to use result of first branch when decoding :or and :orn Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants