Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validateUrlFn props on UrlInput component #2330

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow external validation func
huchenlei committed Jan 23, 2025
commit 8fdc4c959ca8a416e5b11dc00ae431e6fc1067b1
22 changes: 13 additions & 9 deletions src/components/common/UrlInput.vue
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ import { checkUrlReachable } from '@/utils/networkUtil'

const props = defineProps<{
modelValue: string
validateUrlFn?: (url: string) => Promise<boolean>
}>()

const emit = defineEmits<{
@@ -53,6 +54,16 @@ const handleInput = (value: string) => {
validationState.value = UrlValidationState.IDLE
}

// Default validation implementation
const defaultValidateUrl = async (url: string): Promise<boolean> => {
if (!isValidUrl(url)) return false
try {
return await checkUrlReachable(url)
} catch {
return false
}
}

const validateUrl = async () => {
const url = props.modelValue.trim()

@@ -62,17 +73,10 @@ const validateUrl = async () => {
// Skip validation if empty
if (!url) return

// First check if it's a valid URL format
if (!isValidUrl(url)) {
validationState.value = UrlValidationState.INVALID
return
}

// Then check if URL is reachable
validationState.value = UrlValidationState.LOADING
try {
const reachable = await checkUrlReachable(url)
validationState.value = reachable
const isValid = await (props.validateUrlFn ?? defaultValidateUrl)(url)
validationState.value = isValid
? UrlValidationState.VALID
: UrlValidationState.INVALID
} catch {