Skip to content

Commit

Permalink
pydantic: use pydantic to check "type"
Browse files Browse the repository at this point in the history
  • Loading branch information
syu-w committed Jul 7, 2023
1 parent d4197d1 commit f2e098a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
11 changes: 2 additions & 9 deletions charmcraft/models/charmcraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import datetime
import pathlib
import os
from typing import Optional, List, Dict, Any, Union
from typing import Optional, List, Dict, Any, Union, Literal

import pydantic

Expand Down Expand Up @@ -116,7 +116,7 @@ class CharmcraftConfig(

metadata_legacy: bool = False

type: str
type: Literal["bundle", "charm"]
name: Optional[pydantic.StrictStr]
summary: Optional[pydantic.StrictStr]
description: Optional[pydantic.StrictStr]
Expand All @@ -140,13 +140,6 @@ class CharmcraftConfig(
links: Optional[Links]
config: Optional[JujuConfig]

@pydantic.validator("type")
def validate_charm_type(cls, charm_type):
"""Verify charm type is valid with exception when instantiated without YAML."""
if charm_type not in ["bundle", "charm"]:
raise ValueError("must be either 'charm' or 'bundle'")
return charm_type

@pydantic.validator("name", pre=True, always=True)
def validate_name(cls, name, values):
"""Verify charm name is valid with exception when instantiated without YAML."""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def test_schema_type_bad_type(
assert str(cm.value) == dedent(
"""\
Bad charmcraft.yaml content:
- must be either 'charm' or 'bundle' in field 'type'"""
- unexpected value; permitted: 'bundle', 'charm' in field 'type'"""
)


Expand Down Expand Up @@ -497,7 +497,7 @@ def test_schema_type_limited_values(
assert str(cm.value) == dedent(
"""\
Bad charmcraft.yaml content:
- must be either 'charm' or 'bundle' in field 'type'"""
- unexpected value; permitted: 'bundle', 'charm' in field 'type'"""
)


Expand Down

0 comments on commit f2e098a

Please sign in to comment.