Skip to content

Commit

Permalink
Make coercion work for local dates
Browse files Browse the repository at this point in the history
Thanks to a gist by Tommi Reiman at
https://gist.github.com/ikitommi/a64314c915cffe46fbd2cef95074a12f

Thanks to this change the API for creating products now also works.
  • Loading branch information
egli committed Nov 22, 2022
1 parent 20c07ab commit a9af30f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
27 changes: 22 additions & 5 deletions src/clj/mdr2/production/spec.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
(ns mdr2.production.spec
(:require [spec-tools.data-spec :as spec]
[spec-tools.core :as st]
[clojure.spec.alpha :as s]))

;; see https://gist.github.com/ikitommi/a64314c915cffe46fbd2cef95074a12f
;; a spec-tools transformer
(defn- decode-local-date [_ value]
(try
(if (string? value)
(java.time.LocalDate/parse value))
(catch Exception _
value)))

;; a local-date spec
(s/def ::local-date
(st/spec
{:spec (partial instance? java.time.LocalDate)
:json-schema/type {:type "string", :format "date"}
:decode/string decode-local-date
:decode/json decode-local-date}))

(s/def ::state (s/and string?
#{"new" "archived" "cataloged" "deleted" "encoded"
"failed" "pending-split" "recorded" "split" "structured"
"repairing"}))
(s/def ::production_type (s/and string? #{"book" "periodical" "other"}))
(s/def ::library_signature (s/and string? #(re-matches #"^ds\d{4,}$" %)))
(s/def ::library_number (s/and string? #(re-matches #"^PNX \d{4,}$" %)))
(s/def ::date #(instance? java.time.LocalDate %))

(s/def ::volumes (s/and int? #{1 2 3 4 5 6 7 8}))
(s/def ::sample-rate (s/and int? #{11025 22050 44100 48000}))
Expand All @@ -21,25 +38,25 @@
(spec/opt :subject) string?
(spec/opt :description) string?
(spec/opt :publisher) string?
:date ::date
:date ::local-date
(spec/opt :type) string?
(spec/opt :format) string?
(spec/opt :id) int?
:identifier string?
(spec/opt :source) string?
:language string?
(spec/opt :rights) string?
(spec/opt :source_date) ::date
(spec/opt :source_date) ::local-date
(spec/opt :source_edition) string?
(spec/opt :source_publisher) string?
(spec/opt :source_rights) string?
(spec/opt :multimedia_type) string?
(spec/opt :multimedia_content) string?
(spec/opt :narrator) string?
(spec/opt :producer) string?
(spec/opt :produced_date) ::date
(spec/opt :produced_date) ::local-date
:revision int?
(spec/opt :revision_date) ::date
(spec/opt :revision_date) ::local-date
(spec/opt :revision_description) string?
(spec/opt :total_time) int?
(spec/opt :audio_format) string?
Expand Down
23 changes: 20 additions & 3 deletions test/rest-api.http
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,26 @@ GET http://localhost:3000/api/productions?search=pending-split

# Add a production
POST http://localhost:3000/api/productions
Authorization: :auth-token
Content-Type: application/json
{
"title": "Hehe",
"creator": "Foo",
"description": "not much",
"source": "978-3-257-07184-11",
"language": "de",
"source_date": "2022-06-16T09:38:03.196Z",
"source_date": "2022-06-16",
"source_publisher": "Vargos",
"library_number": "PNX 8434"
"library_number": "PNX 9435",
"state": "new",
"identifier": "978-3-570-15263-0",
"revision": 0,
"date": "2022-11-22"
}

# Add a production using transit
POST http://localhost:3000/api/productions
Authorization: :auth-token
Content-Type: application/transit+json
[
"^ ",
Expand All @@ -95,7 +101,18 @@ Content-Type: application/transit+json
"2022-01-01"
],
"~:library_number",
"PNX 8434"
"PNX 9434",
"~:revision",
0,
"~:state",
"new",
"~:identifier",
"978-3-570-15263-0",
"~:date",
[
"~#LocalDate",
"2022-01-01"
]
]

# Delete a production
Expand Down

0 comments on commit a9af30f

Please sign in to comment.