A Phoenix LiveComponent for visually building, editing, and validating JSON Schemas.
- Visual Editing: Recursively build and edit deeply nested objects and arrays.
- JSON Viewer: Dedicated component for displaying JSON with syntax highlighting and indentation guides.
- Tabbed Interface: Switch between a visual editor, live JSON preview, and the Test Lab.
- Advanced Logic: Support for
oneOf,anyOf,allOfand conditional keywords (if,then,else,not). - Test Lab: Validate sample JSON data against your schema in real-time with detailed error reporting.
- Schema Generation: Automatically infer a JSON Schema from a pasted JSON object.
- Undo/Redo: Full history support for all schema modifications.
- Real-time Validation: In-editor logic checking (e.g.,
min <= max) with immediate visual feedback. - Draft 07 Support: Includes constraints, enums, constants,
nulltype, and $schema management. - Copy to Clipboard: One-click export of the generated schema.
- Lightweight: Zero external JS dependencies (uses native Phoenix hooks), only requires
phoenix_live_view.
This library uses a small CSS file for styling and a JavaScript hook for clipboard functionality.
def deps do
[
{:json_schema_editor, "~> 0.9.3"}
]
end@import "../../deps/json_schema_editor/assets/css/json_schema_editor.css";In your assets/js/app.js, import and register the hook:
import { Hooks as JSEHooks } from "../../deps/json_schema_editor/assets/js/json_schema_editor"
// ... existing hooks
let liveSocket = new LiveSocket("/live", Socket, {
params: {_csrf_token: csrfToken},
hooks: { ...JSEHooks, ...other_hooks }
})The editor supports JSON Schema Draft 07. It automatically injects the $schema URI if not provided.
def mount(_params, _session, socket) do
schema = %{
"type" => "object",
"properties" => %{
"user" => %{
"type" => "object",
"properties" => %{
"name" => %{"type" => "string", "minLength" => 2}
}
}
}
}
{:ok, assign(socket, my_schema: schema)}
end<.live_component
module={JSONSchemaEditor}
id="json-editor"
schema={@my_schema}
on_save={fn updated_schema -> send(self(), {:schema_saved, updated_schema}) end}
/>def handle_info({:schema_saved, updated_schema}, socket) do
# The schema is guaranteed to be logically consistent here
{:noreply, assign(socket, my_schema: updated_schema)}
endYou can also use the standalone viewer component to display any JSON data with syntax highlighting.
<JSONSchemaEditor.Viewer.render json={@my_schema} />It accepts maps, lists, or even raw JSON strings (which it will attempt to decode and pretty-print).
mix test --coverelixir examples/demo.exsVisit http://localhost:4040 to see the editor in action with a sample schema.
