-
Notifications
You must be signed in to change notification settings - Fork 13
Description
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
- 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 - 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()fromschema_path_validator.tsto 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, thensafeParse()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
- Create:
src/domain/inputs/input_override_validation_service.ts- New validation service - Create:
src/domain/inputs/input_override_validation_service_test.ts- Tests - Modify:
src/domain/inputs/mod.ts- Export new service - Modify:
src/cli/commands/model_method_run.ts- Add validation before overrides - 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"}'