Schema validation reference
Validate JSON request bodies with Vectrune schemas, required scalar fields, and explicit HTTP 400 failures.
Declare a schema and validate it
@Schema/Greeting declares name = string and enthusiastic = bool. Parse the request body, then call validate body #Greeting before using the input.
The expect = Greeting route metadata connects POST and PUT request bodies to the generated OpenAPI schema. Keep explicit validation: expect does not perform runtime validation.
Alternatively, verify body as Greeting into greeting parses the JSON string and validates it before writing greeting. Unlike scalar validate, verify also supports nested named schemas and arrays, such as [Sku]. See examples/api/verify_order.rune.
#!RUNE
@App
name = Greeting API
type = REST
port = 3000
swagger = true
@Schema/Greeting
name = string
enthusiastic = bool
@Route/GET /health
description = Check that the API is running
run:
respond 200 "OK"
@Route/GET /greetings/{name}
description = Read a name from the URL
run:
result = { name: name }
respond 200 result
@Route/POST /greetings
description = Validate and echo a greeting
expect = Greeting
run:
parse-json
validate body #Greeting
respond 201 body
Supported validation behavior
- string accepts a JSON string.
- number accepts a JSON number.
- bool accepts a JSON boolean; use bool in the Rune schema, not boolean.
- Every field declared in the schema is required.
- Additional JSON properties are not rejected by this validator.
- The legacy validate path does not implement nested schemas, optional fields, or array constraints. verify supports nested schemas and arrays, but not optional fields.
Failure responses
Missing fields and type mismatches return HTTP 400 and stop execution. Examples include Missing field `name` and Field `enthusiastic` type mismatch. A missing schema or value returns Validation failed.
verify returns HTTP 400 for malformed JSON or invalid data. A separate parse-json step on malformed JSON returns a builtin error and HTTP 500.
When several fields are invalid, do not depend on which field error is returned first.
Exercise valid and invalid requests
The companion test file checks health, a URL parameter, valid JSON, a missing required field, and a scalar type mismatch.
#!RUNE
import "app.rune"
@Test/Health
run:
req = { method: "GET", path: "/health" }
res = test-request req
assert res.status == 200
assert res.text == "OK"
@Test/PathParameter
run:
req = { method: "GET", path: "/greetings/Ada" }
res = test-request req
assert res.status == 200
assert res.body.name == "Ada"
@Test/ValidGreeting
run:
req = { method: "POST", path: "/greetings", body: { name: "Ada", enthusiastic: true } }
res = test-request req
assert res.status == 201
assert res.body.name == "Ada"
assert res.body.enthusiastic == true
@Test/MissingField
run:
req = { method: "POST", path: "/greetings", body: { name: "Ada" } }
res = test-request req
assert res.status == 400
assert res.text contains "enthusiastic"
@Test/WrongType
run:
req = { method: "POST", path: "/greetings", body: { name: "Ada", enthusiastic: "yes" } }
res = test-request req
assert res.status == 400
assert res.text contains "type mismatch"
vectrune test api.test.rune --json