Skip to content

Commit

Permalink
Add better validation for ButtonPanel actions
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Mar 28, 2024
1 parent 25343a6 commit 5b6366c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
8 changes: 5 additions & 3 deletions schemas/pvi.device.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@
"description": "One-or-more buttons that poke a PV with a value\n\nArgs:\n actions: Dict of button label to value the button sends",
"properties": {
"actions": {
"additionalProperties": {
"type": "string"
},
"default": {
"Go": "1"
},
"description": "PV poker buttons",
"patternProperties": {
"^([A-Z][a-z0-9]*)*$": {
"type": "string"
}
},
"title": "Actions",
"type": "object"
},
Expand Down
12 changes: 7 additions & 5 deletions src/pvi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def enforce_pascal_case(s: str) -> str:
return s[0].upper() + s[1:]


PascalStr = Annotated[str, Field(pattern="^([A-Z][a-z0-9]*)*$")]


class TextFormat(Enum):
"""Format to use for display of Text{Read,Write} widgets on a UI"""

Expand Down Expand Up @@ -177,7 +180,9 @@ class ButtonPanel(WriteWidget):
"""

actions: Dict[str, str] = Field(default={"Go": "1"}, description="PV poker buttons")
actions: Dict[PascalStr, str] = Field(
default={"Go": "1"}, description="PV poker buttons"
)


class TextWrite(WriteWidget):
Expand Down Expand Up @@ -280,10 +285,7 @@ class SubScreen(Layout):


class Named(TypedModel):
name: str = Field(
description="PascalCase name to uniquely identify",
pattern=r"^([A-Z][a-z0-9]*)*$",
)
name: PascalStr = Field(description="PascalCase name to uniquely identify")


class Component(Named):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
from pydantic import ValidationError

from pvi._format.base import Formatter, IndexEntry
from pvi._format.dls import DLSFormatter
Expand Down Expand Up @@ -95,6 +96,11 @@ def test_button(tmp_path, helper):
helper.assert_output_matches(expected_bob, output_bob)


def test_button_raises(tmp_path, helper):
with pytest.raises(ValidationError, match="String should match pattern"):
ButtonPanel(actions={"start": "1", "Stop": "0"})


def test_pva_table(tmp_path, helper):
formatter_yaml = HERE / "format" / "input" / "dls.bob.pvi.formatter.yaml"
formatter = Formatter.deserialize(formatter_yaml)
Expand Down

0 comments on commit 5b6366c

Please sign in to comment.