Skip to content

Validate step input overrides against model schema #244

@stack72

Description

@stack72

Context

PR #240 added "implicit inputs" - the ability for step/CLI inputs to override model definition attributes. Currently, inputs are applied blindly without validation:

for (const [key, value] of Object.entries(inputs)) {
  evaluatedDefinition.setAttribute(key, value);
}

Requirements

  1. Fail if input key doesn't exist: If a user provides an input with a key that doesn't exist in the model's inputAttributesSchema, throw an error
  2. Fail if type doesn't match: If a user provides a value with a type that doesn't match the expected type in the schema (e.g., providing an integer where a string is expected), throw an error

Proposed Approach

Create an InputOverrideValidationService that validates input overrides against a model's Zod schema. Use existing utilities from schema_path_validator.ts and patterns from method_execution_service.ts.

1. Create InputOverrideValidationService

File: src/domain/inputs/input_override_validation_service.ts

export interface InputOverrideValidationResult {
  valid: boolean;
  errors: InputOverrideError[];
}

export interface InputOverrideError {
  key: string;
  message: string;
  suggestion?: string;
  availableKeys?: string[];
}

export class InputOverrideValidationService {
  validate(
    inputs: Record<string, unknown>,
    schema: z.ZodTypeAny,
  ): InputOverrideValidationResult;
}

Logic:

  • Use getObjectKeys() from schema_path_validator.ts to get valid keys from schema
  • For each input key, check if it exists in schema keys
  • If key doesn't exist, use findClosestKey() for typo suggestions
  • For type validation, use getObjectProperty() to get the property schema, then safeParse() to validate the value

2. Update CLI Path

File: src/cli/commands/model_method_run.ts (lines 189-192)

Validate inputs before applying them as overrides. Throw UserError with helpful message listing unknown keys or type mismatches.

3. Update Workflow Path

File: src/domain/workflows/execution_service.ts (lines 270-273)

Same pattern as CLI path - validate step inputs against the model's schema before applying overrides.

4. Add Tests

File: src/domain/inputs/input_override_validation_service_test.ts

Test cases:

  • Valid input key with correct type passes
  • Invalid input key fails with helpful error message
  • Valid key with wrong type fails (e.g., string instead of number)
  • Typo suggestions work correctly
  • Empty inputs always pass

Files to Modify

  1. Create: src/domain/inputs/input_override_validation_service.ts - New validation service
  2. Create: src/domain/inputs/input_override_validation_service_test.ts - Tests
  3. Modify: src/domain/inputs/mod.ts - Export new service
  4. Modify: src/cli/commands/model_method_run.ts - Add validation before overrides
  5. Modify: src/domain/workflows/execution_service.ts - Add validation before overrides

Manual Testing

# Test invalid key (should fail)
swamp model method run testEcho run --input '{"invalidKey": "value"}'

# Test type mismatch (should fail)
swamp model method run testEcho run --input '{"message": 123}'

# Test valid override (should succeed)
swamp model method run testEcho run --input '{"message": "override"}'

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions