Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 9 days ago
JSON representation
JavaScript implementation of json-micro-schema validation format
- Host: GitHub
- URL: https://github.com/vitalets/micro-schema
- Owner: vitalets
- License: mit
- Created: 2019-11-23T15:12:53.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T19:14:26.000Z (almost 2 years ago)
- Last Synced: 2024-09-14T09:35:03.503Z (about 2 months ago)
- Topics: json, json-api, json-schema, json-validation
- Language: JavaScript
- Homepage:
- Size: 491 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)