Bisql can attach :malli/in and :malli/out metadata to generated CRUD queries.
That metadata can then be used for runtime validation.
gen-crud now generates:
schema.clj files:malli/in and :malli/out declarations inside generated CRUD SQLFor example:
/*:name crud.get-by-id */
/*:cardinality :one */
/*:malli/in [:map {:closed true} [:id int?]] */
/*:malli/out [:maybe sql.postgresql.public.users.schema/row] */
SELECT *
FROM users
WHERE id = /*$id*/1
Generated map schemas are closed, so unexpected keys are rejected.
Bisql validates through the execution path in the :next-jdbc adapter.
Validation is controlled by bisql.validation/*bisql-malli-validation-mode*.
Supported modes:
:off:when-present:strictYou can also control input and output separately:
{:in :when-present
:out :off}
For applications, it is usually better to set this once at startup:
(require '[bisql.core :as bisql])
(bisql/set-malli-validation-mode! {:in :when-present
:out :when-present})
For temporary checks in a REPL or test, use binding:
(require '[bisql.validation :as validation])
(binding [validation/*bisql-malli-validation-mode* :strict]
...)
(ns sql
(:require [bisql.core :as bisql]
[next.jdbc :as jdbc]))
(def ds
(jdbc/get-datasource
{:dbtype "postgresql"
:host "localhost"
:port 5432
:dbname "bisql_dev"
:user "bisql"
:password "bisql"}))
(bisql/defquery)
(bisql/set-malli-validation-mode! {:in :when-present
:out :when-present})
(sql.postgresql.public.users.crud/get-by-id ds {:id 1})
Unexpected input keys are rejected:
(sql.postgresql.public.users.crud/get-by-id ds {:id 1 :foo 1})
This fails with a message like:
Malli input validation failed for crud.get-by-id. {:foo ["disallowed key"]}
Generated CRUD gets Malli metadata automatically.
Hand-written SQL does not.
If you want runtime validation there too, add :malli/in and :malli/out
declarations yourself.
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |