Server-side validation
validateSubmission() from @formhaus/core/server checks a submitted payload against the same JSON definition the browser renders.
npm install @formhaus/coreExpress
import express from 'express';
import { parseDefinition } from '@formhaus/core';
import { validateSubmission } from '@formhaus/core/server';
import json from './definition.json' with { type: 'json' };
const definition = parseDefinition(json);
const app = express();
app.use(express.json({ limit: '100kb' }));
app.post('/api/submit', (req, res) => {
const { values, errors } = validateSubmission(definition, req.body?.values, {
skippedSteps: req.body?.skippedSteps,
});
if (Object.keys(errors).length > 0) return res.status(422).json({ errors });
res.json({ values });
});
app.listen(3000);Save values, not req.body.values. validateSubmission() throws a FormDefinitionError when the definition itself has an error, so check stored definitions with checkDefinition() before you save them. Next.js and Nuxt versions are in Next.js and Nuxt.
Client
Post the values and the skipped step ids. Return the 422 errors as a submit result.
import type { SubmitResult } from '@formhaus/core';
async function submit(values: Record<string, unknown>, skippedSteps: string[]): Promise<SubmitResult> {
const response = await fetch('/api/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ values, skippedSteps }),
});
if (response.status === 422) return { ok: false, errors: (await response.json()).errors };
if (!response.ok) throw new Error('Could not save the form');
return { ok: true };
}
<FormRenderer definition={definition} onSubmit={submit} /><script setup lang="ts">
import type { SubmitResult } from '@formhaus/core';
async function submit(values: Record<string, unknown>, skippedSteps: string[]): Promise<SubmitResult> {
const response = await fetch('/api/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ values, skippedSteps }),
});
if (response.status === 422) return { ok: false, errors: (await response.json()).errors };
if (!response.ok) throw new Error('Could not save the form');
return { ok: true };
}
</script>
<template>
<FormRenderer :definition="definition" @submit="submit" />
</template>The second argument of validateSubmission() is the values object, not the whole request body. Passing the body adds an error under the values key that says so.
What it checks
Both run the validation rules on the active path and skip fields and steps hidden by conditions and inactive branches. Both also run the same type checks for built-in types: email, date (YYYY-MM-DD), datetime (ISO 8601) and time (HH:mm) formats, booleans for checkbox and switch, whole numbers in range for scale and rating, static options for select, radio and multiselect, and the 10,000 character cap on text, 2000 on hidden fields without maxLength. Both skip the options check on fields with optionsFrom, so validate dynamic lists in your app. The browser also skips file fields. The returned values hold only fields on the active path; unknown keys are dropped. A missing key stays missing: validateSubmission() applies no defaultValue, defaultItems or item field default to it. The fields of a step in skippedSteps are reset as Skip resets them in the browser: values sent for them are ignored, and conditions see their defaultValue. validateSubmission() also handles what a browser cannot be trusted to send:
| Behavior | validateSubmission() | FormEngine.validate() |
|---|---|---|
Steps in skippedSteps dropped, only when the step has skip | Yes | No |
number, scale and rating decimal string such as "5" converted to a number | Yes | No. The string fails the type check |
[] in a built-in field other than multiselect dropped like a missing answer | Yes | No. It counts as empty and stays in the payload |
A value that fails its type check, at top level or in a group item, left out of values and treated as unanswered by conditions and validators | Yes | No. It stays in the payload and conditions see it |
Named validator functions | Ignored unless passed in validators | Yes |
A type error can name a field the user never saw, sent by a stale or tampered client. Pass server errors back through a rejected submit or setErrors(), which shows errors of fields that are not on screen as form-level errors, or show them at form level yourself.
FormEngine.validate() is the browser check. Do not use it on untrusted input.
For a repeatable group, a value with more items than maxLength fails with the maxItems message, and a value that is not an array of plain objects, or that has more than 100 items without maxLength or more than 1000 items in any case, fails with the value message. Both are rejected before any item is checked, and the value is left out of values. It drops unknown item keys and checks every visible item field. Item errors use paths such as participants.1.email, and values holds the cleaned arrays. Upgrade the server before the clients: core 0.13 and older pass a group value through without checks.
Hidden values
A client with hiddenValues: 'keep' sends the values of hidden fields. Pass the same option to the server:
const { values, errors } = validateSubmission(definition, req.body?.values, {
skippedSteps: req.body?.skippedSteps,
hiddenValues: 'keep',
});The returned values then hold the hidden values, trimmed and converted like visible ones. Each hidden value is checked against the same rules as a visible one except required, including validators you pass. A hidden value that fails is dropped without an error. Fields of skipped steps and unknown keys are dropped as before. Without the option, the server drops hidden values.
WARNING
The client decides which fields are hidden: it answers the fields their conditions read. Treat a kept hidden value as you would any other submitted value.
Stored answers
Pass the stored values when a submission edits a saved record. Read them from your database, never from the request. A disabled option the stored answer already holds stays valid.
const { values, errors } = validateSubmission(definition, req.body?.values, {
skippedSteps: req.body?.skippedSteps,
previousValues: record.values,
});Without previousValues, every disabled option fails with the option message, except a field's defaultValue. In a group, a disabled option is held as many times as the stored items hold it.
Custom validators
validateSubmission() ignores validation.validator unless you pass the function in validators:
import type { ValidatorFn } from '@formhaus/core';
const validators: Record<string, ValidatorFn> = {
companyAvailable: (value) => (String(value).toLowerCase() === 'acme' ? 'This company already has a workspace' : null),
};
const { values, errors } = validateSubmission(definition, req.body?.values, {
skippedSteps: req.body?.skippedSteps,
validators,
});Validators run only for fields on the active path, never on an empty value.
Messages
Errors use definition.messages. Pass messages to override them, such as with the messages of a localized definition:
const { errors } = validateSubmission(definition, req.body?.values, { messages: { required: 'Pflichtfeld' } });Limits
patternregexes run synchronously, with no time limit.validateSubmission()runs every pattern, including thosecheckDefinition()reports asslow-pattern. Rewrite those patterns, and with definitions from untrusted authors, run the call in a worker thread with a timeout. See Pattern safety.validateSubmission()does not limit the request body. Set a limit in your framework or proxy.- Submitted values and recalled answers are user input. Encode them for each output before use: see Output safety.
Check a definition
checkDefinition() returns a list of issues, for example before you store a definition an agent wrote. Each issue has a code, a severity of error or warning, a JSON pointer path, and a message:
import { checkDefinition } from '@formhaus/core/server';
const errors = checkDefinition(definition).filter((issue) => issue.severity === 'error');Embedded runtimes
@formhaus/core/server.iife.js exposes validateSubmission and checkDefinition on a Formhaus global, for JS runtimes without module loading.