Package Exports
- joi-schema-validator
- joi-schema-validator/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (joi-schema-validator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
joi-schema-validator
A world-class Joi validation error formatter for structured, user-friendly error handling in Node.js applications.
Features
- Human-readable error messages for better debugging.
- Customizable error messages to fit your requirements.
- Supports nested object validation.
- Enterprise-ready validation handling.
- TypeScript support for strict type safety.
- Lightweight and efficient.
Installation
You can install this package using npm or yarn:
npm install joi-schema-validatoror
yarn add joi-schema-validatorUsage
Basic Example
import Joi from 'joi';
import { joiSchemaValidator } from 'joi-schema-validator';
const schema = Joi.object({
name: Joi.string().min(3).required(),
email: Joi.string().email().required(),
age: Joi.number().min(18)
});
const validator = joiSchemaValidator(schema);
const { value, error } = validator.validate({
name: 'Jo',
email: 'invalid-email'
});
console.log(value); // Outputs validated data
console.log(error); // Outputs formatted error messagesExample Output
[
{
"field": "name",
"type": "string.min",
"message": "Name must have at least 3 characters."
},
{
"field": "email",
"type": "string.email",
"message": "Email must be a valid email address."
}
]Custom Error Messages
You can provide custom error messages by passing them as the second argument:
const customMessages = {
'string.min': '{#label} is too short!',
'string.email': 'Please enter a valid email!'
};
const validator = joiSchemaValidator(schema, customMessages);
const { error } = validator.validate({ name: 'Jo', email: 'invalid-email' });
console.log(error);Example Output
[
{ "field": "name", "type": "string.min", "message": "Name is too short!" },
{
"field": "email",
"type": "string.email",
"message": "Please enter a valid email!"
}
]API Reference
joiSchemaValidator(schema: Schema, messages?: ErrorMessages)
Creates a validation function with customized error handling.
Parameters:
| Parameter | Type | Description |
|---|---|---|
schema |
Joi.Schema |
Joi schema definition for validation. |
messages |
ErrorMessages (optional) |
Custom error messages. |
.validate(input: any, options?: Joi.ValidationOptions)
Validates input data against the schema.
Parameters:
| Parameter | Type | Description |
|---|---|---|
input |
any |
Data to be validated. |
options |
Joi.ValidationOptions (optional) |
Joi validation options. |
Returns:
{
value: any; // The validated data
error: ValidationErrorItemFormatted[] | null; // Formatted errors
}Type Definitions
export interface ValidationErrorItemFormatted {
field: string;
type: string;
message: string;
}Contributing
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Push to the branch.
- Submit a pull request.
License
This project is licensed under the MIT License. See LICENSE for details.
Issues & Support
For any issues, please report them here.
Author
Developed by Pavan Dulam.