Discussion:
[elm-discuss] Json.Decode without failing on the first error?
'Rupert Smith' via Elm Discuss
2017-11-09 12:39:45 UTC
Permalink
Hi,

Has anyone any thoughts on writing a Json decoder that keeps going when it
encounters errors, until it gets all the way to the end of the data, and
then produces an error report detailing all of the errors that it
encountered, rather than just the first one?

Here is an example data model for discussion with a decoder for it:

import Set exposing (Set)
import Dict exposing (Dict)
import Json.Decode as Decode exposing (..)
import Json.Decode.Extra exposing ((|:), withDefault)

type Item =
Item
{ name : Maybe String
, description : Maybe String
, price : Maybe Int
, sku : Maybe String
, stockAvailable : Maybe Int
, internationalShipping : Maybe Bool
}

itemDecoder : Decoder Item
itemDecoder =
(Decode.succeed
(\name description price sku stockAvailable internationalShipping ->
Item
{
name = name
,description = description
,price = price
,sku = sku
,stockAvailable = stockAvailable
,internationalShipping = internationalShipping
}
)
)
|: Decode.maybe (field "name" Decode.string)
|: Decode.maybe (field "description" Decode.string)
|: Decode.maybe (field "price" Decode.int)
|: Decode.maybe (field "sku" Decode.string)
|: Decode.maybe (field "stock-available" Decode.int)
|: Decode.maybe (field "international-shipping" Decode.bool)


You can see that in my data model, I made all the fields Maybes. This is
because I wanted to not fail if a field was missing, not because the fields
in this data model are really optional.

I would rather that the fields were not Maybes, and that when applying
itemDecoder to some Json using Decode.decodeString or Decode.decodeValue
that I would get either

Ok { ... the data .. }

or

Error [ "price is not an int", "field order-date is not allowed", "field
sku is missing" ]

To achieve this I think I would need to write a more sophisticated version
of the '|:' operator from json-extra, but not sure exactly how.

Rupert
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ilias Van Peer
2017-11-09 14:39:45 UTC
Permalink
Yeah, I did that some time ago
- package.elm-lang.org/packages/zwilias/json-decode-exploration/latest

Demo here, the very last one in the output is a scenario not unlike yours
:) https://ellie-app.com/3vVLfrNDnSXa1/2
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Rupert Smith' via Elm Discuss
2017-11-09 14:50:19 UTC
Permalink
Yeah, I did that some time ago -
package.elm-lang.org/packages/zwilias/json-decode-exploration/latest
Demo here, the very last one in the output is a scenario not unlike yours
:) https://ellie-app.com/3vVLfrNDnSXa1/2
That is exactly what I want, thank you.
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...