Skip to content

Commit a5dbba4

Browse files
committed
Docs: Added 'What is a valid message' section to README [ci skip]
1 parent a75625d commit a5dbba4

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Each message type provides a set of methods with each method doing just one thin
9191
Note that **Message** refers to any message type below.
9292

9393
* **Message.verify**(message: `Object`): `null|string`<br />
94-
explicitly performs verification prior to encoding / converting a plain object. Instead of throwing, it returns the error message as a string, if any.
94+
explicitly performs verification prior to encoding a plain object. Instead of throwing, it returns the error message as a string, if any.
9595

9696
```js
9797
var payload = "invalid (not an object)";
@@ -160,6 +160,30 @@ Note that **Message** refers to any message type below.
160160

161161
See also: [ConversionOptions](http://dcode.io/protobuf.js/global.html#ConversionOptions)
162162

163+
**What is a valid message?**
164+
165+
A valid message is an object not missing any required fields and exclusively using JS types for its fields that are understood by the wire format writer.
166+
167+
* Calling `Message.verify` with a valid message returns `null` and otherwise the error as a string.
168+
* Calling `Message.create` or `Message.encode` with any object assumes valid types.
169+
* Calling `Message.fromObject` with any object naively converts all values to the optimal JS type.
170+
171+
| Type | Expected JS type (create) | Naive conversion (fromObject)
172+
|--------|-----------------|-------------------
173+
| int32<br />uint32<br />sint32<br />fixed32<br />sfixed32 | `Number` (32 bit integer) | `value | 0`<br /> `value >>> 0`
174+
| int64<br />uint64<br />sint64<br />fixed64<br />sfixed64 | `Long`-like (optimal)<br />`Number` (53 bit integer) | `Long.fromValue(value)`<br />`parseInt(value, 10)` without long.js
175+
| float<br />double | `Number` | `Number(value)`
176+
| bool | `Boolean` | `Boolean(value)`
177+
| string | `String` | `String(value)`
178+
| bytes | `Uint8Array` (optimal)<br />`Buffer` (optimal)<br />`Array.<Number>` (8 bit integers)<br />`String` (base64) | `base64.decode(value)` if a String<br />`Object` with non-zero `.length` is kept
179+
| enum | `Number` (32 bit integer) | Looks up the numeric id if a string
180+
| message | Valid message | `Message.fromObject(value)`
181+
182+
* Explicit `undefined` and `null` are considered as not set when optional.
183+
* Repeated fields are `Array.<T>`.
184+
* Map fields are `Object.<string,T>` with the key being the string representation of the respective value or an 8 characters long binary hash string for Long-likes.
185+
* `String` refers to both objects and values while `Number` refers to values only.
186+
163187
Examples
164188
--------
165189

0 commit comments

Comments
 (0)