Liking cljdoc? Tell your friends :D

conjtest-clj

Validate data structures against policies.

Conjtest-clj is a Clojure/babashka library for validating parsed data structures against policies, which are just Clojure functions or malli schemas.

Part of the conjtest project.

Project status

Clojars Project Slack cljdoc badge bb compatible

Project is active and in alpha.

Check CHANGELOG.md for any breaking changes.

Usage

API documentation

clj -Sdeps '{:deps {org.conjtest/conjtest-clj {:mvn/version "0.4.0"} org.flatland/ordered {:mvn/version "1.15.12"}}}'
(require '[conjtest.core :as conjtest]
         '[flatland.ordered.map :refer [ordered-map]])

(set! *data-readers* {'ordered/map #'flatland.ordered.map/ordered-map})

(def parsed-yaml
  {"test-resources/test.yaml" #ordered/map([:apiVersion "v1"]
                                           [:kind "Service"]
                                           [:metadata #ordered/map([:name "hello-kubernetes"])]
                                           [:spec
                                            #ordered/map([:type "LoadBalancer"]
                                                         [:ports (#ordered/map([:port 9999] [:targetPort 8080]))]
                                                         [:selector #ordered/map([:app "hello-kubernetes"])])])})

(defn deny-allow-only-port-80
  [input]
  (and (= "v1" (:apiVersion input))
       (= "Service" (:kind input))
       (not= 80 (-> input :spec :ports first :port))))

(conjtest/test! parsed-yaml deny-allow-only-port-80)
;; Execution error (ExceptionInfo) at conjtest.core/test-with-opts! (core.clj:284).
;; FAIL - test-resources/test.yaml - :conjtest/rule-validation-failed
;;
;; 1 tests, 0 passed, 0 warnings, 1 failures

Instead of a map, where the keys should denote the parsed data structure's location on the file system, you may instead pass conjtest/test! an anonymous collection of inputs using a vector.

(require '[conjtest.core :as conjtest])

(defn deny-integers-only
  [input]
  (when-not (every? integer? (get-in input [:foo :bar]))
    "all entries in :foo :bar should be integers"))

(conjtest/test! [{:foo {:bar [1 "2" 3]}}] deny-integers-only)
;; Execution error (ExceptionInfo) at conjtest.core/test-with-opts! (core.clj:284).
;; FAIL - null - all entries in :foo :bar should be integers
;; 
;; 1 tests, 0 passed, 0 warnings, 1 failures

(conjtest/test! [{:foo {:bar [1 2 3]}}] deny-integers-only)
;; => {:summary {:total 1, :passed 1, :warnings 0, :failures 0}, :summary-report "1 tests, 1 passed, 0 warnings, 0 failures\n", :result ({:message nil, :name nil, :rule-type :deny, :failure? false})}

See tests for more usage examples.

For documentation on policies, see Usage chapter from Conjtest book.

For how to integrate with Babashka and ilmoraunio/conjtest using Babashka tasks, see example.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close