Optionaloptions: ValidatorTypeboxOptionsConfiguration options for the validator
Optionalonerror: ErrorRequestHandlerError handler called when validation fails. If not provided, returns HTTP 400 with error details.
OptionalschemaProperty: stringName of the route parameter property containing the schema.
If undefined, uses req.meta.parameters directly as the schema.
If specified (e.g., 'schema'), looks for the schema at req.meta.parameters.schema.
A request handler middleware function that validates incoming requests
import routing from '@novice1/routing';
import { validatorTypebox } from '@novice1/validator-typebox';
import { Type } from 'typebox';
const router = routing();
// Set up validator with custom error handler
router.setValidators(
validatorTypebox(
{ parse: true },
(err, req, res, next) => {
res.status(400).json({ error: 'Validation failed', details: err });
},
'schema'
)
);
// Create validated route
router.post(
{
path: '/users',
parameters: {
schema: {
body: Type.Object({
name: Type.String(),
email: Type.String({ format: 'email' })
})
}
}
},
(req, res) => {
res.json({ success: true, data: req.body });
}
);
Creates a TypeBox validator middleware for use with @novice1/routing.
Validates request properties (params, body, query, headers, cookies, files) against TypeBox schemas. The validator can be configured globally and overridden per-route.