Skip to content

Commit 41ad8e5

Browse files
committed
Add tests and documentation for format validators
1 parent f1f7802 commit 41ad8e5

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"wol-soft/php-json-schema-model-generator-production": "dev-master",
14+
"wol-soft/php-json-schema-model-generator-production": "^0.16.0",
1515
"wol-soft/php-micro-template": "^1.3.2",
1616

1717
"php": ">=7.2",

docs/source/types/string.rst

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\String\\MinLengthE
7878
Pattern validation
7979
------------------
8080

81-
To add a pattern validation to the property use `pattern` keyword.
81+
To add a pattern validation to the property use the `pattern` keyword.
8282

8383
.. warning::
8484

@@ -115,4 +115,66 @@ The thrown exception will be a *PHPModelGenerator\\Exception\\String\\PatternExc
115115
Format
116116
------
117117

118-
String formats are currently not supported.
118+
To add a format validation to the property use the `format` keyword.
119+
120+
.. code-block:: json
121+
122+
{
123+
"$id": "example",
124+
"type": "object",
125+
"properties": {
126+
"example": {
127+
"type": "string",
128+
"format": "myFormat"
129+
}
130+
}
131+
}
132+
133+
Possible exceptions:
134+
135+
* Value for property must match the format __FORMAT__
136+
137+
The thrown exception will be a *PHPModelGenerator\\Exception\\String\\FormatException* which provides the following methods to get further error details:
138+
139+
.. code-block:: php
140+
141+
// get the expected format
142+
public function getExpectedFormat(): string
143+
// get the name of the property which failed
144+
public function getPropertyName(): string
145+
// get the value provided to the property
146+
public function getProvidedValue()
147+
148+
Builtin formats
149+
^^^^^^^^^^^^^^^
150+
151+
Currently no builtin formats are available.
152+
153+
Custom formats
154+
^^^^^^^^^^^^^^
155+
156+
You can implement custom format validators and use them in your schema files. You must add your custom format to the generator configuration to make them available.
157+
158+
.. code-block:: php
159+
160+
$generator = new Generator(
161+
(new GeneratorConfiguration())
162+
->addFormat(new MyCustomFormat())
163+
);
164+
165+
Your format validator must implement the interface **PHPModelGenerator\\Format\\FormatValidatorInterface**.
166+
167+
If your custom format is representable by a regular expression you can bypass implementing an own class and simply add a **FormatValidatorFromRegEx** (for example a string which must contain only numbers):
168+
169+
.. code-block:: php
170+
171+
$generator = new Generator(
172+
(new GeneratorConfiguration())
173+
->addFormat(new FormatValidatorFromRegEx('/^\d*$/'))
174+
);
175+
176+
.. hint::
177+
178+
Pull requests for common usable format validators are always welcome.
179+
A new format validator must be added in the *GeneratorConfiguration* method *initFormatValidator*.
180+
If the format validator requires a class implementation and can't be added via the *FormatValidatorFromRegEx* the class must be added to the *wol-soft/php-json-schema-model-generator-production* repository.

tests/Objects/StringPropertyTest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace PHPModelGenerator\Tests\Objects;
44

5+
use PHPModelGenerator\Exception\ErrorRegistryException;
56
use PHPModelGenerator\Exception\FileSystemException;
67
use PHPModelGenerator\Exception\RenderException;
78
use PHPModelGenerator\Exception\SchemaException;
9+
use PHPModelGenerator\Format\FormatValidatorFromRegEx;
810
use PHPModelGenerator\Model\GeneratorConfiguration;
911
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
1012
use stdClass;
@@ -249,11 +251,50 @@ public function invalidPatternProvider(): array
249251
);
250252
}
251253

252-
public function testStringFormatCheckThrowsNotSupportedException(): void
254+
public function testUnknownFormatThrowsAnException(): void
253255
{
254256
$this->expectException(SchemaException::class);
255-
$this->expectExceptionMessage('Format is currently not supported');
257+
$this->expectExceptionMessage('Unsupported format onyNumbers');
256258

257259
$this->generateClassFromFile('StringPropertyFormat.json');
258260
}
261+
262+
public function testStringFormatCheckIsValid(): void
263+
{
264+
$className = $this->generateClassFromFile(
265+
'StringPropertyFormat.json',
266+
(new GeneratorConfiguration())->addFormat('onyNumbers', new FormatValidatorFromRegEx('/^\d+$/'))
267+
);
268+
269+
$object = new $className(['property' => '12345']);
270+
$this->assertSame('12345', $object->getProperty());
271+
}
272+
273+
/**
274+
* @dataProvider invalidStringFormatDataProvider
275+
*
276+
* @param string $value
277+
*/
278+
public function testInvalidStringFormatCheck(string $value): void
279+
{
280+
$this->expectException(ErrorRegistryException::class);
281+
$this->expectExceptionMessage('Value for property must match the format onyNumbers');
282+
283+
$className = $this->generateClassFromFile(
284+
'StringPropertyFormat.json',
285+
(new GeneratorConfiguration())->addFormat('onyNumbers', new FormatValidatorFromRegEx('/^\d+$/'))
286+
);
287+
288+
new $className(['property' => $value]);
289+
}
290+
291+
public function invalidStringFormatDataProvider(): array
292+
{
293+
return [
294+
'empty string' => [''],
295+
'spaces' => [' '],
296+
'only non numeric chars' => ['abc'],
297+
'mixed string' => ['1234a'],
298+
];
299+
}
259300
}

tests/Schema/StringPropertyTest/StringPropertyFormat.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"properties": {
44
"property": {
55
"type": "string",
6-
"format": "email"
6+
"format": "onyNumbers"
77
}
88
}
99
}

0 commit comments

Comments
 (0)