Addison Lee
Addison Lee

Reputation: 1

Generate json schema in addition to, or from pact contract

In my client code, I use json schemas to validate json responses (using ajv). I am planning to use pact-js to generate pacts to run against the provider for contract testing.

I would like a single source of truth for the schema that my client code uses during runtime validation and that the contract tests use during pact verification. Is there a way to generate json (or swagger) schemas from the pact file, or alternatively is there a library which uses the pact file itself for runtime validation of individual http responses (as opposed to just being used for contract verifications in contract tests)?

I realize this is a bit of a chicken and egg problem because I would need to first run the client code as part of a test run to generate the pacts (and in turn the json schema), so I would have to figure out a good work flow for that.

I also realize that contract tests should reduce the need for runtime validation, but contract testing is still not comprehensive enough to catch all real world scenarios.

Upvotes: 0

Views: 1055

Answers (1)

Matthew Fellows
Matthew Fellows

Reputation: 4065

The short answer is no, although I believe there are a few OSS libraries that attempt to address this (none are official but I recall this coming up on forums at one point).

Either way, although the intentions are good we believe the task is futile. The two technologies are complimentary yet will no doubt evolve over time, making this more difficult to manage.

You could look to integrate with Swagger in a few ways, for example, the awesome folks at Atlassian have created a few you could look at [1][2].

[1] https://bitbucket.org/atlassian/swagger-request-validator

[2] https://bitbucket.org/atlassian/swagger-mock-validator

[3] https://github.com/pact-foundation/pact-specification/issues/28

Upvotes: -1

Related Questions