diff --git a/composer.json b/composer.json index a6cf474..4b1cf45 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "php": "^8.1", "guzzlehttp/guzzle": "^7.8", - "phpstan/phpdoc-parser": "^1.33" + "phpstan/phpdoc-parser": "^1.33", + "opis/json-schema": "^2.3" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.59", diff --git a/composer.lock b/composer.lock index 0a940bf..5b6bea7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0e1de9afccdf23bb44b5d1e4b3d37f75", + "content-hash": "6f7422b6d3402672ae20118117fecc9b", "packages": [ { "name": "guzzlehttp/guzzle", @@ -331,6 +331,196 @@ ], "time": "2024-07-18T11:15:46+00:00" }, + { + "name": "opis/json-schema", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/opis/json-schema.git", + "reference": "c48df6d7089a45f01e1c82432348f2d5976f9bfb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/opis/json-schema/zipball/c48df6d7089a45f01e1c82432348f2d5976f9bfb", + "reference": "c48df6d7089a45f01e1c82432348f2d5976f9bfb", + "shasum": "" + }, + "require": { + "ext-json": "*", + "opis/string": "^2.0", + "opis/uri": "^1.0", + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "ext-bcmath": "*", + "ext-intl": "*", + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Opis\\JsonSchema\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Sorin Sarca", + "email": "sarca_sorin@hotmail.com" + }, + { + "name": "Marius Sarca", + "email": "marius.sarca@gmail.com" + } + ], + "description": "Json Schema Validator for PHP", + "homepage": "https://opis.io/json-schema", + "keywords": [ + "json", + "json-schema", + "schema", + "validation", + "validator" + ], + "support": { + "issues": "https://github.com/opis/json-schema/issues", + "source": "https://github.com/opis/json-schema/tree/2.3.0" + }, + "time": "2022-01-08T20:38:03+00:00" + }, + { + "name": "opis/string", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/opis/string.git", + "reference": "9ebf1a1f873f502f6859d11210b25a4bf5d141e7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/opis/string/zipball/9ebf1a1f873f502f6859d11210b25a4bf5d141e7", + "reference": "9ebf1a1f873f502f6859d11210b25a4bf5d141e7", + "shasum": "" + }, + "require": { + "ext-iconv": "*", + "ext-json": "*", + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Opis\\String\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Marius Sarca", + "email": "marius.sarca@gmail.com" + }, + { + "name": "Sorin Sarca", + "email": "sarca_sorin@hotmail.com" + } + ], + "description": "Multibyte strings as objects", + "homepage": "https://opis.io/string", + "keywords": [ + "multi-byte", + "opis", + "string", + "string manipulation", + "utf-8" + ], + "support": { + "issues": "https://github.com/opis/string/issues", + "source": "https://github.com/opis/string/tree/2.0.1" + }, + "time": "2022-01-14T15:42:23+00:00" + }, + { + "name": "opis/uri", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/opis/uri.git", + "reference": "0f3ca49ab1a5e4a6681c286e0b2cc081b93a7d5a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/opis/uri/zipball/0f3ca49ab1a5e4a6681c286e0b2cc081b93a7d5a", + "reference": "0f3ca49ab1a5e4a6681c286e0b2cc081b93a7d5a", + "shasum": "" + }, + "require": { + "opis/string": "^2.0", + "php": "^7.4 || ^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Opis\\Uri\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Marius Sarca", + "email": "marius.sarca@gmail.com" + }, + { + "name": "Sorin Sarca", + "email": "sarca_sorin@hotmail.com" + } + ], + "description": "Build, parse and validate URIs and URI-templates", + "homepage": "https://opis.io", + "keywords": [ + "URI Template", + "parse url", + "punycode", + "uri", + "uri components", + "url", + "validate uri" + ], + "support": { + "issues": "https://github.com/opis/uri/issues", + "source": "https://github.com/opis/uri/tree/1.1.0" + }, + "time": "2021-05-22T15:57:08+00:00" + }, { "name": "phpstan/phpdoc-parser", "version": "1.33.0", diff --git a/tests/api/ListingTest.php b/tests/api/ListingTest.php index 598cffa..e24b1d1 100644 --- a/tests/api/ListingTest.php +++ b/tests/api/ListingTest.php @@ -14,6 +14,10 @@ use CloudForest\ApiClientPhp\Schema\Enum\CompartmentTypeEnum; use CloudForest\ApiClientPhp\Schema\Enum\GeojsonGeometryTypeEnum; use CloudForest\ApiClientPhp\Schema\Enum\SubcompartmentTypeEnum; +use CloudForest\ApiClientPhp\Scripts\JsonSchema; +use Opis\JsonSchema\Validator; +use Opis\JsonSchema\Errors\ErrorFormatter; + final class ListingTest extends TestBase { @@ -36,7 +40,7 @@ public function testCreate(): void // Inventory 2, create a centroid Geojson $centroid = new GeojsonSchema(GeojsonGeometryTypeEnum::Point); - $centroid->geometry->coordinates = [-1.05, 53.25]; + $centroid->geometry->coordinates = [[-1.05, 53.25]]; // Inventory 3, create a compartment $compartment = new CompartmentSchema('2'); @@ -80,6 +84,24 @@ public function testCreate(): void $listing->description = 'This is a test Listing from PHP Unit'; $listing->inventory = [$compartment]; + // Verify the listing's inventory can be validated against our schema + // This also tests the schema generation as well as ensuring the DTO + // produices valid JSON. The JSON encode then decode casts our schema + // (assoc array) and compartment data (classes) to a stdClass object for + // the validator. + $generator = new JsonSchema(); + $schemaObj = json_decode(json_encode($generator->generate('CompartmentSchema'))); + $compartmentObj = json_decode(json_encode($compartment)); + $validator = new Validator(); + $result = $validator->validate($compartmentObj, $schemaObj); + if ($result->isValid()) { + $valid = true; + } else { + $valid = false; + print_r((new ErrorFormatter())->format($result->error())); + } + $this->assertEquals(true, $valid); + // Use the API to create the listing in CloudForest $listingUuid = $api->listing->create($listing); $this->assertIsString($listingUuid);