Skip to content

Commit

Permalink
feat(prompts): add rule for identifier pattern on ui forms for prompt…
Browse files Browse the repository at this point in the history
… and tag names (#6204)

Rule is relaxed to include underscores.
  • Loading branch information
RogerHYang authored Jan 29, 2025
1 parent 4b8c938 commit afd7e39
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/src/pages/playground/SavePromptForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Button, Flex, View } from "@phoenix/components";
import { SavePromptFormQuery } from "@phoenix/pages/playground/__generated__/SavePromptFormQuery.graphql";
import { PromptComboBox } from "@phoenix/pages/playground/PromptComboBox";

import { identifierPattern } from "../../utils/identifierUtils";

export type SavePromptSubmitHandler = (params: SavePromptFormParams) => void;

export type SavePromptFormParams = {
Expand Down Expand Up @@ -100,6 +102,7 @@ export function SavePromptForm({
message: "Prompt is required",
value: true,
},
pattern: identifierPattern,
}}
render={({ field: { onBlur, onChange } }) => (
<PromptComboBox
Expand Down
8 changes: 3 additions & 5 deletions app/src/pages/prompt/NewPromptVersionTagDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
View,
} from "@phoenix/components";

import { identifierPattern } from "../../utils/identifierUtils";

type PromptVersionTagFormParams = {
name: string;
description: string;
Expand Down Expand Up @@ -99,11 +101,7 @@ export function NewPromptVersionDialog({
control={control}
rules={{
required: "Name is required",
pattern: {
value: /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/,
message:
"Invalid identifier. Must be alphanumeric and with dashes",
},
pattern: identifierPattern,
}}
render={({
field: { name, onChange, onBlur, value },
Expand Down
7 changes: 7 additions & 0 deletions app/src/utils/identifierUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const identifierRegex = /^[a-z0-9]([_a-z0-9-]*[a-z0-9])?$/;

export const identifierPattern = {
value: identifierRegex,
message:
"Invalid identifier. Must be alphanumeric and with dashes or underscores",
};
2 changes: 1 addition & 1 deletion src/phoenix/db/types/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@


class Identifier(RootModel[str]):
root: Annotated[str, Field(pattern=r"^[a-z0-9]([a-z0-9-]*[a-z0-9])?$")]
root: Annotated[str, Field(pattern=r"^[a-z0-9]([_a-z0-9-]*[a-z0-9])?$")]
2 changes: 1 addition & 1 deletion tests/unit/db/types/test_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"a b c",
"αβγ",
*string.punctuation,
*(f"x{p}y" for p in string.punctuation if p not in ("-")),
*(f"x{p}y" for p in string.punctuation if p not in ("_", "-")),
],
)
def test_invalid_identifier(name: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/server/api/routers/v1/test_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def test_get_prompt_version_by_tag_name(
"a b c",
"αβγ",
*(p for p in string.punctuation if p not in (".", "/")),
*(f"x{p}y" for p in string.punctuation if p not in ("-", ".", "/")),
*(f"x{p}y" for p in string.punctuation if p not in ("_", "-", ".", "/")),
],
)
async def test_invalid_identifier(
Expand Down

0 comments on commit afd7e39

Please sign in to comment.