You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
95
95
96
96
```js
97
97
var payload ="invalid (not an object)";
@@ -160,6 +160,30 @@ Note that **Message** refers to any message type below.
160
160
161
161
See also: [ConversionOptions](http://dcode.io/protobuf.js/global.html#ConversionOptions)
162
162
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)
| 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
* 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.
0 commit comments