@@ -259,7 +259,10 @@ export class Decoder<A> {
259
259
260
260
/**
261
261
* An higher-order decoder that runs decoders on specified fields of an object,
262
- * and returns a new object with those fields.
262
+ * and returns a new object with those fields. If `object` is called with no
263
+ * arguments, then the outer object part of the json is validated but not the
264
+ * contents, typing the result as a dictionary where all keys have a value of
265
+ * type `any`.
263
266
*
264
267
* The `optional` and `constant` decoders are particularly useful for decoding
265
268
* objects that match typescript interfaces.
@@ -270,11 +273,16 @@ export class Decoder<A> {
270
273
* ```
271
274
* object({x: number(), y: number()}).run({x: 5, y: 10})
272
275
* // => {ok: true, result: {x: 5, y: 10}}
276
+ *
277
+ * object().map(Object.keys).run({n: 1, i: [], c: {}, e: 'e' })
278
+ * // => {ok: true, result: ['n', 'i', 'c', 'e']}
273
279
* ```
274
280
*/
275
- static object < A > ( decoders : DecoderObject < A > ) : Decoder < A > {
276
- return new Decoder < A > ( ( json : any ) => {
277
- if ( isJsonObject ( json ) ) {
281
+ static object ( ) : Decoder < { [ key : string ] : any } > ;
282
+ static object < A > ( decoders : DecoderObject < A > ) : Decoder < A > ;
283
+ static object < A > ( decoders ?: DecoderObject < A > ) {
284
+ return new Decoder ( ( json : any ) => {
285
+ if ( isJsonObject ( json ) && decoders ) {
278
286
let obj : any = { } ;
279
287
for ( const key in decoders ) {
280
288
if ( decoders . hasOwnProperty ( key ) ) {
@@ -292,6 +300,8 @@ export class Decoder<A> {
292
300
}
293
301
}
294
302
return Result . ok ( obj ) ;
303
+ } else if ( isJsonObject ( json ) ) {
304
+ return Result . ok ( json ) ;
295
305
} else {
296
306
return Result . err ( { message : expectedGot ( 'an object' , json ) } ) ;
297
307
}
0 commit comments