Define validation schemas for validating maps.
Schemas are any number of paths through a nested map, paired with a validator.
Validators are either a single predicate or schema, or a seq of predicates or schems (or a mix of predicates or schemas).
A path may also be marked as an optional-path
. By default a value at a given
path is assumed to be a single value; to mark it sequential, wrap the validator
in the 'sequence-of' function. NOTE: 'nil' is a passing value for a 'sequence-of' validator.
Example Schema:
[[:a :b :c] pred [:x :y :z] [pred2 pred3 z-schema] ;; implicit 'and' - all three must pass [:p :q :r] [:or nil? r-schema] ;; an 'or' statement - need just one to pass (optional-path [:z]) (sequence-of string?) [:a b :d] (loose-valdiation-schema [[:cat :name] String ;; can use Java Class objects directly [:cat :color] String]) ... ]
defschema
creates a strict schema, which expects only the paths it
describes to be present on the given map.
def-loose-schema
creates a loose schema, which expects its paths to
be present but does not complain about extra paths.
Define validation schemas for validating maps. Schemas are any number of paths through a nested map, paired with a validator. Validators are either a single predicate or schema, or a seq of predicates or schems (or a mix of predicates or schemas). A path may also be marked as an `optional-path`. By default a value at a given path is assumed to be a single value; to mark it sequential, wrap the validator in the 'sequence-of' function. NOTE: 'nil' is a passing value for a 'sequence-of' validator. Example Schema: [[:a :b :c] pred [:x :y :z] [pred2 pred3 z-schema] ;; implicit 'and' - all three must pass [:p :q :r] [:or nil? r-schema] ;; an 'or' statement - need just one to pass (optional-path [:z]) (sequence-of string?) [:a b :d] (loose-valdiation-schema [[:cat :name] String ;; can use Java Class objects directly [:cat :color] String]) ... ] `defschema` creates a strict schema, which expects only the paths it describes to be present on the given map. `def-loose-schema` creates a loose schema, which expects its paths to be present but does not complain about extra paths.
(covered-by-wildcard-path? [path-first & path-rest :as path-to-check]
[wildcard-first & wildcard-rest :as wildcard-path])
(def-loose-schema name & schema-vectors)
Creates a named var for a loose schema that can be used within other schemas.
Creates a named var for a loose schema that can be used within other schemas.
(defschema name & schema-vectors)
Creates a named var for a strict schema that can be used within other schemas.
Creates a named var for a strict schema that can be used within other schemas.
(filter-schema pred schema)
Takes a pred like (fn [[path validator]] ...) and selects all schema rows that match.
Takes a pred like (fn [[path validator]] ...) and selects all schema rows that match.
(loose-schema & vs)
From a seq of vectors, creates a schema that can be used within other schemas. Checks for the presence of all paths; other paths may also exist.
From a seq of vectors, creates a schema that can be used within other schemas. Checks for the presence of all paths; other paths may also exist.
(ns->schemas the-ns)
All schemas in a namespace
All schemas in a namespace
(optional-path schema-path)
Takes a schema path and morphs it into a path that is optional. Optional paths may or may not be present on the validated map, but if they are present they must be valid against the given validator.
Takes a schema path and morphs it into a path that is optional. Optional paths may or may not be present on the validated map, but if they are present they must be valid against the given validator.
(select-schema-keys schema & ks)
Returns a new schema with only the paths starting with the specified keys.
Returns a new schema with only the paths starting with the specified keys.
(strict-schema & vs)
From a seq of vectors, creates a schema that can be used within other schemas. Any paths found in addition to the ones specified are considered a violation.
From a seq of vectors, creates a schema that can be used within other schemas. Any paths found in addition to the ones specified are considered a violation.
(subtract-paths schema & paths)
Returns a new schema minus some paths.
Returns a new schema minus some paths.
(subtract-wildcard-paths schema)
Returns a schema that is the same in all respects, except it has none of the wildcard paths.
Returns a schema that is the same in all respects, except it has none of the wildcard paths.
(validation-errors schema m)
(validation-errors parent-path schema m)
Returns a set of all the validation errors found when comparing a given map m, against the supplied schema.
A validator is either a schema, predicate, Class or vector of them. See this ns's :doc meta for more details.
Returns a set of all the validation errors found when comparing a given map m, against the supplied schema. A validator is either a schema, predicate, Class or vector of them. See this ns's :doc meta for more details.
(wild validator)
Upgrades a validator to be used within a path as a wildcard. Ex. [:a (wild Integer) (wild String)], matches all paths like [:a 1 "product-1"] or [:a 42 "product-2"]
Upgrades a validator to be used within a path as a wildcard. Ex. [:a (wild Integer) (wild String)], matches all paths like [:a 1 "product-1"] or [:a 42 "product-2"]
(wildcard-path->concrete-paths m [path-first & path-rest :as the-wildcard-path])
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close