This example shows how you could share a JSON Schema document with the Pact Broker as a substitute for a Pact contract, and validate that the contract is valid by a provider using the same workflows you would with a standard Pact test.
It's based on the CI/CD workshop from https://docs.pactflow.io/docs/workshops/ci-cd/.
It is using a public tenant on Pactflow, which you can access here using the credentials dXfltyFMgNOFZAxr8io9wJ37iUpY42M
/O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1
.
- Docker
- NodeJS 10+
- jq
npm i
This phase is analogous to the Pact unit testing phase, where the consumers' client code is tested and as an output produces a contract.
In the case of a JSON schema, this may be through static generation (e.g. using tools such as https://github.com/YousefED/typescript-json-schema), a recording proxy or otherwise.
To emulate a "CI" process, you can run:
make fake_ci_consumer
This will:
- Generate the contract (using the TS -> schema generation)
- Publish the contract to Pactflow (any valid JSON file, so long as it has the
consumer
andprovider
properties, will be accepted by the broker CLI tooling) - Run the
can-i-deploy
check (first time, this should fail) - If successful, "deploy" to production and tag the application version as moved to
prod
Uses https://www.npmjs.com/package/json-schema-diff to do a diff on the JSON schema to check for backwards incompatible contract changes.
make fake_ci_provider
This will:
- Fetch the contract (currently it specifically fetches the exact consumer, but it should really use the "pacts for verification" endpoint in a real implementation)
- Extract the schema from the interaction and perform a semantic diff using
json-schema-diff
. - Generate a schema from it's local type (
provider/product.ts
) - Compare the downloaded schema to the golden schema (
provider/schema/schema.json
) - they must be identical - Compare the downloaded schema to the golden schema (
provider/schema/schema.json
) - the downloaded schema (contract) must be compatible with the golden schema - Send the verification results back to Pactflow
- Run the
can-i-deploy
check - If successful, "deploy" to production and tag the application version as moved to
prod
npm run build
npm start
curl localhost:3000 | jq .
# will produce:
#
# {
# "item": "pancakes",
# "price": 27.4
# }