opis / json-schema

JSON Schema validator for PHP
https://opis.io/json-schema
Apache License 2.0
567 stars 57 forks source link

JSON Schema to Open API 3.0 (Swagger) schema #120

Open outcomer opened 2 years ago

outcomer commented 2 years ago

Hello! I have general strategic question. This is not a bug or error. Is it possible to add functionality to the package that will return a fully resolved schema, replacing all $refs and $injects and in it with real values, keeping schema structure?

Here is an example schema:

$schema = (object) [
    'type'                 => 'object',
    'properties'           => [
        'headers' => [
            '$ref'                 => "{$host}/src/AppBundle/Service/Validation/Definition/Headers/headers",
            'required'             => [
                'accept',
                'accept-language',
                'content-type',
                'x-client-hash',
            ],
            'additionalProperties' => true,
        ],
        'params'  => [
            'type'                 => 'object',
            'properties'           => [
                'XDEBUG_SESSION_START' => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/xdebugSessionStart"],
                'positionId'           => [
                    '$ref'    => "{$host}/src/AppBundle/Service/Validation/Definition/Params/dbId",
                    'default' => 0,
                ],
                'firstName'            => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string"],
                'lastName'             => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string"],
                'email'                => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/email"],
                'phone'                => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/phone"],
                'message'              => [
                    '$ref'    => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string",
                    'default' => self::MSG_NOT_PROVIDED,
                ],
                'marketingAgree'       => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/bool"],
                'personalAgree'        => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/bool"],
                'utmSource'            => [
                    '$ref'    => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string",
                    'default' => self::MSG_NOT_PROVIDED,
                ],
            ],
            'required'             => [
                'positionId',
                'firstName',
                'lastName',
                'email',
                'phone',
                'message',
                'marketingAgree',
                'personalAgree',
                'utmSource',
            ],
            'additionalProperties' => false,
        ],
    ],
    'required'             => [
        'headers',
        'params',
    ],
    'additionalProperties' => false,
];

One of $ref used here:

self::$schema = [
    '$schema'  => 'https://json-schema.org/draft/2020-12/schema',
    '$id'      => "{$host}/src/AppBundle/Service/Validation/Definition/Params/xdebugSessionStart",
    '$comment' => 'Parameter xdebug session start value.',
    '$slots'   => [
        'xdebugSessionStart-parameter' => [
            'type'   => ['string', 'null', 'boolean'],
            '$error' => [
                'type' => static::ERROR_TYPE,
            ],
        ],
    ],
];

So XDEBUG_SESSION_START key from base schema should be after resolving:

'XDEBUG_SESSION_START' => [
    'type'   => [
        'string',
        'null',
        'boolean',
    ],
    '$error' => ['type' => 'Provided value ({type}) must match the type: {expected}.'],
],

If $injects used for $slots xdebugSessionStart-parameter - then resolved schema must contain injected data.


Idea and motivation If one day it became possible we can translate JSON Schema into Open API schema therefor having JSON Schema for each API route give you a chance to automattically build API docs for any route that was provided with validation schema

razvanphp commented 1 year ago

Are you aware that openapi 3.1 now has json schema 100% compatibility?

https://www.openapis.org/blog/2021/02/18/openapi-specification-3-1-released