beekeeper-studio/beekeeper-studio · error

Invalid JSON

Error message

Invalid JSON

What it means

`UserProvidedEnum` wraps a user-supplied enum definition and validates the raw JSON in its constructor. It throws 'Invalid JSON' when `json` is missing `name` or `variants`, when `name` is not a string, or when `variants` is not an array. This fails fast on malformed enum definitions before they are used elsewhere.

Source

Thrown at apps/studio/src/lib/UserProvidedEnum.ts:9

import _ from "lodash";

export class UserProvidedEnum {
	name: string;
	variants: { id: string, value: string }[];
	constructor(json: any) {
		if (json.name == undefined || json.variants == undefined || 
			!_.isString(json.name) || !_.isArray(json.variants)) {
			throw new Error('Invalid JSON');
		}

		this.name = json.name;
		this.variants = [];

		for (let i = 0; i < json.variants.length; i++) {
			const variant = json.variants[i];

			if (variant.id != undefined && variant.value != undefined) 
				this.variants.push({ id: variant.id, value: variant.value });
		}

		if (this.variants.length == 0) throw new Error(`Enum ${this.name} does not have any variants`);
	}

	// TODO (day): should probably clean this up for other types
	findMatch(id: string): string {
		const variant = this.variants.find((val) => val.id == id);

View on GitHub (pinned to 4e3e03e322)

Solutions

  1. Validate the object shape before constructing: name is a string, variants is an array
  2. Fix the JSON keys so `name` and `variants` exist at the top level
  3. Check that the file/string parsed successfully (JSON.parse errors) before passing in
  4. Check the source file is the expected enum definition, not another config

Example fix

// before
const e = new UserProvidedEnum(JSON.parse(raw));
// after
const parsed = JSON.parse(raw);
if (typeof parsed?.name !== 'string' || !Array.isArray(parsed?.variants)) {
  throw new Error('Enum definition must have string name and variants array');
}
const e = new UserProvidedEnum(parsed);
Defensive patterns

Strategy: validation

Validate before calling

function isValidEnumJson(json: any): boolean {
  return !!json && typeof json.name === 'string' && Array.isArray(json.variants);
}

Type guard

function isUserProvidedEnumJson(v: any): v is { name: string; variants: { id: string; value: string }[] } {
  return typeof v?.name === 'string' && Array.isArray(v?.variants) &&
    v.variants.every((x: any) => typeof x?.id === 'string' && typeof x?.value === 'string');
}

Try / catch

try {
  const e = new UserProvidedEnum(json);
} catch (err) {
  if (err.message === 'Invalid JSON') {
    console.warn('Malformed enum definition, skipping', json);
    return null;
  }
  throw err;
}

Prevention

When it happens

Trigger: Constructing `new UserProvidedEnum(parsed)` where parsed.name is absent/not a string, parsed.variants is absent or not an array, or the JSON failed to parse and something undefined/null was passed.

Common situations: Hand-edited enum JSON files with a typo in a key (e.g. 'variantes'); loading a config saved by an older schema; JSON.parse result wrapped differently ({enum: {...}}); file read returned empty string parsed to null.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of beekeeper-studio/beekeeper-studio@4e3e03e322 (2026-08-31). Data as JSON: /api/errors/e2118942d79875fc. Report an issue: GitHub.