Skip to content

JSON Schema ​

Provide::Schema — crates/api/src/module.rs; the helpers — crates/config/src/schema.rs. The whole schema is assembled by the loader (ours_native_config::json_schema, the ours schema command) — see JSON Schema in the configuration.

rust
Provide::Schema { name: &'static str, schema: SchemaPart }
type SchemaPart = fn(decoders: &Decoders) -> Value;

Every module describes its own part; the loader puts the parts into $defs under their names, and a part may refer to another: ours_config::schema::reference(name) → {"$ref": "#/$defs/<name>"}. SchemaPart gets the build's Decoders so as to describe only the variants built in (for example, rule conditions).

Part names:

  • a top-level section — by its key: dns, ntp, cache, route, rule_sets, groups;
  • a protocol block — inbounds.<type>, outbounds.<type>.

The schema does not describe the values of leaf members: decoding checks them. The schema lists block members so that an editor suggests names and catches extra ones.

The ours_config::schema helpers ​

FunctionWhat it gives
inbound::<T>()an inbound protocol block from the field table of T: without listen_port and the shared blocks; bind_interface → interface, routing_mark → fwmark (listener)
outbound::<T>()an outbound protocol block: without server_port, tls/transport/multiplex and the members of DialerOptions; udp_over_tcp is a nested block
fields_of::<T>(without), table_names(table, without)field names from a FieldTable (ours_codec::Fields)
without(part, names), replaced(part, remove, key, value)part without the members names; part with the members remove replaced by key: value
renamed(names, renames)rename, sort, remove duplicates
block(names), with(names, blocks)an object of leaf members (any values) and nested blocks
object(properties)an object with exactly these members (additionalProperties: false), allows null (key: without a value)
map_of(value), list_of(value), one_of(choices)a map, an array, anyOf
string(), strings(values), any(), schema(members)a string, an enumeration, anything, an arbitrary schema object
condition(fields, added)match: the rule fields + geoip/geosite + conditions added by modules + any/all/not
rule(conditions, actions, targets)a rule: match, do, to
conditions(decoders, place)the schemas of conditions from the conditions point allowed in Place::Route/Place::Dns

Example (modules/protocols/anytls/src/lib.rs):

rust
ours_api::Provide::Schema {
    name: "inbounds.anytls",
    schema: |_| ours_config::schema::inbound::<options::AnyTLSInboundOptions>(),
},

GPL-3.0-or-later