Skip to content

Commit 551a190

Browse files
committed
feat: more support for draft-06
1 parent 376709c commit 551a190

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
5656
$this->checkForKeyword('const', $value, $schema, $path, $i);
5757
$this->checkForKeyword('multipleOf', $value, $schema, $path, $i);
5858
$this->checkForKeyword('format', $value, $schema, $path, $i);
59+
$this->checkForKeyword('pattern', $value, $schema, $path, $i);
5960
}
6061

6162
/**

src/JsonSchema/Constraints/Drafts/Draft06/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Factory extends \JsonSchema\Constraints\Factory
3838
'contains' => ContainsConstraint::class,
3939
'propertyNames' => PropertiesNamesConstraint::class,
4040
'patternProperties' => PatternPropertiesConstraint::class,
41+
'pattern' => PatternConstraint::class,
4142
'properties' => PropertiesConstraint::class,
4243
'items' => ItemsConstraint::class,
4344
];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JsonSchema\Constraints\Drafts\Draft06;
6+
7+
use JsonSchema\ConstraintError;
8+
use JsonSchema\Constraints\ConstraintInterface;
9+
use JsonSchema\Entity\ErrorBagProxy;
10+
use JsonSchema\Entity\JsonPointer;
11+
12+
class PatternConstraint implements ConstraintInterface
13+
{
14+
use ErrorBagProxy;
15+
16+
/** @var Factory */
17+
private $factory;
18+
19+
public function __construct(?Factory $factory = null)
20+
{
21+
$this->factory = $factory ?: new Factory();
22+
$this->initialiseErrorBag($this->factory);
23+
}
24+
25+
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
26+
{
27+
if (!property_exists($schema, 'pattern')) {
28+
return;
29+
}
30+
31+
if (!is_string($value)) {
32+
return;
33+
}
34+
35+
if (preg_match('/' . str_replace('/', '\/', $schema->pattern) . '/', $value) === 1) {
36+
return;
37+
}
38+
39+
$this->addError(ConstraintError::PATTERN(), $path, ['found' => $value, 'pattern' => $schema->pattern]);
40+
}
41+
}

0 commit comments

Comments
 (0)