Skip to content

Schema Export

dreamcli can export the complete CLI schema as JSON — for tooling, documentation generation, IDE integration, or config file validation.

Two Export Formats

Definition Metadata

generateSchema() produces a JSON document describing the full CLI tree: commands, flags, args, types, constraints, env bindings, prompts, and more.

ts
import {  } from 'node:fs';
Cannot find name 'node:fs'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
import { , , , } from '@kjanat/dreamcli'; const = ('mycli').(('deploy')); const = (.); ( 'cli-schema.json', .(, null, 2), );

Output includes a $schema URL pointing at the CDN-hosted definition schema. For offline or CI-friendly setups, use the local copy instead:

json
{
  "$schema": "./node_modules/@kjanat/dreamcli/dreamcli.schema.json"
}

The schema is also importable as @kjanat/dreamcli/schema.

Full example output:

json
{
  "$schema": "https://cdn.jsdelivr.net/npm/@kjanat/dreamcli/dreamcli.schema.json",
  "name": "mycli",
  "version": "1.0.0",
  "commands": [
    {
      "name": "deploy",
      "description": "Deploy the app",
      "flags": {
        "region": {
          "kind": "enum",
          "presence": "defaulted",
          "defaultValue": "us",
          "enumValues": ["us", "eu", "ap"],
          "envVar": "REGION"
        }
      },
      "args": [
        {
          "name": "target",
          "kind": "string",
          "presence": "required"
        }
      ],
      "commands": []
    }
  ]
}

Input Validation Schema

generateInputSchema() produces a JSON Schema (draft 2020-12) that validates CLI input as a JSON object — useful for config file validation.

ts
import {  } from 'node:fs';
Cannot find name 'node:fs'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
import { , , , } from '@kjanat/dreamcli'; const = ('mycli').(('deploy')); const = (.); ( 'input-schema.json', .(, null, 2), );

For multi-command CLIs, the output is a oneOf discriminated union with a command property identifying each branch:

json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "oneOf": [
    {
      "type": "object",
      "properties": {
        "command": { "const": "deploy" },
        "region": {
          "type": "string",
          "enum": ["us", "eu", "ap"]
        },
        "target": { "type": "string" }
      },
      "required": ["command", "region", "target"]
    }
  ]
}

You can also pass a single CommandSchema for a flat object schema without the command discriminator.

Nested subcommands use dot-delimited paths ("deploy.rollback").

Adding a Schema Command

ts
import {  } from '@kjanat/dreamcli';
import {
  ,
  ,
  ,
  ,
} from '@kjanat/dreamcli';

const  = ('mycli');

const  = ('schema')
  .('Export CLI schema as JSON')
  .(
    'input',
    
      .()
      .('Output JSON Schema for input validation'),
  )
  .(({ ,  }) => {
    const  = .
      ? (.)
      : (.);
    .();
  });

Options

Both functions accept JsonSchemaOptions:

OptionDefaultDescription
includeHiddentrueInclude commands marked as hidden
includePromptstrueInclude prompt config on flags (definition only)
ts
import {
  ,
  ,
  ,
} from '@kjanat/dreamcli';

const  = ('mycli').(('deploy'));

(., { : false });

What's Included

Definition Metadata

Per command: name, description, aliases, hidden, examples, flags, args, nested commands.

Per flag: kind, presence, defaultValue, aliases, envVar, configPath, description, enumValues, elementSchema, prompt, deprecated, propagate.

Per arg: name, kind, presence, variadic, stdinMode, defaultValue, description, envVar, enumValues, deprecated.

What's Omitted

Non-serializable runtime values are always excluded:

  • Parse functions (parseFn)
  • Middleware handlers
  • Interactive resolvers
  • Action handlers

What's Next?

Released under the MIT License.