Skip to content

Instantly share code, notes, and snippets.

@bhurlow
Created March 2, 2022 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhurlow/c0d0e7a98f0014b1726e6e7d23e3ff2f to your computer and use it in GitHub Desktop.
Save bhurlow/c0d0e7a98f0014b1726e6e7d23e3ff2f to your computer and use it in GitHub Desktop.
schema generation
(ns yd.seed.core
(:require [malli.core :as m]
[malli.error :as me]
[malli.generator :as mg]))
(def event-types
[:enum
:post
:comment
:reaction
:view
:badge])
(defn timestamp? [x]
(and
(> x 1451606400000)
(< x 32503680000000)))
(def board
[:map
[:board/title "hello"]])
(def ref-field
[:or
string?
int?
map?])
; (m/validate
; ref-field
; [123])
;; how to we describe the schema of a :ref field?
;; depends on when we're doing the validation
;; when validating a transaction, it should be a long or temp-id
;;
(def post-event
[:map
[:post/external-body :string]
[:event/type event-types]
[:event/board ref-field]
[:system/created-at [:fn timestamp?]]
[:post/author ref-field]
[:post/reply-to {:optional true} ref-field]])
(def user
[:map
[:user/username string?]
[:user/firstname {:optional true} string?]
[:user/lastname {:optional true} string?]
[:user/primary-email]])
(m/explain
post-event
{:event/type :comment
:event/board 123123
:system/created-at 1638591958204
:post/external-body "foo"
:post/author 21323})
(m/explain
post-event
{:event/type :comment
:event/board 123123
:system/created-at 1638591958204
:post/external-body "foo"
:post/author 21323
:post/reply-to 123123})
(mg/generate post-event)
(me/humanize
(m/explain
post-event
{:foo "SDFE"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment