Skip to content

TobiEiss/go-jsonforms

Repository files navigation

go-jsonforms

A Go implementation of JSONForms - a framework for building forms based on JSON Schema and UI Schema.

Go Reference Go Report Card License: MIT

Features

  • 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

Installation

go get github.com/TobiEiss/go-jsonforms

Quick Start

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)
}

Documentation

Builder Methods

  • WithSchemaMap(schema map[string]interface{}): Set schema using a Go map
  • WithSchemaBytes(schema []byte): Set schema using JSON bytes
  • WithSchemaFile(filepath string): Set schema from a JSON file
  • WithUISchemaMap(uiSchema map[string]interface{}): Set UI schema using a Go map
  • WithUISchemaBytes(uiSchema []byte): Set UI schema using JSON bytes
  • WithUISchemaFile(filepath string): Set UI schema from a JSON file
  • WithDataMap(data map[string]interface{}): Set initial data using a Go map
  • WithDataBytes(data []byte): Set initial data using JSON bytes
  • WithDataFile(filepath string): Set initial data from a JSON file
  • WithMenu(menu []MenuItem): Add navigation menu items
  • Build(withIndex bool): Generate the HTML form

Examples

Check the example directory for complete working examples:

Schema 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"
    }
  ]
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • JSONForms for the original implementation and specification
  • Gabs for JSON parsing

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published