sickn33/agentic-awesome-skills · warning · Error

Data must be an object

Error message

Data must be an object

What it means

Thrown by the throw-based validateUser in fp-refactor when the input is falsy or not an object — the outermost structural guard before field checks. It exists to reject null, undefined, primitives, and array misuse in the 'Before' code the skill refactors away.

Solutions

  1. Validate the shape at the boundary with a schema library so non-objects never reach this function
  2. Return Either/Result with the reason instead of throwing
  3. Add a reusable isRecord(x) type guard shared across validators

Example fix

// before
if (!data || typeof data !== 'object') throw new Error('Data must be an object')

// after
const isRecord = (x: unknown): x is Record<string, unknown> =>
  typeof x === 'object' && x !== null
const validateUser = (data: unknown): E.Either<string, User> =>
  !isRecord(data) ? E.left('Data must be an object') : E.right(data as User)
Defensive patterns

Strategy: type-guard

Validate before calling

const isRecord = (x: unknown) => typeof x === 'object' && x !== null && !Array.isArray(x)

Type guard

const isRecord = (x: unknown): x is Record<string, unknown> =>
  typeof x === 'object' && x !== null

Prevention

When it happens

Trigger: Calling validateUser(null), validateUser('name=Bob'), validateUser(42), or validateUser(undefined) from an unvalidated request body.

Common situations: Optional request bodies not checked before validation; query-string parsers returning strings; JSON.parse of 'null' producing null; form-encoded endpoints feeding primitives.

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 sickn33/agentic-awesome-skills@58d857988f (2026-08-26). Data as JSON: /api/errors/cc5eb4772e7d331d. Report an issue: GitHub.

Appendix: source

Thrown at skills/fp-refactor/SKILL.md:69

- Composing multiple fallible operations is verbose

### Pattern: Synchronous try-catch to Either

#### Before (Imperative)

```typescript
function parseJSON(input: string): unknown {
  try {
    return JSON.parse(input);
  } catch (error) {
    throw new Error(`Invalid JSON: ${error}`);
  }
}

function validateUser(data: unknown): User {
  try {
    if (!data || typeof data !== 'object') {
      throw new Error('Data must be an object');
    }
    const obj = data as Record<string, unknown>;
    if (typeof obj.name !== 'string') {
      throw new Error('Name is required');
    }
    if (typeof obj.age !== 'number') {
      throw new Error('Age must be a number');
    }
    return { name: obj.name, age: obj.age };
  } catch (error) {
    throw error;
  }
}

// Usage with nested try-catch
function processUserInput(input: string): User | null {
  try {
    const data = parseJSON(input);

View on GitHub (pinned to 58d857988f)