Zod Integration (in progress)
ds-express-errors automatically identifies Zod validation errors and maps them to 400 Bad Request.
The Zod mapper works out of the box and does not require Zod to be passed to ds-express-errors.
- Basic integration
- Supported errors
- Configuration
- Strict error detection
- Example
- Resulting responses
1. Basic Integration
Use Zod normally to validate data in your Express application and add the ds-express-errors error handler after all routes.
Add errorHandler after your application routes:
import express from 'express'
import { errorHandler } from 'ds-express-errors'
const app = express();
// Your routes
// Add Error Handler at the very end
app.use(errorHandler);2. Supported Errors
The Zod mapper handles validation errors containing a Zod issues array.
Each issue is converted into a message containing its path and validation message.
For example, Zod issues such as:
[
{
path: ['email'],
message: 'Invalid email address'
},
{
path: ['profile', 'age'],
message: 'Too small: expected number to be >=18'
}
]are formatted as:
Note
email: Invalid email address; profile.age: Too small: expected number to be >=18
The resulting error is mapped to 400 Bad Request.
3. Configuration
By default, all available mappers are enabled.
If you want ds-express-errors to use only selected mappers, configure them with needMappers:
import { setConfig } from 'ds-express-errors'
setConfig({ needMappers: ['zod'] })Note
With this configuration, the built-in Zod mapper is enabled while other built-in third-party mappers are not used.
4. Strict Error Detection
By default, ds-express-errors follows its zero dependency policy and identifies supported third-party errors without requiring Zod as its own dependency.
The default detection checks for an issues array with a validation path.
Info
Strict detection is optional. Zod remains a dependency of your application and is not installed as a dependency of ds-express-errors.
For strict detection, you can provide Zod through errorClasses:
import { setConfig } from 'ds-express-errors'
import { z } from 'zod'
setConfig({
errorClasses: {
Zod: z
}
})When Zod is provided, ds-express-errors uses:
err instanceof z.ZodErrorto identify Zod errors.
5. Example
import { z } from 'zod'
const userSchema = z.object({
email: z.string().email(),
age: z.number().min(18)
})Invalid data can be passed to parse() normally:
userSchema.parse({
email: 'not-an-email',
age: 16
})Zod throws a validation error containing its validation issues.
You do not need to catch the error and manually convert every Zod issue into an HTTP error. Once it reaches errorHandler, the Zod mapper handles the conversion.
6. Resulting Responses
For a supported Zod error, the final response follows the standard ds-express-errors response format. (configured by setConfig())
Info
When debug logging is enabled, the formatted Zod validation issues are also written to the configured logger.
{
"status": "fail",
"message": "[Validation error]: email: Invalid email address; age: Too small: expected number to be >=18"
}Warning
url, stack, method are available in development mode, if you want to change that setting, you can redefine them by setConfig
To see how Production and Development responses differ, visit the Configuration part
Look at formatError property
ds-express-errors