Skip to main content

JSON Schema Integration

forms4s provides integration with JSON Schema, allowing you to generate form elements directly from a JSON Schema definition. This makes it easy to create forms based on existing data models.

Usage​

"org.business4s" %% "forms4s-jsonschema" % "0.2.0"
// This is just an example, schema can be acquired or produced in any way
case class MyForm(name: String, age: Int) derives sttp.tapir.Schema
val jsonSchema: sttp.apispec.Schema =
sttp.tapir.docs.apispec.schema.TapirSchemaToJsonSchema(
summon[sttp.tapir.Schema[MyForm]],
markOptionsAsNullable = true,
)

// Convert the JSON Schema to a form
import forms4s.jsonschema.*
val form = FormElement.fromJsonSchema(jsonSchema)

Limitations​

  • Not all JSON Schema validation keywords are supported yet
  • Complex schema combinations (allOf, anyOf) are not fully supported
  • Custom formats may require additional configuration

For more advanced use cases, you may need to create form elements manually or extend the FormFromJsonSchema module.

Supported types​

Description JSON Schema Form Element Type
Basic text field
{
  "type" : "string"
}
Text
Multiline text field
{
  "type" : "string",
  "format" : "multiline"
}
Text (Multiline)
Date field
{
  "type" : "string",
  "format" : "date"
}
Text (Date)
Time field
{
  "type" : "string",
  "format" : "time"
}
Text (Time)
Date and time field
{
  "type" : "string",
  "format" : "date-time"
}
Text (DateTime)
Email field
{
  "type" : "string",
  "format" : "email"
}
Text (Email)
Integer number field
{
  "type" : "integer"
}
Number (Integer)
Decimal number field
{
  "type" : "number"
}
Number (Decimal)
Boolean checkbox field
{
  "type" : "boolean"
}
Checkbox
Array of form elements
{
  "type" : "array",
  "items" : {
    "type" : "string"
  }
}
Multivalue
Dropdown selection field
{
  "type" : "string",
  "enum" : [
    "Option 1",
    "Option 2",
    "Option 3"
  ]
}
Select
Group of form elements
{
  "type" : "object",
  "properties" : {
    "field1" : {
      "type" : "string"
    },
    "field2" : {
      "type" : "string"
    }
  }
}
Group
  • field1
  • field2
Alternative form elements
{
  "oneOf" : [
    {
      "title" : "Option 1",
      "type" : "object",
      "properties" : {
        "field1" : {
          "type" : "string"
        }
      }
    },
    {
      "title" : "Option 2",
      "type" : "object",
      "properties" : {
        "field2" : {
          "type" : "string"
        }
      }
    }
  ]
}
Alternative
  • Option 1
    • field1
  • Option 2
    • field2

Supported validations​

TODO :)