From 4ba0d397e9fd3944da618a73e32db4a51a409a80 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 27 Mar 2018 15:01:45 +0200 Subject: [PATCH] move specs to domain namespace --- src/wanijo/domain/schema.clj | 12 +++++++++++- src/wanijo/forms.clj | 11 +++++++++++ src/wanijo/schema/routes.clj | 8 ++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/wanijo/domain/schema.clj b/src/wanijo/domain/schema.clj index 89fe948..1e67df4 100644 --- a/src/wanijo/domain/schema.clj +++ b/src/wanijo/domain/schema.clj @@ -1,7 +1,17 @@ (ns wanijo.domain.schema (:require [clojurewerkz.neocons.rest.cypher :as cypher] + [clojure.spec.alpha :as spec] [wanijo.neo4j :as neo4j])) +(spec/def ::name + (spec/and string? not-empty)) + +(spec/def ::created-at + (spec/or :int (spec/and int? + #(> % 20000101000000)) + :str (spec/and string? + #(re-matches #"\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}" %)))) + (defn all-created-by! [ident] (->> (cypher/tquery @@ -9,6 +19,6 @@ "MATCH (s:schema)-[created_by]->(u:user) WHERE u.ident = {ident} RETURN s - ORDER BY s.created_at" + ORDER BY s.name" {:ident ident}) (map #(:data (get % "s"))))) diff --git a/src/wanijo/forms.clj b/src/wanijo/forms.clj index d88d23c..f73d619 100644 --- a/src/wanijo/forms.clj +++ b/src/wanijo/forms.clj @@ -28,3 +28,14 @@ (hform/text-field {:required (when required "required")} field (get-in req [:params field]))))) + +(defn valid? [req form-def] + (reduce-kv + (fn [result field value] + (if (spec/valid? + (get-in form-def [:fields field :spec]) + value) + true + (reduced false))) + true + (:params req))) diff --git a/src/wanijo/schema/routes.clj b/src/wanijo/schema/routes.clj index b05b34e..988dc36 100644 --- a/src/wanijo/schema/routes.clj +++ b/src/wanijo/schema/routes.clj @@ -3,18 +3,14 @@ [ring.util.anti-forgery :refer [anti-forgery-field]] [ring.util.response :as resp] [hiccup.form :as hform] - [clojure.spec.alpha :as spec] [wanijo.domain.schema :as domain-schemas] [wanijo.view :as view] [wanijo.forms :as forms])) -(spec/def ::schema-name - (spec/and some? string? not-empty)) - (def new-schema-form {:fields {:schema-name {:label "Nimi" :required true - :spec ::schema-name}}}) + :spec :wanijo.domain.schema/name}}}) (defn- overview! [req] (let [session (:session req) @@ -42,7 +38,7 @@ (anti-forgery-field))]))) (defn- new! [req] - (if (spec/valid? ::new-schema (:params req)) + (if (forms/valid? req new-schema-form) (resp/redirect "/schema") (overview! req)))