Options
All
  • Public
  • Public/Protected
  • All
Menu

eiki

Index

Type aliases

Schema

Schema: Record<string, ValidationFunction>

ValidationFunction

ValidationFunction: (o: any) => boolean

Type declaration

    • (o: any): boolean
    • A function which can be used to validate values.

      Parameters

      • o: any

      Returns boolean

Functions

Const and

  • Returns a function which will return true if every validation function returns true for a given value.

    Parameters

    Returns (o: any) => boolean

    The function which can test the value.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const arr

  • Returns a function which returns true either if the given object is an array, or if all members of that array pass a validation function.

    example
    const stringArr = arr(str)
    stringArr(['a'])            // true
    stringArr(['a', 2])         // false
    stringArr([])               // true
    const anyArr = arr()
    anyArr([])                  // true
    anyArr([undefined, 'a', 2]) // true
    

    Parameters

    Returns (a: any) => boolean

    A function which can be used to test if a value is an array and all members pass a validation function.

      • (a: any): boolean
      • Parameters

        • a: any

        Returns boolean

Const date

  • date(o: any): boolean
  • Returns true if o is a valid date string.

    example
    date('2021-01-07') // true
    date('a')          // false
    date(3)            // false, because this tests for a date _string_
    

    Parameters

    • o: any

      The data to test.

    Returns boolean

    A boolean describing if the data is a date string.

Const def

  • def(o: any): boolean
  • Checks that a value is not undefined.

    Parameters

    • o: any

      The value to check.

    Returns boolean

    Whether the value is defined.

Const len

  • len(limits: Limits): (o: any) => boolean
  • Returns a function that checks that the length of this entity is within the max or min. If no max or min are provided, then just checks that this entity has a .length property and is a number.

    Parameters

    • limits: Limits

      An object which describes a maximum and minimum value.

    Returns (o: any) => boolean

    A function which checks length.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const lim

  • lim(limits: Limits): (o: any) => boolean
  • Tests if the value is between the minimum and maximum values.

    Parameters

    • limits: Limits

      The limits to test.

    Returns (o: any) => boolean

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const loose

  • loose(s: Schema): (o: any) => boolean
  • Parameters

    Returns (o: any) => boolean

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const max

  • max(max?: number): (o: any) => boolean
  • Returns a function that can check if a number is below the maximum value. Max value is optional; if omitted, simply acts like num.

    Parameters

    • Optional max: number

      The maximum value.

    Returns (o: any) => boolean

    A function which test values to tell if they're below the maximum value.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const maybe

  • Helper for possibly undefined values. Equivalent to or(func, undef).

    Parameters

    Returns (o: any) => boolean

    The validation function, but also accepting undefined values.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const min

  • min(min?: number): (o: any) => boolean
  • Returns a function that can check if a number is above the minimum value. Min value is optional; if omitted, simply acts like num.

    Parameters

    • Optional min: number

      The minimum value.

    Returns (o: any) => boolean

    A function which test values to tell if they're above the minimum value.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const num

  • num(o: any): boolean
  • Returns true if the given data is a number.

    example
    num(2)   // true
    num('a') // false
    

    Parameters

    • o: any

      The data to test.

    Returns boolean

    True if the data is a number.

Const obj

  • obj(s: Schema): (o: any) => boolean
  • Returns a function which returns true if the validation functions in a given schema all pass.

    example
    const schema = obj({
      title: str,
      author: obj({
        firstName: str,
        lastName: str,
        age: num
      })
    })
    schema({
     title: 'The Law',
     deep: {
       firstName: 'Shiki',
       lastName: 'Eiki',
       age: 32498
     }
    }) // true
    schema(undefined) // false
    schema({
     title: 'The Grimoire of Marisa',
     description: '', // We don't care if additional keys are added.
     deep: obj({
         firstName: 'Marisa',
         lastName: 'Kirisame' // And in non-strict, we don't care if keys aren't present.
     }) // true
    })
    const strictSchema = obj({
      name: str,
      age: num
    }, true) // Ensure you set `strict` if you want to ensure that every key is present.
    strictSchema({ }) // false
    

    Parameters

    • s: Schema

      A schema - an object with keys mapping to boolean validation functions.

    Returns (o: any) => boolean

    A function which can be used to validate objects against a schema.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const or

  • Returns a validation function which returns true if any of the given functions return true.

    example
    const stringOrNumber = or(str, num)
    stringOrNumber('a')       // true
    stringOrNumber(2)         // true
    stringOrNumber(undefined) // false
    

    Parameters

    Returns (o: any) => boolean

    A validation function, which returns true if any given function returns true on the tested data.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const regex

  • regex(r: RegExp): (o: any) => boolean
  • Returns a function which can validate a value against a given regex.

    Parameters

    • r: RegExp

    Returns (o: any) => boolean

    A function which can test using that regex.

      • (o: any): boolean
      • Parameters

        • o: any

        Returns boolean

Const str

  • str(o: any): boolean
  • Returns true if o is a string.

    example
    str('eiki') // true
    str(3)      // false
    str(['a'])  // false
    

    Parameters

    • o: any

      The data to test.

    Returns boolean

    A boolean describing if the data is a string.

Const undef

  • undef(o: any): boolean
  • Returns true if o is undefined.

    Parameters

    • o: any

      The data to test.

    Returns boolean

    True if o is undefined.

Const url

  • url(o: any): boolean
  • Returns true if o is a URL.

    example
    url('https://google.com') // true
    url(2)                    // false
    url('aaaa')               // false
    

    Parameters

    • o: any

      The data to test.

    Returns boolean

    A boolean describing if the data is a url.

Generated using TypeDoc