# run-function-with-signature

> throws an error if the input or output of a function fails to validate against schemas

Latest version **0.0.5** (published 2016-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install run-function-with-signature
pnpm add run-function-with-signature
yarn add run-function-with-signature
bun add run-function-with-signature
```

## 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.5 |
| Published | 2016-10-10 |
| First published | 2016-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Troy Wolfe |
| Maintainers | troythewolfe |
| Keywords | json, schema, validator, signature, function, interface, parameter, return, input, output |

## Links

- npm: https://www.npmjs.com/package/run-function-with-signature
- Repository: https://github.com/troythewolfe/run-function-with-signature
- Homepage: https://github.com/troythewolfe/run-function-with-signature#readme
- Issues: https://github.com/troythewolfe/run-function-with-signature/issues
- npm.io page: https://npm.io/package/run-function-with-signature

## Dependencies (1)

- [jsonschema](https://npm.io/package/jsonschema.md) 1.1.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.5 (latest) — 2016-10-10
- 0.0.4 — 2016-10-10
- 0.0.3 — 2016-10-10
- 0.0.2 — 2016-10-10
- 0.0.1 — 2016-10-09

## README

#run-function-with-signature (SchemaFunction)

## Description

Name your function (ex. 'My Special Function With Signatures'), write your function body, optionally write a schema to validate the function's parameter object and/or the function's response object.

If anything fails validation, `throw new Error(err)`.

Set properties on construction using the 'params' parameter or set properties after construction using 'instance.property = value;' syntax.

Export as simple, single property function.

## API

```
var SchemaFunction = require('run-function-with-signature');

/**
 * SchemaFunction
 * Creates a function that will validate it's input and output against provided schemas.
 * @type {class}
 * @param {string} id - string indentifier, can include spaces.
 * @param {object} params - an object to set the properties on construction
 * @param {function} params.func - the function that this object runs
 * @param {object} params.input - the input object for params.func
 * @param {object} params.inputSchema - json schema describing the input object
 * @param {object} params.outputSchema - json schema describing the output object
 * @return {object} An instance of SchemaFunction
**/
```

## Usage

```
/**/
let myFunc = new SchemaFunction('test function', { func: (input) => {

    // logs { a: 3, b: 'this is my input' }
    console.log(input);

    return {
      c: 'this is my response',
      d: 3,
    }
  }
});

myFunc.inputSchema = {
  properties: {
      a: { type: 'integer' },
      b: { type: 'string' },
  },
  required: ['a', 'b'],
};

myFunc.outputSchema = {
  properties: {
      c: { type: 'string' },
      d: { type: 'integer' },
  },
  required: ['c', 'd'],
};

// Export as function
function myRunFunction(input){
  return myFunc.run(input);
}

const returnFromTheFunc = myRunFunction({
  a: 3,
  b: 'this is my input',
});

// logs { c: 'this is my response', d: 3 }
console.log(returnFromTheFunc);
/**/
```

## Error

If anything fails validation, SchemaFunction throws an error and passes an error object.

```
/**
 * ThrowError
 * Creates an error object and throws an error
 * @type {class}
 * @param {string} id - string indentifier, can include spaces.
 * @param {string} message - a description of why the error was thrown
 * @param {object} error - the error returned by the validator
**/
```

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