Skip to content

Null handling? #5

@suzdalnitski

Description

@suzdalnitski

Hi,

It seems that null values in Json can't be parsed. Eg the following fails:

`
{
    "errors": [
        {
            "extensions": {
                "code": "unauthenticated",
                "reference": null
            },
            "message": "You must be authenticated to perform this action",
            "path": [ "startPlanCheckout" ]
        }
    ]
}
`
->Js.Json.parseExn
->JsonCombinators.Json.decode(DecodeErrors.fetchResult)

My decoder is:

module DecodeErrors = {
  open JsonCombinators.Json.Decode

  type extensions = {
    code: string,
    reference: option<string>,
  }

  type error = {
    message: string,
    path: array<string>,
    extensions: extensions,
  }

  type fetchResult = {errors: option<array<error>>}

  let errorField = object(field => {
    message: field.required(. "message", string),
    path: field.required(. "path", array(string)),
    extensions: field.required(.
      "extensions",
      object(field => {
        code: field.required(. "code", string),
        reference: field.optional(. "reference", string),
      }),
    ),
  })

  let fetchResult = object(field => {
    errors: field.optional(. "errors", array(errorField)),
  })
}

This results in the following error: Expected string, got null\n\tat field 'reference'\n\ta…tensions'\n\tin array at index 0\n\tat field 'errors'

It seems that optional doesnt support nulls (which makes sense since Noneis compiled toundefined` in Rescript).

Is there a way to make nulls also be treated as optional?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions