Dynamic data validation using registered component specs.
Components can register a spec against a keyword:
(ns example.core
(:require [dpsec.core :as dspec]))
(s/def ::string string?)
(defn -main [& args]
(dspec/register :a ::string)
(dspec/register :b ::string))
Data can then be validated against the registered component specs:
(dspec/validate {:a "foo" :b "bar"})
If data fails to conform to the registered component specs a RuntimeException
will be thrown, with ex-data that contains the map of s/explain-data, keyed by the given component keyword.
Using a component library such as component or mount:
(defstate a :start (dspec/register :a ::string))
(defstate b :start (dspec/register :b ::string))
(defstate config :start (dspec/validate data))
A utility function load-config
exists to load and parse a yaml configuration file:
(config/load-config "resources/Service.yml")
This can be validated against any registered configuration specs:
(dspec/validate (config/load-config "resources/Service.yml"))
A service using mount to manage component state can be found in the simple example app. On service start, the configuration is validated against the specs defined for the component dependencies of the service.
Copyright © 2017 Brydon Cheyney