clojure - 如何使用 plumatic/schema 生成用户友好的验证消息?

标签 clojure plumatic-schema

我希望能够为这些模式中的验证错误生成用户友好的或指定自定义错误消息:

(def Uuid (s/constrained String #(re-matches #"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" (name %))))
(def FirstName s/Str)
(def LastName s/Str)

(s/defschema Person {(s/required-key :id)         Uuid,
                     (s/required-key :first-name) FirstName,
                     (s/required-key :last-name)  LastName})

有效架构:

{
 :uuid "e143499c-1257-41e4-b951-c9e586994ff9" 
 :first-name "john" 
 :last-name "smith"
}

无效的架构:

{
 :uuid "" 
 :first-name nil 
 :last-name nil
}

无效的架构 - 错误:

{
 "id" : "(not (app.person/fn--4881 \"\"))",
 "first-name" : "(not (instance? java.lang.String nil))"
 "last-name" : "(not (instance? java.lang.String nil))"
}

我希望能够生成一些对非程序员来说更具可读性的东西,例如:

{
 "id" : "invalid uuid",
 "first-name" : "must be a string"
 "last-name" : "must be a string"
}

最佳答案

有趣的是,这正是几天前作为库发布的。

参见:

https://github.com/siilisolutions/humanize

首先,您还需要标记您的 Uuid 架构,以便稍后匹配它:

;; Note the last param I added: 
(def Uuid (sc/constrained
            String
            #(re-matches #"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
                         (name %))
            'UUID))

(require '[schema.core :as sc]
         '[humanize.schema :as hs])

(#'hs/explain (sc/check Person {:id "foo"
                                :first-name "foo"
                                :last-name 3})
  (fn [x]
    (clojure.core.match/match
      x
      ['not ['UUID xx]]
      (str xx " is not a valid UUID")

      :else x)))

结果:

=> {:id "foo is not a valid UUID", :last-name "'3' is not a string but it should be."}

请注意,不幸的是,hs/explain 是私有(private)的,因此它需要一些小技巧。

关于clojure - 如何使用 plumatic/schema 生成用户友好的验证消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49194814/

相关文章:

java - 如何在 Clojure 中实现具有具体类型参数的接口(interface)?

mysql - 无法开始使用 clojure kORMa

clojure - 我们可以考虑 Clojure 的 STM 'functional' 吗?

mongodb - 如何使用 MongoDB 在 Prisma ORM 中创建类别及其子类别

clojure - 在 Clojure 中使用 Prismatic/schema 进行函数验证

clojure - 为什么 clojure.core 中的某些 Clojure 函数没有记录在案?

clojure - Compojure/Noir 使用 Spring Security 进行身份验证和身份验证?

clojure - 为什么我不能使用正则表达式来验证字符串作为映射键?

graphql - Prisma数据建模有很多属于

clojure - 具有最小和最大限制的架构?