1.0.7 • Published 3 years ago

owi-validator v1.0.7

Weekly downloads
13
License
ISC
Repository
github
Last release
3 years ago

owi-validator

owi-validator is a beginner friendly light weight validation library built with javascript. Build Status Coverage Status Github All Releases GitHub forks GitHub stars GitHub issues

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;

Contributors

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago