Skip to content

Fallback validation for JSON is too shallow #21

@marklearst

Description

@marklearst

Problem:
Validation only checks the existence of meta.variableCollections and meta.variables, so malformed structures can still pass as valid.

References:

  • src/utils/typeGuards.ts

Citations:

  • src/utils/typeGuards. ts

Code Snippets

src/utils/typeGuards.ts:

export function isLocalVariablesResponse(data:  unknown): data is LocalVariablesResponse {
  if (typeof data !== 'object' || data === null) return false
  const obj = data as Record<string, unknown>
  if (typeof obj. meta !== 'object' || obj.meta === null) return false
  const meta = obj.meta as Record<string, unknown>
  if (typeof meta.variableCollections !== 'object' || meta.variableCollections === null) return false
  if (typeof meta.variables !== 'object' || meta.variables === null) return false
  return true
}

tests/utils/typeGuards.test.ts (valid/invalid fallback validation):

it('should return true for valid LocalVariablesResponse', () => {
  const validData = {
    meta: {
      variableCollections: { 'col-1': {} },
      variables: { 'var-1': {} },
    },
  }
  expect(isLocalVariablesResponse(validData)).toBe(true)
})
it('should return false for null', () => {
  expect(isLocalVariablesResponse(null)).toBe(false)
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions