Skip to content

Turning DecoderError into user friendly ValidationErrors #113

Description

@daniellittledev

I'd like decoder errors to be able to work with custom validation errors. Many of my types follow the "always valid" pattern, where types cannot be constructed without valid data.

As an example, given the following JSON, which has some type and value issues:

{
	"number": "a",
	"audit": {
		"createdBy": null,
		"date": "2021-07-66"
	}
}

I would like to generate some errors like the following:

[
	{ field = "number"; message = "The value 'a' was not a valid number" }
	{ field = "audit.createdBy"; message = "The field 'createdBy' is required" }
	{ field = "audit.date"; message = "The value '2021-07-66' was not a valid date" }
]

However, it does not appear to be possible, I cannot access a hierarchy of errors form a Decoder Error. The type seems to represent only a single error. The signature is as follows:

type DecoderError = string * ErrorReason

I was expecting the Decoder Error to be a Generic Data Structure which would be able to represent arbitrary errors passed up from a decoder all the fields, something like:

type DecoderError = DecoderItemError[]
type DecoderItemError = field: string * reason: ErrorReason * children: DecoderItemError[]

Is it possible to do something like this with Thoth? Or is the error always a string?

The docs point to this as an example of a decoder error:

Error at: `$.user.firstname`
Expecting an object with path `user.firstname` but instead got:
{
    "user": {
        "name": "maxime",
        "age": 25
    }
}
Node `firstname` is unknown.

I'm looking for an approach to achieve these validation errors using Thoth, thanks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions