{"schema_version":"1.0","publisher":"Primitive SDKs","canonical_url":"https://test.abhinandan.one/memory-json-value-helper","markdown_url":"https://test.abhinandan.one/memory-json-value-helper.md","article":{"id":"a0571303-3f2d-42b2-adc8-2bf4f6b01a3b","article_slug":"memory-json-value-helper","parent_article_slug":null,"parent_article_title":null,"kind":"reference","published_at":"2026-08-11T18:55:07.222264+00:00","keywords":["isMemoryJsonValue","MemoryJsonValue","@primitivedotdev/api-core","client.memories.set","Primitive Memories JSON value","memory value validation"],"meta_description":"isMemoryJsonValue(value) returns true only if value is a valid JSON value: string, finite number, boolean, null, array, or plain object, recursively.","og_image_url":null,"source_file_paths":[],"recording_id":null,"replayable":false,"task_name":"Memory Value Validation Helper","category":"API Core / OpenAPI Generation","summary":null,"description":"isMemoryJsonValue is a type-guard function that checks whether a value satisfies the JSON constraints Primitive Memories accepts, letting you validate data before calling client.memories.set.","content_kind":"repo_page","content_markdown":"## What it does\n\n`isMemoryJsonValue` is a TypeScript type-guard function that checks whether an arbitrary JavaScript value conforms to the `MemoryJsonValue` type Primitive Memories accepts. Call it before writing a value with [`client.memories.set`](node-sdk-api-client) to fail fast on invalid input instead of getting a rejected API request.\n\nIt is defined in the workspace-internal [api-core](api-core-overview) package and reaches consumers through `@primitivedotdev/sdk/api`:\n\n```typescript\nimport { isMemoryJsonValue } from \"@primitivedotdev/sdk/api\";\n\nisMemoryJsonValue({ step: 2, done: false }); // true\nisMemoryJsonValue(undefined); // false\n```\n\n<Note>\n\nPrimitive Memories itself, the durable JSON key-value store behind `client.memories`, is documented on [Generated API Client and Primitive Memories](node-sdk-api-client). This page covers only the validation helper.\n\n</Note>\n\n## Signature\n\n```typescript\nfunction isMemoryJsonValue(value: unknown): value is MemoryJsonValue;\n```\n\n| Parameter | Type | Description |\n|---|---|---|\n| `value` | `unknown` | The value to check. |\n\n**Returns:** `boolean`. When `true`, TypeScript narrows `value` to `MemoryJsonValue` in the calling scope.\n\n## The `MemoryJsonValue` type\n\n```typescript\ntype MemoryJsonValue =\n  | null\n  | string\n  | number\n  | boolean\n  | MemoryJsonValue[]\n  | { [key: string]: MemoryJsonValue };\n```\n\nThis is the exact type the generated OpenAPI client uses for the Memories API's `value` field. It is recursive: array elements and object property values must themselves be valid `MemoryJsonValue`s.\n\n## What is accepted\n\n| Input | Accepted? |\n|---|---|\n| `string` | Yes |\n| Finite `number` | Yes |\n| `boolean` | Yes |\n| `null` | Yes |\n| Array of valid `MemoryJsonValue`s | Yes |\n| Plain object with valid `MemoryJsonValue` values | Yes |\n\n## What is rejected\n\n| Input | Rejected? | Why |\n|---|---|---|\n| `undefined` | Yes | Not representable in JSON |\n| `bigint` | Yes | Not a JSON type |\n| `symbol` | Yes | Not a JSON type |\n| `function` | Yes | Not a JSON type |\n| `NaN` / `Infinity` / `-Infinity` | Yes | Not finite numbers |\n| Sparse arrays (holes) | Yes | A hole is not a valid element |\n| Class instances (e.g. `Date`, `Map`, custom classes) | Yes | Not plain objects |\n| Cyclic structures | Yes | Cannot serialize to JSON |\n\n## Where it's used internally\n\nThe Node SDK's `client.memories.set` calls the same validation logic on its `value` field before sending the request, so a value that fails `isMemoryJsonValue` also fails at `client.memories.set` with a `TypeError`:\n\n```text\nclient.memories.set value must be a JSON value: string, finite number, boolean,\nnull, array, or plain object. Undefined, bigint, symbol, function, NaN, Infinity,\nsparse arrays, class instances, and cyclic values are not valid memory values.\n```\n\nCalling `isMemoryJsonValue` yourself lets you validate a value earlier in your code path, for example, before constructing the object you plan to store, or when accepting arbitrary data from an upstream source.\n\n## Example: guarding a value before storing it\n\n```typescript\nimport {\n  createPrimitiveClient,\n  isMemoryJsonValue,\n} from \"@primitivedotdev/sdk/api\";\n\nconst client = createPrimitiveClient({ apiKey: process.env.PRIMITIVE_API_KEY! });\n\n\nfunction toStorableState(candidate: unknown): Record<string, unknown> {\n  if (!isMemoryJsonValue(candidate)) {\n    throw new TypeError(\"state is not a valid Primitive Memories JSON value\");\n  }\n  return candidate as Record<string, unknown>;\n}\n\nconst state = toStorableState({ step: 2, lastEmailId: \"em_123\" });\n\nawait client.memories.set({ key: \"thread:latest\", value: state });\n```\n\n## Related fix in codegen\n\nThe generated `MemoryJsonValue` TypeScript type required a post-processing repair step during codegen, because `@hey-api/openapi-ts` widens the recursive `type: \"null\"` branch of the schema to `unknown`. See [Generated TypeScript Client Fixups](typescript-client-fixups) for how `fix-generated-api-imports.ts` patches this.\n\n## Next steps\n\n<CardGroup cols={2}>\n\n<Card title=\"Generated API Client and Primitive Memories\" href=\"node-sdk-api-client\">\n\nStore, search, and delete durable JSON records with client.memories.\n\n</Card>\n\n<Card title=\"Generated TypeScript Client Fixups\" href=\"typescript-client-fixups\">\n\nSee the post-processing pass that repairs the generated MemoryJsonValue type.\n\n</Card>\n\n<Card title=\"Operation Manifest Reference\" href=\"operation-manifest\">\n\nLook up the Memories API operations and their request/response schemas.\n\n</Card>\n\n<Card title=\"PrimitiveApiClient\" href=\"primitive-api-client\">\n\nConstruct the host-aware client that exposes memory operations directly.\n\n</Card>\n\n</CardGroup>","canonical_base_url":"https://test.abhinandan.one","seo_indexing_enabled":true,"last_modified":"2026-08-21T18:22:43.359885+00:00","video_url":null,"voiceover_url":null,"tools_used":[],"demonstrated_by":[],"steps":[],"related_links":[],"intro":null,"prerequisites":[],"verification":[],"troubleshooting":[],"suggest_edit_url":null,"raise_issue_url":"https://github.com/abhi-browzer/primitive-sdks/issues/new?title=Docs+feedback%3A+Memory+Value+Validation+Helper&body=Page%3A+https%3A%2F%2Ftest.abhinandan.one%2Fmemory-json-value-helper","page_feedback_enabled":true,"verified_ref":null,"verified_at":"2026-08-11T18:38:45.205849+00:00"}}