Import instead of code: panel forms (steps, choice lists, dates), layout, page route, GEO routes, links, section fields
- Forms: step and checkboxGroup field types (radio, date on), server validation for them, useBuilderForm (logic) + BuilderForm (unstyled default) + RenderForm (/rsc: rich text, bound action, public part of the form only — the Emails tab never reaches the browser) - Notifications: next / back / stepOf texts - createContentHelpers: getSiteShell, getFormTexts, getLinkOptions, resolveLink, resolveNav, resolvePageRedirect, one-line route handlers (llms, llms-full, md, mcp, health, indexnow, manifest) - /rsc: SiteProviders, createPageRoute, createNotFoundPage - resolveLink / resolveNav / homeSlugFrom, buildSectionHeaderField / buildSectionSettingsField - Dev sandbox frontend built on these imports; tests (unit + real Payload) Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -4,8 +4,11 @@ import type { BasePayload } from 'payload'
|
||||
type FormField = {
|
||||
blockType?: string
|
||||
label?: string
|
||||
/** checkboxGroup limits. */
|
||||
maxSelected?: null | number
|
||||
minSelected?: null | number
|
||||
name?: string
|
||||
/** select / radio choices defined in the panel. */
|
||||
/** select / radio / checkboxGroup choices defined in the panel. */
|
||||
options?: { value?: unknown }[] | null
|
||||
required?: boolean | null
|
||||
}
|
||||
@@ -35,7 +38,10 @@ export type FormValidationResult =
|
||||
| { ok: false; reason: 'not_found' }
|
||||
|
||||
/** Field block types that don't carry a submittable value. */
|
||||
const NON_DATA_BLOCKS = new Set(['message'])
|
||||
const NON_DATA_BLOCKS = new Set(['message', 'step'])
|
||||
|
||||
/** YYYY-MM-DD (a date input) or a full ISO timestamp. */
|
||||
const DATE = /^\d{4}-\d{2}-\d{2}(?:T[\d:.]+(?:Z|[+-]\d{2}:?\d{2})?)?$/
|
||||
|
||||
/**
|
||||
* Keys injected by the captcha widget itself, not by the form definition.
|
||||
@@ -63,7 +69,7 @@ const MAX_EMAIL_LENGTH = 254
|
||||
// eslint-disable-next-line no-control-regex
|
||||
const CONTROL = /[\u0000-\u0008\v\f\u000E-\u001F\u007F\u202A-\u202E\u2066-\u2069]/g
|
||||
|
||||
type Checked = { error?: 'invalid_format'; value?: unknown }
|
||||
type Checked = { error?: 'invalid_format' | 'required'; value?: unknown }
|
||||
|
||||
/**
|
||||
* Type-checks one value against its field's block type, as defined in the
|
||||
@@ -76,6 +82,23 @@ function checkValue(field: FormField, value: unknown): Checked {
|
||||
switch (field.blockType) {
|
||||
case 'checkbox':
|
||||
return typeof value === 'boolean' ? { value } : { error: 'invalid_format' }
|
||||
case 'checkboxGroup': {
|
||||
// string[] of distinct option values, within the panel's min/max.
|
||||
if (!Array.isArray(value) || value.some((v) => typeof v !== 'string')) {return { error: 'invalid_format' }}
|
||||
const picked = [...new Set(value as string[])]
|
||||
const allowed = new Set((field.options ?? []).map((o) => String(o?.value)))
|
||||
if (picked.some((v) => !allowed.has(v))) {return { error: 'invalid_format' }}
|
||||
if (picked.length === 0) {return { value: undefined }}
|
||||
if (field.minSelected && picked.length < field.minSelected) {return { error: 'required' }}
|
||||
if (field.maxSelected && picked.length > field.maxSelected) {return { error: 'invalid_format' }}
|
||||
return { value: picked }
|
||||
}
|
||||
case 'date': {
|
||||
if (typeof value !== 'string' || !DATE.test(value.trim()) || Number.isNaN(Date.parse(value.trim()))) {
|
||||
return { error: 'invalid_format' }
|
||||
}
|
||||
return { value: value.trim() }
|
||||
}
|
||||
case 'email': {
|
||||
if (typeof value !== 'string') {return { error: 'invalid_format' }}
|
||||
const email = value.trim()
|
||||
@@ -154,7 +177,10 @@ export async function validateSubmission(
|
||||
for (const field of fields) {
|
||||
const value = data[field.name]
|
||||
const isBlank =
|
||||
value == null || (typeof value === 'string' && value.trim() === '') || value === false
|
||||
value == null ||
|
||||
(typeof value === 'string' && value.trim() === '') ||
|
||||
value === false ||
|
||||
(Array.isArray(value) && value.length === 0)
|
||||
|
||||
// GDPR consent gate: a field matching the consent name must be truthy
|
||||
// (checked). Enforced independently of `required`, so it can't be weakened
|
||||
|
||||
Reference in New Issue
Block a user