Skip to content

Validator

Steve Hu edited this page Oct 3, 2016 · 2 revisions

The framework encourages design driven implementation so swagger specification should be done before the implementation starts. With the swagger-codegen undertow generator, the server stub can be generated and start running within minutes. However, we cannot rely on generator for validation as specification will be changed along the life cycle of the API. This is why we have provided a validator that works on top of the specification at runtime. In this way, the generator should only be used once and the validator will take the latest spec. and validate according the spec. at runtime.

Fail fast

As you may noticed that our Status object only supports one code and message. This is the indication the framework validation is designed as fail fast. Whenever there is an error, the server will stop processing the request and return the error to the consumer immediately. There are two reasons on this design:

  1. Security - you don't want to return so many errors if someone is trying to hack your server.
  2. Performance - you don't want to spend resource to handle invalid request to the next step.

ValidatorHandler

This is the entry point of the validator and it is injected during server start up if validator.json enableValidator is true. By default, only RequestValidator will be called. However, ResponseValidator can be enabled by setting enableResponseValidator to true in validator.json.

RequestValidator

It will validate the following:

  • uri
  • method
  • header
  • query parameters
  • path parameters
  • body if available

When necessary, schema validator will be called to do json schema validation.

ResponseValidator

It will validate the following:

  • header
  • response code
  • body if available

when necessary, json schema validator will be called.

SchemaValidator

If schema is defined in swagger.json, then the json-schema-validator will be called to validate the input against a json schema defined in draft v4.

Test

In order to test validator, the test suite starts an undertow server and serves petstore api for testing. It is a demo on how to unit test your API during development.