Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema validation) : pydantic migratation #192

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions disdrodb/l0/check_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List, Optional, Union

import numpy as np
from pydantic import BaseModel, ValidationError, validator
from pydantic import BaseModel, ValidationError, field_validator, model_validator

from disdrodb.l0.standards import (
available_sensor_name,
Expand Down Expand Up @@ -128,25 +128,31 @@ class NetcdfEncodingSchema(BaseModel):
chunksizes: Optional[Union[int, List[int]]]

# if contiguous=False, chunksizes specified, otherwise should be not !
@validator("chunksizes")
def check_chunksizes(cls, v, values):
if not values.get("contiguous") and not v:
@model_validator(mode="before")
def check_chunksizes_and_zlib(cls, values):
contiguous = values.get("contiguous")
chunksizes = values.get("chunksizes")
if not contiguous and not chunksizes:
raise ValueError("'chunksizes' must be defined if 'contiguous' is False")
return v
return values

# if contiguous = True, then zlib must be set to False
@validator("zlib")
def check_zlib(cls, v, values):
if values.get("contiguous") and v:
@model_validator(mode="before")
def check_contiguous_and_zlib(cls, values):
contiguous = values.get("contiguous")
zlib = values.get("zlib")
if contiguous and zlib:
raise ValueError("'zlib' must be set to False if 'contiguous' is True")
return v
return values

# if contiguous = True, then fletcher32 must be set to False
@validator("fletcher32")
def check_fletcher32(cls, v, values):
if values.get("contiguous") and v:
@model_validator(mode="before")
def check_contiguous_and_fletcher32(cls, values):
contiguous = values.get("contiguous")
fletcher32 = values.get("fletcher32")
if contiguous and fletcher32:
raise ValueError("'fletcher32' must be set to False if 'contiguous' is True")
return v
return values


def check_l0b_encoding(sensor_name: str) -> None:
Expand Down Expand Up @@ -199,12 +205,12 @@ class RawDataFormatSchema(BaseModel):
n_decimals: Optional[int]
n_naturals: Optional[int]
data_range: Optional[List[float]]
nan_flags: Optional[str]
valid_values: Optional[List[float]]
dimension_order: Optional[List[str]]
n_values: Optional[int]
nan_flags: Optional[Union[int, str]] = None
valid_values: Optional[List[float]] = None
dimension_order: Optional[List[str]] = None
n_values: Optional[int] = None

@validator("data_range", pre=True)
@field_validator("data_range")
def check_list_length(cls, value):
if value:
if len(value) != 2:
Expand Down
4 changes: 2 additions & 2 deletions disdrodb/tests/test_config_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest
import yaml
from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator


# Define the pydantic models for *.bins.yaml config files
Expand All @@ -13,7 +13,7 @@ class raw_data_format_2n_level(BaseModel):
n_decimals: Optional[int]
data_range: Optional[list]

@validator("data_range", pre=True)
@field_validator("data_range")
def check_list_length(cls, value):
if value:
if len(value) != 2:
Expand Down
Loading