← Back to Official Package Registry
Tier 1: Backend Essentials • Schemas & OpenAPI 3.1v1.0.0 • 52 KB • Ed25519 Verified

openapi@1.0.0

Homoiconic [ ... ] data validator, JSON Schema Draft 2020-12 emitter, OpenAPI 3.1 spec builder, and interactive HTML API Explorer.

use [openapi]use [openapi/schema]
veko pkg add openapi@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

openapi

Pure-Veko homoiconic schema validator (openapi/schema), JSON Schema Draft 2020-12 emitter, OpenAPI 3.1.0 specification generator, and zero-CDN Light-First interactive HTML API Explorer for web/App.

Overview

openapi unifies runtime request/response validation and live API documentation from a single declarative source of truth:

  • openapi/schema: Define schemas using either typed combinators (sc/object, sc/field, sc/opt, sc/str-len, sc/int-range, sc/email, sc/array-of) OR Veko's homoiconic quote [...] dialect (sc/from-quote). Validate arbitrary JSON/Veko payloads with exact dotted/indexed error paths (limits.cpu, tags[1]), and emit standard JSON Schema Draft 2020-12 maps.
  • openapi: Build OpenAPI 3.1.0 specifications (new-api, add-schema, add-endpoint, to-openapi-doc, to-openapi-json) with automatic :param -> {param} route conversion and mount a self-contained, zero-CDN Light-First HTML5 API Explorer (mount-explorer) directly onto any web/App.

Installation

powershell
veko pkg add openapi@1.0.0

Import in .vk modules:

rebol
use [
    oa  = openapi
    sc  = openapi/schema
    web
]

