@@ -4,23 +4,54 @@ import { Select } from "../../shared/ui/select";
44import { VisualJsonEditor } from "../../shared/ui/visual-json-editor" ;
55import { FormField } from "../../widgets/auth-shell/FormField" ;
66
7+ type ConfigEntryContentType = "text" | "number" | "boolean" | "json" ;
8+ type ConfigEntryScope = "client" | "server" | "all" ;
9+
710interface ProjectParamCreateFormProps {
811 onCancel : ( ) => void ;
912 onSubmit : ( data : {
1013 key : string ;
1114 value : string ;
12- contentType : "text" | "number" | "boolean" | "json" ;
13- scope : "client" | "server" | "all" ;
15+ contentType : ConfigEntryContentType ;
16+ scope : ConfigEntryScope ;
1417 displayName : string ;
1518 description : string ;
1619 } ) => void ;
1720 isPending : boolean ;
1821}
1922
23+ export function isValidConfigEntryValue ( contentType : ConfigEntryContentType , value : string ) : boolean {
24+ const trimmed = value . trim ( ) ;
25+
26+ if ( trimmed . length === 0 ) {
27+ return false ;
28+ }
29+
30+ if ( contentType === "text" ) {
31+ return true ;
32+ }
33+
34+ try {
35+ const parsed = JSON . parse ( trimmed ) as unknown ;
36+
37+ if ( contentType === "json" ) {
38+ return true ;
39+ }
40+
41+ if ( contentType === "number" ) {
42+ return typeof parsed === "number" && Number . isFinite ( parsed ) ;
43+ }
44+
45+ return typeof parsed === "boolean" ;
46+ } catch {
47+ return false ;
48+ }
49+ }
50+
2051export function ProjectParamCreateForm ( props : ProjectParamCreateFormProps ) {
2152 const [ cfgKey , setCfgKey ] = createSignal ( "" ) ;
2253 const [ cfgValue , setCfgValue ] = createSignal ( "" ) ;
23- const [ cfgType , setCfgType ] = createSignal < "text" | "number" | "boolean" | "json" > ( "text" ) ;
54+ const [ cfgType , setCfgType ] = createSignal < ConfigEntryContentType > ( "text" ) ;
2455 const [ cfgDisplayName , setCfgDisplayName ] = createSignal ( "" ) ;
2556 const [ cfgDescription , setCfgDescription ] = createSignal ( "" ) ;
2657
@@ -30,21 +61,7 @@ export function ProjectParamCreateForm(props: ProjectParamCreateFormProps) {
3061 }
3162 } ;
3263
33- const isValidJson = ( str : string ) : boolean => {
34- try {
35- JSON . parse ( str ) ;
36- return true ;
37- } catch {
38- return false ;
39- }
40- } ;
41-
42- const isAddInvalid = ( ) => {
43- if ( cfgType ( ) === "json" ) {
44- return ! isValidJson ( cfgValue ( ) ) ;
45- }
46- return false ;
47- } ;
64+ const isAddInvalid = ( ) => ! isValidConfigEntryValue ( cfgType ( ) , cfgValue ( ) ) ;
4865
4966 const handleSubmit = ( e : Event ) => {
5067 e . preventDefault ( ) ;
@@ -92,7 +109,7 @@ export function ProjectParamCreateForm(props: ProjectParamCreateFormProps) {
92109 < Select
93110 value = { cfgType ( ) }
94111 onChange = { ( val : string ) => {
95- setCfgType ( val as "text" | "number" | "boolean" | "json" ) ;
112+ setCfgType ( val as ConfigEntryContentType ) ;
96113 setCfgValue ( "" ) ;
97114 } }
98115 options = { [ "text" , "number" , "boolean" , "json" ] }
0 commit comments