export const validateObjectContainsKey = function()

in packages/database/src/core/util/validation.ts [438:494]


export const validateObjectContainsKey = function (
  fnName: string,
  argumentName: string,
  obj: unknown,
  key: string,
  optional: boolean,
  optType?: string
) {
  const objectContainsKey =
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    obj && typeof obj === 'object' && contains(obj as any, key);

  if (!objectContainsKey) {
    if (optional) {
      return;
    } else {
      throw new Error(
        errorPrefixFxn(fnName, argumentName) +
          'must contain the key "' +
          key +
          '"'
      );
    }
  }

  if (optType) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const val = safeGet(obj as any, key);
    if (
      (optType === 'number' && !(typeof val === 'number')) ||
      (optType === 'string' && !(typeof val === 'string')) ||
      (optType === 'boolean' && !(typeof val === 'boolean')) ||
      (optType === 'function' && !(typeof val === 'function')) ||
      (optType === 'object' && !(typeof val === 'object') && val)
    ) {
      if (optional) {
        throw new Error(
          errorPrefixFxn(fnName, argumentName) +
            'contains invalid value for key "' +
            key +
            '" (must be of type "' +
            optType +
            '")'
        );
      } else {
        throw new Error(
          errorPrefixFxn(fnName, argumentName) +
            'must contain the key "' +
            key +
            '" with type "' +
            optType +
            '"'
        );
      }
    }
  }
};