# @sa0001/api-validator

> API-Validator is a library designed for validating input/output of API methods, with a very powerful but compact syntax.

Latest version **0.0.14** (published 2020-12-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sa0001/api-validator
pnpm add @sa0001/api-validator
yarn add @sa0001/api-validator
bun add @sa0001/api-validator
```

## 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.0.14 |
| Published | 2020-12-03 |
| First published | 2019-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 160.6 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | sa0001 |
| Maintainers | sa0001 |

## Links

- npm: https://www.npmjs.com/package/@sa0001/api-validator
- npm.io page: https://npm.io/package/@sa0001/api-validator

## Dependencies (6)

- [uuid](https://npm.io/package/uuid.md) ^8.3.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [tz-ids](https://npm.io/package/tz-ids.md) ^1.0.0
- [@sa0001/type-of](https://npm.io/package/@sa0001/type-of.md) 0.0.6
- [@sa0001/deep-diff](https://npm.io/package/@sa0001/deep-diff.md) 0.0.10
- [@sa0001/pretty-print](https://npm.io/package/@sa0001/pretty-print.md) 0.0.7

## Recent versions

- 0.0.14 (latest) — 2020-12-03
- 0.0.13 — 2020-12-03
- 0.0.11 — 2020-10-01
- 0.0.10 — 2020-08-26
- 0.0.9 — 2020-05-13
- 0.0.8 — 2020-04-28
- 0.0.7 — 2019-12-23
- 0.0.6 — 2019-09-12
- 0.0.5 — 2019-07-08
- 0.0.4 — 2019-07-02
- 0.0.3 — 2019-07-02
- 0.0.2 — 2019-07-02

## README

# API-Validator

API-Validator is a library designed for validating input/output of API methods, with a very powerful but compact syntax.

## Simple Usage

A line of code is worth a thousand words.  Here's a carefully curated minimal example of a validator schema, and both passing and failing input.

	var ApiValidator = require('@sa0001/api-validator')
	var validator = ApiValidator.new() // use default options
	
	var schema = validator.schema({obj:1,schema:{
		a: {int:1},
	}})
	
	var goodInput = {
		a: 1,
	}
	var badInput = {
		a: true,
	}
	
	// passing
	result = schema.test(goodInput)
	
	// failing
	try {
		result = schema.test(badInput)
	catch (err) {
		err
	}
	
	var output = ApiValidator.new().schema(schema).input(input).test()

## Guiding Principles

- the primary purpose of this library is for validating API input/output in JSON format, so don't count on values other than the following to be preserved:
 -- boolean
 -- number
 -- string
 -- array
 -- plain object
- all other types of value, including errors, symbols, may not be preserved
- only `null` can be JSON-stringified, so all the following values are considered the same as null:
 -- `undefined`, `null`,`""`, `NaN`, `Infinity`, `-Infinity`

## Concepts

Simple Types: these are...
- `arr`, `array`, `isArray`
- `bool`, `boolean`, `isBoolean`
- `num`, `number`, `isNumber`
- `obj`, `object`, `isObject`
- `str`, `string`, `isString`
- `any`, `mix`, `mixed`
-- allows any type of value, however most operators cannot be used

Complex Types: these are...
- `int`, `integer`, `isInteger`

Operators: these are...
- `trim`, `toTrim`, `strip`, `toStrip` (for Ruby lovers)
- `isRound`
- `isLower`, `isLowerCase`
- `isUpper`, `isUpperCase`
- `isTrim`, `isTrimmed`, `isStripped` (for Ruby lovers)

Descriptors: these are...

## Example

```
	# example: sorted & unique array of enum values
	{arr:1,sort:1,uniq:1,vals:{str:1,in:SomeEnum}}
	
	# example: array of lat/lon
	{arr:1,schema:[
	    {num:1,isRound:8,btw:[-90,90]}
	    {num:1,isRound:8,btw:[-180,180]}
	]}
	
	# example: object of lat/lon
	{obj:1,schema:{
	    lat: {num:1,isRound:8,btw:[-90,90]}
	    lon: {num:1,isRound:8,btw:[-180,180]}
	}}
	
	# example: object of sorters (col -> dir)
	{obj:1,keys:{str:1,regex:/^[\w]+$/},vals:{str:1,in:['ASC','DESC']}}
```

## TODO

- operator: compact
- operator: filter
- operator: reject

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