[BUG] or another strange behavior of input validation in rule sets

@Rebekka, i run into another issue with the input validation :frowning:

CMK version: 2.4.0p17.cee
OS version: 2.4.0p17.cee

If you have a rule set with a list of string and a input validation
for the strings in the list, the input validation marks correct
entries as invalid and removes correct entries from the list.

Here the issue in action

Start with a list of four entries, two valid, thow invalid

Try to save the rule, the first invalid entry gets marked as invalied → correct behavior

Remove the marked entry

Try to save again

The input validation finds the the second invalid entry, but marks a valid entry as invalid, adds an empty field at the beginning of the list, and removs the last entry from the list (which was a valid entry).

Here a very basic script to reproduce the issue.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from cmk.rulesets.v1 import Title
from cmk.rulesets.v1.form_specs import (
    DictElement,
    Dictionary,
    List,
    String,
)
from cmk.rulesets.v1.form_specs.validators import (
    Message,
    ValidationError,
)
from cmk.rulesets.v1.rule_specs import ActiveCheck, Topic

allowed_hosts = [
    'C9540-7-1',
    'C9348-7-1',
    'C9348-5-2',
]


class ValidateMonitoredHost:
    def __call__(self, value: str) -> None:
        if value not in allowed_hosts:
            raise ValidationError(
                Message(f'{value} not allowed. {",".join(allowed_hosts)} are allowed only')
            )


def parameter_form_nvdct_test() -> Dictionary:
    return Dictionary(
        elements={
            'seed_devices': DictElement(
                parameter_form=List(
                    title=Title('Seed Devices'),
                    element_template=String(  # should be MonitoredHost
                        custom_validate=[ValidateMonitoredHost()],
                    ),
                )),
        })


rule_spec_nvdct_test = ActiveCheck(
    topic=Topic.NETWORKING,
    title=Title('NVDCT Test'),
    name='nvdct_test',
    parameter_form=parameter_form_nvdct_test,
)

Cheers
Thomas

2 Likes