Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace Validations

Validations meant to be used with storeObject's validate function

Index

Functions

Functions

  • keys<O>(value: Readonly<any>, requiredKeys: readonly Keys<O>[]): boolean
  • Validate that only expected keys are present on an object

    example
    import { storeObject, Validations } from 'ls-proxy'

    const myObj = storeObject(
    'myObj',
    { foo: 'bar' },
    { validate: value => Validations.keys(value, ['foo']) },
    )

    myObj.foo = 'abc' // no error
    myObj.bar = 'xyz' // error

    Type parameters

    • O: Record<string, any>

      The stored object

    Parameters

    • value: Readonly<any>

      The unknown value to validate types of

    • requiredKeys: readonly Keys<O>[]

      The only keys that should be present

    Returns boolean

  • types<O>(value: Readonly<any>, typesMap: Readonly<Record<Keys<O>, string>>): boolean
  • Validate that the types passed for an object are expected

    example
    import { storeObject, Validations } from 'ls-proxy'

    const typesMap = {
    onlyString: 'string',
    onlyNumber: 'number',
    }

    const runtimeCheckedTypes = storeObject(
    'runtimeCheckedTypes',
    {
    onlyString: 'abc',
    onlyNumber: 123,
    },
    { validate: value => Validations.types(value, typesMap) },
    )

    runtimeCheckedTypes.onlyString = 'xyz' // Succeeds
    runtimeCheckedTypes.onlyNumber = 'abc' // Fails

    Type parameters

    • O: Record<string, any>

      The stored object

    Parameters

    • value: Readonly<any>

      The unknown value to validate types of

    • typesMap: Readonly<Record<Keys<O>, string>>

      A map of expected keys for an object to expected types, checked like typeof value[key] === typesMap[key]

    Returns boolean

Generated using TypeDoc