Welcome to Joiful
Joiful is a powerful data validation library for Node.js, ensuring your application’s data integrity and reliability. Its extensive suite of APIs provides developers with robust tools for all their validation needs. In this guide, we’ll explore dozens of useful APIs provided by Joiful and their usage through practical code snippets.
Introduction to Joiful
Joiful makes data validation simple and efficient. By using decorators, it simplifies the process of defining validation rules directly on your classes. Let’s dive into some of the most useful APIs Joiful offers.
Basic Validation
Use @jf.string()
to ensure a property is a string.
const jf = require('joiful'); class User { @jf.string().required() name; @jf.string().email() email; } const user = new User(); user.name = "John Doe"; user.email = "john.doe@example.com"; const { error } = jf.validate(user); if (error) { console.error(error.details); } else { console.log("Validation succeeded!"); }
Number Validation
Joiful allows you to validate numbers using @jf.number()
.
class Product { @jf.number().integer().min(0) id; @jf.number().greater(0) price; } const product = new Product(); product.id = 1; product.price = 99.99; const result = jf.validate(product); if (result.error) { console.error(result.error.details); } else { console.log("Number validation succeeded!"); }
Date Validation
Using @jf.date()
, you can validate date properties effectively.
class Event { @jf.date().required() startDate; @jf.date().greater('now') endDate; } const event = new Event(); event.startDate = new Date('2023-10-01'); event.endDate = new Date('2023-12-01'); const validation = jf.validate(event); if (validation.error) { console.error(validation.error.details); } else { console.log("Date validation succeeded!"); }
Boolean Validation
Joiful also supports boolean validation with @jf.boolean()
.
class Settings { @jf.boolean().required() enabled; @jf.boolean() notifications; } const settings = new Settings(); settings.enabled = true; settings.notifications = false; const { error } = jf.validate(settings); if (error) { console.error(error.details); } else { console.log("Boolean validation succeeded!"); }
Real-world Application Example
Let’s see how these APIs can be used in a real-world application. Here is an example of a simple Node.js application using Joiful for validation:
const express = require('express'); const jf = require('joiful'); class User { @jf.string().required() username; @jf.string().required() password; } const app = express(); app.use(express.json()); app.post('/register', (req, res) => { const newUser = new User(); newUser.username = req.body.username; newUser.password = req.body.password; const { error } = jf.validate(newUser); if (error) { return res.status(400).json({ errors: error.details }); } return res.status(200).send('User registered successfully'); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
With Joiful, you can easily enforce data integrity and ensure reliable data processing in your Node.js applications. The above examples illustrate just a few powerful features of Joiful, but its API is extensive and adaptable to various validation requirements.
Hash: 1bfa6eb7b9dd9e5c657f333ce52093e6810ebd667e5d37936e74149a6160b699