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

Not possible to m/encode value from mt/default-value-transformer #576

Closed
mpereira opened this issue Nov 20, 2021 · 2 comments · Fixed by #577
Closed

Not possible to m/encode value from mt/default-value-transformer #576

mpereira opened this issue Nov 20, 2021 · 2 comments · Fixed by #577
Labels
enhancement New feature or request

Comments

@mpereira
Copy link

mpereira commented Nov 20, 2021

Hi @ikitommi,

First of all, thanks for Malli, it's such a great library. It quickly became the "Org mode of data specification libraries" for me.

I wanted to use it for generating empty values from schemas, based on :defaults passed to mt/default-value-transformer and noticed that it doesn't work as I'd expect. I created this test to illustrate it:

(deftest decoding-from-default-transformers
  (is (= {:age nil
          :name ""
          :weight nil
          :address {:street ""
                    :lonlat nil}}
         (m/decode
          [:map
           [:age int?]
           [:name string?]
           [:weight double?]
           [:address [:map
                      [:street string?]
                      [:lonlat [:tuple double? double?]]]]]
          nil
          (mt/default-value-transformer
           {:defaults {:map (constantly {})
                       :tuple (constantly nil)
                       string? (constantly "")
                       int? (constantly nil)
                       double? (constantly nil)}})))))

Running it results in:

expected: {:age nil, :name , :weight nil, :address {:street , :lonlat nil}}

  actual: {:address {}}          
    diff: - {:address {:street "", :lonlat nil}, :age nil, :name "", :weight nil}          
          + {:address nil}            

Before I go ahead and try to fix it I wanted to see if you think that this is a fair expectation of how m/encodeing a value with these default value transformers should work. I noticed that #169 touches on exactly the thing I want to do with :defaults, but it doesn't seem to work as I'd expect. Maybe I'm doing something incorrectly here (or have the wrong expectations), so feel free to show me the right way as well.

@ikitommi
Copy link
Member

Hi, and thanks!

3 things here:

  1. you need to use the m/type of the schemas, for pre-mapped functions, it's the symbol form, e.g. 'double?
  2. mt/default-value-transformer didn't accept nil as a value, fixed in allow nil in default-value-transformer #577
  3. there was a bug in m/-tuple-transformer too, fixed that too
(testing "nil as default value, #576"
  (is (= {:age nil
          :name ""
          :weight nil
          :address {:street ""
                    :lonlat nil}}
         (m/decode
           [:map
            [:age int?]
            [:name string?]
            [:weight double?]
            [:address [:map
                       [:street string?]
                       [:lonlat [:tuple double? double?]]]]]
           nil
           (mt/default-value-transformer
             {:defaults {:map (constantly {})
                         :tuple (constantly nil)
                         'string? (constantly "")
                         'int? (constantly nil)
                         'double? (constantly nil)}})))))

@ikitommi ikitommi added the enhancement New feature or request label Nov 21, 2021
@mpereira
Copy link
Author

Hey @ikitommi,

  1. you need to use the m/type of the schemas, for pre-mapped functions, it's the symbol form, e.g. 'double?

I didn't realize that, thanks for letting me know.

And thanks for fixing the other two things so quickly. I'm already using the newest Malli with the improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants