Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Created October 2, 2019 13:47
Show Gist options
  • Save eraserhd/fd368792488cbfe4762c6223957abef6 to your computer and use it in GitHub Desktop.
Save eraserhd/fd368792488cbfe4762c6223957abef6 to your computer and use it in GitHub Desktop.
Finding the minimum array or string dimensions required by a JSON schema
(defn- minimum-required-dimensions
"Find a list of minimum required dimensions for the schema.
e.g. An array of array of strings might be '(2 0 3), meaning the string must
have length three, there can be zero or more strings in an array, and there
must be three or more arrays of strings in the array of arrays of strings.
To simplify logic, we keep a dimension for the innermost type, even if it is
an atomic type and doesn't make sense."
[schema]
(reduce
(fn [as bs]
(let [length (max (count as) (count bs))
as (concat as (repeat (- length (count as)) 0))
bs (concat bs (repeat (- length (count bs)) 0))]
(map max as bs)))
(list)
(m/search schema
{"minLength" (m/some ?n)} (list ?n)
{"type" "array"} (list 0 0)
{"items" (m/some (m/cata ?dims))} (cons 0 ?dims)
{"allOf" (m/scan (m/cata ?dims))} ?dims
_ (list 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment