Skip to content

JSON Schema ​

Code: crates/native-config/src/schema.rs (json_schema), crates/config/src/schema.rs, crates/api/src/config.rs (SchemaPart, Decoders::schemas), app/ours/src/main.rs.

The schema is needed by editors (yaml-language-server): key suggestions and highlighting extra ones. It describes the form — which keys are allowed where; the values of leaf fields are not described (any()), decoding checks them.

The command ​

ours schema [output.json]

json_schema(&modules::get().decoders()) — the schema of this build: types, sections and conditions of modules not built into the binary do not get into it (except for the places the loader describes itself, see below).

Parts ​

A module describes what it decodes:

rust
Provide::Schema { name: "outbounds.vless", schema: |_| ours_config::schema::outbound::<VLESSOutboundOptions>() }
Provide::Schema { name: "dns", schema: schema::dns }
// SchemaPart = fn(decoders: &Decoders) -> Value

The naming rule: a part named after a top-level key (dns, route, groups, ntp, cache, rule_sets) is the schema of that section; inbounds.<type>/outbounds.<type> is the type's protocol block; transport.<kind> is the transport block of that kind (described by the transport module). SchemaPart gets Decoders so as to describe what other modules add: the router inserts into match the conditions from the conditions point (ours_config::schema::conditions(decoders, place) — from the Entry::schema of every condition for Place::Route/Place::Dns).

Assembling json_schema ​

  1. The loader puts in its own parts: version, dial, listener, log, reload, runtime, route (without the rules module — only what the kernel reads), inbounds, outbounds, endpoints, groups, subscriptions, and placeholders that modules replace: tls, certificate, network_namespaces, http_clients, transport, mux.inbound, mux.outbound. Only dial and listener are described from the ours-config option types, not from the build's modules.
  2. The modules' parts replace the loader's parts of the same name and are added.
  3. inbounds/outbounds — map_of(object) with protocol blocks by $ref to inbounds.<type>/outbounds.<type>, plus tls, transport, mux, dial (outbound), reload; subscriptions.patch — the outbound schema. transport — the kinds from the transport.<kind> parts of the build's modules, so quic and transports not built into the binary do not appear in the schema.
  4. The root is an object with the keys version and TOP_LEVEL that have a part, by $ref; all parts are in $defs; $schema is draft 2020-12.

The ours_config::schema building blocks ​

FunctionWhat it gives
object(props)a map with these keys and only them (additionalProperties: false), null is allowed (key: without a value)
block(names), with(names, blocks)leaf fields any() + nested blocks
fields_of::<T>(without), table_namesfield names from the Fields table of an options type
renamed(names, renames)native names (bind_interface → interface, server_name → sni)
inbound::<T>(), outbound::<T>(), listener(table)a protocol block as listener/dialer sees it: without listen_port/server_port, the dial fields, the shared blocks; udp_over_tcp is a block
map_of, list_of, one_of, string, strings, any, referencecommon constructs
condition, rule, conditionsmatch (rule fields + module conditions + any/all/not), a match/do/to rule

Field names are taken from the same field tables the codec decodes by, so a new field of an options type appears in the schema without editing the schema. Renames and exclusions repeat what listener/dialer and the loader do — they have to be kept consistent by hand.

Tests ​

crates/native-config/tests/json_schema.rs, crates/native-config/tests/schema.rs.

GPL-3.0-or-later