Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/vitalets/micro-schema

JavaScript implementation of json-micro-schema validation format
https://github.com/vitalets/micro-schema

json json-api json-schema json-validation

Last synced: 1 day ago
JSON representation

JavaScript implementation of json-micro-schema validation format

Awesome Lists containing this project

README

        

# micro-schema
[![Actions Status](https://github.com/vitalets/micro-schema/workflows/autotests/badge.svg)](https://github.com/vitalets/micro-schema/actions)
[![npm](https://img.shields.io/npm/v/@vitalets/micro-schema.svg)](https://www.npmjs.com/package/@vitalets/micro-schema)
[![license](https://img.shields.io/npm/l/@vitalets/micro-schema.svg)](https://www.npmjs.com/package/@vitalets/micro-schema)

JavaScript implementation of [JSON micro schema](https://github.com/vitalets/json-micro-schema) validation format.

## Contents

- [Installation](#installation)
- [Usage](#usage)
- [Docs](#docs)
- [License](#license)

## Installation
```bash
npm install @vitalets/micro-schema
```

## Usage
1. Require `validate` function:
```js
const { validate } = require('@vitalets/micro-schema');
```

2. Define schema:
```js
const schema = {
productId: {
$type: 'number',
$required: true
},
productName: {
$type: 'string',
$required: true,
$maxLength: 255
},
tags: [{
$type: 'string'
}]
};
```

3. Validate object:
```js
const object = {
productId: '1',
productName: undefined,
tags: [42]
};

const errors = validate(schema, object);
```

4. Handle validation errors:
```json
[
{
"validator": "$type",
"path": "productId",
"expectedType": "number",
"actualType": "string"
},
{
"validator": "$required",
"path": "productName"
},
{
"validator": "$type",
"path": "tags.0",
"expectedType": "string",
"actualType": "number"
}
]
```

## Docs
Please see [JSON micro schema docs](https://github.com/vitalets/json-micro-schema) for all available validators.

## License
MIT @ [Vitaliy Potapov](https://github.com/vitalets)