# json-schema-function-signatures

> Validating function inputs/outputs with json schemas

Latest version **0.0.3** (published 2018-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-schema-function-signatures
pnpm add json-schema-function-signatures
yarn add json-schema-function-signatures
bun add json-schema-function-signatures
```

## 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.3 |
| Published | 2018-07-23 |
| First published | 2018-07-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 21.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Yanick Champoux |
| Maintainers | yanick |
| Keywords | json schema |

## Links

- npm: https://www.npmjs.com/package/json-schema-function-signatures
- Repository: https://github.com/yanick/json-schema-function-signatures
- npm.io page: https://npm.io/package/json-schema-function-signatures

## Dependencies (3)

- [ajv](https://npm.io/package/ajv.md) ^6.5.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.10
- [json-schema-shorthand](https://npm.io/package/json-schema-shorthand.md) ^0.3.0

## 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.0.3 (latest) — 2018-07-23
- 0.0.2 — 2018-07-23
- 0.0.1 — 2018-07-23

## README

# json-schema-function-signatures 

This package provides a way to validate the inputs, output, and context 
of functions via [JSON schema](https://json-schema.org) definitions.

## Basic usage

Using the proposed pipeline operator via [babel](https://www.npmjs.com/package/@babel/plugin-proposal-pipeline-operator).


    import { number, object, string } from "json-schema-shorthand";

    import { funcSchema } 
        from "json-schema-function-signatures";

    const myFunc =
        function(foo, bar) {
            return { sum: foo + bar, ctx: { baz: this } };
        }
            |> funcSchema(
                [number({ minimum: 2 }), number({ maximum: 3 })],
                object({                                          
                    sum: number({ minimum: 10 }),
                    ctx: object({ baz: string({ minLength: 3 }) })
                }),
                string()
            );


Same thing, but using [lodash](https://github.com/lodash)'s `_.flow`.

    import { number, object, string } from "json-schema-shorthand";

    import { funcSchema } from "json-schema-function-signatures";

    import _ from 'lodash';

    const myFunc = _.flow([
        function(foo, bar) {
            return { sum: foo + bar, ctx: { baz: this } };
        },
        funcSchema(
            [number({ minimum: 2 }), number({ maximum: 3 })],
            object({
                sum: number({ minimum: 10 }),
                ctx: object({ baz: string({ minLength: 3 }) })
            }),
            contextSchema(string()
        )
    ]);

Still same thing, barebone without any helper function.

    import { number, object, string } from "json-schema-shorthand";

    import { funcSchema } from "json-schema-function-signatures";

    const myFunc = funcSchema(
        [number({ minimum: 2 }), number({ maximum: 3 })],
        object({
            sum: number({ minimum: 10 }),
            ctx: object({ baz: string({ minLength: 3 }) })
        }),
        contextSchema(string()
    )(
        function(foo, bar) {
            return { sum: foo + bar, ctx: { baz: this } };
        },
    );


## Exports

### inSchema( schema )( targetFunction )

Validates the arguments of the target function against the `schema`. 

If `schema` is an array, it's automatically inflated as the `items` of 
an `array` type. I.e.,

    const myFunc = ( foo => { ... } )
        |> inSchema([{type => 'string'}]);

    // equivalent to 

    import { array } from 'json-schema-shorthand';
    const myFunc = ( foo => { ... } )
        |> inschema({ type => 'array', items => [{type => 'string'}] });


### outSchema( schema )( targetFunction )

Validates the return value of the target function against the `schema`. 

### contextSchema( schema )( targetFunction )

Validates the context of the target function against the `schema`. 

### funcSchema( inSchema, outSchema, contextSchema )( targetFunction )

Shortcut to call one or more of the `in`/`out`/`context`Schema wrappers.
Passing `undefined` as one schema disables that validation.

### FunctionSignatures 

    import FunctionSignatures from 'json-schema-function-signatures';

    const myValidator = new FunctionSignatures();

    const myFunc = function(x,y) { return x+y }
        |> myValidator.outSchema({ type: 'number', minimum: 12 });

The default export of the package is the `FunctionSignatures` class.

#### new FunctionSignatures(options) 

    const myValidator = new FunctionSignatures({
        ajv:      new Ajv(),
        onError:  error => { ... },
        disabled: false,
    )};

Accepts the following options:

1. *ajv*: custom [Ajv](https://github.com/epoberezkin/ajv) 
object for the schema validations.

1. *onError*: callback invoked when a validation fails. Will be passed
    an error object encapsulating the validation errors. Default to throwing an
   exception.

1. *disabled*: if set to true, no validation is performed.

### disabled 

Getter/setter for the `disabled` attribute of the object. If `true`, no
validation is performed.

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