npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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/contextSchema 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 object for the schema validations.

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

  3. 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.