# jsonql-params-validator

> JSONQL parameters validator written in ES6+ to use with the client / server

Latest version **1.7.0** (published 2020-04-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install jsonql-params-validator
pnpm add jsonql-params-validator
yarn add jsonql-params-validator
bun add jsonql-params-validator
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.7.0 |
| Published | 2020-04-17 |
| First published | 2019-04-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 18 |
| Unpacked size | 59.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Joel Chu |
| Maintainers | joelchu |
| Keywords | validation, jsonql |

## Links

- npm: https://www.npmjs.com/package/jsonql-params-validator
- Repository: https://gitee.com:to1source/jsonql
- Homepage: http://jsonql.org
- Issues: https://gitee.com/to1source/jsonql/issues
- npm.io page: https://npm.io/package/jsonql-params-validator

## Dependencies (18)

- [acorn](https://npm.io/package/acorn.md) ^7.1.1
- [rollup](https://npm.io/package/rollup.md) ^2.6.1
- [jsonql-utils](https://npm.io/package/jsonql-utils.md) ^1.4.0
- [jsonql-errors](https://npm.io/package/jsonql-errors.md) ^1.2.1
- [jsonql-constants](https://npm.io/package/jsonql-constants.md) ^2.1.3
- [rollup-plugin-copy](https://npm.io/package/rollup-plugin-copy.md) ^3.3.0
- [rollup-plugin-alias](https://npm.io/package/rollup-plugin-alias.md) ^2.2.0
- [rollup-plugin-async](https://npm.io/package/rollup-plugin-async.md) ^1.2.0
- [rollup-plugin-buble](https://npm.io/package/rollup-plugin-buble.md) ^0.19.8
- [rollup-plugin-serve](https://npm.io/package/rollup-plugin-serve.md) ^1.0.1
- [rollup-plugin-terser](https://npm.io/package/rollup-plugin-terser.md) ^5.3.0
- [rollup-plugin-replace](https://npm.io/package/rollup-plugin-replace.md) ^2.2.0
- [rollup-plugin-analyzer](https://npm.io/package/rollup-plugin-analyzer.md) ^3.2.2
- [rollup-plugin-commonjs](https://npm.io/package/rollup-plugin-commonjs.md) ^10.1.0
- [rollup-plugin-bundle-size](https://npm.io/package/rollup-plugin-bundle-size.md) ^1.0.3
- [rollup-plugin-node-globals](https://npm.io/package/rollup-plugin-node-globals.md) ^1.4.0
- [rollup-plugin-node-resolve](https://npm.io/package/rollup-plugin-node-resolve.md) ^5.2.0
- [rollup-plugin-node-builtins](https://npm.io/package/rollup-plugin-node-builtins.md) ^2.1.2

## 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.7.0 (latest) — 2020-04-17
- 1.6.3 — 2020-04-11
- 1.6.2 — 2020-03-22
- 1.6.1 — 2020-03-15
- 1.6.0 — 2020-03-14
- 1.5.3 — 2020-03-11
- 1.5.2 — 2019-12-10
- 1.5.1 — 2019-12-09
- 1.5.0 — 2019-12-05
- 1.4.13 — 2019-12-01
- 1.4.12 — 2019-12-01
- 1.4.11 — 2019-09-16
- 1.4.10 — 2019-09-16
- 1.4.9 — 2019-09-16
- 1.4.8 — 2019-09-06
- … 47 more at https://npm.io/package/jsonql-params-validator/versions

## README

# jsonql-params-validator

This is for use with jsonql server and client side for type validation, using the `contract` as reference point.

The default export is in `umd` format. There is also an `cjs` version in the dist folder
named `jsonql-params-validator.cjs.js` for use with node.js

For validation you can use the following methods:

```js
import {
  validateSync,
  validateAsync,
  normalizeArgs,
  isObject
} from 'jsonql-params-validator'
```

- `validateSync` this is the only one that we need to call `validate(args, params) = Array`
- `validateAsync` same as above but return a promise `validateAsync(args, params) = Promise`
- `normalizeArgs` this is export main for use on the client side `normalizeArgs(args, params)`
- `isObject` this is a wrapper of our `checkIsObject` method to check if the argument is a plain object

For detail please refer to documentation (work in progress at the moment)

If you have problem to import the module, you might need to do

```js

jsonqlParamsValidator.default

```

## Utility for checking configuration

We also have several methods for checking configuration input, and make sure they are what we expected.
The reason we create this is during years of development, how often you find a bug that you never come across
when user is using your software - and it turns out the end user is passing a wrong configuration property that
cause it? Or the wrong type of the value you expect it. And we heavily use this across our entire jsonql tool set
to ensure developer are passing the right configurations, hence to eliminated potential bug.

```js
const {
  checkConfig, // sync version
  checkConfigAsync, // return promise
  constructConfig
} = require('jsonql-params-validator')
```

**TBC about how to use them**

## Custom Errors for checking

We also export 3 custom errors that we throw from within the validation function. And you can use them to check
against your code to know what went wrong with your configuration. This will be in a separate package [jsonql-errors](https://www.npmjs.com/package/jsonql-errors)

```js
const {
  JsonqlTypeError,
  JsonqlEnumError ,
  JsonqlCheckerError
} = require('jsonql-errors')
```

Example:

```js
import { BOOLEAN_TYPE, NUMBER_TYPE, STRING_TYPE } from 'jsonql-constants'

let appProps = {
  importantProp: constructConfig(true, BOOLEAN_TYPE),
  anotherProp: constructConfig(123, NUMBER_TYPE),
  anythingProp: constructConfig('*', [NUMBER_TYPE, STRING_TYPE])
}
let constProps = {
  propDontWantToChange: 'I-must-be-here!'
}

let config = {importantProp: 'ok'}; // passing a string?!

checkConfigAsync(config, appProps, constProps)
  .then(result => {
    // do your thing
  })
  .catch(error => {
    if (error instanceof JsonqlTypeError) {
      console.error(`The config value for ${error.message} you passed is wrong!`)
    }
  })
```

---

[Joel Chu](https://joelchu.com)

[NEWBRAN LTD](https://newbran.ch) / [TO1SOURCE CN](https://to1source.cn) (c) 2019

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