Skip to content

Array-element-specific validators #300

@ethanmusser

Description

@ethanmusser

Feature request to support using the built-in value/string validators on individual elements in an array.

Rationale

Sometimes arrays do not represent uniform data and instead are used as ordered groups. For example, an array may represent an ordered list of gains where each gain has different bounds. In this case, the user could simply name each element in the array, but this becomes tedious for large groups or nameless hyperparameters.

Proposed Implementation

The built-in value and string validators could be applied to individual elements of an array.

A new/modified syntax would need to be introduced to call out these array elements. Perhaps something like the following where indices are specified in square brackets. Optionally, support could be included for specifying multiple indices for a single validator via a comma-separated list, ranges, or a wildcard for including all elements.

my_params:
  camera:
    resolution:
      type: int_array
      description: Camera resolution and bitrate as [width, height, bitrate].
      validators:
        #
        # Array validators
        #

        # Array must have size 3
        fixed_size<>: 3

        #
        # Element-specific validators
        #

        # First element must be <= 1920
        lt_eq[0]<>: 1920

        # Second element must be <= 1080
        lt_eq[1]<>: 1080

        # First two elements must be >0
        gt[0,1]<>: 0
        gt[0-1]<>: 0

        # Third element must be one of {128, 256, 512}
        one_of[2]<>: [[128, 256, 512]]

        # Alternate syntax: equivalent to element_bounds<>: [[0, 10000]]
        bounds[*]<>: [[0, 10000]]

Workarounds

Currently this can be implemented with a less-clean syntax with custom validators that accept an index as an argument. E.g.,

my_params:
  camera:
    resolution:
      type: int_array
      description: Camera resolution and bitrate as [width, height, bitrate].
      validators:
        # Bound the first element to range [0, 1920].
        single_element_bounds<>: [0, [0, 1920]]

Metadata

Metadata

Assignees

No one assigned

    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