Skip to content

Instantly share code, notes, and snippets.

View ikitommi's full-sized avatar
💭

Tommi Reiman ikitommi

💭
View GitHub Profile
@ikitommi
ikitommi / handler.clj
Last active October 23, 2017 16:34
2-way spec coercion
(ns handler
(:require [clj-time.core :as t]
[clj-time.coerce :as t.c]
[spec-tools.conform :as conform]
[clojure.spec.alpha :as s]
[spec-tools.core :as st]
[compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]))
(def timestamp
@ikitommi
ikitommi / intro.md
Last active October 2, 2017 17:16
Clojure = perf test
```clj
;;
;; Model Name: MacBook Pro
;; Model Identifier: MacBookPro11,3
;; Processor Name: Intel Core i7
;; Processor Speed: 2,5 GHz
;; Number of Processors: 1
;; Total Number of Cores: 4
;; L2 Cache (per Core): 256 KB
@ikitommi
ikitommi / repl.clj
Last active August 28, 2017 09:32
Reitit + spec-coercion
(require '[reitit.ring :as ring])
(require '[reitit.coercion :as coercion])
(require '[reitit.coercion.spec :as spec])
(def app
(ring/ring-handler
(ring/router
["/api"
["/ping" {:parameters {:body {:x int?, :y int?}}
:responses {200 {:schema {:total pos-int?}}}
{
"basePath": "/",
"consumes": [
"application/json",
"application/x-yaml",
"application/msgpack",
"application/transit+msgpack",
"application/transit+json",
"application/edn"
],
@ikitommi
ikitommi / handler.clj
Created May 9, 2017 18:42
Optional query-param with compojure-api resources
(require '[compojure.api.sweet :refer [resource])
(require '[ring.util.http-response :refer [ok])
(def handler
(resource
{:summary "increments query-param x, defaulting to 1"
:parameters {:query-params {(s/optional-key :x) Long}}
:handler (fn [request]
(ok (-> request :query-params :x (or 0) inc)))}))
@ikitommi
ikitommi / specs.clj
Created April 5, 2017 19:58
Walking the specs
(require '[clojure.spec :as s])
(require '[spec-tools.visitor :as visitor])
(s/def ::age number?)
(s/def ::first-name string?)
(s/def ::last-name string?)
(s/def ::name (s/keys :req-un [::first-name ::last-name]))
(s/def ::user (s/keys :req-un [::name ::age]))
(let [specs (atom {})]
@ikitommi
ikitommi / Proposal.md
Last active March 4, 2024 01:49
Specs & Schema for Clojure web apps

Specs & Schema for Clojure web apps

Common ground for different modelling libs (Spec, Schema, ...) for web usage.

Additional metadata on models

  • for Schema, there are multiple ways to add extra metadata (at least in ring-swagger, schema-tools and in compojure-api)
  • emerging tools for spec
  • could have a common model
    • less syntax to learn
@ikitommi
ikitommi / user.clj
Created February 8, 2017 07:24
Schema matching
(require '[schema.core :as s])
(require '[schema.coerce :as sc])
(require '[schema-tools.core :as st])
(require '[schema-tools.coerce :as stc])
(s/defschema IpAddress (s/maybe s/Str))
(s/defschema Customer {:ip IpAddress, :name s/Str})
(def matcher (stc/or-matcher
{IpAddress #(if (and (string? %) (< (count %))) %)}
sc/json-coercion-matcher))
@ikitommi
ikitommi / swagger.json
Created December 10, 2016 09:42
Inlined Body Schemas for Swagger
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore (Simple)"
},
"paths": {
"/pets": {
"post": {
"description": "Creates a new pet in the store. Duplicates are allowed",
(ns spec-swagger.core
(:require
[clojure.spec :as s]
[spec-tools.core :as st]
#?@(:clj [[clojure.spec.gen :as gen]]
:cljs [[clojure.test.check.generators]
[cljs.spec.impl.gen :as gen]])))
;;
;; helpers