GitHub - mpenet/legba: Clojure library for building OpenAPI services (original) (raw)

Legba

He who controls access between the two, deciding who and what can pass through

Legba is a library designed for building fully OpenAPI 3.1 compliantservices in Clojure.

Legba streamlines the creation of OpenAPI based servers by emphasizing theOpenAPI schema file as the foundation of your service definition. Unlike other libraries that generate the schema from routes & validation defined in a separate DSL, Legba takes a different approach: it uses your OpenAPI schema to create the necessary routes and wraps your supplied handlers with OpenAPI validation.

Legba ensures that the final OpenAPI file exposed to your users remainsunrestricted, reviewable, and editable.

Legba goals

Note:

/!\ While already good enough for production I reserve myself the right to break backward compatibly until I strip the alpha tag from the released version.

How does it work

You can either:

Route Syntax

Examples:

[:get "/items"] [:get "/item/{itemId}"] ;; Path parameter [:post "/items"] [:delete "/user/{userId}/role/{roleId}"] ;; Multiple params [:get "/assets/*"] ;; Prefix matching

Route Syntax Rules:

Installation

Clojars Project

Or via git deps:

com.s-exp/legba {:git/url "https://github.com/mpenet/legba.git" :git/sha "..."}

Documentation

API docs

Usage

/!\

Please note that this is still an alpha release. While it is fairly stable and I make an effort to maintain API contracts, some aspects are still under development. I reserve the right to make changes until the alpha tag is removed.

(require '[s-exp.legba :as l])

(l/routing-handler {[:get "/item/{itemId}"] (fn [_request] {...}) [:get "/items"] (fn [_request] {...}) [:get "/search"] (fn [_request] {...}) [:post "/items"] (fn [_request] {..})} "classpath://schema/oas/3.1/catalog.json" ;; {...} ; options )

There's also an extra argument with options:

Notes

Standalone Json-Schema validation

Using s-exp.legba.json-schema you can also perform standalone json-schema validation.

Example

Define a simple JSON schema file json-schema.json

{"type" "object" "properties" {"name" {"type" "string"} "age" {"type" "integer"}} "required" ["name" "age"] "additionalProperties" false}

(require '[s-exp.legba.json-schema :as json-schema]

;; Validate the data (-> (json-schema/schema "classpath://json-schema.json") (json-schema/validate! "{"name":"Alice","age":30}"))

OpenAPI Overlay Support

Legba provides utilities for OpenAPI Overlay via s-exp.legba.overlay.

This allows you to dynamically update or remove parts of your OpenAPI schema using overlay instructions, making it easy to tailor or extend schemas programmatically.

Main Functionality

Example

(require '[s-exp.legba.overlay :as overlay])

(def openapi-schema (slurp "path/to/openapi.json")) (def overlay-json "{"actions": [ {"target": "$..['x-private']", "remove": true} ]}")

;; Returns updated schema JSON string with actions applied (overlay/apply openapi-schema overlay-json)

This can be useful for customizing schemas for different consumers, removing internal fields, or overlaying additional information as needed.

License

Copyright © 2024-2025 Max Penet

Distributed under the Eclipse Public License version 1.0.