brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Incomplete error message regarding valid data-types #42

Closed matatk closed 8 years ago

matatk commented 8 years ago

I think I've found a bug whereby the list of valid data-types reported in an error message is missing at least one possible valid data-type. I'm using this library via prmd. I mistakenly entered "int" as a data-type (have been writing C a lot recently :-)) and was given the following error:

/usr/local/lib/ruby/gems/2.2.0/gems/json_schema-0.10.0/lib/json_schema/parser.rb:40:in `parse!': #/definitions/game/definitions/id: "int" is not a valid format, must be one of date, date-time, email, hostname, ipv4, ipv6, regex, uri, uuid. (RuntimeError)

However I checked Understanding JSON Schema and found that "integer" is a valid data-type (though there seems to be some ambiguity about this -- though it does seem that if "integer" isn't, "number" should be). When I changed my "int" to "integer" all worked well.

It seems to me that this means the error message needs updating to include all possible valid data-types. Not a truly massive issue, but I noticed it so thought I'd let you know. Thanks for this code!

matatk commented 8 years ago

I apologise, I was putting "int"/"integer" into the "format" field and not the "type" field by mistake. However, when I remove the "format" field and put "int" into the "type" field, I get this error:

failed schema #/properties/type: No subschema in "anyOf" matched.

which seems a little cryptic (to me, as a novice). If I put "integer" in the "type" field then there is no error and everything works fine.

So this isn't quite the bug I first thought it was, but perhaps the above error message might be made more explicit about "int" being the problem, if possible? (I'd offer to make a PR but I think the above qualifies me as too much of a n00b to go near your code!)

brandur commented 8 years ago

Hey @matatk, no worries!

As you can see in this validation method here the parser is indeed supposed to validate that a type is one of the known classes, so you should see an error when you try to use int.

Based on this error message that you pasted:

failed schema #/properties/type: No subschema in "anyOf" matched.

I suspect that it's possible that there may be some conflation here between the schema type keyword and a schema property also named "type" (the path #/properties/type suggests that we're under properties rather than the root of the schema). The type keyword just looks like:

{
    "type": "integer"
}

Whereas a property called "type" would look like"

{
    "properties": {
        "type": {
            ...
        }
    }
}

I'm not sure whether that's helpful :)

Anyway, I'm going to close this issue out for now, but let me know if you have any other thoughts/ideas/questions!

matatk commented 8 years ago

It seems I never replied to this to say thanks @brandur for your explanation -- it did indeed help and is appreciated. When issues get closed they kind-of drop off my RADAR; I should've just replied immediately as soon as I got the notification :-). Thanks!