Skip to content
Snippets Groups Projects
Commit 452e70e7 authored by Cam Saül's avatar Cam Saül
Browse files

Minor improvements to function that generates API dox from schemas

parent 3d8fb6eb
Branches
Tags
No related merge requests found
......@@ -30,12 +30,13 @@
"Special endpoint for creating the first user during setup.
This endpoint both creates the user AND logs them in and returns a session ID."
[:as {{:keys [token] {:keys [name engine details is_full_sync]} :database, {:keys [first_name last_name email password]} :user, {:keys [allow_tracking site_name]} :prefs} :body}]
{token SetupToken
site_name su/NonBlankString
first_name su/NonBlankString
last_name su/NonBlankString
email su/Email
password su/ComplexPassword}
{token SetupToken
site_name su/NonBlankString
first_name su/NonBlankString
last_name su/NonBlankString
email su/Email
password su/ComplexPassword
allow_tracking (s/maybe (s/cond-pre s/Bool su/BooleanString))}
;; Now create the user
(let [session-id (str (java.util.UUID/randomUUID))
new-user (db/insert! User
......@@ -49,18 +50,16 @@
;; set a couple preferences
(public-settings/site-name site_name)
(public-settings/admin-email email)
(public-settings/anon-tracking-enabled (if (m/boolean? allow_tracking)
allow_tracking
true)) ; default to `true` if allow_tracking isn't specified
(public-settings/anon-tracking-enabled (or (nil? allow_tracking) ; default to `true` if allow_tracking isn't specified
allow_tracking)) ; the setting will set itself correctly whether a boolean or boolean string is specified
;; setup database (if needed)
(when (driver/is-engine? engine)
(->> (db/insert! Database
:name name
:engine engine
:details details
:is_full_sync (if-not (nil? is_full_sync)
is_full_sync
true))
:is_full_sync (or (nil? is_full_sync) ; default to `true` is `is_full_sync` isn't specified
is_full_sync))
(events/publish-event! :database-create)))
;; clear the setup token now, it's no longer needed
(setup/clear-token!)
......
......@@ -2,6 +2,7 @@
"Various schemas that are useful throughout the app."
(:require [cheshire.core :as json]
[clojure.string :as str]
[medley.core :as m]
[metabase.util :as u]
[metabase.util.password :as password]
[schema.core :as s]))
......@@ -40,6 +41,12 @@
(when (instance? schema.core.EnumSchema schema)
(format "value must be one of: %s." (str/join ", " (for [v (sort (:vs schema))]
(str "`" v "`")))))
;; For cond-pre schemas we'll generate something like
;; value must satisfy one of the following requirements: 1) value must be a boolean. 2) value must be a valid boolean string ('true' or 'false').
(when (instance? schema.core.CondPre schema)
(str "value must satisfy one of the following requirements: "
(str/join " " (for [[i child-schema] (m/indexed (:schemas schema))]
(format "%d) %s" (inc i) (api-error-message child-schema))))))
;; do the same for sequences of a schema
(when (vector? schema)
(str "value must be an array." (when (= (count schema) 1)
......@@ -112,7 +119,7 @@
"Schema for a string that is a valid representation of a boolean (either `true` or `false`).
Something that adheres to this schema is guaranteed to to work with `Boolean/parseBoolean`."
(with-api-error-message (s/constrained s/Str boolean-string?)
"value must be a valid boolean (true or false)."))
"value must be a valid boolean string ('true' or 'false')."))
(def JSONString
"Schema for a string that is valid serialized JSON."
......
(ns metabase.util.schema-test
(:require [metabase.util.schema :as su]
[schema.core :as s]
[expectations :refer :all]))
;; check that the API error message generation is working as intended
(expect
"value may be nil, or if non-nil, value must satisfy one of the following requirements: 1) value must be a boolean. 2) value must be a valid boolean string ('true' or 'false')."
(su/api-error-message (s/maybe (s/cond-pre s/Bool su/BooleanString))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment