Skip to content
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

Validation of JSON-Schema #353

Closed
o5 opened this issue Jan 20, 2017 · 11 comments
Closed

Validation of JSON-Schema #353

o5 opened this issue Jan 20, 2017 · 11 comments

Comments

@o5
Copy link

o5 commented Jan 20, 2017

Am I missing something or there is not validation of schema itself? I don't know if it is possible, but it would be nice to throw something like json-schema parse error.

For json-schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
      "id": {
        "type": "number"
      },
      "type": "object"
    }
}

I've expected similar error message as I got from jsonschemavalidator.net:

Unexpected token encountered when reading value for 'type'. Expected StartObject, got String. Path 'properties.type', line 7, position 22.
@shmax
Copy link
Collaborator

shmax commented Jan 29, 2017

I don't think the library has that built-in, but if you want to set that up in your own code you might have a look at the files in /tests/fixtures for some ideas; they appear to be validation schemas for the different draft specs.

erayd added a commit to erayd/json-schema that referenced this issue Feb 22, 2017
erayd added a commit to erayd/json-schema that referenced this issue Feb 22, 2017
erayd added a commit to erayd/json-schema that referenced this issue Feb 22, 2017
@mathroc
Copy link
Contributor

mathroc commented Feb 23, 2017

@o5 you can validate it yourself using the validator and the json-schema schema

$validator->validate(json_decode($schema), (object)['$ref' => 'http://json-schema.org/draft-04/schema']);

erayd added a commit to erayd/json-schema that referenced this issue Feb 23, 2017
erayd added a commit to erayd/json-schema that referenced this issue Feb 23, 2017
erayd added a commit to erayd/json-schema that referenced this issue Mar 1, 2017
@o5
Copy link
Author

o5 commented Apr 10, 2017

Hello @mathroc,

I'm sorry for my late response. I'm trying to use validate feature:

$schema = json_decode('{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"properties": {
		"id": {
			"type": "number"
		},
		"type": "object"
	}
}');

$validator = new Validator();
$validator->validate(
    $schema,
    (object) ['$ref' => 'http://json-schema.org/draft-04/schema#'],
    Constraint::CHECK_MODE_VALIDATE_SCHEMA
);

I'm expecting some error like Error parsing schema, but none given. Am I missing something?

@erayd
Copy link
Contributor

erayd commented Apr 10, 2017

I'm expecting some error like Error parsing schema, but none given. Am I missing something?

You're asking it to validate the schema, and the schema is valid (it's the official draft-04 meta-schema). That isn't an error condition.

$schema is not a valid schema, but you are presenting it as the document to be validated against the official spec, not as the schema. If I'm correctly understanding what you're doing there, the result should be a validation error.

@o5
Copy link
Author

o5 commented Apr 10, 2017

@erayd: sorry, the snippet should be this :)

$value = (object) [
    'id' => 10,
];

$schema = json_decode('{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "id": {
            "type": "number"
        },
        "type": "object"
    }
}');

$validator = new Validator();
$validator->validate($value, $schema, Constraint::CHECK_MODE_VALIDATE_SCHEMA);

I'm expecting error like in validator:

screen shot 2017-04-10 at 11 13 49

@erayd
Copy link
Contributor

erayd commented Apr 10, 2017

@o5 Thanks - have reproduced this, and it does look like there's a bug somewhere. I'll get see if I can nail down the root cause today.

@erayd
Copy link
Contributor

erayd commented Apr 11, 2017

Found it - there's an issue with object validation where "properties" is not defined, and "additionalProperties" is present.

Fixing this bug causes parts of the draft-04 test suite to fail - it looks like there is another bug that this one was hiding...

@erayd
Copy link
Contributor

erayd commented Apr 11, 2017

...and the second bug is a result of fixing the first bug.

The root cause is the library attempting to use the same variable for two different things:

  • As the schema definition of an entire object; and
  • As the list defined by "properties" in an object's schema.

I'll push a combined fix for both bugs hopefully sometime today (it's 12:22pm here).

erayd added a commit to erayd/json-schema that referenced this issue Apr 11, 2017
Object validation attempts to use a single variable to store both the
object definition, and its properties. This causes validation to be
incomplete where "properties" is not set, but "additionalProperties" is.

This commit fixes both bugs in issue jsonrainbow#353.
@erayd
Copy link
Contributor

erayd commented Apr 11, 2017

Both fixed in #411. This PR will be backported to 5.x.x once it's merged.

bighappyface pushed a commit that referenced this issue Apr 11, 2017
Object validation attempts to use a single variable to store both the
object definition, and its properties. This causes validation to be
incomplete where "properties" is not set, but "additionalProperties" is.

This commit fixes both bugs in issue #353.
erayd added a commit to erayd/json-schema that referenced this issue Apr 11, 2017
Object validation attempts to use a single variable to store both the
object definition, and its properties. This causes validation to be
incomplete where "properties" is not set, but "additionalProperties" is.

This commit fixes both bugs in issue jsonrainbow#353.
bighappyface pushed a commit that referenced this issue May 16, 2017
* Split $objectDefinition into $schema and $properties (#411)

Object validation attempts to use a single variable to store both the
object definition, and its properties. This causes validation to be
incomplete where "properties" is not set, but "additionalProperties" is.

This commit fixes both bugs in issue #353.

* Issue-414: Allow The Option of T or space for Date time. (#415)

* Testcase for minProperties with properties defined (#416)

+ Fix Test

* Tweak phpdocumentor dependency to avoid install conflicts (#421)

* [BUGFIX] Cast empty schema arrays to object (#409)

* Cast root to object

* Use function_exists to allow polyfill compatibility

* Move array->object conversion to SchemaConstraint & SchemaStorage

Fixes issue #408

* fix bug when applying defaults for array items when the schema is for (#405)

all items and add support for minItems when applying defaults

* [BUGFIX] Split "uri" format into "uri" & "uri-reference", fix meta-schema bug (#419)

* Split "uri" format into "uri" and "uri-reference"

* Correct format for id & $ref in draft-03/04 meta-schemas

See json-schema-org/JSON-Schema-Test-Suite#177 (comment)
@o5
Copy link
Author

o5 commented Jun 5, 2017

I would like to confirm that it works in version 5.2.1. Thank you!

@o5 o5 closed this as completed Jun 5, 2017
@erayd
Copy link
Contributor

erayd commented Jun 5, 2017

Most welcome :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants