Skip to content

Parse strings according to JSON schema patternsΒ #63

Open
@dragonwasrobot

Description

@dragonwasrobot

See http://json-schema.org/latest/json-schema-validation.html#format

For example, if we have the node

"datum": {
  "required: ["createdDate"]
  "properties": {
    "createdDate": {
      "type": "string",
      "format": "date-time"
    }
  }
}

we want it to generate the type

type alias Datum =
      { createdDate: Date
      }

rather than

type alias Datum =
      { createdDate: String
      }

Possible decoder for date-time could be:

dateDecoder : Decoder Date
dateDecoder =
    Decode.string
        |> Decode.andThen
            (\dateStr ->
                case Date.fromIsoString dateStr of
                    Ok date ->
                        Decode.succeed date

                    Err err ->
                        Decode.fail err
            )

Possible encoder

encodeDate : Date -> Value
encodeDate date =
    date
        |> Date.toIsoString
        |> Encode.string

However, the implications for the parser has to be checked first.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions