From dd63cee18f90c084551ec54ab0335aeb31f8df66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Sat, 13 Apr 2024 18:22:38 +0200 Subject: [PATCH] Implement profile validation --- src/packer/core.clj | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/packer/core.clj b/src/packer/core.clj index ff7db21..1d561b8 100644 --- a/src/packer/core.clj +++ b/src/packer/core.clj @@ -30,10 +30,22 @@ "\n" (produce-items items nights i18n)))) +(defn validate-profile [store profile] + (let [all-items (->> store + vals + flatten + (map :key)) + missing-items (remove (set all-items) profile) + missing-items-str (clojure.string/join ", " missing-items)] + (when (not (empty? missing-items)) + (throw (Exception. (str "The following keys are found in the profile, but they are not defined in store: " missing-items-str)))))) + (defn produce [store profile nights i18n] - (->> store - keys - (map (fn [category] [category (filter #(.contains profile (:key %)) (get store category))])) - (filter #(not (empty? (second %)))) - (map #(produce-category (first %) (second %) nights i18n)) - (clojure.string/join "\n\n"))) \ No newline at end of file + (do + (validate-profile store profile) + (->> store + keys + (map (fn [category] [category (filter #(.contains profile (:key %)) (get store category))])) + (filter #(not (empty? (second %)))) + (map #(produce-category (first %) (second %) nights i18n)) + (clojure.string/join "\n\n")))) \ No newline at end of file