Skip to content

Required schema args not enforced: unresolved path params sent as literals #96

Description

@MattDevy

Bug

When a command has required path parameters (like cluster info's --target) but the user provides no input at all (no CLI flags, no stdin, no --input-file), the handler runs with parsed.input as undefined. The Zod validation step is skipped entirely, and the required {param} placeholder remains as a literal string in the URL sent to Elasticsearch.

Steps to reproduce

$ elastic es cluster info

Expected: Error message indicating --target is required.

Actual:

{
  "error": {
    "code": "transport_error",
    "status_code": 400,
    "body": {
      "error": {
        "type": "illegal_argument_exception",
        "reason": "request [/_info/{target}] contains unrecognized target: [{target}]"
      }
    }
  }
}

The literal {target} is sent in the URL path.

Root cause

src/factory.ts, in the cmd.action() handler:

  1. Schema-derived args are registered with cmd.option() (optional), never cmd.requiredOption()
  2. When no input is provided at all, inputValue remains undefined
  3. The validation block (if (inputValue !== undefined) { ... safeParse ... }) is skipped
  4. parsed.input stays undefined, and the handler runs
  5. interpolatePath() in the request builder can't substitute {target} because input is undefined, and arg.required is true so the optional-path-stripping branch doesn't apply

Suggested fix

Options:

  1. Register required schema args with cmd.requiredOption() instead of cmd.option() so Commander enforces them before the handler runs
  2. Run Zod validation even when inputValue is undefined: construct a minimal input from CLI args (even if empty) and validate it to catch missing required fields
  3. In interpolatePath(), check for unresolved {param} placeholders after interpolation and throw a clear error

Option 2 is probably the most robust since it catches required body/query fields too, not just path params.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions