@@ -42,6 +42,8 @@ export interface ProviderSettingsFieldModel {
4242 /** Typed `SettingsSecretValue`: may name a secret provider reference instead of a value. */
4343 readonly acceptsSecretReference ?: boolean | undefined ;
4444 readonly defaultBooleanValue ?: boolean | undefined ;
45+ /** Whether the field's schema decodes a typed string; one it rejects is never stored. */
46+ readonly accepts ?: ( ( value : string ) => boolean ) | undefined ;
4547
4648 readonly options ?: ReadonlyArray < ProviderSettingsFormOption > | undefined ;
4749}
@@ -89,6 +91,13 @@ function readFieldBooleanDefault(
8991 return Option . isSome ( decoded ) && typeof decoded . value === "boolean" ? decoded . value : undefined ;
9092}
9193
94+ function fieldAcceptsString (
95+ fieldSchema : ProviderClientDefinition [ "settingsSchema" ] [ "fields" ] [ string ] ,
96+ ) : ( value : string ) => boolean {
97+ const decode = Schema . decodeUnknownOption ( fieldSchema as Schema . Decoder < unknown > ) ;
98+ return ( value ) => Option . isSome ( decode ( value ) ) ;
99+ }
100+
92101export function deriveProviderSettingsFields (
93102 definition : SettingsFormDefinition ,
94103 value ?: unknown ,
@@ -142,6 +151,7 @@ export function deriveProviderSettingsFields(
142151 ...( formAnnotation . control === "switch"
143152 ? { defaultBooleanValue : readFieldBooleanDefault ( fieldSchema ) }
144153 : { } ) ,
154+ accepts : fieldAcceptsString ( fieldSchema ) ,
145155 ...( formAnnotation . control === "select" && formAnnotation . options
146156 ? { options : formAnnotation . options }
147157 : { } ) ,
@@ -322,7 +332,7 @@ export function nextProviderConfigWithFieldValue(
322332 const trimmed = value . trim ( ) ;
323333 if ( field . clearWhenEmpty === "omit" && trimmed . length === 0 ) {
324334 delete base [ field . key ] ;
325- } else {
335+ } else if ( field . accepts ?. ( value ) !== false ) {
326336 base [ field . key ] = value ;
327337 }
328338 return Object . keys ( base ) . length > 0 ? base : undefined ;
@@ -434,6 +444,50 @@ function ProviderSettingsSelect({
434444 ) ;
435445}
436446
447+ function ProviderSettingsTextInput ( {
448+ field,
449+ value,
450+ inputId,
451+ descriptionId,
452+ onChange,
453+ } : {
454+ readonly field : ProviderSettingsFieldModel ;
455+ readonly value : unknown ;
456+ readonly inputId : string ;
457+ readonly descriptionId : string | undefined ;
458+ readonly onChange : ProviderSettingsFormProps [ "onChange" ] ;
459+ } ) {
460+ const [ rejected , setRejected ] = useState < string | null > ( null ) ;
461+ return (
462+ < div className = "flex w-full min-w-0 flex-col gap-1 @min-[32rem]/settings-row:w-56" >
463+ < DraftInput
464+ id = { inputId }
465+ aria-describedby = { descriptionId }
466+ size = "sm"
467+ className = "w-full max-w-full"
468+ type = { field . control === "password" ? "password" : undefined }
469+ autoComplete = { field . control === "password" ? "off" : undefined }
470+ value = { rejected ?? readProviderConfigString ( value , field . key ) }
471+ placeholder = { field . placeholder }
472+ onCommit = { ( next ) => {
473+ const clears = field . clearWhenEmpty === "omit" && next . trim ( ) . length === 0 ;
474+ if ( ! clears && field . accepts ?.( next ) === false ) {
475+ setRejected ( next ) ;
476+ return ;
477+ }
478+ setRejected ( null ) ;
479+ onChange ( nextProviderConfigWithFieldValue ( value , field , next ) ) ;
480+ } }
481+ aria-invalid = { rejected !== null || undefined }
482+ spellCheck = { false }
483+ />
484+ { rejected !== null ? (
485+ < p className = "text-2xs text-destructive" > Enter a valid value. It was not saved.</ p >
486+ ) : null }
487+ </ div >
488+ ) ;
489+ }
490+
437491function FieldFrame ( props : {
438492 readonly variant : ProviderSettingsFormProps [ "variant" ] ;
439493 readonly children : ReactNode ;
@@ -541,19 +595,12 @@ function ProviderSettingsFieldRow({
541595 ) : null }
542596 </ div >
543597 ) : (
544- < DraftInput
545- id = { inputId }
546- aria-describedby = { descriptionId }
547- size = "sm"
548- className = "w-full max-w-full @min-[32rem]/settings-row:w-56"
549- type = { field . control === "password" ? "password" : undefined }
550- autoComplete = { field . control === "password" ? "off" : undefined }
551- value = { readProviderConfigString ( value , field . key ) }
552- placeholder = { field . placeholder }
553- onCommit = { ( next ) => {
554- onChange ( nextProviderConfigWithFieldValue ( value , field , next ) ) ;
555- } }
556- spellCheck = { false }
598+ < ProviderSettingsTextInput
599+ field = { field }
600+ value = { value }
601+ inputId = { inputId }
602+ descriptionId = { descriptionId }
603+ onChange = { onChange }
557604 />
558605 ) ;
559606
0 commit comments