niho / json-schema-form / Json.Schema.Form

Generate validating forms from JSON schemas.

Types


type alias Options =
{ errors : Errors
, formats : Dict String Format 
}

Customize the generated form.


type alias State =
{ options : Options
, schema : Json.Schema.Definitions.Schema
, form : Form ErrorValue Value 
}

The form state.


type alias Msg =
Form.Msg

Form messages for update.

Init/update lifecycle

init : Options -> Json.Schema.Definitions.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`.