ArraySchema

Schema to validate arrays.

Implements: Schema

Description

The ArraySchema class allows you to validate arrays based on a specified validation rule.

Constructors

Creates a new validation rule and sets its values. See TypeCode

ArraySchema(valueType: Any = None, required: bool = None, rules: List[IValidationRule] = None)

  • valueType: Any - a type of array elements. Null means that elements may have any type.
  • required: bool - (optional) true to always require non-None values.
  • rules: List[IValidationRule] - (optional) a list with validation rules.

Instance methods

get_value_type

Gets the type of array elements. Null means that elements may have any type.

get_value_type(): Any

  • returns: Any - the type of array elements.

perform_validation

Validates a given value against the schema and configured validation rules.

_perform_validation(path: str, value: Any, results: List[ValidationResult])

  • path: str - a dot notation path to the value.
  • value: Any - a value to be validated.
  • results: List[ValidationResult] - a list with validation results to add new results.

set_value_type

Sets the type of array elements. Null means that elements may have any type.

set_value_type(value: Any)

  • value: Any - a type of array elements.

Examples

schema = ArraySchema(TypeCode.String)

schema.validate(["A", "B", "C"])    # Result: no errors
schema.validate([1, 2, 3])          # Result: element type mismatch
schema.validate("A")                # Result: type mismatch