Architecture & Security Notes

  1. Zero External CDN Dependencies: mount-explorer serves 100% inline HTML, CSS, and JavaScript directly from your Veko binary. No external scripts, fonts, or stylesheets are requested by client browsers, satisfying strict Content-Security-Policy environments.
  2. XSS & Script-Island Hardening: API metadata (title, version, description) is HTML-escaped via html/escape, and embedded OpenAPI JSON escapes </ sequences (<\/) so schema descriptions containing </script> can never break out of the <script type="application/json"> data island.
  3. JSON Schema Draft 2020-12 Integer Semantics: In accordance with JSON Schema Draft 2020-12 Section 6.1.1, .int_t accepts both native i64 values and whole-number f64 values decoded by json/decode (8443.0), while rejecting fractional numbers (3.14).
  4. Light-First Dual-Theme UI: The Explorer defaults to data-theme="light" (#f8fafc canvas, #ffffff cards) with a persistent localStorage Dark Mode toggle, tag sub-tabs, on-demand JSON Lightbox Modal (Escape to dismiss), and non-blocking toast notifications.

Complete API Reference

openapi/schema

Type / SignatureDescription
Schema, FieldSpec, ValidationErrorCore schema AST and validation failure structures.
str-type = fn [] Schema / str-len = fn [min_n i64, max_n i64] SchemaString schema constructors with optional UTF-8 length bounds.
email = fn [] Schema / uri-type = fn [] Schema / uuid-type = fn [] SchemaFormat-constrained string schemas (email, uri, uuid).
enum-of = fn [allowed block] SchemaString enum constraint.
int-type = fn [] Schema / int-range = fn [min_v i64, max_v i64] SchemaInteger schema constructors with inclusive bounds.
num-type = fn [] Schema / num-range = fn [min_v f64, max_v f64] SchemaFloating-point/numeric schema constructors.
bool-type = fn [] Schema / any-type = fn [] Schema / nullable = fn [inner Schema] SchemaBoolean, unconstrained, and nullable schema wrappers.
array-of = fn [items_schema Schema /min min_n i64 = -1 /max max_n i64 = -1] SchemaArray schema with item schema and optional length bounds.
field = fn [name str, schema Schema] FieldSpec / opt = fn [name str, schema Schema] FieldSpecRequired and optional object property descriptors.
object = fn [fields block /strict disallow_extra bool = false] SchemaObject schema combinator (/strict sets additionalProperties: false).
from-quote = fn [dsl block /strict disallow_extra bool = false] SchemaCompiles a homoiconic quote [...] block into an object Schema.
validate = fn [schema Schema, val any] anyReturns .ok or { ok = false, errors = [ { path = '...', message = '...' } ] }.
to-json-schema = fn [s Schema /root include_meta bool = false] mapEmits a JSON Schema Draft 2020-12 map.

openapi

Type / SignatureDescription
ApiSpec, EndpointSpec, QueryParamOpenAPI 3.1.0 specification and operation descriptors.
new-api = fn [title str, version str /desc description str = '' /server server_url str = ''] ApiSpecInitializes an OpenAPI 3.1.0 builder.
add-schema = fn [mut api ApiSpec, name str, schema sc/Schema] enumRegisters #/components/schemas/<name>.
add-endpoint = fn [mut api ApiSpec, ep EndpointSpec] enumRegisters an HTTP endpoint operation on api.
to-openapi-doc = fn [api ApiSpec] mapEmits the structured OpenAPI 3.1.0 document map.
to-openapi-json = fn [api ApiSpec] strSerializes the OpenAPI 3.1.0 specification as JSON.
render-explorer-html = fn [api ApiSpec, spec_json_path str] strGenerates the standalone HTML5 API Explorer page.
mount-explorer = fn [mut app web/App, prefix str, api ApiSpec] enumMounts /openapi.json and the interactive Explorer at prefix on mut app.

Idiomatic Veko Examples

Homoiconic quote [...] Validation & OpenAPI Explorer Mounting

rebol
use [oa = openapi, sc = openapi/schema, web]

CreateNodeSchema = sc/from-quote/strict (quote [
    hostname str .min 3 .max 64,
    admin_email str .email,
    port i64 .min 1 .max 65535,
    roles block[str] .optional
]) true

mut api = oa/new-api/desc 'Cluster API' '1.0.0' 'Node lifecycle management'
_ = oa/add-endpoint mut api (oa/EndpointSpec {
    method         = 'POST'
    path           = '/nodes/:region'
    summary        = 'Register cluster node'
    tags           = ['Nodes']
    request_schema = CreateNodeSchema
    status_code    = 201
    auth_bearer    = true
})

mut app = web/new-app
_ = oa/mount-explorer mut app '/openapi' api

_ = web/post mut app '/nodes/:region' fn [req web/Request] web/Response [
    body = web/bind-json/or req [ return (web/bad-request 'invalid JSON') ]
    chk = sc/validate CreateNodeSchema body
    if chk != .ok [ return (web/json-resp 422 chk) ]
    web/json-resp 201 { registered = true, region = (web/param req 'region' '') }
]

Verification

powershell
veko check --strict-types packages/openapi/src/schema.vk
veko check packages/openapi/src/mod.vk
veko check packages/openapi/examples/demo.vk
veko run -C all packages/openapi/examples/demo.vk
packages/openapi/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/openapi/examples/demo.vk
// Runnable verification suite for `openapi` and `openapi/schema`.

use [
	oa  = openapi
	sc  = openapi/schema
	web
]

assert-eq = fn [actual any, expected any, label str] any [
	if actual != expected [
		return (err (str/join ['ASSERT FAILED [' label ']: got ' (mold actual) ', expected ' (mold expected)] ''))
	]
	.ok
]

assert-true = fn [cond bool, label str] any [
	if not cond [
		return (err (str/join ['ASSERT FAILED [' label ']'] ''))
	]
	.ok
]

has-error-at-path? = fn [errs block, want_path str] bool [
	mut i = 0
	while i < (len errs) [
		e = errs[i]
		if (map/get e 'path') == want_path [ return true ]
		i = i + 1
	]
	false
]

main = fn [] any [
	println '=== openapi 1.0.0 Verification Suite ==='

	// 1. Combinator Schema & Homoiconic `quote [...]` Schema Compilation
	UserSchema = sc/object [
		sc/field 'username' (sc/str-len 3 32)
		sc/field 'email'    (sc/email)
		sc/field 'port'     (sc/int-range 1 65535)
		sc/opt   'tags'     (sc/array-of (sc/str-type))
	]

	QuoteUserSchema = sc/from-quote (quote [
		username str .min 3 .max 32,
		email str .email,
		port i64 .min 1 .max 65535,
		tags block[str] .optional
	])
	if err? QuoteUserSchema [ return QuoteUserSchema ]

	valid_user = {
		username = 'ada_lovelace'
		email    = 'ada@veko-lang.org'
		port     = 8443
		tags     = ['admin' 'builder']
	}
	r1 = assert-eq (sc/validate UserSchema valid_user) .ok 'combinator schema valid payload'
	if err? r1 [ return r1 ]
	r2 = assert-eq (sc/validate QuoteUserSchema valid_user) .ok 'quote DSL schema valid payload'
	if err? r2 [ return r2 ]

	// Optional field omitted
	valid_no_tags = {
		username = 'kai'
		email    = 'kai@veko-lang.org'
		port     = 443
	}
	r3 = assert-eq (sc/validate QuoteUserSchema valid_no_tags) .ok 'optional tags omitted'
	if err? r3 [ return r3 ]

	// Invalid payload: verify exact field paths ('username', 'email', 'port', 'tags[1]')
	bad_user = {
		username = 'ab'
		email    = 'not-an-email'
		port     = 70000
		tags     = ['ok' 999]
	}
	vres1 = sc/validate UserSchema bad_user
	vres2 = sc/validate QuoteUserSchema bad_user
	r4 = assert-eq vres1.ok false 'combinator invalid payload rejected'
	if err? r4 [ return r4 ]
	r5 = assert-eq vres2.ok false 'quote DSL invalid payload rejected'
	if err? r5 [ return r5 ]
	r6 = assert-true (has-error-at-path? vres2.errors 'username') 'error path: username'
	if err? r6 [ return r6 ]
	r7 = assert-true (has-error-at-path? vres2.errors 'email') 'error path: email'
	if err? r7 [ return r7 ]
	r8 = assert-true (has-error-at-path? vres2.errors 'port') 'error path: port'
	if err? r8 [ return r8 ]
	r9 = assert-true (has-error-at-path? vres2.errors 'tags[1]') 'error path: tags[1]'
	if err? r9 [ return r9 ]

	// Nested strict object via `from-quote`
	NestedSchema = sc/from-quote/strict (quote [
		service str .min 2 .max 40,
		mode str .enum ['active' 'standby'],
		limits object [
			cpu f64 .min 0.1 .max 64.0,
			mem_mb i64 .min 64 .max 65536
		]
	]) true
	bad_nested = {
		service = 'gw'
		mode    = 'invalid_mode'
		limits  = { cpu = 0.01, mem_mb = 256 }
		rogue   = true
	}
	vn = sc/validate NestedSchema bad_nested
	r10 = assert-true ((has-error-at-path? vn.errors 'mode') and (has-error-at-path? vn.errors 'limits.cpu') and (has-error-at-path? vn.errors 'rogue')) 'nested & strict additionalProperties paths'
	if err? r10 [ return r10 ]
	println '  [PASS] Combinator & homoiconic quote [...] schema validation + exact error paths'

	// 2. Draft 2020-12 JSON Schema emission
	jschema = sc/to-json-schema/root QuoteUserSchema true
	r11 = assert-eq (map/get jschema '$schema') 'https://json-schema.org/draft/2020-12/schema' 'Draft 2020-12 $schema URI'
	if err? r11 [ return r11 ]
	props = map/get jschema 'properties'
	port_prop = map/get props 'port'
	r12 = assert-eq (map/get port_prop 'maximum') 65535 'JSON Schema port maximum'
	if err? r12 [ return r12 ]
	r13 = assert-eq (map/get jschema 'required') ['username' 'email' 'port'] 'JSON Schema required list'
	if err? r13 [ return r13 ]
	println '  [PASS] JSON Schema Draft 2020-12 map emission'

	// 3. OpenAPI 3.1.0 Specification Builder & `web/App` Explorer Mount
	UserRespSchema = sc/from-quote (quote [
		id str .uuid,
		username str,
		email str .email
	])

	mut api = oa/new-api/desc/server 'Veko Control Plane API' '1.0.0' 'Zero-CDN OpenAPI 3.1 service specification' 'https://api.veko-lang.org'
	_ = oa/add-schema mut api 'UserInput' QuoteUserSchema
	_ = oa/add-schema mut api 'UserRecord' UserRespSchema

	_ = oa/add-endpoint mut api (oa/EndpointSpec {
		method          = 'POST'
		path            = '/users'
		summary         = 'Register a new user'
		tags            = ['Users']
		request_schema  = QuoteUserSchema
		response_schema = UserRespSchema
		status_code     = 201
		auth_bearer     = true
	})

	_ = oa/add-endpoint mut api (oa/EndpointSpec {
		method          = 'GET'
		path            = '/users/:id'
		summary         = 'Fetch user by ID'
		tags            = ['Users']
		query_params    = [
			(oa/QueryParam { name = 'verbose', required = false, description = 'Include audit metadata', schema = (sc/bool-type) })
		]
		response_schema = UserRespSchema
		status_code     = 200
		auth_bearer     = true
	})

	mut app = web/new-app
	_ = web/use-security-headers mut app
	_ = oa/mount-explorer mut app '/openapi' api

	_ = web/post mut app '/users' fn [req web/Request] web/Response [
		payload = web/bind-json/or req [ return (web/json-resp 400 { error = 'malformed json' }) ]
		chk = oa/validate QuoteUserSchema payload
		if chk != .ok [
			return (web/json-resp 422 chk)
		]
		web/json-resp 201 {
			id       = '550e8400-e29b-41d4-a716-446655440000'
			username = map/get payload 'username'
			email    = map/get payload 'email'
		}
	]

	// Dispatch GET /openapi.json
	req_json = web/Request {
		method = 'GET', path = '/openapi.json', target = '/openapi.json',
		query = {}, params = {}, headers = {}, body = (bytes/with-cap 0)
	}
	resp_json = web/handle-request app req_json
	r14 = assert-eq resp_json.status 200 'GET /openapi.json status 200'
	if err? r14 [ return r14 ]
	oa_doc = json/decode (bytes/to-str resp_json.body)
	r15 = assert-eq (map/get oa_doc 'openapi') '3.1.0' 'OpenAPI version 3.1.0'
	if err? r15 [ return r15 ]
	oa_paths = map/get oa_doc 'paths'
	r16 = assert-true (map/has? oa_paths '/users/{id}') 'Veko :id converted to OpenAPI /users/{id}'
	if err? r16 [ return r16 ]
	get_user_op = map/get (map/get oa_paths '/users/{id}') 'get'
	op_params = map/get get_user_op 'parameters'
	r17 = assert-eq (len op_params) 2 'path + query parameters emitted'
	if err? r17 [ return r17 ]

	// Dispatch GET /openapi (Light-First HTML Explorer)
	req_html = web/Request {
		method = 'GET', path = '/openapi', target = '/openapi',
		query = {}, params = {}, headers = {}, body = (bytes/with-cap 0)
	}
	resp_html = web/handle-request app req_html
	html_body = bytes/to-str resp_html.body
	r18 = assert-eq resp_html.status 200 'GET /openapi status 200'
	if err? r18 [ return r18 ]
	r19 = assert-true (str/contains? html_body 'data-theme="light"') 'Explorer defaults to Light Mode'
	if err? r19 [ return r19 ]
	r20 = assert-true (str/contains? html_body 'id="lightbox-backdrop"') 'Explorer includes Lightbox Modal'
	if err? r20 [ return r20 ]
	r21 = assert-true (str/contains? html_body 'Veko Control Plane API') 'Explorer renders API title'
	if err? r21 [ return r21 ]

	// Dispatch POST /users (valid -> 201, invalid -> 422)
	req_post_ok = web/Request {
		method = 'POST', path = '/users', target = '/users',
		query = {}, params = {}, headers = { 'Content-Type' = 'application/json' },
		body = (bytes/from-str (json/encode valid_user))
	}
	resp_post_ok = web/handle-request app req_post_ok
	r22 = assert-eq resp_post_ok.status 201 'POST /users valid -> 201'
	if err? r22 [ return r22 ]

	req_post_bad = web/Request {
		method = 'POST', path = '/users', target = '/users',
		query = {}, params = {}, headers = { 'Content-Type' = 'application/json' },
		body = (bytes/from-str (json/encode bad_user))
	}
	resp_post_bad = web/handle-request app req_post_bad
	r23 = assert-eq resp_post_bad.status 422 'POST /users invalid -> 422'
	if err? r23 [ return r23 ]

	println '  [PASS] OpenAPI 3.1.0 JSON generation & Light-First web/App Explorer dispatch'
	println 'ALL openapi TESTS PASSED'
	.ok
]

res = main
if err? res [
	println (mold res)
	os/exit 1
]
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/openapi/1.0.0.vkp
Archive SHA-256: e11ccf0ac55e283be2eee5bb2366126e1c8a5d9470e3cdfbdc6335fac333e2ad
Merkle Root: 6d4e8e9ca7ed7b04b4bdda0345e854587517fee0500c7875bfca67142809d898
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"openapi","version":"1.0.0","tier":1.0,"tier_label":"Tier 1: Backend Essentials","category":"Schemas & OpenAPI 3.1","description":"Homoiconic [ ... ] data validator, JSON Schema Draft 2020-12 emitter, OpenAPI 3.1 spec builder, and interactive HTML API Explorer.","modules":["openapi","openapi/schema"],"homepage":"https://veko-lang.org/packages/openapi"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md6 KBe47e4d2c0f93410ea9c5755f8b6dc71bec26bf355a158a62ab245120dfcb87c7
examples/demo.vk8 KBe3c89426a5d662f9d84d9adb48610ffe17f9975658a8f2027a1f2bba9dc65f00
mod.vk19 KB5dae43d085d74502242093bc718af7052d94ce35f8972f5933dc157494ffb923
schema.vk17 KB7cf07c2261d7dd4cee8f921b7312f4b565f39a9defeb9211ad067cbdfe52f40f