Skip to content

Commit

Permalink
Constructed JSON Blueprint schemas
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <tom@inflatablecookie.com>
  • Loading branch information
betterthanclay committed Jul 24, 2024
1 parent 29b16e1 commit cebd124
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## v0.1.0 (2024-07-24)
* Built initial Blueprint implementation
* Added Blueprint Factory
* Constructed JSON Blueprint schemas
13 changes: 11 additions & 2 deletions examples/artist-profile-setup.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://schema.decodelabs.com/chronos/0.1/program",
"-$schema": "https://schema.decodelabs.com/chronos/0.1/program.json",
"$schema": "../schema/0.1/program.json",
"id": "artist-profile-setup",
"name": "Artist Profile Setup",
"description": "Get your artist profile in check - get your biography written, photos uploaded and best foot forward.",
Expand Down Expand Up @@ -42,7 +43,15 @@
"priority": "low",
"duration": "1 hour",
"actions": {
"Profile.UploadPhotos": { }
"Profile.UploadPhotos": {
"id": "hi hi",
"count": 5,
"test": {
"Test.Jam": {
"hello": "world"
}
}
}
}
} ]
}
24 changes: 24 additions & 0 deletions schema/0.1/actions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://schema.decodelabs.com/chronos/0.1/actions.json",
"$anchor": "actions",
"type": "object",
"description": "A list of functional actions that must be completed to finish the step.",
"patternProperties": {
"^(([A-Z][a-zA-Z0-9]+)\\.)?([A-Z][a-zA-Z0-9]+)(\\:\\$?([a-zA-Z0-9]+))?$": {
"type": "object",
"description": "A functional action that must be completed to finish the step.",
"patternProperties": {
"^([a-zA-Z0-9]+)$": {
"anyOf": [
{ "type": [ "string", "number" ] },
{ "type": "array", "items": { "type": "string" } },
{ "$ref": "#actions" }
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
58 changes: 58 additions & 0 deletions schema/0.1/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://schema.decodelabs.com/chronos/0.1/program.json",
"type": "object",
"properties": {
"$schema": {
"type": "string",
"description": "The schema that this document should conform to.",
"enum": [
"https://schema.decodelabs.com/chronos/0.1/program.json",
"../schema/0.1/program.json"
]
},
"id": {
"$ref": "shared.json#id",
"description": "The unique identifier for this program."
},
"name": {
"type": "string",
"description": "The name of the program."
},
"description": {
"type": "string",
"description": "A short description of what the program does."
},
"categories": {
"type": "array",
"description": "A list of IDs of categories that this program belongs to.",
"items": {
"$ref": "shared.json#id",
"description": "The unique identifier for a category."
}
},
"duration": {
"type": "string",
"description": "A string representation of the estimated time it will take to complete the program."
},
"priority": {
"$ref": "shared.json#priority",
"description": "A string representation of the priority of the program."
},
"steps": {
"type": "array",
"items": {
"$ref": "step.json"
}
}
},
"required": [
"id",
"name",
"description",
"categories",
"duration",
"priority",
"steps"
]
}
27 changes: 27 additions & 0 deletions schema/0.1/shared.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://schema.decodelabs.com/chronos/0.1/shared.json",
"type": "object",
"properties": {
"id": {
"$anchor": "id",
"type": "string",
"description": "The unique identifier for this item.",
"pattern": "^[a-z0-9-_]+$",
"minLength": 5,
"maxLength": 64
},
"priority": {
"$anchor": "priority",
"type": "string",
"description": "A string representation of the priority of this item.",
"default": "medium",
"enum": [
"low", "Low",
"medium", "Medium",
"high", "High",
"critical", "Critical"
]
}
}
}
43 changes: 43 additions & 0 deletions schema/0.1/step.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://schema.decodelabs.com/chronos/0.1/step.json",
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "The unique identifier for this step."
},
"name": {
"type": "string",
"description": "The readable name of the step."
},
"description": {
"type": "string",
"description": "A short description of what the step does."
},
"priority": {
"$ref": "shared.json#priority",
"description": "A string representation of the priority of the step."
},
"duration": {
"type": "string",
"description": "A string representation of the estimated time it will take to complete the step."
},
"await": {
"type": "object",
"description": "A list of actions and optional time offsets that must be completed before this step can be started.",
"patternProperties": {
"^[a-z0-9-_]+$": {
"type": [ "string", "null" ]
}
}
},
"actions": {
"$ref": "actions.json"
}
},
"required": [
"id",
"actions"
]
}
12 changes: 8 additions & 4 deletions src/Blueprint/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,19 @@ protected function getSchemaClass(
);
}

if (!str_starts_with($schema, self::BASE_URL)) {
if (
(
!str_starts_with($schema, self::BASE_URL) &&
!preg_match('/^(\.\.)?\//', $schema)
) ||
!preg_match('/\/[0-9\.]{1,4}\/([a-z0-9-]+)+\.json$/', $schema, $matches)
) {
throw Exceptional::UnexpectedValue(
'Unknown blueprint schema: ' . $schema
);
}

$name = substr($schema, strlen(self::BASE_URL));
$parts = explode('/', $name, 2);
$name = $parts[1] ?? '';
$name = $matches[1];

if (!isset(self::SCHEMAS[$name])) {
throw Exceptional::UnexpectedValue(
Expand Down
12 changes: 11 additions & 1 deletion src/Blueprint/WithIdentityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
$name = Dictum::name($id);
}

$this->id = $id;
$this->setId($id);
$this->name = $name;
$this->description = $description;
}
Expand All @@ -47,6 +47,16 @@ public function __construct(
public function setId(
string $id
): void {
if (
!preg_match('/^[a-z0-9-_]+$/', $id) ||
strlen($id) < 5 ||
strlen($id) > 64
) {
throw Exceptional::InvalidArgument(
'Invalid blueprint ID: ' . $id
);
}

$this->id = $id;
}

Expand Down

0 comments on commit cebd124

Please sign in to comment.