Skip to content

Commit

Permalink
#118. Add validate method to check custom and required types
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Dec 3, 2018
1 parent 9df38a4 commit 473a0e4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function handle()
try {
$this->actionIndex($ramlFile);
} catch (\Exception $e) {
$this->info($e->getMessage());
$this->error($e->getTraceAsString());
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/controllers/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,22 @@ public function actionIndex(string $file)
*/
private function validate()
{
// required yaml fields will be thrown as exceptions
if (empty($this->data[ApiInterface::OPEN_API_KEY])) {
throw new SchemaException('There must be ' . ApiInterface::OPEN_API_KEY . ' document root field');
throw new SchemaException('There must be ' . ApiInterface::OPEN_API_KEY . ', ' . ApiInterface::API_SERVERS . ' document root fields');
}

$schemas = $this->data[ApiInterface::API_COMPONENTS][ApiInterface::API_SCHEMAS];
if (empty($schemas[CustomsInterface::CUSTOM_TYPES_ID])
|| empty($schemas[CustomsInterface::CUSTOM_TYPES_TYPE])
|| empty($schemas[CustomsInterface::CUSTOM_RELATIONSHIPS_DATA_ITEM])) {
throw new SchemaException('At least these types must be declared: ' . CustomsInterface::CUSTOM_TYPES_ID
. ', ' . CustomsInterface::CUSTOM_TYPES_TYPE . ', ' . CustomsInterface::CUSTOM_RELATIONSHIPS_DATA_ITEM
. ' as components -> schemas descendants');
}

if (empty($this->data[ApiInterface::API_INFO])) {
$this->warn(ApiInterface::API_INFO . ': field would be convenient to show users what this API is about');
}
}

Expand All @@ -104,7 +118,7 @@ private function generateOpenApi()
$this->migrationsDir = DirsInterface::MIGRATIONS_DIR;

foreach ($this->data[ApiInterface::API_SERVERS] as $server) {
$vars = $server[ApiInterface::API_VARS];
$vars = $server[ApiInterface::API_VARS];
$this->version = $vars[ApiInterface::API_BASE_PATH][ApiInterface::API_DEFAULT];

if (env('APP_ENV') === 'dev') { // for test env based on .env
Expand Down
1 change: 1 addition & 0 deletions src/types/ApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface ApiInterface
{
public const OPEN_API_KEY = 'openapi';
public const API_SERVERS = 'servers';
public const API_INFO = 'info';

public const API_VARS = 'variables';
public const API_BASE_PATH = 'basePath';
Expand Down
21 changes: 12 additions & 9 deletions src/types/CustomsInterface.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

namespace rjapi\types;

interface CustomsInterface
{
public const CUSTOM_TYPES_ID = 'ID';
public const CUSTOM_TYPES_TYPE = 'Type';
public const CUSTOM_TYPES_RELATIONSHIPS = 'Relationships';
public const CUSTOM_TYPES_QUERY_PARAMS = 'QueryParams';
public const CUSTOM_TYPES_FILTER = 'Filter';
public const CUSTOM_TYPES_ATTRIBUTES = 'Attributes';
public const CUSTOM_TYPES_TREES = 'Trees';
public const CUSTOM_PROP_JWT = 'jwt';
public const CUSTOM_TYPE_REDIS = 'Redis';
public const CUSTOM_TYPES_ID = 'ID';
public const CUSTOM_TYPES_TYPE = 'Type';
public const CUSTOM_RELATIONSHIPS_DATA_ITEM = 'RelationshipsDataItem';

public const CUSTOM_TYPES_RELATIONSHIPS = 'Relationships';
public const CUSTOM_TYPES_QUERY_PARAMS = 'QueryParams';
public const CUSTOM_TYPES_FILTER = 'Filter';
public const CUSTOM_TYPES_ATTRIBUTES = 'Attributes';
public const CUSTOM_TYPES_TREES = 'Trees';
public const CUSTOM_PROP_JWT = 'jwt';
public const CUSTOM_TYPE_REDIS = 'Redis';
}

0 comments on commit 473a0e4

Please sign in to comment.