English
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) -> ValueThe 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
- 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. Onlydialandlistenerare described from theours-configoption types, not from the build's modules. - The modules' parts replace the loader's parts of the same name and are added.
inbounds/outbounds—map_of(object)with protocol blocks by$reftoinbounds.<type>/outbounds.<type>, plustls,transport,mux,dial(outbound),reload;subscriptions.patch— the outbound schema.transport— the kinds from thetransport.<kind>parts of the build's modules, soquicand transports not built into the binary do not appear in the schema.- The root is an
objectwith the keysversionandTOP_LEVELthat have a part, by$ref; all parts are in$defs;$schemais draft 2020-12.
The ours_config::schema building blocks
| Function | What 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_names | field 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, reference | common constructs |
condition, rule, conditions | match (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.