# schema-json

> Creates Json schema, validate it

Latest version **0.0.0-alpha-0f** (published 2017-10-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install schema-json
pnpm add schema-json
yarn add schema-json
bun add schema-json
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.0-alpha-0f |
| Published | 2017-10-06 |
| First published | 2017-10-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Atikur Rahman |
| Maintainers | atik |
| Keywords | json, schema, validation, json validation, secure endpoint request, secure, endpoint, request |

## Links

- npm: https://www.npmjs.com/package/schema-json
- npm.io page: https://npm.io/package/schema-json

## Dependencies (3)

- [underscore](https://npm.io/package/underscore.md) ^1.8.3
- [@types/node](https://npm.io/package/@types/node.md) ^8.0.32
- [@types/underscore](https://npm.io/package/@types/underscore.md) ^1.8.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.0-alpha-0f (latest) — 2017-10-06
- 0.0.0-alpha-0e — 2017-10-06
- 0.0.0-alpha-0d — 2017-10-06
- 0.0.0-alpha-0c — 2017-10-06
- 0.0.0-alpha-0b — 2017-10-06
- 0.0.0-alpha-0a — 2017-10-06

## README

### Check json Schema or format
It's a simple module to check a json schema

### Create Json Schema
```typescript
import {SchemaJson} from 'schema-json';

let schema = new SchemaJson({
    name: String,
    phone: [{
        code:{type:Number, required: true, error: "hello"},
        number: Number
    }],
    address:{
        vill: { type:String, required: true, message: "Custom message"},
        dist: String,
        pin: Number
    }
});
```
`field` can be javascript data type `Number`, `String`, `Object`, `Boolean` and `Array`
    
####field options
1. `type`: Can be jvascript data type `Number`, `String`, `Object`, `Boolean` and `Array`.
2. `required`: Makes a field required. default it is `false`.
3. `message`: Sets custom message for field error.


### Check data with schema
```typescript

let result = schema.isValid({
     name: "asdfgh",
     phone: [{
             code: 12345,
             number: 1234567
     }],
     address: {
         vill:"north Raipur p1-1",
         dist: "QWERTYU",
         pin: 123456
     }
    });
console.log(result)  // Result true
```

##Error handling
if callback is provided in `isValid(data, callback)` then it will be called with two argument `err` and `isValid`
```typescript
import {SchemaJson} from 'schema-json';
let s = new SchemaJson({
    name: String,
    phone: [Number],
    address: String

});

let result = s.isValid({
    name: "asdfgh",
    phone: [123456,"wertyu"],
    address: 23456
}, (err: any, isValid: boolean)=>{
    console.log(err); // {"phone":[{"index":1,"error":{"type":false,"message":"Value is of invalid type"}}],"address":{"type":false,"message":"Value is of invalid type"}}
});

```
The error will come with the corresponding path
```json
{
  "phone":[
    {
      "index":1,
      "error":{
        "type":false,
        "message":"Value is of invalid type"
      }
    }
  ],
  "address":{
    "type":false,
    "message":"Value is of invalid type"
  }
}
```

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