簡體   English   中英

錯誤 -> 應包含 clojure.spec 中的密鑰

[英]Error -> should contain keys in clojure.spec

我是 Clojure 的新手,我遇到了一個我真的找不到解決方案的問題。

我有一個已經用 Clojure 規范定義的結構,它的鍵和數據類型與 JSON 文件匹配。

進行檢查時出現錯誤,感謝您的幫助


規格文件

(ns invoice-spec
  (:require
    [clojure.spec.alpha :as s] ))

(s/def :customer/name string?)
(s/def :customer/email string?)
(s/def :invoice/customer (s/keys :req-un [:customer/name
                                        :customer/email]))

;(s/def :tax/tax_category #{:IVA})
(s/def :tax/tax_category string?)
(s/def :tax/tax_rate int?)
(s/def ::tax (s/keys :req-un [:tax/tax_category
                           :tax/tax_rate]))
(s/def :invoice-item/taxes (s/coll-of ::tax :kind vector? :min-count 1))

(s/def :invoice-item/price double?)
(s/def :invoice-item/quantity double?)
(s/def :invoice-item/sku string?)

(s/def ::invoice-item
  (s/keys :req-un [:invoice-item/price
                :invoice-item/quantity
                :invoice-item/sku
                :invoice-item/taxes]))

;(s/def :invoice/issue_date inst?)
(s/def :invoice/issue_date string?)
(s/def :invoice/items (s/coll-of ::invoice-item :kind vector? :min-count 1))

(s/def ::invoice
  (s/keys :req-un [:invoice/issue_date
                :invoice/customer
                :invoice/items]))

文件

(defn invoice
  [name_json]
  (json/read (clojure.java.io/reader name_json) :key-fn keyword))

驗證

(s/valid? ::invoice/invoice (invoice "invoice.json"))
=> false

查看錯誤

(expound/expound ::invoice/invoice (invoice "invoice.json"))
-- Spec failed --------------------

  {:invoice
   {:payment_means_type "DEBITO",
    :order_reference "PEDID_0001",
    :number "1",
    :issue_date "13/10/2020",
    :payment_date "12/11/2020",
    :customer
    {:name "ANDRADE RODRIGUEZ MANUEL ALEJANDRO",
     :email "cgallegoaecu@gmail.com"},
    :payment_means "DEBIT_CARD",
    :items
    [{:price 10000.0,
      :quantity 1.0,
      :sku "SUPER-1",
      :taxes [{:tax_category "IVA", :tax_rate 5}]}
     {:price 20000.0,
      :quantity 1.0,
      :sku "SUPER-2",
      :taxes [{:tax_category "IVA", :tax_rate 19}]}
     {:price 30000.0,
      :quantity 1.0,
      :sku "SUPER-3",
      :taxes [{:tax_category "IVA", :tax_rate 19}]}],
    :retentions
    [{:tax_category "RET_FUENTE", :tax_rate 15.0}
     {:tax_category "RET_IVA", :tax_rate 15.0}]}}

should contain keys: :customer, :issue_date, :items

| key         | spec                                         |
|=============+==============================================|
| :customer   | (keys :req [:customer/name :customer/email]) |
|-------------+----------------------------------------------|
| :issue_date | string?                                      |
|-------------+----------------------------------------------|
| :items      | (coll-of                                     |
|             |  :invoice-spec/invoice-item                  |
|             |  :min-count                                  |
|             |  1                                           |
|             |  :kind                                       |
|             |  vector?)                                    |

-- Relevant specs -------

:invoice-spec/invoice:
  (clojure.spec.alpha/keys
   :req-un
   [:invoice/issue_date :invoice/customer :invoice/items])

-------------------------
Detected 1 error
=> nil

JSON 文件

{
  "invoice": {
    "issue_date": "13/10/2020",
    "order_reference": "PEDID_0001",
    "payment_date": "12/11/2020",
    "payment_means": "DEBIT_CARD",
    "payment_means_type": "DEBITO",
    "number": "1",
    "customer": {
      "name": "ANDRADE RODRIGUEZ MANUEL ALEJANDRO",
      "email": "cgallegoaecu@gmail.com"
    },
    "items": [
      {
        "price": 10000.00,
        "quantity": 1.00,
        "sku": "SUPER-1",
        "taxes": [
          {
            "tax_category": "IVA",
            "tax_rate": 5
          }
        ]
      },
      {
        "price": 20000.00,
        "quantity": 1.00,
        "sku": "SUPER-2",
        "taxes": [
          {
            "tax_category": "IVA",
            "tax_rate": 19
          }
        ]
      },
      {
        "price": 30000.00,
        "quantity": 1.00,
        "sku": "SUPER-3",
        "taxes": [
          {
            "tax_category": "IVA",
            "tax_rate": 19
          }
        ]
      }
    ],
    "retentions": [
      {
        "tax_category": "RET_FUENTE",
        "tax_rate": 15.00
      },
      {
        "tax_category": "RET_IVA",
        "tax_rate": 15.0
      }
    ]
  }
}

我的目標是獲得以下結果

(s/valid? ::invoice/invoice (invoice "invoice.json"))
=> true

你有 main 關鍵字:invoice ,所以你還需要一個s/def ,我叫::data

(s/def ::data (s/keys :req-un [::invoice]))

(s/valid? ::invoice/data (invoice "invoice.json"))
=> true

沒有那個def ,你必須得到:invoice的值,這是 map 鍵issue_datecustomeritems

(s/valid? ::invoice/invoice (:invoice (invoice "invoice.json")))
=> true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM