Skip to content

[BUG] Enum validation only works for top-level parameters #179

@alexzerntev

Description

@alexzerntev

Describe the Bug

Enum validation only works for top-level parameters. Enum constraints inside nested object properties are NOT validated at runtime, allowing invalid values to pass through.

To Reproduce

Steps to reproduce the behavior:

  1. Create a tool with a nested object parameter containing an enum field
  2. Run the tool with an invalid value for the nested enum
  3. Observe that no validation error is raised
mxcp run tool test --param preferences='{"theme":"invalid"}'
# Result: Succeeds with "invalid" - no validation error

Expected Behavior

Nested enum values should be validated at runtime, and invalid values should raise a ValidationError with a message like: "Value must be one of: ['light', 'dark', 'auto']".

Environment

  • MXCP Version: latest from pip
  • Python Version: 3.11+
  • Operating System: Any
  • Installation Method: pip

Configuration

parameters:
  - name: preferences
    type: object
    properties:
      theme:
        type: string
        enum: ["light", "dark", "auto"]

Error Output

No error is raised - this is the bug. The invalid value "invalid" is accepted.

Additional Context

Root Cause Analysis:
Enum validation is done in core.py:84-88 at the top-level parameter loop only:

# core.py:82-88
param_schema = param_lookup[name]

# Check enum values first
if param_schema.enum is not None and value not in param_schema.enum:
    raise ValidationError(
        f"Invalid value for {name}. Must be one of: {param_schema.enum}"
    )

# Convert and validate the parameter
validated[name] = TypeConverter.convert_parameter(value, param_schema)

The issue is that:

  1. Enum validation happens BEFORE convert_parameter is called
  2. convert_parameter in converters.py handles nested objects recursively but DOES NOT check enum constraints
  3. There's no enum validation code in converters.py at all

Fix Required:
Add enum validation in converters.py within the string/number/integer type handlers:

# Add to string/number/integer handling in convert_parameter
if schema.enum is not None and value not in schema.enum:
    raise ValidationError(f"Value must be one of: {schema.enum}")

Location in code:

  • core.py:84-88 - Top-level validation (works)
  • converters.py - Missing nested enum validation

Note: Top-level enum validation DOES work correctly. Only nested enums inside object properties are affected.

This happens consistently when using nested enum fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions