Code Monkey home page Code Monkey logo

make-validation's Introduction

make-validation

Documentation https://make-validation.netlify.app/

Install

npm i @withvoid/make-validation --save

Usage

const makeValidation = require('@withvoid/make-validation');

const result = makeValidation((types) => {
  return {
    payload: {},
    checks: {},
  };
});

console.log('result', result.success, result.message, result.errors);

Examples

This library was intended to validate user req.body in your node/express projects for example.

See the code version example here

express-example example

Api

callback

const validation = makeValidation((types) => {});

makeValidation method returns a callback, the callback has all the valid types of validations available.

callback example

payload

const validation = makeValidation((types) => {
  return {
    payload: {
      firstName: 'john',
      lastname: 'doe',
    },
  };
});

payload is the actual data you want to verify

checks

const validation = makeValidation(types => {
  return {
    payload: {
      firstName: 'john',
      lastname: 'doe'
    }
    checks: {
      firstName: { type: types.string },
      lastname: { type: types.string },
    }
  }
});

checks will check the data in the payload if they are of the right type.

For every check type there are some options available.

types.string

  • options.empty (default false) will check if the string is allowed to be empty '' or not.

types.array

  • options.unique (default false) will check if the array is unique or not
  • options.stringOnly (default false) will check if the all the values in array are strings or not
  • options.empty (default true) will check if the array is empty allowed or not

types.enum

  • options.enum (default {}, required: yes) It can be of 2 types string and object.
checks: {
  userType1: { type: types.enum, options: { enum: 'admin user' } },
  userType2: {
    type: types.enum,
    options: {
      enum: { 0: 'admin', 1: 'user' },
    },
  },
},
  • If options.enum a string the enum is seperated by space.
  • If options.enum an object the enum are the values in the objects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.