Skip to content

Commit

Permalink
Merge pull request #3 from stackhpc/feature/migrate-to-pydantic2
Browse files Browse the repository at this point in the history
Changes to support Pydantic v2
  • Loading branch information
mkjpryor authored Nov 9, 2023
2 parents c208718 + e4dd3ab commit d032154
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pyhelm3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

from pydantic import (
BaseModel,
TypeAdapter,
Field,
PrivateAttr,
DirectoryPath,
FilePath,
AnyUrl,
HttpUrl,
AnyUrl as PydanticAnyUrl,
HttpUrl as PydanticHttpUrl,
constr,
validator
field_validator
)
from pydantic.functional_validators import AfterValidator

from .command import Command, SafeLoader

Expand All @@ -37,11 +39,11 @@ def __init__(self, _command: Command, **kwargs):


#: Type for a name (chart or release)
Name = constr(regex = r"^[a-z0-9-]+$")
Name = constr(pattern = r"^[a-z0-9-]+$")


#: Type for a SemVer version
SemVerVersion = constr(regex = r"^v?\d+\.\d+\.\d+(-[a-zA-Z0-9\.\-]+)?(\+[a-zA-Z0-9\.\-]+)?$")
SemVerVersion = constr(pattern = r"^v?\d+\.\d+\.\d+(-[a-zA-Z0-9\.\-]+)?(\+[a-zA-Z0-9\.\-]+)?$")


#: Type variables for forward references to the chart and release types
Expand All @@ -50,6 +52,17 @@ def __init__(self, _command: Command, **kwargs):
ReleaseRevisionType = t.TypeVar("ReleaseRevisionType", bound = "ReleaseRevision")


#: Type annotation for validating a string using a Pydantic type
def validate_str_as(validate_type):
adapter = TypeAdapter(validate_type)
return lambda v: str(adapter.validate_python(v))


#: Annotated string types for URLs
AnyUrl = t.Annotated[str, AfterValidator(validate_str_as(PydanticAnyUrl))]
HttpUrl = t.Annotated[str, AfterValidator(validate_str_as(PydanticHttpUrl))]


class ChartDependency(BaseModel):
"""
Model for a chart dependency.
Expand Down Expand Up @@ -200,7 +213,7 @@ class Chart(ModelWithCommand):
_crds: t.List[t.Dict[str, t.Any]] = PrivateAttr(None)
_values: t.Dict[str, t.Any] = PrivateAttr(None)

@validator("ref")
@field_validator("ref")
def ref_is_abspath(cls, v):
"""
If the ref is a path on the filesystem, make sure it is absolute.
Expand Down

0 comments on commit d032154

Please sign in to comment.