diff --git a/src/modules/forms/submitForm.ts b/src/modules/forms/submitForm.ts index 8e360b3..7eb18dc 100644 --- a/src/modules/forms/submitForm.ts +++ b/src/modules/forms/submitForm.ts @@ -10,7 +10,8 @@ import { validateSubmission } from './validateSubmission.js' export type SubmitFormArgs = { /** * 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'. + * for the submission to succeed (enforced server-side). Defaults to + * ipalKit({ forms: { consentFieldName } }), then 'consent'. */ consentFieldName?: string /** Submitted field data — shape matches the form's fields. */ @@ -129,7 +130,10 @@ export async function submitForm({ // 3. Validate against the form's schema. A public endpoint can't trust the // shape of `data` — drop unknown keys, enforce required, cap length. - const validation = await validateSubmission(payload, formId, data, consentFieldName) + // Explicit argument → ipalKit({ forms: { consentFieldName } }) → 'consent'. + const configured = (payload.config?.custom as { ipal?: { consentFieldName?: unknown } } | undefined)?.ipal?.consentFieldName + const consentField = consentFieldName ?? (typeof configured === 'string' && configured ? configured : undefined) + const validation = await validateSubmission(payload, formId, data, consentField) if (!validation.ok) { if (validation.reason === 'not_found') { return { reason: 'not_found', success: false } diff --git a/test/submitForm.test.ts b/test/submitForm.test.ts index 6bf9d17..d953475 100644 --- a/test/submitForm.test.ts +++ b/test/submitForm.test.ts @@ -132,6 +132,18 @@ describe('submitForm — other gates', () => { }) }) + test('ipalKit forms.consentFieldName (payload.config.custom) names the consent field', async () => { + const { payload } = fakePayload({ forms: { f: { fields: [{ name: 'zgoda', blockType: 'checkbox' }] } } }) + Object.assign(payload, { config: { custom: { ipal: { consentFieldName: 'zgoda' } } } }) + expect(await submitForm({ data: { zgoda: false }, formId: 'f', ip: nextIp(), payload })).toEqual({ + field: 'zgoda', + reason: 'consent', + success: false, + }) + // an explicit argument still wins + expect((await submitForm({ consentFieldName: 'other', data: { zgoda: false }, formId: 'f', ip: nextIp(), payload })).success).toBe(true) + }) + test('consent must be checked', async () => { const { payload } = fakePayload({ forms }) expect(await submitForm({ data: { ...data, consent: false }, formId: 'contact', ip: nextIp(), payload })).toEqual({