Skip to content

Adding a lab file key ​

How to add a new key to lab.yaml, from the model to the docs. A key touches four places that must agree: the Swift model, the JSON Schema, the validator and the runtime.

IMPORTANT

Open an issue before you start. The lab file is the public interface of vz, and a key is hard to remove once people use it.

Checklist ​

  • [ ] Model: property, CodingKeys case, default in the decoder
  • [ ] JSON Schema: property with type, description and default
  • [ ] Validator: rules that decoding cannot check
  • [ ] Runtime: a machine part, a switch or link setting, or a platform change
  • [ ] Tests: LoaderTests, ValidatorTests, fixtures
  • [ ] Docs: Lab file reference, and Validation for new rules
  • [ ] Examples: feature tour 22 (node keys) or 23 (switch keys)
  • [ ] make test-core and make schema-check pass

1. Model ​

The model lives in Sources/LabCore/Model. Every type decodes strictly so that it behaves like the JSON Schema:

  • try decoder.rejectUnknownKeys(CodingKeys.self) rejects any key that CodingKeys does not list, with "unknown key 'x' (allowed: ...)".
  • Optional keys use c.decode(.key, default: value) from StrictDecoding.swift, so a missing key gets its documented default.
  • Values with the right type but a bad format throw invalidValue(decoder, "...") with a readable message.

For example, a new node flag foo:

swift
public struct Node: Sendable, Equatable {
    // ...
    public var foo: Bool
}

extension Node: Decodable {
    enum CodingKeys: String, CodingKey, CaseIterable {
        case name, image, cpus, memory, nestedVirtualization, secureBoot, rosetta, consoleLog
        case disks, `init`, shares, displays, clipboard, sound, usb, interfaces
        case foo
    }

    public init(from decoder: Decoder) throws {
        try decoder.rejectUnknownKeys(CodingKeys.self)
        let c = try decoder.container(keyedBy: CodingKeys.self)
        // ...
        foo = try c.decode(.foo, default: false)
    }
}

Also add the parameter, with the same default, to the memberwise init that tests use.

NOTE

LabCore must not import Virtualization or vmnet. Keep the model in plain Swift types, and translate to framework types in LabRuntime.

2. JSON Schema ​

Edit schema/27.json. Every object has "additionalProperties": false, so a key missing from the schema makes valid lab files fail make schema-check and editor validation.

json
"foo": {
  "description": "What it does, and the framework API it maps to.",
  "type": "boolean",
  "default": false
}

Reuse the shared definitions in $defs (name, size, mac, ipv4Cidr, mtu, port, ...) instead of new patterns. Keep the description close to the doc comment in Swift: users see it in their editor through the yaml-language-server modeline.

3. Validator ​

Validator.swift checks what decoding cannot: ranges, references between sections, combinations of keys, host limits.

  • Add the rule to the matching check... function of Run.
  • Report with error(path, message) or warning(path, message). The path points into the file the way users read it: nodes[\(i)].foo, switches.\(name).mtu, nodes[\(i)].disks[\(j)].cache.
  • Never stop at the first problem. Every issue is collected so users fix them all at once.
  • Write messages that say what to do: "cache needs overlay: true", "port forwarding needs NAT; set nat4 or nat6 to true".
  • Use a warning for things that may work (host-dependent features, documented vmnet limits), an error for things that will not.

Rules that depend on the OS image belong in Composition.validate in Composition.swift, not in LabCore.

4. Runtime ​

Where the key takes effect depends on what it is:

Key affectsChange
Node hardware, the same for every platformA part in LabHardware.swift, added in LabHardware.parts(for:plugs:claimed:)
Firmware, console, display or first-boot setupThe platform adapters in Sources/LabOS/Platforms: GenericPlatform.swift (GenericUEFI), ApplePlatform.swift. A key one platform cannot use is rejected in that platform's validate
DisksDiskBuilder.swift
SwitchesSwitchBuilder.swift
LinksLinkBuilder.swift
Something a running lab can changeAn operation in LabRunner.perform, a command in RuntimeChanges.swift, and the control protocol docs

A part reads the lab file switch once and installs devices into the MachineAssembly. Parts never check each other. See VM assembly.

If the key changes vz node ls or vz topology output, update the report types and their --json shape too.

5. Tests ​

  • LoaderTests.swift: the default when the key is missing, a written value, and a bad value with its path.
  • ValidatorTests.swift: one test per rule, with a small inline lab (issues(lab(...)), hasError, hasWarning).
  • Fixtures in Tests/LabCoreTests/Fixtures: add the key to a valid/ file if it is common. A new invalid/ file needs a matching case in LoaderTests.
  • Runtime behavior that needs Virtualization goes in LabRuntimeTests (run with make test).
bash
make test-core
make schema-check

6. Docs and examples ​

  • Add the key to Lab file reference with its type, default and an example.
  • Add new errors and warnings to Validation.
  • Mention it in the guide where users look for it (for example Storage for a disk key).
  • Show it in a feature tour: lab 22 for node keys, lab 23 for switch keys. Say in its README how to see the difference. Commented-out lines work for host-dependent features.

See also ​