# snobject2

> A lightweight validator for JS objects

Latest version **0.2.0** (published 2017-10-19) · MIT license · 0 weekly downloads

## Install

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

## 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.2.0 |
| Published | 2017-10-19 |
| First published | 2017-08-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | David Lorenz |
| Maintainers | julianxhokaxhiu |
| Keywords | validate, objects, json |

## Links

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

## Dependencies (1)

- [jsonpointer](https://npm.io/package/jsonpointer.md) ^4.0.1

## 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.2.0 (latest) — 2017-10-19
- 0.1.1 — 2017-08-31
- 0.1.0 — 2017-08-31

## README

# snobject2
A promise-based, lightweight object validator that makes use of jsonpointer to use flat-hierarchy access of data (like XPath).

## Usage
Note that the validator freezes the validation specification object using `Object.freeze` to avoid unwanted side effects.
```javascript
createValidator = require('snobject2')

var objectToValidate = {
  'foo': {
    'bar': {
      'xyz': 'foobar'
    }
  },
  'paramX': /^my-regex$/,
  'paramY': [
    1,2,3
  ]
};

var myValidator = createValidator({
  '/foo/bar/xyz': {
    required: true,
    type: 'String'
  },
  '/paramX': {
    required: false,
    type: 'RegExp'
  },
  '/paramY': {
    type: 'Array',
    required: true,
    validate: function(value, __) {
      if (value.indexOf(2) == -1) {
        return Promise.reject(__('The entry `2` is not in your array ?path')); //__ will replace ?path with the corresponding name
      } 
      
      return Promise.resolve();
    }
  }
});
```

## Fetching all errors in one shot

If you want to fetch all errors, without stopping the validation at the first one that may be encountered, you can pass a second argument to the validate function, as an object with `fetchAll` key, set to `true`. See the following example.

```javascript
snobject = require('snobject2')

const validate = snobject({
    '/test': {
      'type': 'String',
      'required': false,
    },
    '/test2': {
      'type': 'Array',
      'required': true
    }
  })

validate({ 'test': true }, { fetchAll: true })
  .then( ( result ) => console.log( result.length ) ) // 2
  .catch( ( err ) => console.log( err ) )
```

## Variant type check

If you wish to, you can also have a variant check of your type. Useful if you expect different kind of types for a specific key ( example, an Array or an Object ). In order to use this type check you can use the pipe character ( `|` ) to separate possible type values. See the following example.

```javascript
const validate = snobject({
    '/test': {
      'type': 'Array|Object',
      'required': false,
    },
    '/test2': {
      'type': 'Array',
      'required': true
    }
  })

  await validate({
    'test': {}, // can be an Array or an Object
    'test2': []
  })
    .then( () => t.pass() )
    .catch( () => t.fail() )
```

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