# jsnvl

> A simple Javascript Object validator.

Latest version **1.1.0** (published 2021-01-11) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install jsnvl
pnpm add jsnvl
yarn add jsnvl
bun add jsnvl
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2021-01-11 |
| First published | 2020-10-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | zkldi |
| Maintainers | zkldi |
| Keywords | JSON, validation, validator |

## Links

- npm: https://www.npmjs.com/package/jsnvl
- Repository: https://github.com/zkldi/jsnvl
- Homepage: https://github.com/zkldi/jsnvl#readme
- Issues: https://github.com/zkldi/jsnvl/issues
- npm.io page: https://npm.io/package/jsnvl

## 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.1.0 (latest) — 2021-01-11
- 1.0.5 — 2020-10-12
- 1.0.4 — 2020-10-12
- 1.0.3 — 2020-10-12
- 1.0.2 — 2020-10-06
- 1.0.0 — 2020-10-06
- 0.0.0 — 2020-10-06

## README

# jsnvl ![Node.js CI](https://github.com/zkldi/jsnvl/workflows/Node.js%20CI/badge.svg)
The simple Javascript Object Validator.

## Notes

JSNVL is an incredibly simple Javascript Object Validator, and is designed for simple schema validation.
There are other libraries available if you require more complicated validation, but this should be enough for most use cases.

## Usage
- Install from NPM, `npm install jsnvl`


```js
import JSNVL from "jsnvl";
```
*****

### JSNVL(object, schema, options)
- object **(Object)**:
    - The JS object you wish to validate.
- schema **(Object)**:
    - The schema you wish to validate the object against.
- options **(Object, optional)**:
    - ignoreExcessKeys **(Boolean, Default `false`)**: Ignores any keys present on the object that are NOT present on the schema.
    - ignoreThrownErrors **(Boolean, Default `false`)**: Ignores any errors thrown by JSNVL. JSNVL throws errors on implementation issues, such as passing an invalid schema. This will convert those errors to a `false` return.
    - silenceLogs **(Boolean, Default `false`)**: If true, JSNVL will not log any information about the validation process (such as when errors occur).
    
### Schema Structure

#### Simple type checks:

```js
let schema = {
  name: "string",
  age: "number",
  sayName: "function",
}

let object = {
  name: "Bob",
  age: 15,
  sayName: () => {}
}

JSNVL(object, schema) // true
```
In short, If a value in a schema is a string, JSNVL will check that the `typeof` comparison counterpart is equal to that string.
An exception is that the string `"object"` checks taht the typeof is an object AND the value is not `null`.

#### Nullable type checks:
```js
let schema = {
  name: "?string",
}

let object = {
  name: null,
}

JSNVL(object, schema) // true
```
Prefixing a type with `?` will allow that field to be nullable.

#### Inside Array

```js
let schema = {
   fruit: ["Banana","Plum"]
}

let object = {
  fruit: "Plum"
}

JSNVL(object, schema) // true
```

If a value inside a schema is an array, JSNVL will check if the value in the object is inside that array.

#### Custom

```js
let schema = {
  oddNumber: (self) => self % 2 === 1
}

let object = {
  oddNumber: 15
}

JSNVL(object, schema) // true
```

If a value inside a schema is a function, JSNVL will evaluate that function, passing the value of the object as the first parameter.
I encourage using `self` for this name, and using currying to evaluate more complex functions.

A second parameter, `parent` is also available, and will pass the object `self` is enclosed in. This can be used for conditional evaluation,
such as:

```js
let schema = {
  start: "number",
  end: (self, parent) => self > parent.start
}

let object = {
  start: 10,
  end: 15
}

JSNVL(object, schema) // true

object.end = 9;

JSNVL(object, schema) // false
```

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