https://github.com/microfleet/validation
Integrates ajv module for various validation tasks
https://github.com/microfleet/validation
Last synced: about 2 months ago
JSON representation
Integrates ajv module for various validation tasks
- Host: GitHub
- URL: https://github.com/microfleet/validation
- Owner: microfleet
- License: mit
- Created: 2015-11-03T19:32:59.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-02T02:50:18.000Z (over 1 year ago)
- Last Synced: 2025-06-29T12:06:26.379Z (9 months ago)
- Language: TypeScript
- Homepage:
- Size: 1010 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Microfleet Validation Module
[](https://semaphoreci.com/microfleet/validation)
This is basically a wrapper of [ajv](https://github.com/epoberezkin/ajv) module.
What it does - is accepts a directory with schemas, reads it in an async or sync fashion based on your preference
and caches validators under it's name, minus it's extension (to be completely honest - it strips down `.json` only).
Based on the bluebird promises.
## Installation
`npm i @microfleet/validation`
## Usage
```ts
// Lets assume that we have a following file structure:
//
// .
// ./schemas/config.json
// ./schemas/ping.json
// ./index.js
//
import Errors = require('common-errors');
import Validator, { HttpStatusError } from '@microfleet/validation';
const validator = new Validator('./schemas');
await validator.init()
// some logic here
validator.validate('config', {
configuration: 'string'
})
.then(doc => {
// all good
// handle doc, which would eq { configuration: 'string' }
})
.catch(HttpStatusError, (error) => {
// handle error here
});
const result = validator.validateSync('config', { data: true });
if (result.error) {
// handle error!
}
// do stuff
// ...
// init filter
validator.init('./dir', null, true); // all schemas in this dir will filter out additional properties instead of throwing an error
// catches when we only have 417 errors
validator.filter('config', { conf: 'string', extra: true })
.then(result => {
// { conf: 'string' }
});
```