MackeyRMS/json-decode-attempt/2.0.0

NOTE: the latest version is 3.0.1

Json.Decode.Attempt

Module for Decoders that always return a valid value.

Type aliases

type Decoder status value

Phantom type to indicate that a Decoder has defaults provided for all of it's fields.

Phantom type to indicate that a Decoder has a risk in it's procedure thus rendering the entire decoder susceptible to failure.

type alias Validated value =
{ errors : List Error
, value : value
}

A record that carries both: A. a properly decoded value & [] like { value = "Stuff", errors = [] } or B. a default value & List Error like { value = "", [Decode.Failure "unexpected null value"] }

Runners

-> Value
-> Result
Error
{ errors : List Error
, value : value
}

Attempt to decode with a decoder that could potentially fail

decode : Decoder WithDefaults value -> Value -> Validated value

Provided with defaults using the primitives defined in this module decode will return a value composed of decoded values & defaults paired with whatever associated errors may have happened along the way.

{ errors : List Error
, value : value
}
-> ( value, List Error )

In case you need a tuple instead of record accessors :man_shrugging:

toDecoder : Decoder status value -> Decoder (Validated value)

Convert a Validated decoder into a regular elm/json Decoder

Constructors

withDefault : a -> Decoder a -> Decoder WithDefaults a

Provide a default value, turning a Decoder that has the potential to fail due to missing fields or bad data or, into a Decoder that will always succeed.

wrap a value in a Decoder

maybe : Decoder a -> Decoder WithDefaults (Maybe a)

turn a regular decoder into a Validated decoder using Maybe & Nothing as the default.

Primitives

Decoder for a unit value, may be useful when responding to DELETE requests that return HTTP 201 No Content

boolOr : Bool -> Decoder WithDefaults Bool

Decoder between a JSON boolean and an Elm Bool

Decoder between a JSON number and an Elm Int

floatOr : Float -> Decoder WithDefaults Float

Decoder between a JSON number and an Elm Float

charOr : Char -> Decoder WithDefaults Char

Decoder between a JSON string of length 1 and an Elm Char

stringOr : String -> Decoder WithDefaults String

Decoder between a JSON string and an Elm String

Core Data Structures

Decoder between a JSON array and an Elm List.

Use when a default for the elements is available to fill if decoding fails. Will still collect errors for the failed decoders

filteredList : Decoder a -> Decoder WithDefaults (List a)

Swallows errors for elements but probably what you want for most use cases...

Decoder between a JSON array and an Elm Array.

Decoder between a JSON object and an Elm Dict.

Datum between a JSON array of length 2 and an Elm Tuple.

Datum between a JSON array of length 3 and an Elm triple.

Records Primitives

String
-> List String
-> Decoder status (f -> b)
-> Decoder status b

Specify the name, and a Decoder for a field.

required propagates errors when the name is missing in the resulting json.

String
-> List String
-> Decoder status (f -> b)
-> Decoder status b

optional only propagates errors where the decoder doesn't match the expected type.

Functor, Applicative, Monad, Alternative

apply : Decoder WithDefaults a -> Decoder status (a -> b) -> Decoder status b

andMap for a Decoder WithDefaults

andMap for adding a call to risk

risk : Decoder a -> Decoder status (a -> b) -> Decoder MissingDefaults b

By introducing a raw elm/json decoder you're exposing the rest of your decoder to the possibility of failing

oneOf : List (Decoder a) -> a -> Decoder WithDefaults a

Provide a list of regular elm/json decoders and if all of them fail, provide a default.

Helpers

Specify that a decoder should use it's defaults when encountering a null

{ hideSensitiveInfo : Bool }

For dealing with a json string inside a json payload.

Tests

tests : Test

Tests