Description
The Schema class provides a basic schema to validate values against a set of validation rules.
Important points
- This schema is used as a basis for specific schemas to validate objects, project properties, arrays and maps.
Constructors
Creates a new instance of validation schema and sets its values.
See IValidationRule
Schema(required: bool, rules: List[IValidationRule])
- required: bool - (optional) true to always require non-None values.
- rules: List[IValidationRule] - (optional) a list with validation rules.
Instance methods
get_rules
Gets validation rules to check values against.
get_rules(): List[IValidationRule]
- returns: List[IValidationRule] - a list with validation rules.
is_required
Gets a flag that always requires non-None values. For None values it raises a validation error.
is_required(): bool
- returns: bool - true to always require non-None values and false to allow None values.
make_optional
Makes validated values optional. Validation for None values will be skipped. This method returns reference to this exception to implement the Builder pattern to chain additional calls.
make_optional(): Schema
- returns: Schema - this validation schema
make_required
Makes validated values always required (non-None). For None values the schema will raise errors. This method returns reference to this exception to implement the Builder pattern to chain additional calls.
make_required(): Schema
- returns: Schema - this validation schema
perform_type_validation
Validates a given value to match specified type. The type can be defined as a Schema, type, a type name or TypeCode When type is a Schema, it executes validation recursively against that Schema.
_perform_type_validation(path: str, type: Any, value: Any, results: List[ValidationResult])
- path: str - a dot notation path to the value.
- type: Any - a type to match the value type
- value: Any - a value to be validated.
- results: List[ValidationResult] - a list with validation results to add new results.
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_required
Sets a flag that always requires non-None values.
set_required(value: bool)
- value: bool - true to always require non-None values and false to allow None values.
set_rules
Sets validation rules to check values against.
set_rules(value: List[IValidationRule])
- value: List[IValidationRule] - a list with validation rules.
validate
Validates the given value and returns a list with the validation results. See ValidationResult.
validate(value: Any): List[IValidationRule]
- value: Any - a value to be validated.
- returns: List[IValidationRule] - a list with validation results.
validate_and_return_exception
Validates the given value and returns a ValidationException if errors were found.
validate_and_return_exception(correlation_id: Optional[str], value: Any, strict: bool = False): ValidationException
- correlation_id: Optional[str] - (optional) transaction id to trace execution through call chain.
- value: Any - a value to be validated.
- strict: boolean = false - true to treat warnings as errors.
- returns: ValidationException - validation exception.
validate_and_throw_exception
Validates the given value and throws a ValidationException if errors were found.
See ValidationException.throw_exception_if_needed
validate_and_throw_exception(correlation_id: Optional[str], value: Any, strict: boolean = false)
- correlation_id: Optional[str] - (optional) transaction id to trace execution through call chain.
- value: Any - a value to be validated.
- strict: bool = false - true to treat warnings as errors.
with_rule
Adds validation rule to this schema. This method returns reference to this exception to implement the Builder pattern to chain additional calls.
with_rule(rule: IValidationRule): Schema
- rule: IValidationRule - a validation rule to be added.
- returns: Schema - this validation schema.