# @node-rs/jsonschema

> A node package based on jsonschema-rs for performing JSON schema validation

Latest version **0.1.2** (published 2021-10-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @node-rs/jsonschema
pnpm add @node-rs/jsonschema
yarn add @node-rs/jsonschema
bun add @node-rs/jsonschema
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2021-10-27 |
| First published | 2021-10-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 14 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 52 |
| Maintainers | dxd_sjtu, broooooklyn |
| Keywords | napi-rs, NAPI, N-API, Rust, node-addon, node-addon-api |

## Links

- npm: https://www.npmjs.com/package/@node-rs/jsonschema
- Repository: https://github.com/ahungrynoob/jsonschema
- Homepage: https://github.com/ahungrynoob/jsonschema#readme
- Issues: https://github.com/ahungrynoob/jsonschema/issues
- npm.io page: https://npm.io/package/@node-rs/jsonschema

## Dependencies (14)

- [@types/node](https://npm.io/package/@types/node.md) ^16.11.6
- [@node-rs/helper](https://npm.io/package/@node-rs/helper.md) ^1.2.1
- [@node-rs/jsonschema-darwin-x64](https://npm.io/package/@node-rs/jsonschema-darwin-x64.md) 0.1.2
- [@node-rs/jsonschema-freebsd-x64](https://npm.io/package/@node-rs/jsonschema-freebsd-x64.md) 0.1.2
- [@node-rs/jsonschema-darwin-arm64](https://npm.io/package/@node-rs/jsonschema-darwin-arm64.md) 0.1.2
- [@node-rs/jsonschema-android-arm64](https://npm.io/package/@node-rs/jsonschema-android-arm64.md) 0.1.2
- [@node-rs/jsonschema-linux-x64-gnu](https://npm.io/package/@node-rs/jsonschema-linux-x64-gnu.md) 0.1.2
- [@node-rs/jsonschema-linux-x64-musl](https://npm.io/package/@node-rs/jsonschema-linux-x64-musl.md) 0.1.2
- [@node-rs/jsonschema-win32-x64-msvc](https://npm.io/package/@node-rs/jsonschema-win32-x64-msvc.md) 0.1.2
- [@node-rs/jsonschema-linux-arm64-gnu](https://npm.io/package/@node-rs/jsonschema-linux-arm64-gnu.md) 0.1.2
- [@node-rs/jsonschema-win32-ia32-msvc](https://npm.io/package/@node-rs/jsonschema-win32-ia32-msvc.md) 0.1.2
- [@node-rs/jsonschema-linux-arm64-musl](https://npm.io/package/@node-rs/jsonschema-linux-arm64-musl.md) 0.1.2
- [@node-rs/jsonschema-win32-arm64-msvc](https://npm.io/package/@node-rs/jsonschema-win32-arm64-msvc.md) 0.1.2
- [@node-rs/jsonschema-linux-arm-gnueabihf](https://npm.io/package/@node-rs/jsonschema-linux-arm-gnueabihf.md) 0.1.2

## Recent versions

- 0.1.2 (latest) — 2021-10-27

## README

# `@node-rs/jsonschema`

![https://github.com/ahungrynoob/jsonschema/actions](https://github.com/ahungrynoob/jsonschema/workflows/CI/badge.svg)
![](https://img.shields.io/npm/dm/@node-rs/jsonschema.svg?sanitize=true)

> A node package based on jsonschema-rs for performing JSON schema validation.

## Install

```
yarn add @node-rs/jsonschema
```

## Support matrix

| Operating Systems| node12 | node14 | node16 |
| ---------------- | ------ | ------ | ------ |
| Windows x64      | ✓      | ✓      | ✓      |
| Windows x32      | ✓      | ✓      | ✓      |
| Windows arm64    | ✓      | ✓      | ✓      |
| macOS x64        | ✓      | ✓      | ✓      |
| macOS arm64      | ✓      | ✓      | ✓      |
| Linux x64 gnu    | ✓      | ✓      | ✓      |
| Linux x64 musl   | ✓      | ✓      | ✓      |
| Linux arm gnu    | ✓      | ✓      | ✓      |
| Linux arm64 gnu  | ✓      | ✓      | ✓      |
| Linux arm64 musl | ✓      | ✓      | ✓      |
| Android arm64    | ✓      | ✓      | ✓      |
| FreeBSD x64      | ✓      | ✓      | ✓      |

## Usage
```javascript
const { isValidSync, validateSync, isValid, validate } = require("@node-rs/jsonschema");

const schema = JSON.stringify({
  type: 'object',
  properties: {
    foo: { type: 'integer' },
    bar: { type: 'string' },
  },
  required: ['foo'],
  additionalProperties: false,
})

const input = JSON.stringify({
  foo: 1,
  bar: 'abc',
})

const exceptionInput = JSON.stringify({
  foo: 'abc',
  bar: 1,
})

// check whether the input meet schema
const result = isValidSync(input, schema);
console.log(result); // true

try {
  validateSync(exceptionInput, schema);
}catch(e){
  // it will throw error if input doesn't meet schema
  console.log(e.message); // Validation error: 1 is not of type "string"; Instance path: /bar; \nValidation error: "abc" is not of type "integer"; Instance path: /foo; \n
}

// promise version of isValidSync
isValid(input, schema).then((result) => {
  console.log(result); // true
})

// promise version of validateSync
validate(input, schema).then(() => {
  console.log("feel good and input meet schema");
}).catch((e) => {
  // it will reject if input doesn't meet schema
  console.log(e.message); // Validation error: 1 is not of type "string"; Instance path: /bar; \nValidation error: "abc" is not of type "integer"; Instance path: /foo; \n
})
```

## API
```typescript
export const isValidSync: (input: string, schema: string) => boolean

export const validateSync: (input: string, schema: string) => void

export const isValid: (
  input: Buffer | string | ArrayBuffer | Uint8Array,
  schema: Buffer | string | ArrayBuffer | Uint8Array,
) => Promise<boolean>

export const validate: (
  input: Buffer | string | ArrayBuffer | Uint8Array,
  schema: Buffer | string | ArrayBuffer | Uint8Array,
) => Promise<null>
```

## Bench

### Hardware
```
Model Name:	MacBook Pro
Model Identifier:	MacBookPro16,1
Processor Name:	6-Core Intel Core i7
Processor Speed:	2.6 GHz
Number of Processors:	1
Total Number of Cores:	6
L2 Cache (per Core):	256 KB
L3 Cache:	12 MB
Hyper-Threading Technology:	Enabled
Memory:	32 GB
```
### Result
```
Running "Validate Sync" suite...
Progress: 100%

  @node-rs/jsonschema::validateSync:
    105 138 ops/s, ±2.04%   | fastest

  ajv::validateSync:
    178 ops/s, ±2.46%       | slowest, 99.83% slower

Finished 2 cases!
  Fastest: @node-rs/jsonschema::validateSync
  Slowest: ajv::validateSync
```

---
_Source: https://npm.io/package/@node-rs/jsonschema · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
