Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse strings according to JSON schema patterns #63

Open
dragonwasrobot opened this issue Apr 13, 2018 · 0 comments
Open

Parse strings according to JSON schema patterns #63

dragonwasrobot opened this issue Apr 13, 2018 · 0 comments

Comments

@dragonwasrobot
Copy link
Owner

dragonwasrobot commented Apr 13, 2018

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.

@dragonwasrobot dragonwasrobot changed the title Parse strings according to pattern Parse strings according to JSON schema patterns Apr 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant