-
Notifications
You must be signed in to change notification settings - Fork 362
Add if then else schema validation #715
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -9,7 +9,11 @@ class ConstraintError extends Enum | |
const ADDITIONAL_ITEMS = 'additionalItems'; | ||
const ADDITIONAL_PROPERTIES = 'additionalProp'; | ||
const ALL_OF = 'allOf'; | ||
const ALWAYS_FAILS = 'alwaysFails'; | ||
const ANY_OF = 'anyOf'; | ||
const CONDITIONAL_IF = 'if'; | ||
const CONDITIONAL_THEN = 'then'; | ||
const CONDITIONAL_ELSE = 'else'; | ||
Comment on lines
+12
to
+16
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. Constants are public. The visibility has been added in the library. It should be added in this fix as-well. |
||
const DEPENDENCIES = 'dependencies'; | ||
const DISALLOW = 'disallow'; | ||
const DIVISIBLE_BY = 'divisibleBy'; | ||
|
@@ -59,12 +63,16 @@ public function getMessage() | |
self::ADDITIONAL_ITEMS => 'The item %s[%s] is not defined and the definition does not allow additional items', | ||
self::ADDITIONAL_PROPERTIES => 'The property %s is not defined and the definition does not allow additional properties', | ||
self::ALL_OF => 'Failed to match all schemas', | ||
self::ALWAYS_FAILS => 'Schema always fails validation', | ||
self::ANY_OF => 'Failed to match at least one schema', | ||
self::DEPENDENCIES => '%s depends on %s, which is missing', | ||
self::DISALLOW => 'Disallowed value was matched', | ||
self::DIVISIBLE_BY => 'Is not divisible by %d', | ||
self::ENUM => 'Does not have a value in the enumeration %s', | ||
self::CONSTANT => 'Does not have a value equal to %s', | ||
self::CONDITIONAL_IF => 'The keyword "if" must be a boolean or an object', | ||
self::CONDITIONAL_THEN => 'The keyword "then" must be a boolean or an object', | ||
self::CONDITIONAL_ELSE => 'The keyword "else" must be a boolean or an object', | ||
self::EXCLUSIVE_MINIMUM => 'Must have a minimum value greater than %d', | ||
self::EXCLUSIVE_MAXIMUM => 'Must have a maximum value less than %d', | ||
self::FORMAT_COLOR => 'Invalid color', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,6 +173,11 @@ protected function &getProperty(&$element, $property, $fallback = null) | |
*/ | ||
protected function validateMinMaxConstraint($element, $objectDefinition, JsonPointer $path = null) | ||
{ | ||
// minProperties and maxProperties constraints only applies on objects elements. | ||
if (!is_object($element)) { | ||
return; | ||
} | ||
|
||
Comment on lines
+176
to
+180
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. This is not necessary anymore. The library already has this validation. |
||
// Verify minimum number of properties | ||
if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) { | ||
if ($this->getTypeCheck()->propertyCount($element) < $objectDefinition->minProperties) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?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 IfThenElseTest extends BaseTestCase | ||
{ | ||
protected $validateSchema = true; | ||
|
||
public function getInvalidTests() | ||
{ | ||
return array( | ||
// If "foo" === "bar", then "bar" must be defined, else Validation Failed. | ||
// But "foo" === "bar" and "bar" is not defined. | ||
array( | ||
'{ | ||
"foo":"bar" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": { | ||
"properties": {"foo": {"enum": ["bar"]}}, | ||
"required": ["foo"] | ||
}, | ||
"then": {"required": ["bar"]}, | ||
"else": false | ||
}' | ||
), | ||
// If "foo" === "bar", then "bar" must be defined, else Validation Failed. | ||
// But "foo" !== "bar". | ||
array( | ||
'{ | ||
"foo":"baz" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": { | ||
"properties": {"foo": {"enum": ["bar"]}}, | ||
"required": ["foo"] | ||
}, | ||
"then": {"required": ["bar"]}, | ||
"else": false | ||
}' | ||
), | ||
// If "foo" === "bar", then "bar" must === "baz", else Validation Failed. | ||
// But "foo" === "bar" and "bar" !== "baz". | ||
array( | ||
'{ | ||
"foo":"bar", | ||
"bar":"potato" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": { | ||
"properties": {"foo": {"enum": ["bar"]}}, | ||
"required": ["foo"] | ||
}, | ||
"then": { | ||
"properties": {"bar": {"enum": ["baz"]}}, | ||
"required": ["bar"] | ||
}, | ||
"else": false | ||
}' | ||
), | ||
// Always go to "else". | ||
// But schema is invalid. | ||
array( | ||
'{ | ||
"foo":"bar" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": false, | ||
"then": true, | ||
"else": { | ||
"properties": {"bar": {"enum": ["baz"]}}, | ||
"required": ["bar"] | ||
} | ||
}' | ||
), | ||
// Always go to "then". | ||
// But schema is invalid. | ||
array( | ||
'{ | ||
"foo":"bar" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": true, | ||
"then": { | ||
"properties": {"bar": {"enum": ["baz"]}}, | ||
"required": ["bar"] | ||
}, | ||
"else": true | ||
}' | ||
) | ||
); | ||
} | ||
|
||
public function getValidTests() | ||
{ | ||
return array( | ||
// Always validate. | ||
array( | ||
'{ | ||
"foo":"bar" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": true, | ||
"then": true, | ||
"else": false | ||
}' | ||
), | ||
// Always validate schema in then. | ||
array( | ||
'{ | ||
"foo":"bar" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": true, | ||
"then": { | ||
"properties": {"foo": {"enum": ["bar"]}}, | ||
"required": ["foo"] | ||
}, | ||
"else": false | ||
}' | ||
), | ||
// Always validate schema in else. | ||
array( | ||
'{ | ||
"foo":"bar" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": false, | ||
"then": false, | ||
"else": { | ||
"properties": {"foo": {"enum": ["bar"]}}, | ||
"required": ["foo"] | ||
} | ||
}' | ||
), | ||
// "If" is evaluated to true, so "then" is to validate. | ||
array( | ||
'{ | ||
"foo":"bar", | ||
"bar":"baz" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": { | ||
"properties": {"foo": {"enum": ["bar"]}}, | ||
"required": ["foo"] | ||
}, | ||
"then": { | ||
"properties": {"bar": {"enum": ["baz"]}}, | ||
"required": ["bar"] | ||
}, | ||
"else": false | ||
}' | ||
), | ||
// "If" is evaluated to false, so "else" is to validate. | ||
array( | ||
'{ | ||
"foo":"bar", | ||
"bar":"baz" | ||
}', | ||
'{ | ||
"type": "object", | ||
"properties": { | ||
"foo": {"type": "string"}, | ||
"bar": {"type": "string"} | ||
}, | ||
"if": { | ||
"properties": {"foo": {"enum": ["potato"]}}, | ||
"required": ["foo"] | ||
}, | ||
"then": false, | ||
"else": { | ||
"properties": {"bar": {"enum": ["baz"]}}, | ||
"required": ["bar"] | ||
} | ||
}' | ||
), | ||
); | ||
} | ||
} |
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.
Those changes are not needed since it has been done in the master branch