Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/corentinth/garant

Simple and lightweight javascript object schema validation library.
https://github.com/corentinth/garant

assert check javascript library object type validation

Last synced: about 1 month ago
JSON representation

Simple and lightweight javascript object schema validation library.

Awesome Lists containing this project

README

        


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](https://www.npmjs.com/package/@hapi/joi), 80 times ratio!)
* **Modular**: easily create new checkers (see [here](#road_map))
* **Typescript support**

## Installation

**Garant** can be installed using yarn or npm.

```shell
npm install garant
# or
yarn add garant
```

## Usage
```javascript
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: '[email protected]',
info: {
age: 22,
height: 165
}
};

const results = validator.check(object);

// {
// hasError: false,
// messages: [],
// data: {
// username: 'Jane',
// email: '[email protected]',
// 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](./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](//corentin-thomasset.fr).

## License
This project is under the [MIT license](./LICENSE.md).