From c0fe653f6f2469dc5de2585e5b2c2e6e4e25be76 Mon Sep 17 00:00:00 2001 From: ted chang Date: Tue, 17 Aug 2021 08:49:55 -0700 Subject: [PATCH] Validate project name at apply (#1766) Signed-off-by: ted chang --- sdk/python/feast/repo_config.py | 20 +++++++++++++++- .../scaffolding/test_repo_config.py | 24 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/sdk/python/feast/repo_config.py b/sdk/python/feast/repo_config.py index afcf30e848..7a5b2e6568 100644 --- a/sdk/python/feast/repo_config.py +++ b/sdk/python/feast/repo_config.py @@ -2,7 +2,14 @@ from typing import Any import yaml -from pydantic import BaseModel, StrictInt, StrictStr, ValidationError, root_validator +from pydantic import ( + BaseModel, + StrictInt, + StrictStr, + ValidationError, + root_validator, + validator, +) from pydantic.error_wrappers import ErrorWrapper from pydantic.typing import Dict, Optional, Union @@ -180,6 +187,17 @@ def _validate_offline_store_config(cls, values): return values + @validator("project") + def _validate_project_name(cls, v): + from feast.repo_operations import is_valid_name + + if not is_valid_name(v): + raise ValueError( + f"Project name, {v}, should only have " + f"alphanumerical values and underscores but not start with an underscore." + ) + return v + class FeastConfigError(Exception): def __init__(self, error_message, config_path): diff --git a/sdk/python/tests/integration/scaffolding/test_repo_config.py b/sdk/python/tests/integration/scaffolding/test_repo_config.py index f4e15d497f..dfa80cb618 100644 --- a/sdk/python/tests/integration/scaffolding/test_repo_config.py +++ b/sdk/python/tests/integration/scaffolding/test_repo_config.py @@ -153,3 +153,27 @@ def test_no_project(): "project\n" " field required (type=value_error.missing)", ) + + +def test_invalid_project_name(): + _test_config( + dedent( + """ + project: foo-1 + registry: "registry.db" + provider: local + """ + ), + expect_error="alphanumerical values ", + ) + + _test_config( + dedent( + """ + project: _foo + registry: "registry.db" + provider: local + """ + ), + expect_error="alphanumerical values ", + )