json-schema-function-signatures
v0.0.3
Published
Validating function inputs/outputs with json schemas
Downloads
2
Readme
json-schema-function-signatures
This package provides a way to validate the inputs, output, and context of functions via JSON schema definitions.
Basic usage
Using the proposed pipeline operator via babel.
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'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:
ajv: custom Ajv object for the schema validations.
onError: callback invoked when a validation fails. Will be passed an error object encapsulating the validation errors. Default to throwing an exception.
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.