Example 1
Example 1
import { validatedStateKey } from '@setu-ts/common'; import { ValidationPlugin, validateBody, validateQuery } from '@setu-ts/validation-plugin'; app.register(ValidationPlugin({ errorFormat: 'rfc9457' })); app.router.post('/users', { middleware: [validateBody(CreateUserSchema)], handler: async (ctx) => { const body = ctx.state.get(validatedStateKey('body')); // body is validated }, });
Default validation service.
-
middleware(): MiddlewareFunctionschema: unknown,target: ValidationTarget
Creates validation middleware for the given request target.
-
validate<T>(): Result<T, readonly ValidationIssue[]>schema: unknown,data: unknown
Validate
dataagainst the given schema.
Create a sanitization function that applies the given rules to each call.
Create a validation middleware function.
Framework-standard validation error formatter.
NestJS-compatible validation error formatter.
Resolve the error format configuration to a concrete formatter function.
Format validation issues as RFC 9457 Problem Details.
Sanitize a single string value with the given rules.
Validate the request body against a schema.
Validate request headers against a schema.
Validate path parameters against a schema.
Validate query parameters against a schema.
Creates the ValidationPlugin.
A single formatted error entry.
-
code: string
Optional machine-readable error code.
-
field: string
Dot-separated field path.
-
message: string
Human-readable message.
The shaped error body produced by a validation error formatter.
-
errors: readonly FormattedError[]
Array of formatted error entries.
-
message: string | string[]
Human-readable summary message (optional; present on default/nestjs, omitted on rfc7807).
Configuration for sanitizing a string value.
-
allowedTags: string[]
Keep only the listed tag names (implies
stripTagsfor non-listed tags). -
htmlEncode: boolean
Encode HTML entities (
<,>,&,",'). -
maxLength: number
Truncate the string to this many characters (applied after other transforms).
-
pattern: RegExp
Replace the entire string with an empty string if it does not match.
-
stripTags: boolean
Remove all HTML tags.
-
toLowerCase: boolean
Convert the string to lowercase.
-
toUpperCase: boolean
Convert the string to uppercase.
-
trim: boolean
Trim leading and trailing whitespace.
Options for ValidationPlugin.
-
errorFormat: ErrorFormat | ValidationErrorFormatter
Error response format. Defaults to
'default'. -
forbidNonWhitelisted: boolean
When true, reject payloads carrying properties the schema does not declare.
-
whitelist: boolean
When true, strip properties the schema does not declare.
The built-in error format identifiers for @setu-ts/validation-plugin.
A function that formats validation issues into a structured error body.
Format validation issues as RFC 7807 Problem Details.
Usage
import * as Zod_compatible_request_validation_plugin_with_RFC_9457_error_formatting__input_sanitization__and_middleware_helpers__ from "validation-plugin/src/index.ts";