Fork me on GitHub
#malli
<
2023-05-14
>
aaron5115:05:04

Can I use a single malli schema to check both a map and the schema of a json-encoded string within it?

aaron5123:05:51

https://github.com/metosin/malli#to-and-from-json I think this is close — I’m trying to validate a reitit param that’s JSON encoded without decoding it first

aaron5121:05:35

I asked chatgpt 😂 and it suggested either a simple-schema or a transformer for this purpose. Any thoughts? I’d want all errors in the json schema to bubble up. What I want to write (in reitit+malli) is: {:get {:parameters {:query {:q [:json [:map [:value [:and int? pos?]]]]}}}}

aaron5121:05:31

An example URL might be ”/someurl?q=%7B%22x%22:123456789%7D" (That’s a json map in the query param)

p-himik23:05:14

How would validating a serialized data structure work without deserializing it first?

aaron5100:05:47

I imagined that I could write an extension/schema/transformer that would deserialize it when it sees the :json keyword. Maybe I should be looking at coercers? I just don’t know where to extend malli to do this

opqdonut06:05:01

if you want to get the errors in the validation phase, coercers/decoders are the wrong way to go. they don't signal errors

opqdonut06:05:53

it should be relatively straightforward to write a schema that does something like....

(reify
 IntoSchema
 (-into-schema [parent properties children options]
  (reify Schema
    (-validator [_]
      (let [child-validator (-validator (first children))]
        (fn [val]
          (child-validator (json-decode val))))))))

🙏 2
opqdonut06:05:35

something like that could go into malli itself if you factor out the json-decode function. it could be called something like a lens-schema ...

aaron5121:05:35

I asked chatgpt 😂 and it suggested either a simple-schema or a transformer for this purpose. Any thoughts? I’d want all errors in the json schema to bubble up. What I want to write (in reitit+malli) is: {:get {:parameters {:query {:q [:json [:map [:value [:and int? pos?]]]]}}}}