Skip to content

Instantly share code, notes, and snippets.

@jcf
Created March 31, 2017 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcf/54af71b2191bdb8e1151366d03075ac0 to your computer and use it in GitHub Desktop.
Save jcf/54af71b2191bdb8e1151366d03075ac0 to your computer and use it in GitHub Desktop.
In a couple of minutes I've got a first version of validating pull syntax via clojure.spec?!
(s/def ::attr-name
qualified-keyword?)
(s/def ::recursion-limit
(s/or :num pos-int?
:str #{"..."}))
(s/def ::wildcard
(s/or :str #{"*"}
:sym #{'*}))
(s/def ::limit
(s/or :str #{"limit"}
:sym #{'limit}))
(s/def ::limit-expr
(s/cat :limit ::limit
:attr-name ::attr-name
:amount (s/or :num pos-int?
:nil nil?)))
(s/def ::default-expr
(s/cat :default (s/or :str #{"default"}
:sym #{'default})
:attr-name ::attr-name
:value any?))
(s/def ::attr-expr
(s/or :limit ::limit-expr
:default-expr ::default-expr))
(s/def ::map-spec
(s/map-of (s/or :attr-name ::attr-name
:limit-expr ::limit-expr)
(s/or :query ::query
:recursion-limit ::recursion-limit)))
(s/def ::attr-spec
(s/or :attr-name ::attr-name
:wildcard ::wildcard
:map-spec ::map-spec
:attr-expr ::attr-expr))
(s/def ::query
(s/+ ::attr-spec))
@jcf
Copy link
Author

jcf commented Mar 31, 2017

It's not production ready, but this took me just a few minutes to throw together. Wow.

user> (clojure.spec/conform ::query '[{:dash/items [:item/name :item/size]}])
[:query [[:map-spec #:dash{:items [:query [[:attr-name :item/name] [:attr-name :item/size]]]}]]]
user> (clojure.spec/conform ::query '[:item/name :item/size])
[:query [[:attr-name :item/name] [:attr-name :item/size]]]
user> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment