Git Product home page Git Product logo

mark-validator's Introduction

mark-validator

This libary is a validator manager. it helps us to validate json easy. like the code below.

There have two advantage.

  • First, you can validate nest object
  • Second, you can custom the validate function which come from plugin or yourself.
        const testObj = {
            people: {
                name: 'Mark',
                age: 35,
            },
            count: 50,
        };

        validator.config = {
            people: {
                name: [requireValue()],
                age: [
                    requireValue(),
                    requireNumberInRange(10, 25),
                ],
            },
            count: [
                requireNumberInRange(0, 25),
            ],
        };

        const result = validator.validate(testObj);

How to use ?

Step1. npm install

npm install mark-lin-validator

Step2. Creating the validates and Executing initValidate method.

You can choose custom validate functions or plugin functions.

custom validate function

There have a import point. the first parameter of custom function should be "you want to validate value".

const validator = require('mark-lin-validator');

function requireValue(value) {
    return !!(value);
}

function isBoolean(value) {
    return (typeof value === 'boolean');
}

function requireNumberInRange(value, min, max) {
    const field = value;
    if (typeof field !== 'number') {
        return false;
    }
    if (isNaN(min) || field < min) {
        return false;
    }
    if (isNaN(max) || field > max) {
        return false;
    }
    return true;
}

validator.initValidate([
    requireValue,
    isBoolean,
    requireNumberInRange,
]);

Plugin validate function

You can choose plugin which you want to use. In the example, we use the chriso/validator plugin ใ€‚

const {
    isEmail,
} = require('validator');
const validator = require('mark-lin-validator');


validator.initValidate([
    isEmail,
]);

Step3. Use ! .

require('./custom-validate'); // It is your excuting initValidate method file.
const validator = require('mark-lin-validator');

const {
    requireValue,
    isBoolean,
    requireNumberInRange,
} = validator.exportFunc();

        const testObj = {
            people: {
                name: 'Mark',
                age: 35,
            },
            count: 50,
        };

        validator.config = {
            people: {
                name: [requireValue()],
                age: [
                    requireValue(),
                    requireNumberInRange(10, 25),
                ],
            },
            count: [
                requireNumberInRange(0, 25),
            ],
        };

        const result = validator.validate(testObj);

Step4. Result.

Result Object


{
    isSuccess : true/false,
    errors: [
        {
            msg: 'Invalid value for *people.age*, requireNumberInRange',
            checkerName: 'requireNumberInRange',
            checkTarget: 'people.age'
        }
    ]

}

Nest Object Validate

If you want to validate the nest object, following the code below.

        const testObj = {
            people: {
                name: 'Mark',
                age: 35,
            },
            count: 50,
        };

        validator.config = {
            people: {
                name: [requireValue()],
                age: [
                    requireValue(),
                    requireNumberInRange(10, 25),
                ],
            },
            count: [
                requireNumberInRange(0, 25),
            ],
        };

Array Object Validate

If you want to validate the object of array, following the code below. The field of data includes two objects. if you want to validate the array (!!!). you can follow to like the code below. the requireValue is the valiator's function of array. and the last of the object is not. it is the object of array validator.

       const testObj = {
            author: 'Mark',
            data: [
                {
                    id: 1,
                    author: 'mark',
                },
                {
                    id: '2',
                    author: 'lin',
                },
            ],
            describe: 'Test',
        };

        validator.config = {
            author: [
                requireValue(),
            ],
            data: [
                requireValue(),   // validate the array.
                {  // validate the object of array.
                    id: [
                        requireValue(),
                        requireNumber(),
                    ],
                    author: [requireValue()],
                },
            ],
            describe: [
                requireValue(),
            ],
        };

mark-validator's People

Contributors

marklin-latte avatar

Watchers

 avatar  avatar

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.