decoders

JSON

Decoders for validating JSON-compatible values.

json

json: Decoder<JSONValue> (source)

Accepts any value that's a valid JSON value.

In other words: any value returned by JSON.parse() should decode without failure.

type JSONValue =
    | null
    | string
    | number
    | boolean
    | { [string]: JSONValue }
    | JSONValue[]
// 👍
json.verify({
  name: 'Amir',
  age: 27,
  admin: true,
  image: null,
  tags: ['vip', 'staff'],
});

jsonObject

jsonObject: Decoder<{ [string]: JSONValue }> (source)

Accepts objects that contain only valid JSON values.

// 👍
jsonObject.verify({});                // {}
jsonObject.verify({ name: 'Amir' });  // { name: 'Amir' }

// 👎
jsonObject.verify([]);                   // throws
jsonObject.verify([{ name: 'Alice' }]);  // throws
jsonObject.verify('hello');              // throws
jsonObject.verify(null);                 // throws

jsonArray

jsonArray: Decoder<JSONValue[]> (source)

Accepts arrays that contain only valid JSON values.

// 👍
jsonArray.verify([]);                  // []
jsonArray.verify([{ name: 'Amir' }]);  // [{ name: 'Amir' }]

// 👎
jsonArray.verify({});                 // throws
jsonArray.verify({ name: 'Alice' });  // throws
jsonArray.verify('hello');            // throws
jsonArray.verify(null);               // throws

On this page