Generate validating forms from JSON schemas.
elm package install niho/json-schema-formtext, select, etc.) with optional labels and descriptions.See the example project for examples of all the supported field types.
module Main exposing (main)
import Browser
import Html exposing (..)
import Json.Schema
import Json.Schema.Form exposing (Msg, State, onSubmit)
main : Program () State Msg
main =
Browser.sandbox { init = init, update = update, view = view }
schema =
"""
{
"type": "object",
"required": ["name"],
"properties": {
"name": {
"type": "string",
"title": "Name"
}
}
}
"""
init : State
init =
case Json.Schema.fromString schema of
Ok schema_ ->
Json.Schema.Form.init
{ errors = \path error -> "Invalid field: " ++ path
, formats = []
}
schema_
Err error ->
Debug.todo error
update : Msg -> State -> State
update msg state =
Json.Schema.Form.update msg state
view : State -> Html Msg
view state =
form [ onSubmit ]
[ Json.Schema.Form.view state
, button [] [ text "Submit" ]
]