-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #507 from martin-helmich/feature/const-support
Add support for "const"
- Loading branch information
Showing
7 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Constraints; | ||
|
||
use JsonSchema\ConstraintError; | ||
use JsonSchema\Entity\JsonPointer; | ||
|
||
/** | ||
* The ConstConstraint Constraints, validates an element against a constant value | ||
* | ||
* @author Martin Helmich <[email protected]> | ||
*/ | ||
class ConstConstraint extends Constraint | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) | ||
{ | ||
// Only validate const if the attribute exists | ||
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { | ||
return; | ||
} | ||
$const = $schema->const; | ||
|
||
$type = gettype($element); | ||
$constType = gettype($const); | ||
|
||
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $constType == 'object') { | ||
if ((object) $element == $const) { | ||
return; | ||
} | ||
} | ||
|
||
if ($type === gettype($const)) { | ||
if ($type == 'object') { | ||
if ($element == $const) { | ||
return; | ||
} | ||
} elseif ($element === $const) { | ||
return; | ||
} | ||
} | ||
|
||
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Tests\Constraints; | ||
|
||
class ConstTest extends BaseTestCase | ||
{ | ||
protected $schemaSpec = 'http://json-schema.org/draft-06/schema#'; | ||
protected $validateSchema = true; | ||
|
||
public function getInvalidTests() | ||
{ | ||
return array( | ||
array( | ||
'{"value":"foo"}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"string","const":"bar"} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
array( | ||
'{"value":5}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"integer","const":6} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
array( | ||
'{"value":false}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"boolean","const":true} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
); | ||
} | ||
|
||
public function getValidTests() | ||
{ | ||
return array( | ||
array( | ||
'{"value":"bar"}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"string","const":"bar"} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
array( | ||
'{"value":false}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"boolean","const":false} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
array( | ||
'{"value":true}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"boolean","const":true} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
array( | ||
'{"value":5}', | ||
'{ | ||
"type":"object", | ||
"properties":{ | ||
"value":{"type":"integer","const":5} | ||
}, | ||
"additionalProperties":false | ||
}' | ||
), | ||
); | ||
} | ||
} |