# pysparky.data_validator

*module*

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L1-L1)

## pysparky.data_validator.DataValidator

*class* · *dataclass*

```python
class DataValidator
```

A class to validate data based on a set of validation rules.

**Attributes**

- `rules` (`list[ValidationRule]`) — A list of validation rules.

> **Example**
>
> ```python
> >>> ValidationRules = [
> ...     ValidationRule("first_name_check", F_.is_printable_only("first_name")),
> ...     ValidationRule("last_name_check", F_.is_printable_only("last_name")),
> ...     ValidationRule("address_check", F_.is_printable_only("address")),
> ...     ValidationRule("region_check", F_.is_printable_only("region")),
> ...     ValidationRule("code_check", [F_.is_two_character_only("code")]),
> ...     ValidationRule("postcode_check", F_.is_printable_only("postcode")),
> ... ]
> 
> >>> validator = DataValidator(ValidationRules)
> 
> >>> conditions = {
> ...     "first_name_check": F_.is_printable_only("first_name"),
> ...     "last_name_check": F_.is_printable_only("last_name"),
> ...     "address_check": F_.is_printable_only("address"),
> ...     "region_check": F_.is_printable_only("region"),
> ...     "code_check": [F_.is_two_character_only("code")],
> ...     "postcode_check": F_.is_printable_only("postcode"),
> ... }
> 
> >>> validator = DataValidator.from_dict(conditions)
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L36-L188)

### pysparky.data_validator.DataValidator.rules

*attribute* · *instance attribute*

```python
rules: list[ValidationRule]
```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L70-L70)

### pysparky.data_validator.DataValidator.query_map

*property*

```python
query_map: dict[str, Column]
```

Gets a dictionary mapping rule names to their combined conditions.
The key is the column name and the value is the Column Object.

**Returns**

- (`dict[str, Column]`) — dict[str, Column]: A dictionary where keys are rule names and values are combined conditions.

> **Example**
>
> ```python
> >>> sdf.withColumns(validator.query_map)
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L101-L116)

### pysparky.data_validator.DataValidator.apply_conditions

*method*

```python
def apply_conditions(sdf: DataFrame) -> DataFrame
```

Applies the combined conditions to the Spark DataFrame.

**Parameters**

- `sdf` (`DataFrame`) — The Spark DataFrame to which the conditions will be applied.

**Returns**

- `DataFrame` (`DataFrame`) — The Spark DataFrame with the conditions applied.

> **Example**
>
> ```python
> >>> validator.apply_conditions(data_sdf)
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L118-L134)

### pysparky.data_validator.DataValidator.filter_invalid

*method*

```python
def filter_invalid(sdf: DataFrame) -> DataFrame
```

Filters out invalid rows from the Spark DataFrame based on the rules.

**Parameters**

- `sdf` (`DataFrame`) — The Spark DataFrame to be filtered.

**Returns**

- `DataFrame` (`DataFrame`) — The Spark DataFrame with invalid rows filtered out.

> **Example**
>
> ```python
> >>> validator.filter_invalid(data_sdf)
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L136-L161)

### pysparky.data_validator.DataValidator.filter_valid

*method*

```python
def filter_valid(sdf: DataFrame) -> DataFrame
```

Filters out valid rows from the Spark DataFrame based on the rules.

**Parameters**

- `sdf` (`DataFrame`) — The Spark DataFrame to be filtered.

**Returns**

- `DataFrame` (`DataFrame`) — The Spark DataFrame with valid rows filtered out.

> **Example**
>
> ```python
> >>> validator.filter_valid(data_sdf).select(data_sdf.columns).show()
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L163-L188)

### pysparky.data_validator.DataValidator.from_dict

*method* · *classmethod*

```python
def from_dict(data: dict[str, Union[list[Column], Column]]) -> DataValidator
```

Creates a DataValidator instance from a dictionary.

**Parameters**

- `data` (`dict[str, list[Column] | Column]`) — A dictionary where keys are rule names and values are lists of conditions or a single condition.

**Returns**

- `DataValidator` (`DataValidator`) — An instance of DataValidator.

> **Example**
>
> ```python
> >>> conditions = {
> ...     "first_name_check": F_.is_printable_only("first_name"),
> ...     "last_name_check": F_.is_printable_only("last_name"),
> ...     "address_check": F_.is_printable_only("address"),
> ...     "region_check": F_.is_printable_only("region"),
> ...     "code_check": [F_.is_two_character_only("code")],
> ...     "postcode_check": F_.is_printable_only("postcode"),
> ... }
> >>> validator = DataValidator.from_dict(conditions)
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L72-L98)

## pysparky.data_validator.ValidationRule

*class* · *dataclass*

```python
class ValidationRule
```

A class to represent a validation rule.

**Attributes**

- `name` (`str`) — The name of the validation rule.
- `conditions` (`list[Column]`) — A list of conditions (Spark Columns) that make up the rule.
- `combined_condition` (`Column`) — The combined condition of all the conditions using logical AND. It will generate from conditions

> **Example**
>
> ValidationRule("first_name_check", F_.printable_only("first_name")),

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L12-L33)

### pysparky.data_validator.ValidationRule.combined_condition

*attribute* · *class attribute* · *instance attribute*

```python
combined_condition: Column = field(init=False)
```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L29-L29)

### pysparky.data_validator.ValidationRule.conditions

*attribute* · *instance attribute*

```python
conditions: list[Column] | Column
```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L28-L28)

### pysparky.data_validator.ValidationRule.name

*attribute* · *instance attribute*

```python
name: str
```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/data_validator.py#L27-L27)
