-
Notifications
You must be signed in to change notification settings - Fork 22
validateBody() throws in case it is provided with no schema #203
Comments
I vote for Gavel not handling any schema validation of any kind. Since we are using third-party libraries to perform JSON bodies validation, I suggest to attempt to execute the validator and catch the error, if any. What Gavel could do is to present the error in a more developer-friendly way, so it doesn't look like some internal exception. It must, however, remain an exception, and must not be silenced by putting it into
Is indeed a confusing error message. Since the end validation library is responsible for more verbose error messages depending on exception scenarios (i.e. malformed or missing JSON schema), we can't presume the origin of their exception on the Gavel's side. Thus, we can tolerate all validator's exceptions with a generic error message: Validating "${fieldName}" failed: ${originalErrorMessage} On the relevant note, as a part of apiaryio/gavel-spec#48 I would like to introduce an interface for input validation of Gavel. We could gradually start to implement input validation, which may include validation of missing properties. I don't see, however, how input validation would help in this way, since Gavel decides to validate "body" only if
I assume that specifying both is an invalid behavior, especially since gavel.js/lib/units/validateBody.js Lines 173 to 179 in 223150f
So, I'm not sure what is the true origin of this error... Is this a malformed or missing schema? If it's missing, why does it even get to Lines 45 to 47 in 223150f
|
@honzajavorek do you know about the scenarios when Gavel received The tests I've discovered assert My main concern is when it would come to input validation in Gavel I would love to get rid of values type ambiguity. So for a |
API Elements provide the schema in textual way, but I don't think Gavel needs to respect that. We can parse it into an object in Dredd prior to validation. Depends on how the JSON Schema validators expect the schema, because if we were to parse it from string to object and then the validator expects a string again, that would be inefficient. |
When I think of it, it might be the case that in Dredd it is always the case Gavel gets schema as a text, but in Gavel tests it is an object. And I'm not sure about how it is in Apiary. |
Considering current behavior (mixture of both object and string), I'd say that current validators we use can digest both types of JSON Schema. I wonder what AJV accepts. Perhaps, we should lean toward it if we plan to adopt it in the future. |
Yes, I think we should see what tv4/ajv expects and Gavel should lean to that. I'm totally for accepting the schema in only one type, not both. |
Note: we discussed this and realized the schema should be a string on input, because otherwise there would be no way for Gavel to support any other kind of schemas except for JSON Schema or generally a schema which can be represented as a JavaScript object. Imagine Gavel gets XML support and would like to accept DTD, XML Schema, or RELAX NG. Another example - consider Gavel gets support for the GraphQL data modelling language and would like to be able to accept it as a schema. |
🎉 This issue has been resolved in version 6.1.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
When my JSON Schema happened to be invalid JSON due to a typo, I got an error it is unparseable JSON. But Gavel didn't halt at that moment and continued to evaluate the situation further, which lead to an error being thrown:
That comes from
https://github.com/apiaryio/gavel.js/blob/master/lib/units/validateBody.js#L235
Which is caused by
expected.bodySchema
being evaluated toundefined
on this line:https://github.com/apiaryio/gavel.js/blob/master/lib/units/validateBody.js#L173
The text was updated successfully, but these errors were encountered: