owi-validator
v1.1.0
Published
**owi-validator** is a beginner friendly light weight validation library built with javascript. <br> [![Build Status](https://travis-ci.org/cleave3/owi_validator.svg?branch=master)](https://travis-ci.org/cleave3/owi_validator) [![Coverage Status](https://
Downloads
246
Maintainers
Readme
owi-validator
owi-validator is a beginner friendly light weight validation library built with javascript.
Getting Started
npm install owi-validator
Features
owi-validator can be used vanilla Js projects, nodeJs and other javascript Applications.
- max()
- min()
- equal()
- number()
- string()
- boolean()
- email()
- telephone()
- url()
- date()
- card()
- length()
- regex()
- array()
- optional()
- required()
- error()
USAGE
Basic usage
//using ES5
const { owi, validate } = require("owi-validator");
//OR
//using ES6
import { owi, validate } from "owi-validator";
const category = "category1";
const price = 2000;
const productName = "product1";
const quantity = 1;
const description = "desc";
const visibility = "public";
const schema = {
productName: owi(productName)
.string()
.min(2)
.error('product name must be atleast 2 characters long')
.required()
.exec(),
category: owi(category).string().error('category is required').required().exec(),
price: owi(price).number().error('price must be a number').required().exec(),
quantity: owi(quantity).number().error('quantity must be a number').optional().exec(),
description: owi(description).string().error('description is required').required().exec(),
visibility: owi(visibility)
.regex(/^(public|private)$/)
.error('visibilty must be either public or private')
.required()
.exec(),
}
const check = validate(schema);
//const {isValid, errors } = check;
// isvalid: boolean ( true || false )
// errors: Array - validation errors if any
if(check.isValid) {
//validation successfull
// do success action
} else {
// Errors found
console.log(check.errors); // Array of validation errors
}
using as middleware in NodeJs
//Application Entry Point index.js
const express = require("express");
const router = require("./route");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(router);
app.listen(PORT, () => console.log(`App is running at port ${PORT}`));
//Validation Class validator.js
const { owi, validate } = require("owi-validator");
class Validate {
static validatedata({ body: { name, email, password } }, res, next) {
const check = validate({
name: owi(name).string().min(2).required().error("Name must be atleast 2 characters long").exec(),
email: owi(email).email().required().error("Please enter a valid email").exec(),
password: owi(password).string().min(6).max(10).exec(),
});
return check.isValid ?
next() :
return res.status(code).json({ status, code, errors: check.errors });
}
}
module.exports = Validate;
//Router router.js file
const { Router } = require("express");
const TestController = require("../controller/dummy");
const Validate = require("../validator");
const router = Router();
const { validatedata } = Validate;
const { postData } = TestController;
router.get("/", (req, res) => res.send("owi-validator is live"));
router.post("/data", validatedata, postData);
module.exports = router;