API reference¶
Main interface.
Classes:
|
Array type schema. |
|
Attribute schema. |
|
Attribute mapping schema. |
|
Chunks schema. |
|
Schema container for Coordinates |
|
Data type schema. |
|
A lightweight xarray.DataArray validator. |
|
A lightweight xarray.Dataset validator. |
|
Dimensions schema. |
|
Name schema. |
|
Shape schema. |
|
Context for tracking validation state during schema tree traversal. |
|
Validation behaviour mode. |
|
Result of schema validation with error location mapping. |
Exceptions:
Custom schema error. |
- class xarray_validate.ArrayTypeSchema(array_type)[source]¶
Bases:
BaseSchemaArray type schema.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a string.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(array[, context])Validate array type against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(array, context=None)[source]¶
Validate array type against this schema.
- Parameters:
array (Any) – Array to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.AttrSchema(type=None, value=None, units=None, units_compatible=None)[source]¶
Bases:
BaseSchemaAttribute schema.
- Parameters:
type (type, optional) – Attribute type definition.
Nonemay be used as a wildcard.value (Any) – Attribute value definition.
Nonemay be used as a wildcard.units (str, optional) – Exact unit validation (tolerates different spellings/abbreviations). Uses pint to validate that the attribute value represents the same unit. For example,
units="metre"accepts “metre”, “m”, or “meter”. Requires pint to be installed.units_compatible (str, optional) – Compatible units validation (allows unit conversions). Uses pint to validate that the attribute value is compatible with the specified unit. For example,
units_compatible="metre"accepts “meter”, “kilometre”, “millimetre”, etc. Requires pint to be installed.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a dictionary.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(attr[, context])Validate attribute against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(attr, context=None)[source]¶
Validate attribute against this schema.
- Parameters:
attr (Any) – Attribute value to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.AttrsSchema(attrs, require_all_keys=True, allow_extra_keys=True)[source]¶
Bases:
BaseSchemaAttribute mapping schema.
- Parameters:
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a dictionary.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(attrs[, context])Validate attributes dictionary against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(attrs, context=None)[source]¶
Validate attributes dictionary against this schema.
- Parameters:
attrs (dict) – Attributes dictionary to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.ChunksSchema(chunks)[source]¶
Bases:
BaseSchemaChunks schema.
- Parameters:
chunks (dict or bool) – Chunks definition. If
bool, whether the validated object should be chunked. Ifdict, mapping of dimension name to chunk size.Nonemay be used as a wildcard.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a dictionary.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(chunks, dims, shape[, context])Validate chunks against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(chunks, dims, shape, context=None)[source]¶
Validate chunks against this schema.
- Parameters:
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.CoordsSchema(coords, require_all_keys=True, allow_extra_keys=True)[source]¶
Bases:
BaseSchemaSchema container for Coordinates
- Parameters:
coords (dict) –
Dict of coordinate keys and
DataArraySchemaobjects. Keys can be either exact coordinate names or patterns:Exact match:
'time'matches only ‘time’Glob pattern:
'x_*'matches x_0, x_1, x_foo, etc.Regex pattern:
'{x_\\d+}'matches x_0, x_1, but not x_foo
require_all_keys (bool, default: True) – Whether to require to all coordinates included in
coords. Only applies to exact keys, not pattern keys.allow_extra_keys (bool, default: True) – Whether to allow coordinates not included in
coordsdict. Coordinates matching pattern keys are not considered “extra”.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from basic Python types.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(coords[, context])Validate object against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(coords, context=None)[source]¶
Validate object against this schema.
- Parameters:
value – Object to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.DTypeSchema(dtype)[source]¶
Bases:
BaseSchemaData type schema.
This schema type validates NumPy’s data type objects.
- Parameters:
dtype (DTypeLike) – DataArray dtype. Generic dtypes
np.integerandnp.floatingare supported.
Examples
Basic instantiation and validation work like this:
>>> schema = DTypeSchema("float64") >>> schema.validate(np.dtype("float64"))
Validation uses
numpy.issubdtype()and is therefore strict:>>> schema.validate(np.dtype("float32")) Traceback (most recent call last): ... SchemaError: dtype mismatch: got dtype('float32'), expected dtype('float64')
Generics are supported. For instance:
>>> DTypeSchema("floating").validate(np.ones(5, dtype="float16").dtype) >>> DTypeSchema("integer").validate(np.ones(5, dtype="int32").dtype)
For flexibility, multiple dtypes can be passed. Validation will pass if at least one validates:
>>> schema = DTypeSchema(["float64", "float32"]) >>> schema.validate(np.dtype("float64")) >>> schema.validate(np.dtype("float32")) >>> schema.validate(np.dtype("float16")) Traceback (most recent call last): ... SchemaError: dtype mismatch: got dtype('float16'), expected one of (dtype('float32'), dtype('float64'))
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a dtype-like object.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(dtype[, context])Validate dtype against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(dtype, context=None)[source]¶
Validate dtype against this schema.
- Parameters:
dtype (DTypeLike) – Dtype to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.DataArraySchema(dtype=None, shape=None, dims=None, name=None, coords=None, chunks=None, attrs=None, array_type=None, checks=NOTHING)[source]¶
Bases:
BaseSchemaA lightweight xarray.DataArray validator.
- Parameters:
dtype (DTypeLike or str or DTypeSchema, optional) – Data type validation schema. If a string is specified, it must be a valid NumPy data type value.
shape (ShapeT or tuple or ShapeSchema, optional) – Shape validation schema.
dims (DimsT or list of str or DimsSchema, optional) – Dimensions validation schema.
name (str, optional) – Name validation schema.
coords (CoordsSchema, optional) – Coordinates validation schema.
chunks (bool or dict or ChunksSchema, optional) – If bool, specifies whether the DataArray is chunked or not, agnostic to chunk sizes. If dict, includes the expected chunks for the DataArray.
attrs (AttrsSchema, optional) – Attributes validation schema.
array_type (type, optional) – Type of the underlying data in a DataArray (e.g.
numpy.ndarray).checks (list of callables, optional) – List of callables that will further validate the DataArray.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from basic Python types.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(da[, context, mode])Validate an xarray.DataArray against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(da, context=None, mode=None)[source]¶
Validate an xarray.DataArray against this schema.
- Parameters:
da (DataArray) – DataArray to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
mode ({"eager", "lazy"}, optional) – Validation mode. If unset, the global default mode (eager) is used.
- Returns:
In eager mode, this method returns
None. In lazy mode, it returns aValidationResultobject.- Return type:
ValidationResult or None
- class xarray_validate.DatasetSchema(data_vars=None, require_all_keys=True, allow_extra_keys=True, coords=None, attrs=None, checks=NOTHING)[source]¶
Bases:
BaseSchemaA lightweight xarray.Dataset validator.
- Parameters:
data_vars (dict, optional) –
Per-variable
DataArraySchemas. Keys can be either exact variable names or patterns:Exact match:
'temperature'matches only ‘temperature’Glob pattern:
'x_*'matches x_0, x_1, x_foo, etc.Regex pattern:
'{x_\\d+}'matches x_0, x_1, but not x_foo
require_all_keys (bool, default: True) – Whether to require all data variables included in
data_vars. Only applies to exact keys, not pattern keys.allow_extra_keys (bool, default: True) – Whether to allow data variables not included in
data_varsdict. Variables matching pattern keys are not considered “extra”.coords (CoordsSchema, optional) – Coordinate validation schema.
attrs (AttrsSchema, optional) – Attributes value validation schema.
checks (list of callables, optional) – List of callables that will further validate the Dataset.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from basic Python types.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(ds[, context, mode])Validate an xarray.DataArray against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(ds, context=None, mode=None)[source]¶
Validate an xarray.DataArray against this schema.
- Parameters:
ds (Dataset) – Dataset to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
mode ({"eager", "lazy"}, optional) – Validation mode. If unset, the global default mode (eager) is used.
- Returns:
In eager mode, this method returns
None. In lazy mode, it returns aValidationResultobject.- Return type:
ValidationResult or None
- class xarray_validate.DimsSchema(dims, ordered=True)[source]¶
Bases:
BaseSchemaDimensions schema.
- Parameters:
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from basic Python types.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(dims[, context])Validate dimensions against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod deserialize(obj)[source]¶
Instantiate schema from basic Python types.
Two input types are supported:
a sequence of strings;
a dictionary that contains a
dimsentry (with a sequence of strings as value) and an optionalorderedentry (with a boolean as value).
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(dims, context=None)[source]¶
Validate dimensions against this schema.
- Parameters:
dims (sequence of str) – Dimensions to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.NameSchema(name)[source]¶
Bases:
BaseSchemaName schema.
- Parameters:
name (str) – Name definition.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a string.
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(name[, context])Validate name against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(name, context=None)[source]¶
Validate name against this schema.
- Parameters:
name (str) – Name to validate.
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- exception xarray_validate.SchemaError[source]¶
Bases:
ExceptionCustom schema error.
Methods:
Exception.with_traceback(tb) -- set self.__traceback__ to tb and return self.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class xarray_validate.ShapeSchema(shape)[source]¶
Bases:
BaseSchemaShape schema.
- Parameters:
shape (sequence of (int or None)) – Shape of the DataArray.
Nonemay be used as a wildcard.
Methods:
convert(value)Attempt conversion of
valueto this schema type.deserialize(obj)Instantiate schema from a sequence of integers (use None as a wildcard).
from_yaml(path)Load schema from a YAML file.
Serialize schema to basic Python types.
validate(shape[, context])Validate shape against this schema.
- classmethod convert(value)¶
Attempt conversion of
valueto this schema type.
- classmethod deserialize(obj)[source]¶
Instantiate schema from a sequence of integers (use None as a wildcard).
- classmethod from_yaml(path)¶
Load schema from a YAML file.
- Parameters:
path (path-like) – Path to the YAML file containing the schema definition.
- Return type:
Schema instance deserialized from the YAML file.
- Raises:
ImportError – If ruamel.yaml is not installed.
- validate(shape, context=None)[source]¶
Validate shape against this schema.
- Parameters:
context (ValidationContext, optional) – Validation context for tracking tree traversal state.
- Return type:
None
- Raises:
SchemaError – If validation fails.
- class xarray_validate.ValidationContext(path=NOTHING, mode=ValidationMode.EAGER, result=NOTHING)[source]¶
Bases:
objectContext for tracking validation state during schema tree traversal.
- Parameters:
path (list of str, optional) – Current validation path through the schema tree.
mode (ValidationMode or str, default:
ValidationMode.EAGER) – Validation behavior mode (eager or lazy). Strings are converted to lowercase and passed to theValidationModeconstructor.result (ValidationResult, optional) – Shared result object for collecting errors in lazy mode.
Methods:
Get all collected errors with their paths.
Get current path as dot-separated string.
handle_error(error)Handle validation error based on mode.
push(component)Create a new context with an additional path component.
Attributes:
Check if any errors have been collected.
- handle_error(error)[source]¶
Handle validation error based on mode.
In EAGER mode: raise the error immediately
In LAZY mode: collect error in result object
- Parameters:
error (SchemaError) – Validation error to handle.
- property has_errors¶
Check if any errors have been collected.
- class xarray_validate.ValidationMode(value)[source]¶
Bases:
EnumValidation behaviour mode.
Attributes:
Raise SchemaError on first validation failure (default behavior)
Collect all validation errors and return them in ValidationResult
- EAGER = 'eager'¶
Raise SchemaError on first validation failure (default behavior)
- LAZY = 'lazy'¶
Collect all validation errors and return them in ValidationResult
- class xarray_validate.ValidationResult(errors=NOTHING)[source]¶
Bases:
objectResult of schema validation with error location mapping.
- Parameters:
errors (list of tuple[str, SchemaError]) – List of (path, error) pairs mapping errors to tree locations.
Methods:
add_error(path, error)Add an error at the specified path.
Get a formatted summary of all validation errors.