Skip to content

Commit

Permalink
ACMS-1925: update starterkit schema logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeshreeputra authored and deepakmishra2 committed Aug 17, 2023
1 parent bb1105c commit 3925721
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 66 deletions.
33 changes: 33 additions & 0 deletions acms/starterkit.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
starterkit:
name:
type: strings
is_required: TRUE
description:
type: strings
is_required: TRUE
modules:
type: arrays
is_required: FALSE
next_level_validation: TRUE
require:
type: arrays
is_required: FALSE
install:
type: arrays
is_required: FALSE
themes:
type: arrays
is_required: FALSE
next_level_validation: TRUE
require:
type: arrays
is_required: FALSE
install:
type: arrays
is_required: FALSE
admin:
type: strings
is_required: FALSE
default:
type: strings
is_required: FALSE
5 changes: 2 additions & 3 deletions src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ public function getStarterKits(): array {
}

// Send each starterkit for validation.
foreach ($starterkits as $starterkit) {
$this->starterKitValidation->validateStarterKitSchema($starterkit);
}
$schema = $this->getAcquiaCmsFile($this->projectDirectory . '/acms/starterkit.schema.yml');
$this->starterKitValidation->validateStarterKits($schema['starterkit'], $starterkits);

// Return starterkit list.
return $starterkits;
Expand Down
97 changes: 39 additions & 58 deletions src/Validation/StarterKitValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,85 +29,66 @@ public function __construct(
}

/**
* Schema for starter-kit.
* Loop each starter-kit and send it for validation.
*/
public function validateStarterKitSchema(array &$starterkit): array {
$schema = [
'name' => [
'type' => 'string',
'is_required' => TRUE,
],
'description' => [
'type' => 'string',
'is_required' => TRUE,
],
'modules' => [
'require' => [
'type' => 'array',
'is_required' => FALSE,
],
'install' => [
'type' => 'array',
'is_required' => FALSE,
],
'type' => 'array',
'is_required' => FALSE,
],
'themes' => [
'require' => [
'type' => 'array',
'is_required' => FALSE,
],
'install' => [
'type' => 'array',
'is_required' => FALSE,
],
'admin' => [
'type' => 'string',
'is_required' => FALSE,
],
'default' => [
'type' => 'string',
'is_required' => FALSE,
],
'type' => 'array',
'is_required' => FALSE,
],
];
$this->validateStarterKit($schema, $starterkit);
public function validateStarterKits(array $schema, array &$starterkits): void {
foreach ($starterkits as $starterkit) {
$this->validateStarterKit($schema, $starterkit);
}
}

return $schema;
/**
* Validates array for starter-kit.
*/
public function validateArray($schema, $starterkit, $key): void {
if ($schema['is_required'] ?
((!isset($starterkit) || empty($starterkit)) && !is_array($starterkit)) :
(empty($starterkit) || !is_array($starterkit))) {
$required = $schema['is_required'] ? 'is mandatory and ' : '';
throw new AcmsCliException($this->arrayfield . ' => ' . $key . ' field ' . $required . 'should be in array format, in ' . $this->starterKitName . ' starterkit.');
}
}

/**
* Validates string for starter-kit.
*/
public function validateString($schema, $starterkit, $key): void {
if ($schema['is_required'] ?
((!isset($starterkit) || empty($starterkit)) && !is_string($starterkit)) :
(empty($starterkit) || !is_string($starterkit))) {
$required = $schema['is_required'] ? 'is mandatory and ' : '';
throw new AcmsCliException($key . ' field ' . $required . 'should be in string format, in starterkit.');
}
}

/**
* Validates each key value for starter-kit.
*/
public function validateStarterKit(array $schema, $starterkit): array {
public function validateStarterKit(array $schema, $starterkit): void {
foreach ($schema as $key => $value) {
if (is_array($schema[$key]) && $schema[$key]['type'] === 'array') {
if ($schema[$key]['type'] == 'arrays' && isset($schema[$key]['next_level_validation'])) {
unset($schema[$key]['type']);
unset($schema[$key]['is_required']);
unset($schema[$key]['next_level_validation']);
$this->validateArray($schema[$key], $starterkit[$key], $key);
$this->validateStarterKit($schema[$key], $starterkit[$key]);
}
else {
switch ($schema[$key]['type']) {
case 'array':
if (isset($starterkit[$key]) && !is_array($starterkit[$key])) {
throw new AcmsCliException($key . ' should be in array format.');
}
case 'arrays':
$this->validateArray($schema[$key], $starterkit[$key], $key);
break;

case 'string':
if (isset($starterkit[$key]) && !is_string($starterkit[$key])) {
throw new AcmsCliException($key . ' field is mandatory and should be in string format.');
}
case 'strings':
$this->arrayfield = '';
$this->validateString($schema[$key], $starterkit[$key], $key);
break;

case 'default':
throw new AcmsCliException('starterkit is not defined properly kindly fix your acms.yml file.');
}
}
}

return $starterkit;
}

}
8 changes: 3 additions & 5 deletions tests/unit/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ public function testExecute() :void {
*/
public function testValidateStarterKit(): void {
$starterKits = $this->getAcmsFileContents()['starter_kits'];
$output = [];
foreach ($starterKits as $starterKit) {
$output = $this->starterKitValidation->validateStarterKitSchema($starterKit);
}
$this->assertIsArray($output);
$schema = $this->acquiaCli->getAcquiaCmsFile($this->projectDirectory . '/acms/starterkit.schema.yml');
$this->starterKitValidation->validateStarterKits($schema, $starterKits);
$this->assertIsArray($starterKits);
}

/**
Expand Down

0 comments on commit 3925721

Please sign in to comment.