mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-02-09 12:31:08 +08:00
- Updated SettingsPage to link to API Gateway settings. - Refactored ApiServerSettings to manage API Gateway settings including model groups and enabled endpoints. - Introduced new Redux actions and state management for API Gateway configuration. - Enhanced migration scripts to support new API Gateway fields and ensure backward compatibility. - Updated types to define GatewayEndpoint and ModelGroup structures.
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
/**
|
|
* Validator configuration for real-time input validation
|
|
*/
|
|
export interface ValidatorConfig {
|
|
/** Validation function, returns error message or null */
|
|
validate?: (value: string) => string | null
|
|
/** Transform input value (e.g., lowercase, remove invalid chars) */
|
|
transform?: (value: string) => string
|
|
/** Debounce delay in milliseconds (default: 300) */
|
|
debounceMs?: number
|
|
}
|
|
|
|
/**
|
|
* Compose multiple validators into one
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const validator = composeValidators(
|
|
* validators.urlSafe(32),
|
|
* { validate: (v) => v.startsWith('-') ? 'Cannot start with hyphen' : null }
|
|
* )
|
|
* ```
|
|
*/
|
|
export function composeValidators(...configs: ValidatorConfig[]): ValidatorConfig {
|
|
return {
|
|
// Chain transforms: apply each transform in order
|
|
transform: (value) => {
|
|
return configs.reduce((v, config) => (config.transform ? config.transform(v) : v), value)
|
|
},
|
|
|
|
// Chain validates: return first error found
|
|
validate: (value) => {
|
|
for (const config of configs) {
|
|
if (config.validate) {
|
|
const error = config.validate(value)
|
|
if (error) return error
|
|
}
|
|
}
|
|
return null
|
|
},
|
|
|
|
// Use the smallest non-zero debounce
|
|
debounceMs: configs.reduce((min, config) => {
|
|
if (config.debounceMs && (min === 0 || config.debounceMs < min)) {
|
|
return config.debounceMs
|
|
}
|
|
return min
|
|
}, 0)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Preset validators for common use cases
|
|
*/
|
|
export const validators = {
|
|
/**
|
|
* URL-safe characters validator (for API paths, slugs, etc.)
|
|
* - Auto lowercase
|
|
* - Only allow a-z, 0-9, and hyphen
|
|
* - Limit to maxLength characters
|
|
*/
|
|
urlSafe: (maxLength = 32): ValidatorConfig => ({
|
|
transform: (v) =>
|
|
v
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9-]/g, '')
|
|
.slice(0, maxLength),
|
|
validate: (v) => (!v ? 'Name is required' : null),
|
|
debounceMs: 300
|
|
}),
|
|
|
|
/**
|
|
* Filename-safe validator
|
|
* - Remove invalid characters: < > : " / \ | ? *
|
|
* - Limit to maxLength characters
|
|
*/
|
|
fileName: (maxLength = 255): ValidatorConfig => ({
|
|
transform: (v) => v.replace(/[<>:"/\\|?*]/g, '').slice(0, maxLength),
|
|
validate: (v) => (!v.trim() ? 'Name is required' : null)
|
|
}),
|
|
|
|
/**
|
|
* Required field validator (non-empty after trim)
|
|
*/
|
|
required: (): ValidatorConfig => ({
|
|
validate: (v) => (!v.trim() ? 'This field is required' : null)
|
|
})
|
|
}
|