Skip to content

Commit

Permalink
validate target field method
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-gbloom committed Jul 11, 2024
1 parent c25b4fb commit 1beb393
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class ApplicationPackageEntity(EntityBase):
)
bundle_root: Optional[Path] = Field(
title="Folder at the root of your project where artifacts necessary to perform the bundle step are stored.",
default="output/bundle/",
default=Path("output/bundle/"),
)
deploy_root: Optional[Path] = Field(
title="Folder at the root of your project where the build step copies the artifacts",
default="output/deploy/",
default=Path("output/deploy/"),
)
generated_root: Optional[Path] = Field(
title="Subdirectory of the deploy root where files generated by the Snowflake CLI will be written.",
default="__generated/",
default=Path("__generated/"),
)
stage: Optional[str] = IdentifierField(
title="Identifier of the stage that stores the application artifacts.",
Expand Down
30 changes: 18 additions & 12 deletions src/snowflake/cli/api/project/schemas/project_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,26 @@ def validate_entities(cls, entities: Dict[str, Entity]) -> Dict[str, Entity]:
# TODO Automatically detect TargetFields to validate
if entity.type == ApplicationEntity.get_type():
if isinstance(entity.from_.target, TargetField):
target = str(entity.from_.target)
if target not in entities:
raise ValueError(f"No such target: {target}")
else:
# Validate the target type
target_cls = entity.from_.__class__.model_fields["target"]
target_type = target_cls.annotation.__args__[0]
actual_target_type = entities[target].__class__
if target_type and target_type is not actual_target_type:
raise ValueError(
f"Target type mismatch. Expected {target_type.__name__}, got {actual_target_type.__name__}"
)
target_key = str(entity.from_.target)
target_class = entity.from_.__class__.model_fields["target"]
target_type = target_class.annotation.__args__[0]
cls._validate_target_field(target_key, target_type, entities)
return entities

@classmethod
def _validate_target_field(
cls, target_key: str, target_type: Entity, entities: Dict[str, Entity]
):
if target_key not in entities:
raise ValueError(f"No such target: {target_key}")
else:
# Validate the target type
actual_target_type = entities[target_key].__class__
if target_type and target_type is not actual_target_type:
raise ValueError(
f"Target type mismatch. Expected {target_type.__name__}, got {actual_target_type.__name__}"
)

defaults: Optional[DefaultsField] = Field(
title="Default key/value entity values that are merged recursively for each entity.",
default=None,
Expand Down

0 comments on commit 1beb393

Please sign in to comment.