GithubHelp home page GithubHelp logo

nova-json-schema-field's Introduction

JSON schema field for Laravel Nova

Packagist

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

  1. For Postgresql, use store the JSON Schema in a JSON column to preserve field order, do not use JSONB for the schema. The JSON data values can be stored in JSON or JSONB.

nova-json-schema-field's People

Contributors

rsubr avatar

Stargazers

Ende Zhong avatar VMK avatar

Watchers

 avatar VMK avatar

nova-json-schema-field's Issues

Not Work inside Belongs to many field

not working inside the belongs to many field !! is there any solutions ??

BelongsToMany::make('Types')->fields(function () {
return [
NovaJsonSchemaField::make('services')->jsonSchema($this->loadSchema())
];
})->allowDuplicateRelations(),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.