-
Notifications
You must be signed in to change notification settings - Fork 354
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
Oneof/AnyOf/AllOf applied to the properties of an object #393
Comments
Investigating this. For anyone else following, cleaned up schemas from the original post: {
"type": "object",
"properties": {
"response": {
"type": "object",
"oneOf": [
{
"required": [
"error"
]
},
{
"required": [
"result"
]
}
],
"properties": {
"error": {
"type": "object"
},
"result": {
"type": "object"
}
}
}
},
"required": [
"response"
]
} {
"type": "object",
"properties": {
"response": {
"type": "object",
"oneOf": [
{
"properties": {
"error": {
"type": "object"
}
}
},
{
"properties": {
"result": {
"type": "object"
}
}
}
]
}
},
"required": [
"response"
]
} |
This one isn't working either: <?php
use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;
$validator = new Validator;
$data = file_get_contents('./test.json');
$data = json_decode($data, true);
$validator->validate(
$data,
json_decode(json_encode([
'type' => 'object',
'properties' => [
'response' => [
'oneOf' => [
['type' => 'object', 'properties' => [
'error' => [
'type' => 'object'
],
'required' => ['error']
]],
['type' => 'object', 'properties' => [
'result' => [
'type' => 'object'
],
'required' => ['result']
]]
]
]
],
'required' => ['response']
])),
Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_EXCEPTIONS
);
var_dump($validator->isValid()); |
@CMCDragonkai Your first example works fine: <?php
require('vendor/autoload.php');
use JsonSchema\Validator;
$v = new Validator();
$schema_a = json_decode(file_get_contents('schema-a.json'));
$data_a=json_decode('{"response":{"result":{}}}');
$v->validate($data_a, $schema_a);
assert($v->isValid()); //true |
Maybe something's going wrong with the json encode and decode that I'm using to avoid typing out |
@CMCDragonkai Your second example works fine too - it fails validation, as it should, because your input data matches both schemas in Your third example has an invalid schema - properties must be objects, but you have defined one named "required" as an array. Could you please explain exactly what your issue is? Because from where I'm sitting, this doesn't look like a problem with the library, or with You may wish to check out http://www.jsonschemavalidator.net/ to assist with writing a valid schema that fulfils your requirements. |
Try this (this throws an error): <?php
use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;
$validator = new Validator;
$data = <<<'JSON'
{
"response": {
"result": {}
}
}
JSON;
$data = json_decode($data, true);
$validator->validate(
$data,
(object)[
'type' => 'object',
'properties' => (object)[
'response' => (object)[
'type' => 'object',
'oneOf' => [
(object)['required' => ['error']],
(object)['required' => ['result']]
],
'properties' => (object)[
'error' => (object)[
'type' => 'object'
],
'result' => (object)[
'type' => 'object'
]
]
]
],
'required' => ['response']
],
Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_EXCEPTIONS
);
var_dump($validator->isValid()); Your examples doesn't apply the constraints that I'm using. |
It's possible that because of |
I'm on 5.1 version as well, and my most recent example really doesn't work. Even if I remove the |
My third example has
None of the properties there are typed as The third example was based on your README showing this:
See |
I found the problem. It's because of |
Compare: <?php
//SUCCEEDS
use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;
$validator = new Validator;
$data = <<<'JSON'
{
"response": {
"result": {
}
}
}
JSON;
$schema = <<<'JSON'
{
"type": "object",
"properties": {
"response": {
"type": "object",
"oneOf": [
{
"required": [
"error"
]
},
{
"required": [
"result"
]
}
],
"properties": {
"error": {
"type": "object"
},
"result": {
"type": "object"
}
}
}
},
"required": [
"response"
]
}
JSON;
$data = json_decode($data);
$schema = json_decode($schema);
var_dump($data);
var_dump($schema);
$validator->validate(
$data,
$schema
);
var_dump($validator->isValid()); <?php
//FAILS
use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;
$validator = new Validator;
$data = <<<'JSON'
{
"response": {
"result": {
}
}
}
JSON;
$schema = <<<'JSON'
{
"type": "object",
"properties": {
"response": {
"type": "object",
"oneOf": [
{
"required": [
"error"
]
},
{
"required": [
"result"
]
}
],
"properties": {
"error": {
"type": "object"
},
"result": {
"type": "object"
}
}
}
},
"required": [
"response"
]
}
JSON;
$data = json_decode($data);
$schema = json_decode($schema);
var_dump($data);
var_dump($schema);
$validator->validate(
$data,
$schema,
Constraint::CHECK_MODE_EXCEPTIONS
);
var_dump($validator->isValid()); |
try: $data = json_decode($data, false); |
@shmax Please see my most recent example. It shows that the problem is with exception constraint, not about json decoding. |
Thanks - I can confirm there is a problem with Regarding the other issues you're having - you are definitely making mistakes with some of your schemas. I strongly recommend you take advantage of http://www.jsonschemavalidator.net/; it will help you ensure that your schemas are valid.
Yes, you have done that. You have |
@erayd I'm sorry but I just have my Spacemacs telling me I do not have Thank you for the fix. |
Look at I have now pointed this out three times. Look at your code. I know there is a bug with |
@erayd I see it. I thought you were referring to the first "required" property, not the inner set. |
The |
* Add URI translation for retrieval & add local copies of spec schema * Add use line for InvalidArgumentException & adjust scope (#372) Fixes issue #371 * add quiet option (#382) * add quiet option * use verbose instead of quiet * add quiet option * always output dump-schema * always output dump-schema-url * fix typo and ws * [BUGFIX] Add provided schema under a dummy / internal URI (fixes #376) (#378) * Add provided schema under a dummy / internal URI (fixes #376) In order to resolve internal $ref references within a user-provided schema, SchemaStorage needs to know about the schema. As user-supplied schemas do not have an associated URI, use a dummy / internal one instead. * Remove dangling use * Change URI to class constant on SchemaStorage * Add option to disable validation of "format" constraint (#383) * Add more unit tests (#366) * Add test coverage for coercion API * Complete test coverage for SchemaStorage * Add test coverage for ObjectIterator * Add exception test for JsonPointer * MabeEnum\Enum appears to use singletons - add testing const * Don't check this line for coverage mbstring is on all test platforms, so this line will never be reached. * Add test for TypeConstraint::validateTypeNameWording() * Add test for exception on TypeConstraint::validateType() * PHPunit doesn't like an explanation with its @codeCoverageIgnore... * Add various tests for UriRetriever * Add tests for FileGetContents * Add tests for JsonSchema\Uri\Retrievers\Curl * Add missing bad-syntax test file * Restrict ignore to the exception line only * Fix exception scope * Allow the schema to be an associative array (#389) * Allow the schema to be an associative array Implements #388. * Use json_decode(json_encode()) for array -> object cast * Skip exception check on PHP versions < 5.5.0 * Skip test on HHVM, as it's happy to encode resources * Enable FILTER_FLAG_EMAIL_UNICODE for email format if present (#398) * Don't throw exceptions until after checking anyOf / oneOf (#394) Fixes #393 * Fix infinite recursion on some schemas when setting defaults (#359) (#365) * Don't try to fetch files that don't exist Throws an exception when the ref can't be resolved to a useful file URI, rather than waiting for something further down the line to fail after the fact. * Refactor defaults code to use LooseTypeCheck where appropriate * Test for not treating non-containers like arrays * Update comments * Rename variable for clarity * Add CHECK_MODE_ONLY_REQUIRED_DEFAULTS If CHECK_MODE_ONLY_REQUIRED_DEFAULTS is set, then only apply defaults if they are marked as required. * Workaround for $this scope issue on PHP-5.3 * Fix infinite recursion via $ref when applying defaults * Add missing second test for array case * Add test for setting a default value for null * Also fix infinite recursion via $ref for array defaults * Move nested closure into separate method * $parentSchema will always be set when $name is, so don't check it * Handle nulls properly - fixes issue #377 * Add option to also validate the schema (#357) * Remove stale files from #357 (obviated by #362) (#400) * Stop #386 sneaking in alongside another PR backport
I have tried these schemas based on these answers:
But I can't get them to work:
The data is like:
The text was updated successfully, but these errors were encountered: