rsubr / nova-json-schema-field
A Laravel Nova field for JSON Schema.
Installs: 206
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 1
Language:Vue
Requires
- php: >=7.1.0
This package is not auto-updated.
Last update: 2025-01-12 20:00:35 UTC
README
Laravel Nova field for displaying JSON schema data. Inspired by nsavinov/nova-json-schema-field.
Installation
You can install the package into a Laravel app that uses Nova via composer:
composer require rsubr/nova-json-schema-field
Usage
In the Laravel Model:
use Illuminate\Database\Eloquent\Casts\AsCollection; protected $casts = [ 'details' => AsCollection::class, ]; // NovaJsonSchemaField does not like empty JSON, so use an empty placeholder protected $attributes = [ 'details' => '{"_": ""}', ];
Using a Static Schema
In the Nova resource:
use Rsubr\NovaJsonSchemaField\NovaJsonSchemaField; public function fields(Request $request) { return [ // ... NovaJsonSchemaField::make('Details') ->jsonSchema($this->loadSchema()) ->rules('json'), ]; } private function loadSchema(): array { $schema = <<<SCHEMA { "type": "object", "required": [ "event_name", "level", "start_date", "duration" ], "properties": { "event_name": { "description": "Event Name" }, "level": { "type": "array", "items": { "enum": [ "State Level", "National", "International" ] }, "description": "Event level" }, "start_date": { "type": "string", "format": "date", "description": "Start Date" }, "duration": { "type": "number", "description": "Duration in Days" } } } SCHEMA; return json_encode($schema, true); }
Using a Dynamic Schema
The schema can be dynamically loaded from a related Model or API.
Eg. to load the JSON schema from an EventType
Model, first create an attribute on the Model and call from the Nova resource.
In the Laravel Model:
public function getJsonSchemaAttribute() { return $this->event_type->json_schema; }
The event_type
Model should have an HasMany
relationship with the model holding the JSON data.
In the Nova Resource:
protected function loadSchema($request) { // NovaJsonSchemaField does not like empty JSON, so use an empty placeholder $schema = $request->findModelQuery()->first()->json_schema ?? array(); return $schema; }
Notes
- For Postgresql, use store the JSON Schema in a
JSON
column to preserve field order, do not useJSONB
for the schema. The JSON data values can be stored inJSON
orJSONB
.