Fork me on GitHub
#malli
<
2021-10-22
>
ikitommi09:10:15

@hello525 sure, you could 1. put a transformer into top-level schema using schema properties as do update-in to the value 2. m/walk the schema and add the paths as schema properties to each schema. With this + :compile in transformer, you can select the schemas that need to changed at transformer creation time. There is malli.util.subschemas which could be looked as example imp how to find paths for each (nested) schema element

👍 1
martinklepsch11:10:04

Is there a function like validate that will throw instead of returning a value?

ikitommi12:10:37

at the moment, no. If that would he added, there would be a reason to add throwing validator, explain, explainer, parse, parser, transform, transformer too

martinklepsch13:10:02

Cool, it’s fine really, just wanted to check 🙂

Karol Wójcik14:10:39

However you can create such function yourself 😄

Karol Wójcik14:10:50

Something like:

(defn- explain!
     [aschema x fn-name]
     (when-let [info (m/explain aschema x)]
       (when (or cfg/BROWSER? cfg/TEST_ENV?)
         (throw (ex-info "[Malli] Schema does not match the argument"
                         {:type :malli-error
                          :x (form->pprint-str x 3)
                          :fn-name fn-name
                          :info (form->pprint-str (me/humanize info) 3)
                          :schema-name (schema-name aschema)
                          :schema-doc (schema-doc aschema)
                          :schema (schema->form-pprint-str (schema-form aschema))})))))

ikitommi14:10:05

some perf numbers 0.6.1 vs 0.7.0 (unreleased):

(def ?schema
  [:map
   [:x boolean?]
   [:y {:optional true} int?]
   [:z [:map
        [:x boolean?]
        [:y {:optional true} int?]]]])

;; 44µs -> 2.9µs (15x)
(p/bench (m/schema ?schema))

;; 240ns (180x, parses entries when actually needed)
(p/bench (m/schema ?schema {::m/lazy-entries true}))

(def schema (m/schema ?schema))

;; 1.7µs -> 64ns (25x)
(p/bench (m/validate schema {:x true, :z {:x true}}))

🔥 8
🏎️ 4