https://github.com/zaggino/z-schema
JSON Schema validator written in JavaScript for NodeJS and Browsers
https://github.com/zaggino/z-schema
Last synced: about 1 month ago
JSON representation
JSON Schema validator written in JavaScript for NodeJS and Browsers
- Host: GitHub
- URL: https://github.com/zaggino/z-schema
- Owner: zaggino
- License: other
- Created: 2013-07-23T02:13:35.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2024-07-29T16:50:07.000Z (over 1 year ago)
- Last Synced: 2025-05-10T07:06:11.428Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 9.24 MB
- Stars: 338
- Watchers: 8
- Forks: 91
- Open Issues: 38
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-json - z-schema - validator written in JavaScript for NodeJS and Browsers. (JSON Schema Validators)
README
# z-schema - a JSON Schema validator
[](https://www.npmjs.com/package/z-schema)
[](https://coveralls.io/github/zaggino/z-schema?branch=main)
## Topics
- [What](#what)
- [Versions](#versions)
- [Usage](#usage)
- [Features](#features)
- [Options](#options)
- [Contributing](#contributing)
- [Contributors](#contributors)
## What
What is a JSON Schema? Find here: [https://json-schema.org/](https://json-schema.org/)
## Versions
- v6 - old version which has been around a long time, supports JSON Schema draft-04
- v7 - modernized version (to ESM module with Typescript) which passes all tests from JSON Schema Test Suite for draft-04
- v8 - by default assumes all schemas without $schema tag are draft-04, the old behaviour from v7 can be explicitly turned on by specifying `validator = new ZSchema({ version: 'none' });`
## Usage
Validator will try to perform sync validation when possible for speed, but supports async callbacks when they are necessary.
### ESM and Typescript:
```javascript
import ZSchema from 'z-schema';
const validator = new ZSchema();
console.log(validator.validate(1, { type: 'number' })); // true
console.log(validator.validate(1, { type: 'string' })); // false
```
### CommonJs:
```javascript
const ZSchema = require('z-schema');
const validator = new ZSchema();
console.log(validator.validate(1, { type: 'number' })); // true
console.log(validator.validate(1, { type: 'string' })); // false
```
### CLI:
```bash
npm install --global z-schema
z-schema --help
z-schema mySchema.json
z-schema mySchema.json myJson.json
z-schema --strictMode mySchema.json myJson.json
```
### Sync mode:
```javascript
var valid = validator.validate(json, schema);
// this will return a native error object with name and message
var error = validator.getLastError();
// this will return an array of validation errors encountered
var errors = validator.getLastErrors();
...
```
### Async mode:
```javascript
validator.validate(json, schema, function (err, valid) {
...
});
```
### Browser:
```html
var validator = new ZSchema();
console.log(validator.validate('string', { type: 'string' }));
```
### Remote references and schemas:
In case you have some remote references in your schemas, you have to download those schemas before using validator.
Otherwise you'll get `UNRESOLVABLE_REFERENCE` error when trying to compile a schema.
```javascript
var validator = new ZSchema();
var json = {};
var schema = { "$ref": "http://json-schema.org/draft-04/schema#" };
var valid = validator.validate(json, schema);
var errors = validator.getLastErrors();
// valid === false
// errors.length === 1
// errors[0].code === "UNRESOLVABLE_REFERENCE"
var requiredUrl = "http://json-schema.org/draft-04/schema";
request(requiredUrl, function (error, response, body) {
validator.setRemoteReference(requiredUrl, JSON.parse(body));
var valid = validator.validate(json, schema);
var errors = validator.getLastErrors();
// valid === true
// errors === undefined
}
```
If you're able to load schemas synchronously, you can use `ZSchema.setSchemaReader` feature:
```javascript
ZSchema.setSchemaReader(function (uri) {
var someFilename = path.resolve(__dirname, '..', 'schemas', uri + '.json');
return JSON.parse(fs.readFileSync(someFilename, 'utf8'));
});
```
## Features
See [FEATURES.md](FEATURES.md) for a full list of features.
## Options
See [OPTIONS.md](OPTIONS.md) for all available options and their descriptions.
## Contributing
These repository has several submodules and should be cloned as follows:
> git clone **--recursive** https://github.com/zaggino/z-schema.git
## Contributors
Big thanks to:
Sergey Shandar
Ivan Goncharov
Pete Gonzalez (OLD ALIAS)
Simon R
Jason Oettinger
Jeremy Whitlock
Evgeny
Dan McGee
Thomas Hallock
Priit Kallas
Marco Santarelli
Jan Pilzer
Geraint
Daniel Gerber
Anna Henningsen
Konstantin Vasilev
barrtender
Roman Hotsiy
RenaudS
Reese
Matti Schneider
Matthew Dahl
José F. Romaniello
Ivan Kasenkov
Tony Lin
Dominik Moritz
Dmitry Semigradsky
Tao Huang
BuBuaBu
and to everyone submitting [issues](https://github.com/zaggino/z-schema/issues) on GitHub