garant
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

npm Node CI coverage badge npm npm NPM

Simple and lightweight javascript object schema validation library.

Description

  • Lightweight: when bundled with rollup and terser, the output weight less than 2kB (vs ~167kB for @hapi/joi, 80 times ratio!)
  • Modular: easily create new checkers (see here)
  • Typescript support

Installation

Garant can be installed using yarn or npm.

npm install garant
# or
yarn add garant

Usage

import {Validator} from 'garant';
// or
const {Validator} = require('garant');

const schema = {
    username: {
        type: 'string',
        required: true
    },
    email: {
        type: 'string',
    },
    info: {
        type: 'object',
        children:{
            age: {
                type: 'number',
            },
            height: {
              type: 'number',
          }
        }
    }   
};

const validator = new Validator(schema);

const object = {
    username: 'Jane',
    email: 'jane@example.com',
    info: {
        age: 22,
        height: 165
    }
};

const results = validator.check(object);

// {
//     hasError: false,
//     messages: [],
//     data: {
//       username: 'Jane',
//       email: 'jane@example.com',
//       info: {
//           age: 22,
//           height: 165
//       }
//     }
// }

Road map

  • Required checker
  • Type checker
  • Children checker
  • Default checker (set default value if undefined)
  • Improve documentation
  • Regex checker
  • Length (min, max) checker
  • Array content checker

Want to add your checker? Simply create yours in the src/checkers directory and register it in the Validator class. Submit your pull request!

Contribute

Pull requests are welcome ! Feel free to contribute.

Credits

Coded with ❤️ by Corentin Thomasset.

License

This project is under the MIT license.

Package Sidebar

Install

npm i garant

Weekly Downloads

4

Version

1.2.1

License

MIT

Unpacked Size

21.2 kB

Total Files

32

Last publish

Collaborators

  • corentinth