json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

Validation ignores unknown types

Open ineu opened this issue 9 years ago • 6 comments

I have a following schema:

{
  "type": "object",
  "properties": {
    "id": {
       "type": "integer"
     },
     "name": {
       "type": "foo"
     },
     "description": {
       "type": "foo"
     },
     "price": {
       "type": "foo"
     },
     "created_at": {
       "type": "Fixnum"
     }
   }
 }

I expect

JSON::Validator.validate!(schema_path, json, strict: true, validate_schema: true)

to raise error saying something like 'type is unknown', unfortunately it doesn't. This way it's very easy to make a typo. I.e. I type 'Integer' instead of 'integer' and this property is just ignored.

ineu avatar Feb 19 '16 07:02 ineu

That's an interesting point.

On Friday, 19 February 2016, Eugene Diachkin [email protected] wrote:

I have a following schema:

{ "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "foo" }, "description": { "type": "foo" }, "price": { "type": "foo" }, "created_at": { "type": "Fixnum" } } }

I expect

JSON::Validator.validate!(schema_path, json, strict: true, validate_schema: true)

to raise error saying something like 'type is unknown', unfortunately it doesn't. This way it's very easy to make a typo. I.e. I type 'Integer' instead of 'integer' and this property is just ignored.

— Reply to this email directly or view it on GitHub https://github.com/ruby-json-schema/json-schema/issues/309.

iainbeeston avatar Feb 19 '16 07:02 iainbeeston

And the spec is pretty clear about this: Allowed are only the primitive types. So we have a bug/incomplete implementation.

RST-J avatar Feb 20 '16 18:02 RST-J

That makes me think it should be in the common json schema test suite. I'll try to get it added

On Saturday, 20 February 2016, Jonas Peschla [email protected] wrote:

And the spec is pretty clear about this: Allowed are only the primitive types http://json-schema.org/latest/json-schema-validation.html#anchor79. So we have a bug/incomplete implementation.

— Reply to this email directly or view it on GitHub https://github.com/ruby-json-schema/json-schema/issues/309#issuecomment-186660267 .

iainbeeston avatar Feb 20 '16 20:02 iainbeeston

I've thought about this some more, and this isn't something that should go into the common test suite at all. validate_schema: true should raise an error as you originally suggested.

iainbeeston avatar Mar 04 '16 17:03 iainbeeston

So I've just tried this, and it does raise an error:

JSON::Schema::ValidationError: The property '#/properties/name/type' of type <type of data> did not match one or more of the required schemas

Is that what you're seeing? So the issue is that the error message doesn't explain the cause of the problem?

iainbeeston avatar Mar 04 '16 18:03 iainbeeston

@iainbeeston Not really. When I opened an issue, it just didn't raise exceptions. I can not reproduce it now, version 2.6.1 raises an exception as expected. Maybe it was my mistake.

But the error message is cryptic indeed. It would be great to paraphrase it to be about 'name', not about 'name/type'. 'Type' is a part of schema format, user is more interested in the property.

UPD: looks like the real issue is with $ref pointing to invalid schemas. Trying to reproduce further

UPD2: So type defined in properties in the json file is not validated if the file is included via $ref

ineu avatar Mar 10 '16 14:03 ineu