Fork me on GitHub
#malli
<
2023-02-14
>
Bingen Galartza Iparragirre13:02:34

Hello! I'm having some trouble to define a custom registry in Malli

(def custom-registry
  {::decimal [:double {:min 2}]})

(def registry
  (-> custom-registry
      (mr/registry)
      (mr/composite-registry m/default-registry)))

(m/validate [::decimal] 2.5 {:registry registry})
Running the above code throws the following exception: :malli.core/invalid-schema {:schema [:double {:min 2}]} What am I doing wrong? Thx in advance

dvingo14:02:16

It's a bit confusing but storing vector schemas in the registry and using vector lookup syntax ins't valid. I had the same thought and opened a PR to "fix" it https://github.com/metosin/malli/pull/848 but it's not a bug, it's how schemas work. You can get around it using either [:ref ::decimal] or [:schema ::decimal] Maybe the docs should be updated to have a note about it

delaguardo14:02:17

you registry is fine, the problem is in [::decimal] . This is invalid syntax for malli.

delaguardo14:02:38

(m/validate ::decimal 2.5 {:registry registry}) works just fine

dvingo14:02:17

it's confusing because [:int] is valid, but refs in vectors are not

Bingen Galartza Iparragirre14:02:55

You are right, just tested that (m/validate ::decimal 2.5 {:registry registry})` works. But what happens if I want to have a schema with children? This fails again:

(def custom-registry
  {::decimal [:double {:min 2}]
   ::object [:map]})

(def registry
  (-> custom-registry
      (mr/registry)
      (mr/composite-registry m/default-registry)))

(m/validate [::object [:item-1 ::decimal]] {:item-1 2.5} {:registry registry})

Bingen Galartza Iparragirre14:02:45

@U051V5LLP if I understood right the workaround you suggest doesn't work in this case, does it?

dvingo14:02:01

yea, looks like it won't work in that case, I'm not sure what's the reasoning behind why though

delaguardo14:02:47

> But what happens if I want to have a schema with children? @UK14W219B you have to create a schema that can accept parameters. Adding just [:map] into the registry doesn't allow your ref schema to have children. There is a way to add custom schema type - https://github.com/metosin/malli#custom-schema-types

delaguardo14:02:28

@U051V5LLP [:int] is valid because :int schema can accept parameters like :min/:max

Bingen Galartza Iparragirre15:02:46

Understood! Thank you both a lot

dvingo16:02:57

@U04V4KLKC it would be awesome, if you don't mind, to put together a little cheat-shee/table of what constitutes an invalid schema and when you have to create your own custom schema type vs just creating a ref schema. I think this would be a great addition to the readme :)

dvingo14:02:36

I ported the new experemental.time schemas to cljs https://github.com/metosin/malli/pull/853

👍 8
Ben Sless18:02:49

This is wonderful! I'm glad it took only a small amount of changes

dvingo19:02:10

yea! great work on the jvm version! Felt pretty magical changing a few clj files to cljc and a few imports and running the generators the first time in a cljs repl, ha