-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for "const" #507
Changes from all commits
9db6594
6d04026
789c97e
1b94558
8c3ee91
853eb09
8a9cd12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,48 @@ public function dataCoerceCases() | |
'string', 'integer', 42, true | ||
); | ||
|
||
$tests = array(); | ||
// #46 check coercion with "const" | ||
$tests[] = array( | ||
'{"properties":{"propertyOne":{"type":"string","const":"42"}}}', | ||
'{"propertyOne":42}', | ||
'integer', 'string', '42', true | ||
); | ||
|
||
// #47 check coercion with "const" | ||
$tests[] = array( | ||
'{"properties":{"propertyOne":{"type":"number","const":42}}}', | ||
'{"propertyOne":"42"}', | ||
'string', 'integer', 42, true | ||
); | ||
|
||
// #48 check boolean coercion with "const" | ||
$tests[] = array( | ||
'{"properties":{"propertyOne":{"type":"boolean","const":false}}}', | ||
'{"propertyOne":"false"}', | ||
'string', 'boolean', false, true | ||
); | ||
|
||
// #49 check boolean coercion with "const" | ||
$tests[] = array( | ||
'{"properties":{"propertyOne":{"type":"boolean","const":true}}}', | ||
'{"propertyOne":"true"}', | ||
'string', 'boolean', true, true | ||
); | ||
|
||
// #50 check boolean coercion with "const" | ||
$tests[] = array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was hoping to see a false... ("true" == true) // true
("false" == true) // true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you go... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beautiful, thank you |
||
'{"properties":{"propertyOne":{"type":"boolean","const":true}}}', | ||
'{"propertyOne":1}', | ||
'integer', 'boolean', true, true | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add some deep object tests. With and without mismatched property types. |
||
|
||
// #51 check boolean coercion with "const" | ||
$tests[] = array( | ||
'{"properties":{"propertyOne":{"type":"boolean","const":false}}}', | ||
'{"propertyOne":"false"}', | ||
'string', 'boolean', false, true | ||
); | ||
|
||
foreach ($types as $toType => $testCases) { | ||
foreach ($testCases as $testCase) { | ||
$tests[] = array( | ||
|
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"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little curious about the potential use cases covered by this implementation. Ten minutes ago I had never heard of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not find any mention of the
My use case for
In this case, documents like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Okay. 👍 |
||
}, | ||
"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 | ||
}' | ||
), | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not correctly implement deep comparison of objects - it results in members being compared using
==
.Could you please change this implementation to ensure type-strict comparison of object properties. You will probably need to iterate them; PHP lacks a suitable native comparison operator that can do it all in one go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following that argumentation, the
enum
implementation (which was my inspiration for this class) should probably be changed as well -- it uses the same comparison.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If enum is similarly broken, then yes - I agree with you that it needs fixing.