Novices Plagiarizing Mozart

    @konglx/data-validate

    1.0.9 • Public • Published

    Data validate

    Validate rules

    A brief data validation library.

    Import

    // ES6
    import validate from '@konglx/data-validate';
    // ES5
    const validate = require('@konglx/data-validate');

    String rules

    The type check is based on Object.prototype.toString.call(o).slice(8, -1).toLocaleLowerCase()

    //  Basic data types
    validate('string', 'i am a string') // => true
    validate('number', 1) // => true
    validate('null', null) // => true
    validate('undefined', undefined) // => true
     
    // Object Array
    validate('object', {}) // => true
    validate('array', []) // => true
     
    // instanceOf
    function Dog() {}
    const dog = new Dog();
    validate(Dog, dog); // => true
     
    // optional
    validate('?:string', undefined) // => true
    validate('?:string', 'ok') // => true
     

    Compose validate rules

    // object
    validate({ a: 'string', b: 'number'}, {
      a: 'i am a string',
      b: 1,
    }) // => true
     
    // array
    validate(['string'], ['a', 'b']) // => true
    validate(['string'], ['a', 1]) // => false
     
    // if you want two type in a Array
    validate(['string|number'], ['a', 1])
     
    // deep
    validate([{
        name: 'string',
        girlFrends: [
            {
                name: 'string',
                friends: [
                    {
                        name: 'string',
                        classmates: [
                            {
                                name: 'string',
                                hobbies: []
                            }
                        ]
                    }
                ]
            }
        ]
    }], [{
      name: 'xiaoming',
      girlFrends: [{
        name: 'zhang',
        friends: [{
          name: 'mao',
          classmates: [
            {
              name: 'li',
              hobbies: [],
            }
          ]
        }]
      }],
    }]) // => true
     

    Error

    // If you want get the error path
    validate({ ok: 'number' }, { ok: '9999' }, falseThrowError = true); // This will throw a Error with error path

    More examples

    Other Tips

    1. Empty Array
    // can pass validate
    validate(
        {
            friends: [{
                name: 'string'
            }],
        },
        {
            names: [],
        }
    ) // => true
     
    // can't pass validate
    validate(
        {
            friends: [{
                name: 'string',
            }],
        },
        {
            names: [{
     
            }],
        }
    ) // => true

    publish

    npm publish --access=public

    Dev

    Test Cover Istanbul

    npm run cover

    Keywords

    none

    Install

    npm i @konglx/data-validate

    DownloadsWeekly Downloads

    2

    Version

    1.0.9

    License

    MIT

    Unpacked Size

    16.5 kB

    Total Files

    16

    Last publish

    Collaborators

    • konglx