feat: href validation
This commit is contained in:
parent
7438f39786
commit
88cfe18a6b
3 changed files with 23 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { GlobalConfig } from 'payload'
|
import { GlobalConfig } from 'payload'
|
||||||
import { isAdmin } from '@/collections/access/admin'
|
import { isAdmin } from '@/collections/access/admin'
|
||||||
import { revalidateTag } from 'next/cache'
|
import { revalidateTag } from 'next/cache'
|
||||||
|
import { validateHref } from '@/globals/ValidateHref'
|
||||||
|
|
||||||
export const FooterGlobal: GlobalConfig = {
|
export const FooterGlobal: GlobalConfig = {
|
||||||
slug: 'footer',
|
slug: 'footer',
|
||||||
|
|
@ -50,6 +51,7 @@ export const FooterGlobal: GlobalConfig = {
|
||||||
label: {
|
label: {
|
||||||
de: 'Zieladresse',
|
de: 'Zieladresse',
|
||||||
},
|
},
|
||||||
|
validate: validateHref,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { Block, GlobalConfig } from 'payload'
|
import { Block, GlobalConfig } from 'payload'
|
||||||
import { hide, isAdmin } from '@/collections/access/admin'
|
import { isAdmin } from '@/collections/access/admin'
|
||||||
import { revalidateTag } from 'next/cache'
|
import { revalidateTag } from 'next/cache'
|
||||||
|
import { validateHref } from '@/globals/ValidateHref'
|
||||||
|
|
||||||
const SimpleItem: Block = {
|
const SimpleItem: Block = {
|
||||||
slug: 'simple-item',
|
slug: 'simple-item',
|
||||||
|
|
@ -18,7 +19,8 @@ const SimpleItem: Block = {
|
||||||
name: 'href',
|
name: 'href',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
label: 'Zieladresse',
|
label: 'Zieladresse',
|
||||||
required: true
|
required: true,
|
||||||
|
validate: validateHref,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'type',
|
name: 'type',
|
||||||
|
|
@ -93,6 +95,7 @@ const MegaMenuItem: Block = {
|
||||||
label: 'Zieladresse',
|
label: 'Zieladresse',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
required: true,
|
required: true,
|
||||||
|
validate: validateHref,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
required: true,
|
required: true,
|
||||||
|
|
|
||||||
16
src/globals/ValidateHref.ts
Normal file
16
src/globals/ValidateHref.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
/**
|
||||||
|
* Payload field validator for href/link fields.
|
||||||
|
* Ensures values start with `/`, `http://`, or `https://`
|
||||||
|
* to prevent malformed or relative URLs.
|
||||||
|
*/
|
||||||
|
export const validateHref = (value: string | null | undefined) => {
|
||||||
|
if (
|
||||||
|
value &&
|
||||||
|
!value.startsWith('/') &&
|
||||||
|
!value.startsWith('http://') &&
|
||||||
|
!value.startsWith('https://')
|
||||||
|
) {
|
||||||
|
return 'Zieladresse muss mit "/", "http://" oder "https://" beginnen'
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue