Skip to content

Commit

Permalink
feat: add validation for valid topic names
Browse files Browse the repository at this point in the history
  • Loading branch information
netomi committed Jan 29, 2024
1 parent 04fc177 commit c7a68b6
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions otterdog/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from __future__ import annotations

import dataclasses
import re
from typing import Any, Callable, ClassVar, Iterator, Optional, cast

from jsonbender import F, Forall, If, K, OptionalS, S, bend # type: ignore
Expand Down Expand Up @@ -235,11 +236,20 @@ def validate(self, context: ValidationContext, parent_object: Any) -> None:
f"{self.get_model_header()} has 'description' that exceeds the maximum allowed length of 350 chars.",
)

if is_set_and_valid(self.topics) and len(self.topics) > 20:
context.add_failure(
FailureType.ERROR,
f"{self.get_model_header()} has more than 20 'topics' defined.",
)
if is_set_and_valid(self.topics):
if len(self.topics) > 20:
context.add_failure(
FailureType.ERROR,
f"{self.get_model_header()} has more than 20 'topics' defined.",
)

for topic in self.topics:
if not self._valid_topic(topic):
context.add_failure(
FailureType.ERROR,
f"{self.get_model_header()} has defined an invalid topic '{topic}'. "
f"Only lower-case, numbers and '-' are allowed characters.",
)

if is_public and disallow_forking:
context.add_failure(
Expand Down Expand Up @@ -387,6 +397,10 @@ def validate(self, context: ValidationContext, parent_object: Any) -> None:
for env in self.environments:
env.validate(context, self)

@staticmethod
def _valid_topic(topic, search=re.compile(r"[^a-z0-9\-]").search):
return not bool(search(topic))

def include_field_for_diff_computation(self, field: dataclasses.Field) -> bool:
# private repos don't support security analysis.
if self.private is True:
Expand Down

0 comments on commit c7a68b6

Please sign in to comment.