I have a JSON payload (request or response) and I want to validate that instance against a swagger schema that I have. How do I do that?
Please note that I am not trying to validate if my spec is an OpenAPI/Swagger spec.
I'd like to achieve this without the use of external JSON validators. I am also trying to achieve this in Go (specifically go-openapi)
Thanks.
You need to get a hold of the schema that defines your validation rules, that's typically stored in the swagger spec definition property.
And you need to get your model (json data structure, can be a map or a struct). Here's an example:
var model models.User
json.Unmarshal(bytes, &model)
var spec *spec.Swagger = getSpec()
schema := spec.Definitions["User"]
if err := validate.AgainstSchema(schema, &model, strfmt.Default); err != nil {
return err
}