PropertySchema

Schema to validate object properties

Implements: Schema

Description

The PropertySchema class allows you to create schemas to validate object properties.

Constructors

Creates a new validation rule and sets its arguments. See IValidationRule, TypeCode

PropertySchema(name: str, typ: Any, required: bool, rules: List[IValidationRule])

  • name: str - (optional) a property name
  • typ: Any - (optional) a property type
  • required: bool - (optional) true to always require non-None values.
  • rules: List[IValidationRule] - (optional) a list with validation rules.

Instance methods

get_name

Gets the property name.

get_name(): str

  • returns: str - the property name.

getType

Gets the property type.

get_type(): Any

  • returns: Any - the property type.

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_name

Sets the property name.

set_name(value: str)

  • value: str - a new property name.

set_type

Sets a new property type. The type can be defined as type, type name or TypeCode

set_type(value: Any)

  • value: str - a new property type.

Examples

schema = ObjectSchema().with_property(PropertySchema("id", TypeCode.String))

schema.validate({ id: "1", name: "ABC" })       # Result: no errors
schema.validate({ name: "ABC" })                # Result: no errors
schema.validate({ id: 1, name: "ABC" })         # Result: id type mismatch

See also