Skip to content

Commit 9a3d12a

Browse files
Implement ConstConstraint
1 parent 9db6594 commit 9a3d12a

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

src/JsonSchema/ConstraintError.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ConstraintError extends Enum
1414
const DISALLOW = 'disallow';
1515
const DIVISIBLE_BY = 'divisibleBy';
1616
const ENUM = 'enum';
17+
const CONSTANT = 'const';
1718
const EXCLUSIVE_MINIMUM = 'exclusiveMinimum';
1819
const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum';
1920
const FORMAT_COLOR = 'colorFormat';
@@ -63,6 +64,7 @@ public function getMessage()
6364
self::DISALLOW => 'Disallowed value was matched',
6465
self::DIVISIBLE_BY => 'Is not divisible by %d',
6566
self::ENUM => 'Does not have a value in the enumeration %s',
67+
self::CONSTANT => 'Does not have a value equal to %s',
6668
self::EXCLUSIVE_MINIMUM => 'Must have a minimum value greater than %d',
6769
self::EXCLUSIVE_MAXIMUM => 'Must have a maximum value less than %d',
6870
self::FORMAT_COLOR => 'Invalid color',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Constraints;
11+
12+
use JsonSchema\ConstraintError;
13+
use JsonSchema\Entity\JsonPointer;
14+
15+
/**
16+
* The ConstConstraint Constraints, validates an element against a constant value
17+
*
18+
* @author Martin Helmich <martin@helmich.me>
19+
*/
20+
class ConstConstraint extends Constraint
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
26+
{
27+
// Only validate enum if the attribute exists
28+
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
29+
return;
30+
}
31+
$const = $schema->const;
32+
33+
$type = gettype($element);
34+
$constType = gettype($const);
35+
36+
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $constType == 'object') {
37+
if ((object) $element == $const) {
38+
return;
39+
}
40+
}
41+
42+
if ($type === gettype($const)) {
43+
if ($type == 'object') {
44+
if ($element == $const) {
45+
return;
46+
}
47+
} elseif ($element === $const) {
48+
return;
49+
}
50+
}
51+
52+
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const));
53+
}
54+
}

src/JsonSchema/Constraints/Constraint.php

+16
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
169169
$this->addErrors($validator->getErrors());
170170
}
171171

172+
/**
173+
* Checks a const element
174+
*
175+
* @param mixed $value
176+
* @param mixed $schema
177+
* @param JsonPointer|null $path
178+
* @param mixed $i
179+
*/
180+
protected function checkConst($value, $schema = null, JsonPointer $path = null, $i = null)
181+
{
182+
$validator = $this->factory->createInstanceFor('const');
183+
$validator->check($value, $schema, $path, $i);
184+
185+
$this->addErrors($validator->getErrors());
186+
}
187+
172188
/**
173189
* Checks format of an element
174190
*

src/JsonSchema/Constraints/Factory.php

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Factory
5858
'string' => 'JsonSchema\Constraints\StringConstraint',
5959
'number' => 'JsonSchema\Constraints\NumberConstraint',
6060
'enum' => 'JsonSchema\Constraints\EnumConstraint',
61+
'const' => 'JsonSchema\Constraints\ConstConstraint',
6162
'format' => 'JsonSchema\Constraints\FormatConstraint',
6263
'schema' => 'JsonSchema\Constraints\SchemaConstraint',
6364
'validator' => 'JsonSchema\Validator'

src/JsonSchema/Constraints/UndefinedConstraint.php

+5
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function validateTypes(&$value, $schema = null, JsonPointer $path, $i = n
9696
if (isset($schema->enum)) {
9797
$this->checkEnum($value, $schema, $path, $i);
9898
}
99+
100+
// check const
101+
if (isset($schema->const)) {
102+
$this->checkConst($value, $schema, $path, $i);
103+
}
99104
}
100105

101106
/**

0 commit comments

Comments
 (0)