Validate booking contacts: required FIO and email, RF +7 phone mask, and email domain.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Alexey Novikov
2026-09-17 01:27:38 +03:00
co-authored by Cursor
parent a6a748281f
commit 2ab7c29576
17 changed files with 248 additions and 28 deletions
+39 -3
View File
@@ -1,5 +1,6 @@
import type { CollectionConfig } from 'payload'
import { ADMIN_GROUP } from '../admin/groups'
import { isValidEmailAddress, isValidFullName, isValidRuPhone, normalizeRuPhone } from '../lib/contact-fields'
export const Inquiries: CollectionConfig = {
slug: 'inquiries',
@@ -16,10 +17,45 @@ export const Inquiries: CollectionConfig = {
update: ({ req: { user } }) => user?.role === 'admin',
delete: ({ req: { user } }) => user?.role === 'admin',
},
hooks: {
beforeValidate: [
({ data }) => {
if (!data) return data
if (typeof data.name === 'string') data.name = data.name.trim().replace(/\s+/g, ' ')
if (typeof data.email === 'string') data.email = data.email.trim()
if (typeof data.phone === 'string') {
const phone = normalizeRuPhone(data.phone)
if (phone) data.phone = phone
}
return data
},
],
},
fields: [
{ name: 'name', type: 'text', required: true, label: 'ФИО' },
{ name: 'email', type: 'email', required: true, label: 'E-mail' },
{ name: 'phone', type: 'text', required: true, label: 'Телефон' },
{
name: 'name',
type: 'text',
required: true,
label: 'ФИО',
validate: (value: string | null | undefined) =>
isValidFullName(String(value ?? '')) ? true : 'Укажите фамилию и имя буквами',
},
{
name: 'email',
type: 'email',
required: true,
label: 'E-mail',
validate: (value: string | null | undefined) =>
isValidEmailAddress(String(value ?? '')) ? true : 'Укажите e-mail в формате name@domain.ru',
},
{
name: 'phone',
type: 'text',
required: true,
label: 'Телефон',
validate: (value: string | null | undefined) =>
isValidRuPhone(String(value ?? '')) ? true : 'Укажите телефон в формате +7 и 10 цифр',
},
{ name: 'service', type: 'relationship', relationTo: 'services', label: 'Услуга' },
{ name: 'doctor', type: 'relationship', relationTo: 'doctors', label: 'Врач' },
{ name: 'branch', type: 'relationship', relationTo: 'branches', required: true, label: 'Филиал' },