The envelope
{
"type": "inventory.changed",
"data": {
"sku": "A-104",
"available": 8
}
}type chooses the route or event. data carries the payload. js-message does not impose a vocabulary or schema inside either value; applications can layer validation above this transport envelope.
What load() validates
| Input | Outcome |
|---|---|
JSON string for an object with own type and data | Loaded |
| Buffer, boxed string, typed view, or other JSON-coercible text | Loaded after native text coercion and parsing. |
Existing object with own type and data | Loaded |
| Malformed JSON | Error envelope with a SyntaxError. |
null, primitive, or array | Error envelope with a TypeError. |
| Object missing either required own key | Error envelope with a TypeError. |
A valid JSON value is not always a valid message.
[], 0, null, and {} parse successfully, but none satisfy the envelope contract.
Normalization rules
- Extra keys in loaded input are ignored when the message is serialized.
- Object input keeps the original
datareference; js-message does not clone payloads. - Direct assignment to
typeanddatais unrestricted for compatibility. JSONuses nativeJSON.stringifybehavior, including omission, conversion, and errors.- A later valid
load()replaces an earlier error state, so one instance can recover.
Keep payloads wire-safe.
If a message must cross JSON, prefer strings, numbers, booleans, null, arrays, and plain objects. Cycles and BigInt make native serialization throw.