Skip to content

validateAgainstShape

validateAgainstShape(data, shapeFields): ShapeValidatorResult

Validate a data payload against shape field definitions.

Shape field types:

  • “number”, “string”, “boolean”: primitive types
  • “wref”: string (reference to another thing)
  • Typed field objects: { type: primitive, description?: string }
  • Nested objects: { fieldName: type, … }
  • Arrays: [elementType]
  • Optional fields: field name ending with ”?” (e.g. “terminal_reason?”: “string”) OR type string ending with ”?” (e.g. “terminal_reason”: “string?”) OR typed object type ending with ”?” (e.g. { type: “string?” })

For v1 this is permissive: validates top-level field existence and basic types. Returns a ShapeValidatorResult discriminated union — read result.valid to branch, then result.errors (only present when invalid) for the list of messages. result.warnings may be present in either branch and carries undeclared top-level field names.

Record<string, unknown>

Record<string, unknown>

ShapeValidatorResult

import { validateAgainstShape } from "@warmhub/sdk-ts";
const shapeFields = { x: "number", y: "number", label: "string?" };
const ok = validateAgainstShape({ x: 1, y: 2 }, shapeFields);
// ok.valid === true
const bad = validateAgainstShape({ x: "not-a-number" }, shapeFields);
if (!bad.valid) {
for (const message of bad.errors) console.error(message);
// ['Field "x" must be a number', 'Missing required field: "y"']
}