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

@sovgut/gatekeeper

v2.3.2

Published

Validation system using schemes in Swagger design

Downloads

46

Readme

Gatekeeper

This package is builded for validate data for any objects using schemes like in Swagger design.

Example usage

import Gatekeeper, { Scheme, Type, Format } from '@sovgut/gatekeeper';

// MODULES FROM API, THIS IS NOT RELATED TO PACKAGE
import { Router } from 'express';
import ServerException from '~core/server-exception';
import HttpStatus from '~core/http-status';

const scheme: Scheme = {
  type: Type.Object,
  properties: {
    query: {
      type: Type.Object,
      properties: {
        limit: {
          required: true,
          type: Type.Number,
          exception: {
            class: ServerException,
            parameters: [HttpStatus.BadRequest],
            message: (initial, key, value, reason) => {
              switch (reason) {
                case Reason.OnValidate:
                  return `${key} have invalid value; expected: more than 10; received: ${value};`;
                case Reason.Type:
                  return `${key} have invalid type; expected: ${
                    Type.Number
                  }; received: ${typeof value};`;
                case Reason.Required:
                  return `${key} is required; expected: 10; received: ${value};`;
                default:
                  return initial;
              }
            },
          },
          onValidate: (value) => value > 10,
        },
        offset: {
          type: Type.Number,
        },
        filter: {
          type: Type.Object,
          properties: {
            createdAtRange: {
              type: Type.Array,
              items: {
                type: Type.String,
                format: Format.DateTime,
              },
            },
            id: {
              type: Type.String,
              onValidate: mongoose.isObjectIdOrHexString,
            },
          },
        },
      },
    },
  },
};

// HERE IS EXAMPLE FOR EXPRESS LIBRARY
export default (router: Router) => {
  router.get('/users', (request, response) => {
    try {
      new Gatekeeper(scheme).validate(request);
      // OR Gatekeeper.validate(request, scheme);

      return response.status(HttpStatus.Ok).end();
    } catch (exception) {
      if (exception instanceof ServerException) {
        return response.status(exception.status).send(exception.message);
      }

      return response.status(HttpStatus.ServerException).end();
    }
  });
};

API

interface Scheme {
  /**
   * Package can skip this field if value is undefined and `required` property is false.
   * In other way is exception is raised.
   *
   * Default: `false`
   */
  required?: boolean;

  /**
   * Package is validate value types with field type,
   * this property is represent which type is required for current field.
   *
   * Default: `Type.String`
   */
  type: Type;

  /**
   * Package can validate value by provided type with format.
   *
   * As example type `Type.String` with format `Format.DateTime` is tested as date.
   */
  format?: Format;

  /**
   * Validate value with enum array (the same as `onValidate: (value) => [...].includes(value)`).
   */
  enum?: (string | number | boolean)[];

  /**
   * The min length of the string value.
   *
   * Used only when type `Type.String`, `Type.Array` or `Type.Number` is provided.
   *
   * Default: `0`.
   */
  minLength?: number;

  /**
   * The max length of the string value.
   *
   * Used only when type `Type.String`, `Type.Array` or `Type.Number` is provided.
   *
   * Default: `Number.MAX_SAFE_INTEGER`.
   */
  maxLength?: number;

  /**
   * Overflow default class exception.
   */
  exception?: Exception;

  /**
   * Process custom validation in current field.
   */
  onValidate?: (value: any) => boolean;

  /**
   * Used only when `Type.Object` type is provided.
   */
  properties?: Properties;

  /**
   * Used only when `Type.Array` type is provided.
   */
  items?: Scheme;
}

interface Exception {
  /**
   * Here you can change the default exception class used for throwing errors.
   *
   * Default: `Error`.
   */
  class?: any;

  /**
   * Is should current exception options is passed through to all children's in scheme.
   *
   * Default: `false`.
   */
  passThrough?: boolean;

  /**
   * Pass arguments to a exception after message.
   *
   * Used only when `exception.class` property is provided.
   */
  parameters?: (string | number | boolean)[];

  /**
   * Overflow validation message which is passed to first argument in exception class.
   */
  message?: (
    initial: string,
    key: string,
    value: any,
    reason: Reason,
  ) => string;
}

interface Properties {
  [property: string]: Scheme;
}

interface Target {
  [property: string]: any;
}

enum Type {
  Object = 'object',
  Number = 'number',
  Boolean = 'boolean',
  String = 'string',
  Array = 'array',
}

enum Format {
  /**
   * This format is used only when `Type.Number` type is provided
   */
  Float = 'float',

  /**
   * This format is used only when `Type.Number` type is provided
   */
  Integer = 'integer',

  /**
   * This format is used only when `Type.String` type is provided
   */
  DateTime = 'date-time',

  /**
   * This format is used only when `Type.String` type is provided
   */
  UUID = 'uuid',

  /**
   * This format is used only when `Type.String` type is provided
   */
  Email = 'email',
}

enum Reason {
  Required = 'required',
  Type = 'type',
  OnValidate = 'on-validate',
  Enum = 'enum',
  Range = 'range',
  Format = 'format',
}