Options
All
  • Public
  • Public/Protected
  • All
Menu
     

Class PType

This class is used to define reusable ParamTypes which are requred in the Descr definitions (specifically, the Descr.type property).

They're used for santization purposes, as well as OpenAPI specification. Once instantiated, they serve as both a callable-function and an object-instance. An instance can also be extended to created an alternate PType who inherits the sanitizers of it's parent in addition to its own sanitzer (if defined).

A sanitizer is just a function that takes the value of a request-parameter sent by Client and ensures that it conformes to a certain standard.

A PType can be set as an iterable, which assume the parameter will be sent as an Array, and perform the sanitization over each item.

Hierarchy

  • Function
    • PType

Callable

  • Further restrictions and transformations can be defined on a PType when it is called as a function. When a PType is extended in this manner, it is still of the same type. It retains the same name and does not appear in the OpenAPI docs -- unless restriction-properties have been set (see PTArgs for more details):

    PT.Text = new PType({
            name: 'Text', type: 'String',
            sanitizers(v) {
                return (!!!v || v===undefined ?
                    "" : v.toString().replace(/'/g,"\\'")
                );
            }
        });
    console.log(PT.Text.sanitize('hello, world')); // "hello, world"
    console.log(PT.Text.sanitize(null)); // ""
    // Further-customize by Calling the PType as a Function
    let PT_TxtToArr = PT.Text({ selects: ["hello","world"] });
    // Prints
    console.log(PT_TxtToArr.sanitize('world')); // "world"
    console.log(PT_TxtToArr.sanitize('noice')); // ""
    console.log(PT_TxtToArr.sanitize('hello, world')); // ""

    Parameters

    • __namedParameters: PTArgs
      • separator: string
      • join: string
      • enclose: [string, string]
      • sort: boolean
      • map: function
          • <T>(value: T, index?: number, array?: Array<T>): Array<T>
          • Type parameters

            • T

            Parameters

            • value: T
            • Optional index: number
            • Optional array: Array<T>

            Returns Array<T>

      • tags: string[]
      • leveled: boolean
      • grouped: boolean
      • selects: object[]
      • omit: string[]
      • slice: [number, number]
      • step: number
      • min: number
      • max: number
      • regex: RegExp
      • hidden: boolean
      • restricted: boolean

    Returns PType

    A customized version of the PType.

Index

Constructors

Constructor Properties

FunctionCall (Cosmetic) Properties

FunctionCall (Restrictive) Properties

FunctionCall (Toggle) Properties

Main Methods

Presentation Methods

Prototype Methods

Constructors

constructor

  • Creates a new PType, which serves as the definition of a Request-Parameter Type.

    // Singular Sanitizer
    new PType({ name: "", type: "", sanitizers(v) {...}, iterable: true });

    Parameters

    Returns PType

    A new PType.

  • Creates a new PType, which serves as the definition of a Request-Parameter Type.

    // Multiple sanitizers, ran in succession
    new PType({
        name: "", type: "", sanitizers: [
            (v) => (...),
            (v) => (...),
            (v) => (...),
        ],
    });

    Parameters

    • __namedParameters: PTOptsMulti
      • name: string
      • type: "String" | "Number" | "Boolean" | "Object"
      • sanitizers: function[]
      • iterable: boolean

    Returns PType

    A new PType.

Constructor Properties

name

name: string

type

type: PTKinds

sanitizers

sanitizers: CBSanitizer[]

A collection of all of the CFG.PNTS.PTOpts.sanitizers defined for this PType.

iterable

iterable: boolean

FunctionCall (Cosmetic) Properties

Private separator

separator: string

Private sort

sort: boolean

Private join

join: string

Private enclose

enclose: [string, string]

Private map

map: function

Type declaration

    • <T>(value: T, index?: number, array?: Array<T>): Array<T>
    • Type parameters

      • T

      Parameters

      • value: T
      • Optional index: number
      • Optional array: Array<T>

      Returns Array<T>

Private tags

tags: string[]

Private leveled

leveled: boolean

Private grouped

grouped: boolean

FunctionCall (Restrictive) Properties

Private selects

selects: Array<object>

Private omit

omit: string[]

Private slice

slice: [number, number]

Private step

step: number

Private min

min: number

Private max

max: number

Private regex

regex: RegExp

Private hidden

hidden: boolean

FunctionCall (Toggle) Properties

Private restricted

restricted: boolean

Main Methods

sanitize

  • Performs each of the sanitizers and any PTArg transformations that have been defined for a particular PType.

    Parameters

    • v: TPQryPrims

      A parameter value froma Client request.

    Returns TPQryPrims

    The sanitized value.

extend

  • Extends the current PType, producing a new one with and different name and other modifications. When a PType is extended, it becomes a new param-type and will be included in the OpenAPI docs (see PTOpts for more details).

    Example

    // Type-checks for Array<String>
    PT.L.ArrStr = new PType({
        name: "StrArray", type: "String",
        sanitizers(v) { return v.constructor.name===this.type; },
        iterable: true
    });
    // Type-checks for Array<Number>
    PT.L.ArrNum = ArrStr.extend({
        name: "NumArray", type: "Number"
    });

    Parameters

    Returns PType

    The brand-new PType.

  • Like the constructor, the extend function can also define an Array of sanitizers in the PTOpts argument.

    Parameters

    • __namedParameters: PTOptsMulti
      • name: string
      • type: "String" | "Number" | "Boolean" | "Object"
      • sanitizers: function[]
      • iterable: boolean

    Returns PType

Private cycle

  • Cycles through the sanitization processes

    Parameters

    Returns TPQryPrims

    The sanitized value

Private unique

  • unique(name?: string, dflt?: any): string
  • A Unique-Identifier for the Type.

    Parameters

    • Optional name: string

      Add a Type-Name to the Unique-Identifier.

    • Optional dflt: any

      A default-value. If set, will assume a Type-Name is needed.

    Returns string

    The unique identifier.

Presentation Methods

toString

  • toString(): string
  • A string representation of the PType.

    Example

    console.log(new PType({
        name: 'TwoNumbers', type: 'Number',
        iterable: true, sanitizers(v) { ... }
    })({
        min: 0, max: 2, slice: [0,2]
    }).toString())

    Console:

    TwoNumbers {
        type: "array",
        items: {
            type: "integer",
            minimum: 1
        },
        minItems: 0,
        maxItems: 2
    };

    Returns string

toDoc

  • A OpenAPI-compliant version of a PType.

    Example

    (new PType({
        name: 'TwoNumbers', type: 'Number',
        iterable: true, sanitizers(v) { ... }
    })({
        min: 0, max: 2, slice: [0,2]
    }).toDoc() === {
        type: "array",
        items: {
            type: "integer",
            minimum: 1
        },
        minItems: 0,
        maxItems: 2
    });

    Returns DocPType<TPDocPrims, boolean>

Prototype Methods

__@hasInstance

  • __@hasInstance(value: any): boolean
  • Determines whether the given value inherits from this function if this function was used as a constructor function.

    A constructor function can control which objects are recognized as its instances by 'instanceof' by overriding this method.

    Parameters

    • value: any

    Returns boolean

apply

  • apply(this: PType, thisArg: any, argArray?: any): any
  • Calls the PType, substituting the specified object for the this value of the PType, and the specified array for the arguments of the PType.

    Parameters

    • this: PType
    • thisArg: any

      The object to be used as the this object.

    • Optional argArray: any

      A set of arguments to be passed to the function.

    Returns any

call

  • call(this: PType, thisArg: any, ...argArray: any[]): any
  • Calls a method of a PType, substituting another PType for the current PType.

    Parameters

    • this: PType
    • thisArg: any

      The object to be used as the current object.

    • Rest ...argArray: any[]

      A list of arguments to be passed to the method.

    Returns any

bind

  • bind(this: PType, thisArg: any, ...argArray: any[]): any
  • Calls the PType, substituting the specified object for the this value of the PType, and the specified array for the arguments of the PType.

    Parameters

    • this: PType
    • thisArg: any

      An object to which the this keyword can refer inside the new function.

    • Rest ...argArray: any[]

      A list of arguments to be passed to the new function.

    Returns any