# jamin-json-schema-validation

> This JSON schema validator allows developers to run a function on each property in a JSON object and test if the JSON is what you expected.

Latest version **1.0.1** (published 2018-03-20) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-03-20 |
| First published | 2018-03-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Benjamin Meyer |
| Maintainers | ben-jam-in-1 |
| Keywords | json, schema, validate, validation, valid |

## Links

- npm: https://www.npmjs.com/package/jamin-json-schema-validation
- Repository: https://github.com/ben-jam-in-1/jamin-json-schema-validation
- Homepage: https://github.com/ben-jam-in-1/jamin-json-schema-validation#readme
- Issues: https://github.com/ben-jam-in-1/jamin-json-schema-validation/issues
- npm.io page: https://npm.io/package/jamin-json-schema-validation

## 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

- 1.0.1 (latest) — 2018-03-20
- 1.0.0 — 2018-03-20

## README

# jamin-json-schema-validation
This JSON schema validator allows developers to run a function on each property in a JSON object and test if the JSON is what you expected.

### Installing
```
npm install jamin-json-schema-validation --save
```

## Use
```javascript
const SchemaValidator = require("jamin-json-schema-validation"),
      validateSchema = SchemaValidator.validateSchema,
      schemaRejection = SchemaValidator.schemaRejection;

let data = {
    name: "ben",
    color: "blue",
    age: 17
}
let schema = {
    name: (name) => {
        if (name.length > 50) {
            throw schemaRejection("Exceeded maximum amount of characters for your name.")
        }
    },
    color: null, //use null if you don't need to validate with a function
    age: (age) =>  {
        if(age < 18){
            throw schemaRejection("You are too young.")
        }
    }
}
validateSchema(data, schema)
    .then((validSchema) => {
        console.log("VALID SCHEMA: ", validSchema)
    })
    .catch((err) => {
        console.log("INVALID SCHEMA: ", err)
    })
```


### Suggested use with Express
```javascript
app.post("/route", function (req, res) {
    let body = req.body
    /*
    body = {
        name: "ben",
        color: "blue",
        age: 17
    }
    */
   validateSchema(body, schema)
    .then((validSchema) => {
        //validSchema == body
        //Now:
        //Perform ops on database...
    })
    .catch((err) => {
        //err is an object in the form {message: "", key: ""}
        //key may be undefined if the error was not found due to a custom thrown schemaRejection
        if(key === "age"){
            res.send(err.message)
            //sends - You are too young.
        }
    })

})
```

NOTE:
The example with Express uses a body parser for the post data
```
npm install body-parser --save
```
Then
```javascript
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
    extended: true
}))
```

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