1.1.1 • Published 6 years ago

stygian v1.1.1

Weekly downloads
1
License
ISC
Repository
-
Last release
6 years ago

stygian

Function for combining schema and generating validators

Function takes a series of json schemas with keywords as defined in ajv-interface-keywords produces a validation function for the passed name

Example

module.exports = () => {
  const compile = require("../index");
  const passData = {
    a: {
      b: 2
    },
    c: {
      d: () => {}
    },
    e: {
      f: {
        g: "",
        i: true,
        l: {
          b: 4
        }
      },
      j: new Set()
    }
  };

  const interfaces = [
    {
      foo: {
        type: "object",
        properties: {
          b: {
            interface: "integer"
          }
        },
        required: ["b"]
      }
    },
    [
      {
        bar: {
          interface: "object",
          properties: {
            d: {
              return: {}
            }
          }
        }
      }
    ],
    [
      [
        [
          [
            [
              [
                {
                  baz: {
                    type: "object",
                    properties: {
                      f: {
                        type: "object",
                        properties: {
                          g: {
                            type: "string"
                          },
                          i: {
                            const: true
                          },
                          l: {
                            interface: "foo"
                          }
                        }
                      },
                      j: {
                        interface: "set"
                      }
                    }
                  }
                }
              ]
            ]
          ]
        ]
      ]
    ],
    {
      bing: {
        interface: "object",
        properties: {
          a: {
            interface: "foo"
          },
          c: {
            interface: "bar"
          },
          e: {
            interface: "baz"
          }
        },
        required: ["a", "c", "e"]
      }
    }
  ];

  return compile("bing", interfaces, { actionOnFail: "false" })(passData); // returns passData
};

Arguments

target

Interface name of the interface to generate a validator for, specified interface must be in interfaces

interfaces

Interfaces which will be flattened and merged, validates to interface

options

Defines how returned function will behave

Returns

function

Recieves value .target and tests if matches interface definition, function effect depends on .options

Options

actionOnConflict

What action to take when multiple interfaces of the same name are detected

default: overwrite

options

  • overwrite: Replace old interface of name with new interface

  • ignore: Keep old interface of name

  • warn-overwrite: Replace old interface of name with new interface of name and print warning

  • warn-ignore: Keep old interface of name and print warning

  • throw: Throw error on interface name conflict

actionOnFail

What action to take when a validation fails

default: throw

options

  • throw: Throw on validate fail

  • warn: Print warning and return target on validate fail

  • false: Return false on validate fail

Static functions

resolveMacrosAllOf

resolve macros with allOf pattern

resolveMacrosDeepmerge

resolve macros with deepmerge pattern

resolveMacrosMerge

resolve macros with merge pattern