File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -655,6 +655,41 @@ only-in-encoded
655655
656656; ; # Custom Schemas
657657
658+ ; ; ## Case study - URL schema
659+
660+ ; ; We already have a URI schema, but let's assume we need one for a URL.
661+
662+ ; ; Instead of doing
663+
664+ [:and :string [:fn (fn [x] (try (java.net.URL. x) (catch Exception _ false )))]]
665+
666+ ; ; We can use the most common schema constructor, `-simple-schema`
667+
668+ (defn -url-schema
669+ []
670+ (m/-simple-schema
671+ {:type :url ,
672+ :pred (fn [x] (instance? java.net.URL x))}))
673+
674+ (m/validate (-url-schema ) (java.net.URL. " https://logseq.com" ))
675+
676+ ; ; What about strings, though? Add a transformer:
677+
678+
679+ (defn -url-schema
680+ []
681+ (m/-simple-schema
682+ {:type :url ,
683+ :pred (fn [x] (instance? java.net.URL x))
684+ :type-properties
685+ {:decode/string
686+ (fn [s]
687+ (when (string? s)
688+ (try (java.net.URL. s)
689+ (catch Exception _ s))))}}))
690+
691+ (m/coerce (-url-schema ) " https://logseq.com" mt/string-transformer)
692+
658693; ; # Recommendations and Best Practices (The Meta)
659694
660695; ; ## Names
You can’t perform that action at this time.
0 commit comments