niho/json-schema-form/2.0.0

NOTE: the latest version is 3.0.0

Json.Schema.Form

Generate validating forms from JSON schemas.

Types

type alias Options =
{ errors : Errors
, formats : Formats
}

Customize the generated form.

  • errors - A function that turns error values into user readable strings.
  • formats - A list of custom formats (see Json.Schema.Form.Format).
type alias State =
{ options : Options
, schema : Schema
, form : Form ErrorValue Value
}

The form state.

type alias Msg =
Msg

Form messages for update.

Init/update lifecycle

init : Options -> Schema -> State

Initialize a form state with options and a schema. Use json-tools/json-schema to parse or build a schema.

update : Msg -> State -> State

Update the form state.

View

view : State -> Html Msg

The form fields as HTML. Use together with submit to submit the form.

submit : Msg

Triggers the form to be submitted for validation.

form [ onSubmit Json.Schema.Form.submit ]
    [ Json.Schema.Form.view state
    , button [] [ text "Submit" ]
    ]

Output

getOutput : State -> Maybe Value

Get the output value of the form if it validates.

case Json.Schema.Form.getOutput state of
    Just value ->
        -- The `value` is a tree of all the fields in the form (see the
        -- `Json.Schema.Form.Value` module).
        -- Use the `Json.Schema.Form.Encode.encode` function to turn
        -- it into a JSON value.
    Nothing ->
        -- If any field in the form is not valid `getOutput` will
        -- return `Nothing`.