A validator class for the configuration provider. This class receives an object with a subset of the keys of the config with a type or array of types corresponding to that config value.

Valid types include: boolean, number, string and null, or including their array types: boolean[], number[] and string[] You can set a type to be an array containing multiple of the ones above.

It is preferable to specify all the types for your config. However, if a type is omitted it will be defaulted to string. Keep this in mind in the case you need to have a boolean or a number in your configuration.

You may also specify custom validators by passing an object that maps the key of the config with its validator. This validator function does not need to return anything, but throw a TypeError if the given value in its parameter is not valid according to your criteria. If you pass a customValidator for a specific key, you should still pass its type because it is used to cast the value coming from environment variables, since they're only strings.

This configuration is most useful for configuration coming from env variables since they are only strings. Although, in a case where only a JSON configuration is specified, you may run into trouble if you don't specify the types for those settings because, while JSON properties can be typed, the validator will default them to string, which will make the validation throw an error during the ConfigProvider creation.

Hierarchy

  • ConfigValidator

Constructors

Properties

customValidators: ConfigCustomValidators

An object that maps a config key to a custom validator function. This validator function will be used to validate the config supplied. It will skip the default type validator and instead use the one specified here. This function should not return anything, but throw a TypeError if the given value is not correct.

Memberof

ConfigValidator

types: Record<string, string | string[]>

The types for this config validator.

Memberof

ConfigValidator

VALID_TYPES: string[] = ...

The valid types that can be used. It is also possible to have an array of these types.

Static

Memberof

ConfigValidator

Methods

  • Casts the values in an object of string values into its corresponding types based on the types defined in this validator. If a cast is not possible, the function will throw. This does not mutate the config object given and instead returns a copy of it with the cast config values. If the type includes string then this function will try to cast first to any of the available types and if it fails to cast to any of them, it will return the string version of the value instead of throwing.

    Parameters

    • config: Record<string, string>

      The object of string values to be cast.

    Returns Record<string, ConfigValue>

    An object with the values cast.

    Throws

    Throws if a config value cannot be cast to any of its specified types.

  • Returns true if the value given conforms the type given.

    Parameters

    • value: ConfigValue

      The value to test.

    • type: string

      The type to test.

    Returns boolean

    Whether the value's type is valid.

  • Tries to cast a value into its type from string. It returns the same value if the cast is not possible.

    Parameters

    • value: string

      The value to cast.

    • type: string

      The type to cast the value to.

    Returns ConfigValue

    The cast value.

    Throws

    Throws if the type is invalid.

    Throws

    Throws if the value cannot be cast to its specified type.

  • Validate that the provided config complies with all the types defined in this validator.

    Parameters

    • config: Record<string, ConfigValue>

      The configuration object to test.

    Returns void

    Throws

    Throws if the config contains a value that does not have a correct type.

  • Tests that all the types are correct and valid.

    Parameters

    • types: Record<string, string | string[]>

      The types to test.

    Returns void

    Throws

    Throws if any of the types specified is invalid.

Generated using TypeDoc