https://github.com/pactflow/pactflow-jsonschema-example
Example JSON Schema Integration with Pactflow
https://github.com/pactflow/pactflow-jsonschema-example
example
Last synced: 9 months ago
JSON representation
Example JSON Schema Integration with Pactflow
- Host: GitHub
- URL: https://github.com/pactflow/pactflow-jsonschema-example
- Owner: pactflow
- License: mit
- Created: 2020-08-14T07:09:21.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-04-27T22:41:26.000Z (9 months ago)
- Last Synced: 2025-04-30T03:44:39.298Z (9 months ago)
- Topics: example
- Language: Makefile
- Size: 70.3 KB
- Stars: 7
- Watchers: 10
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON Schema Example
[](https://travis-ci.com/pactflow/pactflow-jsonschema-example)
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](https://test.pact.dius.com.au) using the credentials `dXfltyFMgNOFZAxr8io9wJ37iUpY42M`/`O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1`.
## Prerequisites
* Docker
* NodeJS 10+
* jq
## Setup
```
npm i
```
## Consumer Test
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:
1. Generate the contract (using the TS -> schema generation)
2. Publish the contract to Pactflow (any valid JSON file, so long as it has the `consumer` and `provider` properties, will be accepted by the broker CLI tooling)
3. Run the `can-i-deploy` check (first time, this should fail)
4. If successful, "deploy" to production and tag the application version as moved to `prod`
## Provider Test
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:
1. Fetch the contract (currently it specifically fetches the exact consumer, but it should really use the ["pacts for verification"](https://github.com/pact-foundation/pact_broker/issues/307) endpoint in a real implementation)
1. Extract the schema from the interaction and perform a semantic diff using `json-schema-diff`.
1. Generate a schema from it's local type (`provider/product.ts`)
1. Compare the downloaded schema to the golden schema (`provider/schema/schema.json`) - they must be identical
1. Compare the downloaded schema to the golden schema (`provider/schema/schema.json`) - the downloaded schema (contract) must be _compatible_ with the golden schema
1. Send the verification results back to Pactflow
1. Run the `can-i-deploy` check
1. If successful, "deploy" to production and tag the application version as moved to `prod`
## Running the Provider
```
npm run build
npm start
curl localhost:3000 | jq .
# will produce:
#
# {
# "item": "pancakes",
# "price": 27.4
# }
```