Lint: fix all ESLint errors (391 -> 0), lint and typecheck tests
Mostly perfectionist import/key sorting and curly braces from --fix, plus: - spread instead of .apply() in the Meta Pixel stub - non-capturing group in the locale-code pattern - React 19 use(Context) / <Context value> in TurnstileProvider - email regex rewritten without super-linear backtracking (covered by a test) - console.warn/error allowed (deliberate server diagnostics) - test/tsconfig.json so the project service and tsc see test files Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { formBuilderPlugin } from '@payloadcms/plugin-form-builder'
|
||||
import type { Plugin } from 'payload'
|
||||
|
||||
import { formBuilderPlugin } from '@payloadcms/plugin-form-builder'
|
||||
|
||||
import type { FormsOption } from './types.js'
|
||||
|
||||
/** REST/GraphQL: only logged-in users. Local API (submitForm) is unaffected. */
|
||||
@@ -24,14 +26,14 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
const group = adminGroup ? { group: adminGroup } : {}
|
||||
return formBuilderPlugin({
|
||||
fields: {
|
||||
checkbox: true,
|
||||
email: true,
|
||||
message: true,
|
||||
number: true,
|
||||
payment: false,
|
||||
select: true,
|
||||
text: true,
|
||||
textarea: true,
|
||||
email: true,
|
||||
number: true,
|
||||
select: true,
|
||||
checkbox: true,
|
||||
message: true,
|
||||
payment: false,
|
||||
...forms.fields,
|
||||
},
|
||||
...(forms.redirectRelationships
|
||||
@@ -43,18 +45,18 @@ export function buildFormsPlugin(forms: FormsOption): Plugin {
|
||||
// Tidy panel by default: both collections under one sidebar group with plain
|
||||
// labels — no per-project overrides needed just to sort the sidebar.
|
||||
formOverrides: {
|
||||
labels: { singular: 'Form', plural: 'Forms' },
|
||||
labels: { plural: 'Forms', singular: 'Form' },
|
||||
...forms.formOverrides,
|
||||
admin: { ...group, ...forms.formOverrides?.admin },
|
||||
},
|
||||
formSubmissionOverrides: {
|
||||
labels: { singular: 'Submission', plural: 'Submissions' },
|
||||
labels: { plural: 'Submissions', singular: 'Submission' },
|
||||
...forms.formSubmissionOverrides,
|
||||
admin: { ...group, defaultColumns: ['form', 'createdAt'], ...forms.formSubmissionOverrides?.admin },
|
||||
access: {
|
||||
create: submissionsCreateAccess,
|
||||
...forms.formSubmissionOverrides?.access,
|
||||
},
|
||||
admin: { ...group, defaultColumns: ['form', 'createdAt'], ...forms.formSubmissionOverrides?.admin },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import 'server-only'
|
||||
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import { getTurnstileConfig } from '../turnstile/config.js'
|
||||
import { verifyTurnstile } from '../turnstile/verify.js'
|
||||
import { validateSubmission } from './validateSubmission.js'
|
||||
import { consumeRateLimit } from './rateLimit.js'
|
||||
import { validateSubmission } from './validateSubmission.js'
|
||||
|
||||
export type SubmitFormArgs = {
|
||||
payload: BasePayload
|
||||
/** Form-builder form ID this submission belongs to. */
|
||||
formId: string
|
||||
/**
|
||||
* Name of the GDPR consent checkbox. A field with this name must be checked
|
||||
* for the submission to succeed (enforced server-side). Defaults to 'consent'.
|
||||
*/
|
||||
consentFieldName?: string
|
||||
/** Submitted field data — shape matches the form's fields. */
|
||||
data: Record<string, unknown>
|
||||
/**
|
||||
* Turnstile token. Required whenever Turnstile is configured (both keys —
|
||||
* see getTurnstileConfig): a missing token is then rejected. Ignored when
|
||||
* Turnstile is not configured.
|
||||
*/
|
||||
turnstileToken?: string
|
||||
/** Form-builder form ID this submission belongs to. */
|
||||
formId: string
|
||||
/** Client IP — used for Turnstile and rate limiting. */
|
||||
ip?: string
|
||||
/**
|
||||
@@ -24,17 +24,19 @@ export type SubmitFormArgs = {
|
||||
* Set to 0 to disable (e.g. when a real limiter sits in front).
|
||||
*/
|
||||
maxPerMinute?: number
|
||||
/**
|
||||
* Name of the GDPR consent checkbox. A field with this name must be checked
|
||||
* for the submission to succeed (enforced server-side). Defaults to 'consent'.
|
||||
*/
|
||||
consentFieldName?: string
|
||||
payload: BasePayload
|
||||
/**
|
||||
* Turnstile can't be verified because of OUR side (missing/wrong secret,
|
||||
* Cloudflare unreachable): 'allow' (default) accepts the submission and shows
|
||||
* the problem in the panel; 'reject' refuses it.
|
||||
*/
|
||||
turnstileOnConfigError?: 'allow' | 'reject'
|
||||
/**
|
||||
* Turnstile token. Required whenever Turnstile is configured (both keys —
|
||||
* see getTurnstileConfig): a missing token is then rejected. Ignored when
|
||||
* Turnstile is not configured.
|
||||
*/
|
||||
turnstileToken?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,26 +57,26 @@ export type SubmitFormArgs = {
|
||||
* - `error` — persistence failed unexpectedly
|
||||
*/
|
||||
export type SubmitFailure =
|
||||
| { success: false; reason: 'rate_limited' }
|
||||
| { success: false; reason: 'turnstile' }
|
||||
| {
|
||||
success: false
|
||||
reason: 'validation'
|
||||
/** The offending field's name, when one field is at fault. */
|
||||
field?: string
|
||||
/** What was wrong with it. */
|
||||
kind?: 'required' | 'too_long' | 'unknown_fields'
|
||||
}
|
||||
| { success: false; reason: 'not_found' }
|
||||
| {
|
||||
reason: 'validation'
|
||||
success: false
|
||||
}
|
||||
| {
|
||||
field?: string
|
||||
/** A GDPR consent field existed on the form but wasn't checked. */
|
||||
reason: 'consent'
|
||||
field?: string
|
||||
success: false
|
||||
}
|
||||
| { success: false; reason: 'error' }
|
||||
| { reason: 'error'; success: false }
|
||||
| { reason: 'not_found'; success: false }
|
||||
| { reason: 'rate_limited'; success: false }
|
||||
| { reason: 'turnstile'; success: false }
|
||||
|
||||
export type SubmitFormResult = SubmitFailure | { success: true; submissionId: string | number }
|
||||
export type SubmitFormResult = { submissionId: number | string; success: true } | SubmitFailure
|
||||
|
||||
/**
|
||||
* Handles a form submission end to end: rate limit, verify Turnstile, validate
|
||||
@@ -90,19 +92,19 @@ export type SubmitFormResult = SubmitFailure | { success: true; submissionId: st
|
||||
* server-only: touches the Turnstile secret.
|
||||
*/
|
||||
export async function submitForm({
|
||||
payload,
|
||||
formId,
|
||||
consentFieldName,
|
||||
data,
|
||||
turnstileToken,
|
||||
formId,
|
||||
ip,
|
||||
maxPerMinute = 5,
|
||||
consentFieldName,
|
||||
payload,
|
||||
turnstileOnConfigError = 'allow',
|
||||
turnstileToken,
|
||||
}: SubmitFormArgs): Promise<SubmitFormResult> {
|
||||
// 1. Rate limit — cheapest gate, drops a flood before any real work.
|
||||
if (maxPerMinute > 0 && ip) {
|
||||
if (!(await consumeRateLimit({ key: ip, max: maxPerMinute })).ok) {
|
||||
return { success: false, reason: 'rate_limited' }
|
||||
return { reason: 'rate_limited', success: false }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,9 +114,9 @@ export async function submitForm({
|
||||
if ((await getTurnstileConfig(payload)).enabled) {
|
||||
const ok =
|
||||
Boolean(turnstileToken) &&
|
||||
(await verifyTurnstile({ token: turnstileToken!, payload, ip, onConfigError: turnstileOnConfigError }))
|
||||
(await verifyTurnstile({ ip, onConfigError: turnstileOnConfigError, payload, token: turnstileToken! }))
|
||||
if (!ok) {
|
||||
return { success: false, reason: 'turnstile' }
|
||||
return { reason: 'turnstile', success: false }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,14 +125,14 @@ export async function submitForm({
|
||||
const validation = await validateSubmission(payload, formId, data, consentFieldName)
|
||||
if (!validation.ok) {
|
||||
if (validation.reason === 'not_found') {
|
||||
return { success: false, reason: 'not_found' }
|
||||
return { reason: 'not_found', success: false }
|
||||
}
|
||||
if (validation.reason === 'consent') {
|
||||
return { success: false, reason: 'consent', field: validation.field }
|
||||
return { field: validation.field, reason: 'consent', success: false }
|
||||
}
|
||||
return {
|
||||
success: false,
|
||||
reason: 'validation',
|
||||
success: false,
|
||||
...(validation.field ? { field: validation.field } : {}),
|
||||
...(validation.kind ? { kind: validation.kind } : {}),
|
||||
}
|
||||
@@ -149,9 +151,9 @@ export async function submitForm({
|
||||
})),
|
||||
} as never,
|
||||
})
|
||||
return { success: true, submissionId: submission.id }
|
||||
return { submissionId: submission.id, success: true }
|
||||
} catch (err) {
|
||||
payload.logger.error(`[ipal] Form submission failed: ${(err as Error).message}`)
|
||||
return { success: false, reason: 'error' }
|
||||
return { reason: 'error', success: false }
|
||||
}
|
||||
}
|
||||
|
||||
+19
-19
@@ -19,25 +19,29 @@ export type FormsCollectionOverrides = {
|
||||
* form-builder plugin. Kept minimal; the plugin passes these through.
|
||||
*/
|
||||
export type FormsOption = {
|
||||
/** Field types available in the form builder. Sensible defaults applied. */
|
||||
fields?: {
|
||||
text?: boolean
|
||||
textarea?: boolean
|
||||
email?: boolean
|
||||
number?: boolean
|
||||
select?: boolean
|
||||
checkbox?: boolean
|
||||
message?: boolean
|
||||
payment?: boolean
|
||||
}
|
||||
/** Collections a form can redirect to (e.g. ['pages']). */
|
||||
redirectRelationships?: string[]
|
||||
/**
|
||||
* Admin sidebar group for Forms + Form Submissions. Default 'Forms'.
|
||||
* `false` = Payload default ("Collections"). An explicit `admin.group` in
|
||||
* formOverrides / formSubmissionOverrides still wins.
|
||||
*/
|
||||
adminGroup?: false | string
|
||||
/**
|
||||
* Name of the checkbox field treated as a GDPR consent gate. A form field
|
||||
* with this name must be checked for submission to succeed — enforced
|
||||
* server-side in submitForm. Defaults to 'consent'.
|
||||
*/
|
||||
consentFieldName?: string
|
||||
/** Field types available in the form builder. Sensible defaults applied. */
|
||||
fields?: {
|
||||
checkbox?: boolean
|
||||
email?: boolean
|
||||
message?: boolean
|
||||
number?: boolean
|
||||
payment?: boolean
|
||||
select?: boolean
|
||||
text?: boolean
|
||||
textarea?: boolean
|
||||
}
|
||||
/**
|
||||
* Override the forms collection. The plugin stays opinion-free about what a
|
||||
* form needs beyond its fields — a client that wants, say, a per-form
|
||||
@@ -53,10 +57,6 @@ export type FormsOption = {
|
||||
formOverrides?: FormsCollectionOverrides
|
||||
/** Override the form-submissions collection (same shape). */
|
||||
formSubmissionOverrides?: FormsCollectionOverrides
|
||||
/**
|
||||
* Admin sidebar group for Forms + Form Submissions. Default 'Forms'.
|
||||
* `false` = Payload default ("Collections"). An explicit `admin.group` in
|
||||
* formOverrides / formSubmissionOverrides still wins.
|
||||
*/
|
||||
adminGroup?: string | false
|
||||
/** Collections a form can redirect to (e.g. ['pages']). */
|
||||
redirectRelationships?: string[]
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ import type { FormErrors, Validator } from './useForm.js'
|
||||
*/
|
||||
|
||||
/** A rule returns `true` when the value is OK, or an error message string. */
|
||||
export type Rule = (value: unknown, values: Record<string, unknown>) => true | string
|
||||
export type Rule = (value: unknown, values: Record<string, unknown>) => string | true
|
||||
|
||||
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
const EMAIL_RE = /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/
|
||||
|
||||
/** Standalone e-mail check, handy outside the rule system too. */
|
||||
export const isEmail = (v: unknown): boolean => typeof v === 'string' && EMAIL_RE.test(v.trim())
|
||||
@@ -35,9 +35,9 @@ export const isEmail = (v: unknown): boolean => typeof v === 'string' && EMAIL_R
|
||||
export const required =
|
||||
(msg = 'required'): Rule =>
|
||||
(v) => {
|
||||
if (v == null) return msg
|
||||
if (typeof v === 'string' && !v.trim()) return msg
|
||||
if (v === false) return msg
|
||||
if (v == null) {return msg}
|
||||
if (typeof v === 'string' && !v.trim()) {return msg}
|
||||
if (v === false) {return msg}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ export function buildValidator<T extends Record<string, unknown>>(
|
||||
const all = values as Record<string, unknown>
|
||||
for (const key in schema) {
|
||||
const rules = schema[key as keyof T]
|
||||
if (!rules || rules.length === 0) continue
|
||||
if (!rules || rules.length === 0) {continue}
|
||||
for (const rule of rules) {
|
||||
const res = rule(all[key], all)
|
||||
if (res !== true) {
|
||||
|
||||
Reference in New Issue
Block a user