Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ObjectDecoder<Specs>

A class representing a decoder for object types. Inherits the Decoder adding a ObjectDecoder.specs field

Type parameters

Hierarchy

  • Decoder<DecodedObject<Specs>>
    • ObjectDecoder

Index

Decode

Readonly decode

decode: (value: unknown) => Result<DecodedObject<Specs>>

Type declaration

    • (value: unknown): Result<DecodedObject<Specs>>
    • Run the decode through the given value (of unknown type) and produce a Result

      number.decodeString("42") // => ✅ 42
      number.decodeString(`"42"`) // => 🟥 "Expected a number, got \"42\" instead"

      Parameters

      • value: unknown

      Returns Result<DecodedObject<Specs>>

decodeString

  • decodeString(json: string): Result<DecodedObject<Specs>>
  • Like Decoder.decode, but applies JSON.parse() before

    number.decodeString("42") // => ✅ 42
    number.decodeString(`"42"`) // => 🟥 "Expected a number, got \"42\" instead"

    Parameters

    • json: string

    Returns Result<DecodedObject<Specs>>

decodeUnsafeThrow

  • decodeUnsafeThrow(value: unknown): DecodedObject<Specs>
  • Like Decoder.decode, but instead of returning a Result, directly returns the decoded value, or throws an error on failure

    ⚠️ throwing errors defeats the whole purpose of type soundness. decoder.decode() is generally preferable

    Parameters

    • value: unknown

    Returns DecodedObject<Specs>

Object

optional

optional: OptionalField<DecodedObject<Specs>> = ...

Represents a mandatory field in a object. Used with the object decoder

//  Decoder<{ x?: string | undefined }>
const decoder = object({ x: string.optional })
decoder.decode({ x: "str" }) // => ✅ { x: "str" }
decoder.decode({ x: 42 }) // => 🟥
decoder.decode({ }) // => ✅ { }
decoder.decode({ x: undefined }) // => 🟥
decoder.decode({ x: null }) // => 🟥

required

required: RequiredField<DecodedObject<Specs>> = ...

Represents a mandatory field in a object. Used with the object decoder

//  Decoder<{ x: string }>
const decoder = object({ x: string.required })
decoder.decode({ x: "str" }) // => ✅ { x: "str" }
decoder.decode({ x: 42 }) // => 🟥
decoder.decode({ }) // => 🟥
decoder.decode({ x: undefined }) // => 🟥
decoder.decode({ x: null }) // => 🟥

default

  • default(value: DecodedObject<Specs>): RequiredField<DecodedObject<Specs>>
  • Represents an required field in a object, but instead of failing when field is not present, the given value is used. Used with the object decoder

    const decoder = object({ x: string.default("NONE") })
    decoder.decode({ x: "str" }) // => ✅ { x: "str" }
    decoder.decode({ x: 42 }) // => 🟥
    decoder.decode({ }) // => ✅ { x: "NONE" }
    decoder.decode({ x: undefined }) // => 🟥
    decoder.decode({ x: null }) // => 🟥

    Parameters

    • value: DecodedObject<Specs>

    Returns RequiredField<DecodedObject<Specs>>

Transform

andThen

  • andThen<U>(f: (value: DecodedObject<Specs>) => Decoder<U>): Decoder<U>
  • Applies the given function to the decoded value (when present), and returns the result decoder. Sometimes known as >>=/bind.

     const stringToInt = (str: string): Result<number> => {
    const parsed = Number.parseInt(str)
    if (Number.isNaN(parsed)) {
    return succeed(`Cannot parse "${str}" as int`)
    } else {
    return never(parsed)
    }
    }

    string.andThen(stringToInt).decode(42) // => 🟥 "Expected a string, got 42 instead"
    string.andThen(stringToInt).decode("42") // => ✅ 42
    string.andThen(stringToInt).decode("abc") // => 🟥 "Cannot parse \"abc\" as int"

    const f = n => n.toString() + "!"
    number.map(f).decode(42) // => ✅ "42!"
    number.map(f).decode("str") // => 🟥 "Expected a number, got \"str\" instead"

    Type parameters

    • U

    Parameters

    • f: (value: DecodedObject<Specs>) => Decoder<U>
        • (value: DecodedObject<Specs>): Decoder<U>
        • Parameters

          • value: DecodedObject<Specs>

          Returns Decoder<U>

    Returns Decoder<U>

map

  • map<U>(f: (value: DecodedObject<Specs>) => U): Decoder<U>
  • Maps the decoded value (when present)

    const f = n => n.toString() + "!"
    number.map(f).decode(42) // => ✅ "42!"
    number.map(f).decode("str") // => 🟥 "Expected a number, got \"str\" instead"

    Type parameters

    • U

    Parameters

    • f: (value: DecodedObject<Specs>) => U
        • (value: DecodedObject<Specs>): U
        • Parameters

          • value: DecodedObject<Specs>

          Returns U

    Returns Decoder<U>

Other

Readonly specs

specs: Specs

The specs passed to the object function

mapSpecs

  • mapSpecs<NewSpecs>(mapper: (specs: Specs) => NewSpecs): ObjectDecoder<NewSpecs>
  • A convenience method for creating a scope for destructuring the objects specs.

    objDec.mapSpecs(f) is the same as object(f(objDec.specs))

    Type parameters

    Parameters

    • mapper: (specs: Specs) => NewSpecs
        • (specs: Specs): NewSpecs
        • Parameters

          • specs: Specs

          Returns NewSpecs

    Returns ObjectDecoder<NewSpecs>

Generated using TypeDoc