DefaultingValidator

class lsst.ts.salobj.DefaultingValidator(schema: dict[str, typing.Any], StandardValidatorClass: ~typing.Type[~jsonschema.validators.Draft7Validator] = <class 'jsonschema.validators.Draft7Validator'>)

Bases: object

A wrapper for jsonschema validators that applies default values.

Parameters
schemadict

Schema against which to validate.

ValidatorClassjsonschema.protocols.Validator, optional

jsonschema validator class, e.g. jsonschema.Draft7Validator.

Notes

Default values are handled at most 2 levels deep in an object hierarchy. For deeper hierarchies, set the default at a higher level. For example:

type: object
properties:
  number1:
    type: number
    default: 1
  subdict1:
    type: object
    properties:
      number2:
        type: number
        default: 2
      subdict2:
        type: object
        properties:
          number3:
            type: number
            # default is ignored this deep; set it at a higher level
        default:
          number3: 3

This class is not a jsonschema.IValidator but it contains two validators:

  • defaults_validator: a validator that sets default values in the data being validated

  • final_validator: a standard validator that does not alter the data being validated.

Methods Summary

validate(data_dict)

Validate data.

Methods Documentation

validate(data_dict: dict[str, Any] | None) dict[str, Any]

Validate data.

Set missing values based on defaults in the schema, then check the final result against the schema (in case any defaults are not valid).

Parameters
data_dictdict or None

Data to validate. If None then an empty dict is used.

Returns
resultdict

Validated data. A copy of data_dict with missing values that have defaults set to those defaults.

Raises
jsonschema.exceptions.ValidationError

If the data does not match the schema.