A Go implementation of JSONForms - a framework for building forms based on JSON Schema and UI Schema.
- Generate HTML forms from JSON Schema and UI Schema
- Support for various form layouts (Vertical, Horizontal)
- Built-in menu system for multi-page forms
- Multiple input methods (JSON files, bytes, or Go maps)
- Embedded HTML templates
- Form data verification
go get github.com/TobiEiss/go-jsonforms
package main
import (
"github.com/TobiEiss/go-jsonforms"
)
func main() {
// Define your schema
schema := map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{
"type": "string",
"description": "Please enter your name",
},
},
}
// Define UI schema
uiSchema := map[string]interface{}{
"type": "VerticalLayout",
"elements": []map[string]interface{}{
{
"type": "Control",
"scope": "#/properties/name",
},
},
}
// Create and render the form
builder := gojsonforms.NewBuilder()
html, err := builder.
WithSchemaMap(schema).
WithUISchemaMap(uiSchema).
Build(false)
if err != nil {
panic(err)
}
// Use the generated HTML in your application
fmt.Println(html)
}
WithSchemaMap(schema map[string]interface{})
: Set schema using a Go mapWithSchemaBytes(schema []byte)
: Set schema using JSON bytesWithSchemaFile(filepath string)
: Set schema from a JSON fileWithUISchemaMap(uiSchema map[string]interface{})
: Set UI schema using a Go mapWithUISchemaBytes(uiSchema []byte)
: Set UI schema using JSON bytesWithUISchemaFile(filepath string)
: Set UI schema from a JSON fileWithDataMap(data map[string]interface{})
: Set initial data using a Go mapWithDataBytes(data []byte)
: Set initial data using JSON bytesWithDataFile(filepath string)
: Set initial data from a JSON fileWithMenu(menu []MenuItem)
: Add navigation menu itemsBuild(withIndex bool)
: Generate the HTML form
Check the example directory for complete working examples:
Basic Schema Example
{
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Please enter your name"
},
"age": {
"type": "integer",
"description": "Please enter your age"
}
},
"required": ["name"]
}
UI Schema Example
{
"type": "VerticalLayout",
"elements": [
{
"type": "Control",
"scope": "#/properties/name"
},
{
"type": "Control",
"scope": "#/properties/age"
}
]
}
This project is licensed under the MIT License - see the LICENSE file for details.