The ObjectParser is a specialized parser designed to safely access and validate values within a plain JavaScript object. It provides type-safe methods for retrieving values of various types (strings, numbers, arrays, etc.), ensuring that required fields are present and correctly typed.

This parser is useful in scenarios where you need to interact with complex objects and ensure the presence and type correctness of specific keys, such as when dealing with parsed JSON or other data structures.

ObjectParser wraps a generic object and provides methods to:

  • Check if a key exists (has).
  • Retrieve values of specific types (string, number, array, etc.).
  • Handle complex nested objects by returning new ObjectParser instances.
  • Validate the type or instance of an object field.

It also throws custom errors (MissingKeyError, InvalidTypeError, InvalidInstanceOfError) to handle validation issues, making debugging easier.

  • Methods like string, number, and array enforce the type correctness of the retrieved values.
  • For nested objects, the object method returns a new ObjectParser instance, allowing safe and structured access to deeply nested data.
  • The instanceOf method ensures that an object is an instance of the specified constructor, which is useful for retrieving specific types like Date.
let data = {
username: "John",
age: 30,
active: true,
metadata: { role: "admin" }
};
let parser = new ObjectParser(data);

parser.string("username"); // "John"
parser.number("age"); // 30
parser.getBoolean("active"); // true
parser.object("metadata").string("role"); // "admin"

Hierarchy (View Summary)

Constructors

Properties

value: object

Methods

  • Retrieves the value associated with the key as an array.

    Parameters

    • key: Key

      The key to retrieve the array for.

    Returns any[]

    The array value.

    If the value is not an array.

  • Retrieves the value associated with the key as a bigint.

    Parameters

    • key: Key

      The key to retrieve the bigint value for.

    Returns bigint

    The bigint value.

    If the value is not a bigint.

  • Retrieves the value associated with the key as a boolean.

    Parameters

    • key: Key

      The key to retrieve the boolean value for.

    Returns boolean

    The boolean value.

    If the value is not a boolean.

  • Retrieves the value associated with the key as a Date object.

    Parameters

    • key: Key

      The key to retrieve the Date for.

    Returns Date

    The Date value.

    If the value is not a Date instance.

  • Retrieves the value associated with the key as a function.

    Parameters

    • key: Key

      The key to retrieve the function for.

    Returns Function

    The function value.

    If the value is not a function.

  • Retrieves the value associated with the specified key.

    Type Parameters

    • O = unknown

    Parameters

    • key: Key

      The key to retrieve the value for.

    Returns O

    The value associated with the key.

    If the key is missing.

  • Checks if the given key exists in the object.

    Parameters

    • key: Key

      The key to check.

    Returns boolean

    true if the key exists, otherwise false.

  • Access a value from the object as an instance of the provided constructor.

    Type Parameters

    • I

    Parameters

    • key: Key

      The key to retrieve the instance for.

    • ctor: new (...args: any[]) => I

      The constructor function to check the instance against.

    Returns I

    The value as an instance of the provided constructor.

    If the value is not an instance of the provided constructor.

  • Checks if the value associated with the key is null.

    Parameters

    • key: Key

      The key to check.

    Returns boolean

    true if the value is null, otherwise false.

  • Checks if the value associated with the key is undefined.

    Parameters

    • key: Key

      The key to check.

    Returns boolean

    true if the value is undefined, otherwise false.

  • Retrieves the value associated with the key as a number.

    Parameters

    • key: Key

      The key to retrieve the number value for.

    Returns number

    The number value.

    If the value is not a number.

  • Retrieves the value associated with the key as an ObjectParser for nested object access.

    Parameters

    • key: Key

      The key to retrieve the object value for.

    Returns ObjectParser

    A new ObjectParser instance for the nested object.

    If the value is not an object.

  • Sets the value associated with the specified key.

    If the key does not exist, it will be created.

    Type Parameters

    • O = unknown

    Parameters

    • key: Key

      The key to set the value for.

    • value: O

      The value to set.

    Returns void

    parser.set("username", "johndoe");
    
  • Retrieves the value associated with the key as a string.

    Parameters

    • key: Key

      The key to retrieve the string value for.

    Returns string

    The string value.

    If the value is not a string.

  • Retrieves the value associated with the key as a symbol.

    Parameters

    • key: Key

      The key to retrieve the symbol for.

    Returns symbol

    The symbol value.

    If the value is not a symbol.

  • Retrieves the type of the value associated with the specified key.

    Parameters

    • key: Key

      The key to check the type for.

    Returns
        | "string"
        | "number"
        | "bigint"
        | "boolean"
        | "symbol"
        | "undefined"
        | "object"
        | "function"

    The type of the value as a string (e.g., "string", "number").