feat(webhooks): add form validation
This commit is contained in:
parent
3d2fe4cf65
commit
779aad1b2d
11
src/helpers/isValidHttpUrl.ts
Normal file
11
src/helpers/isValidHttpUrl.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export function isValidHttpUrl(urlToCheck: string): boolean {
|
||||||
|
let url
|
||||||
|
|
||||||
|
try {
|
||||||
|
url = new URL(urlToCheck)
|
||||||
|
} catch (_) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return url.protocol === 'http:' || url.protocol === 'https:'
|
||||||
|
}
|
@ -366,6 +366,7 @@
|
|||||||
"targetUrlInvalid": "Please provide a valid URL.",
|
"targetUrlInvalid": "Please provide a valid URL.",
|
||||||
"events": "Events",
|
"events": "Events",
|
||||||
"eventsHint": "Select all events this webhook should recieve updates for (within the current project).",
|
"eventsHint": "Select all events this webhook should recieve updates for (within the current project).",
|
||||||
|
"mustSelectEvents": "You must select at least one event.",
|
||||||
"delete": "Delete this webhook",
|
"delete": "Delete this webhook",
|
||||||
"deleteText": "Are you sure you want to delete this webhook? External targets will not be notified of its events anymore.",
|
"deleteText": "Are you sure you want to delete this webhook? External targets will not be notified of its events anymore.",
|
||||||
"deleteSuccess": "The webhook was successfully deleted.",
|
"deleteSuccess": "The webhook was successfully deleted.",
|
||||||
|
@ -23,6 +23,7 @@ import WebhookModel from '@/models/webhook'
|
|||||||
import BaseButton from '@/components/base/BaseButton.vue'
|
import BaseButton from '@/components/base/BaseButton.vue'
|
||||||
import Fancycheckbox from '@/components/input/fancycheckbox.vue'
|
import Fancycheckbox from '@/components/input/fancycheckbox.vue'
|
||||||
import {success} from '@/message'
|
import {success} from '@/message'
|
||||||
|
import {isValidHttpUrl} from '@/helpers/isValidHttpUrl'
|
||||||
|
|
||||||
const {t} = useI18n({useScope: 'global'})
|
const {t} = useI18n({useScope: 'global'})
|
||||||
|
|
||||||
@ -73,10 +74,20 @@ const newWebhook = ref(new WebhookModel())
|
|||||||
const newWebhookEvents = ref({})
|
const newWebhookEvents = ref({})
|
||||||
|
|
||||||
async function create() {
|
async function create() {
|
||||||
const selectedEvents = Object.entries(newWebhookEvents.value)
|
|
||||||
.filter(([event, use]) => use)
|
validateTargetUrl()
|
||||||
.map(([event]) => event)
|
if (!webhookTargetUrlValid.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedEvents = getSelectedEventsArray()
|
||||||
newWebhook.value.events = selectedEvents
|
newWebhook.value.events = selectedEvents
|
||||||
|
|
||||||
|
validateSelectedEvents()
|
||||||
|
if (!selectedEventsValid.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
newWebhook.value.projectId = project.value.id
|
newWebhook.value.projectId = project.value.id
|
||||||
const created = await webhookService.create(newWebhook.value)
|
const created = await webhookService.create(newWebhook.value)
|
||||||
webhooks.value.push(created)
|
webhooks.value.push(created)
|
||||||
@ -85,8 +96,24 @@ async function create() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const webhookTargetUrlValid = ref(true)
|
const webhookTargetUrlValid = ref(true)
|
||||||
|
|
||||||
function validateTargetUrl() {
|
function validateTargetUrl() {
|
||||||
|
webhookTargetUrlValid.value = isValidHttpUrl(newWebhook.value.targetUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedEventsValid = ref(true)
|
||||||
|
|
||||||
|
function getSelectedEventsArray() {
|
||||||
|
return Object.entries(newWebhookEvents.value)
|
||||||
|
.filter(([_, use]) => use)
|
||||||
|
.map(([event]) => event)
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateSelectedEvents() {
|
||||||
|
const events = getSelectedEventsArray()
|
||||||
|
if (events.length === 0) {
|
||||||
|
selectedEventsValid.value = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -115,6 +142,7 @@ function validateTargetUrl() {
|
|||||||
class="input"
|
class="input"
|
||||||
:placeholder="$t('project.webhooks.targetUrl')"
|
:placeholder="$t('project.webhooks.targetUrl')"
|
||||||
v-model="newWebhook.targetUrl"
|
v-model="newWebhook.targetUrl"
|
||||||
|
@focusout="validateTargetUrl"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p class="help is-danger" v-if="!webhookTargetUrlValid">
|
<p class="help is-danger" v-if="!webhookTargetUrlValid">
|
||||||
@ -152,10 +180,14 @@ function validateTargetUrl() {
|
|||||||
:key="event"
|
:key="event"
|
||||||
class="mr-2"
|
class="mr-2"
|
||||||
v-model="newWebhookEvents[event]"
|
v-model="newWebhookEvents[event]"
|
||||||
|
@update:model-value="validateSelectedEvents"
|
||||||
>
|
>
|
||||||
{{ event }}
|
{{ event }}
|
||||||
</fancycheckbox>
|
</fancycheckbox>
|
||||||
</div>
|
</div>
|
||||||
|
<p class="help is-danger" v-if="!selectedEventsValid">
|
||||||
|
{{ $t('project.webhooks.mustSelectEvents') }}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<x-button @click="create" icon="plus">
|
<x-button @click="create" icon="plus">
|
||||||
{{ $t('project.webhooks.create') }}
|
{{ $t('project.webhooks.create') }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user