11# 🛡️ Data Contract Validator
22
3- > ** Catch breaking changes between your dbt models and your FastAPI/Pydantic APIs — before they hit production.**
3+ > ** Your dbt models don't stop at the warehouse. This checks that whatever they
4+ > feed — a CRM, an API — still gets the shape it expects, and fails the PR when
5+ > it doesn't.**
46
57[ ![ PyPI version] ( https://badge.fury.io/py/data-contract-validator.svg )] ( https://badge.fury.io/py/data-contract-validator )
68[ ![ Tests] ( https://github.com/OGsiji/data-contract-validator/workflows/Tests/badge.svg )] ( https://github.com/OGsiji/data-contract-validator/actions )
79[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT )
810
9- ## 🎯 What it solves
11+ ## 🎯 The problem
1012
11- Your analytics team changes a dbt model. Your API team's FastAPI service still
12- expects the old shape. Nobody notices until production 500s at 2 AM .
13+ dbt gives you tests, git history and code review for everything up to the
14+ warehouse. Past that, nothing .
1315
14- This tool sits on that boundary. It extracts the schema your ** dbt models
15- produce** and the schema your ** Pydantic models expect** , compares them, and
16- fails CI when the data side can no longer satisfy the API side.
16+ Your models get ** synced into** a CRM. They get ** read by** an API. Those
17+ destinations have schemas too, and they drift:
18+
19+ - ** Someone renames a property in a HubSpot settings page.** No pull request,
20+ no git history, no review — a dropdown and a save button. Your sync keeps
21+ running and quietly writes into a field that no longer means what it used
22+ to. Nobody finds out until a campaign goes out against bad data.
23+ - ** Your analytics team drops a column.** The FastAPI service still expects
24+ it. Nobody notices until production 500s at 2 AM.
25+
26+ Neither shows up in your dbt tests, because dbt doesn't know those
27+ destinations exist. The contract between them lives in someone's memory.
28+
29+ This tool sits on that boundary. It reads what your ** dbt models actually
30+ produce** , reads what the ** destination actually expects** , compares them, and
31+ fails CI when they've drifted apart.
1732
1833```
19- dbt models Data Contract Validator FastAPI / Pydantic
20- (what the pipeline ──▶ extract → normalize → compare ◀── (what the API expects)
21- produces) ↓
22- critical issues block the build
34+ dbt models Data Contract Validator destinations
35+ (what the pipeline ──▶ extract → normalize → compare ◀── HubSpot CRM
36+ produces) ↓ FastAPI / Pydantic
37+ critical issues block the build
2338```
2439
2540### Built for trust
2641
27- A check that gates a deploy is only useful if it doesn't cry wolf. v1.1
28- re-architected extraction around that principle:
42+ A check that gates a deploy is only useful if it doesn't cry wolf:
2943
3044- ** Canonical types** — dbt ` varchar ` and Pydantic ` str ` are understood to be
3145 the same thing, so you don't get drowned in fake "type mismatch" warnings.
@@ -51,6 +65,24 @@ contract-validator test
5165contract-validator validate
5266```
5367
68+ Guarding a reverse-ETL sync into HubSpot takes one block of config:
69+
70+ ``` yaml
71+ target :
72+ hubspot :
73+ type : " hubspot"
74+ object_type : " contacts" # or companies, deals, or a custom object
75+ fields : # the properties your sync actually writes
76+ - email
77+ - lifecyclestage
78+ - lifetime_value
79+ ` ` `
80+
81+ ` ` ` bash
82+ export HUBSPOT_ACCESS_TOKEN=pat-xxxx # HubSpot Private App token
83+ contract-validator validate
84+ ```
85+
5486## 🚀 Getting started, step by step
5587
5688If you're setting this up on a project for the first time, the order below
@@ -67,16 +99,16 @@ avoids the sharp edges:
6799 ``` bash
68100 contract-validator init --interactive
69101 ```
70- You'll be asked: where your dbt project is, which API framework you use,
71- whether your models live in this local project or a different GitHub
72- repo, and then the local path (or the ` org/repo ` + path within it, plus
73- an optional branch/tag/commit — blank reads the repo's default branch).
74- Local-vs-GitHub is asked explicitly rather than guessed from the path's
75- shape — a local path like ` app/models ` is syntactically identical to a
76- GitHub ` org/repo ` string, so there's no reliable way to infer which one
77- you mean. If you pick GitHub, it checks the path actually exists before
78- writing the config — so a typo surfaces here instead of at ` validate `
79- time.
102+ You'll be asked: where your dbt project is, which destination you're
103+ validating against, whether your models live in this local project or a
104+ different GitHub repo, and then the local path (or the ` org/repo ` + path
105+ within it, plus an optional branch/tag/commit — blank reads the repo's
106+ default branch). Local-vs-GitHub is asked explicitly rather than guessed
107+ from the path's shape — a local path like ` app/models ` is syntactically
108+ identical to a GitHub ` org/repo ` string, so there's no reliable way to
109+ infer which one you mean. If you pick GitHub, it checks the path actually
110+ exists before writing the config — so a typo surfaces here instead of at
111+ ` validate ` time.
80112
81113 ` init ` refuses to touch an existing ` .retl-validator.yml ` or workflow
82114 file — it won't clobber hand-added ` mapping ` entries just because you
@@ -103,8 +135,8 @@ avoids the sharp edges:
103135 contract-validator test
104136 ```
105137 Confirms the config parses, the dbt project is found, and the target
106- (local path or GitHub path) is reachable. If this fails, ` validate ` will
107- fail the same way — fix it here first.
138+ (local path, GitHub path, or CRM connection ) is reachable. If this fails,
139+ ` validate ` will fail the same way — fix it here first.
108140
1091416 . ** Run it** :
110142 ``` bash
@@ -120,7 +152,7 @@ avoids the sharp edges:
120152 - A table that's genuinely populated by something other than dbt (e.g. a
121153 separate streaming pipeline) and has no source model on purpose → add
122154 it to ` mapping.exclude ` . ` table=True ` alone is ** not** used to infer
123- this automatically — see [ FastAPI side ] ( #fastapi-side ) for why.
155+ this automatically — see [ FastAPI / Pydantic ] ( #fastapi--pydantic ) for why.
124156
1251578 . ** For accurate type-checking** (not just column-presence checks), run
126158 ` dbt docs generate ` before ` validate ` so it picks up ` catalog.json ` (Tier 1,
@@ -172,52 +204,15 @@ doesn't mirror your dbt repo's branch names — it silently falls back to the
172204target's default branch, so this never turns into a spurious failure. An
173205explicit ` --fastapi-ref ` or ` target.*.ref ` always overrides auto-matching.
174206
175- ## 🔍 How extraction works (and why it's accurate)
176-
177- ### dbt side — tiered, best-source-wins
178-
179- | Tier | Source | Types | Confidence | Notes |
180- | ---| ---| ---| ---| ---|
181- | 1 | ` target/catalog.json ` | ** Real warehouse types** | high | Produced by ` dbt docs generate ` . Most accurate. |
182- | 2 | ` sqlglot ` SQL parse | Inferred (often unknown) | medium | Trusted column ** names** ; enriched with documented types from ` manifest.json ` . Detects ` SELECT * ` . |
183- | 3 | regex parse | Guessed | low | Last resort. Never used to hard-fail a build. |
184-
185- The tool auto-detects what's available and degrades gracefully — so it works
186- offline in pre-commit ** and** with full type fidelity in a warehouse-connected
187- CI job.
188-
189- > 💡 ** Tip:** run ` dbt docs generate ` in CI before validating to unlock Tier 1
190- > (real types). Without it, you still get accurate column-presence checks from
191- > Tier 2. The workflow ` init ` generates includes this step already, commented
192- > out — it needs your warehouse adapter and credentials filled in, which
193- > can't be guessed, so it isn't active by default.
194-
195- ### FastAPI side
196-
197- Pydantic / SQLModel classes are parsed from source with Python's ` ast ` (no
198- imports executed). ` Optional[...] ` controls whether a field is required.
199- An explicit ` __tablename__ ` is used as the table name when present;
200- otherwise the class name is converted to ` snake_case ` .
201-
202- ` table=True ` SQLModel classes are validated the same as any other class —
203- they are ** not** skipped. Whether a table is meant to come from dbt is
204- business knowledge that isn't recoverable from the Python source: two
205- structurally identical ` table=True ` classes can need opposite treatment (one
206- is a normal dbt-fed table your API also returns directly; another is
207- populated by a Kafka stream and was never meant to have a dbt model). Use
208- ` mapping.exclude ` to state the latter case explicitly rather than relying on
209- ` table=True ` to imply it.
210-
211- ### HubSpot side (reverse-ETL destinations)
207+ ## 🎯 Destinations
212208
213- A CRM is the other half of reverse ETL: dbt models don't just feed an API,
214- they get ** synced into** tools like HubSpot. That destination has a schema
215- too — but unlike a codebase it lives in an admin UI, editable by anyone with
216- the right permissions, with no code review and no git history. A property
217- renamed in a HubSpot settings page can break a sync exactly the way a dropped
218- dbt column can, and nothing in your repo would show it.
209+ ### HubSpot CRM (reverse ETL)
219210
220- Point a target at it and the same validation applies:
211+ A CRM is the far end of reverse ETL, and the least guarded surface in the
212+ whole pipeline. Unlike a codebase, its schema lives in an admin UI, editable
213+ by anyone with the right permissions, with no code review and no git history.
214+ A property renamed in a settings page breaks a sync exactly the way a dropped
215+ dbt column does — and nothing in your repo would show it.
221216
222217``` yaml
223218target :
@@ -256,11 +251,47 @@ are genuinely load-bearing for your sync so they fail the build instead.
256251> HubSpot ** Settings → Integrations → Private Apps** with the
257252> ` crm.schemas.<object>.read ` scope.
258253
254+ ### FastAPI / Pydantic
255+
256+ Pydantic / SQLModel classes are parsed from source with Python's ` ast ` (no
257+ imports executed). ` Optional[...] ` controls whether a field is required.
258+ An explicit ` __tablename__ ` is used as the table name when present;
259+ otherwise the class name is converted to ` snake_case ` .
260+
261+ ` table=True ` SQLModel classes are validated the same as any other class —
262+ they are ** not** skipped. Whether a table is meant to come from dbt is
263+ business knowledge that isn't recoverable from the Python source: two
264+ structurally identical ` table=True ` classes can need opposite treatment (one
265+ is a normal dbt-fed table your API also returns directly; another is
266+ populated by a Kafka stream and was never meant to have a dbt model). Use
267+ ` mapping.exclude ` to state the latter case explicitly rather than relying on
268+ ` table=True ` to imply it.
269+
270+ ## 🔍 How extraction works (and why it's accurate)
271+
272+ ### dbt side — tiered, best-source-wins
273+
274+ | Tier | Source | Types | Confidence | Notes |
275+ | ---| ---| ---| ---| ---|
276+ | 1 | ` target/catalog.json ` | ** Real warehouse types** | high | Produced by ` dbt docs generate ` . Most accurate. |
277+ | 2 | ` sqlglot ` SQL parse | Inferred (often unknown) | medium | Trusted column ** names** ; enriched with documented types from ` manifest.json ` . Detects ` SELECT * ` . |
278+ | 3 | regex parse | Guessed | low | Last resort. Never used to hard-fail a build. |
279+
280+ The tool auto-detects what's available and degrades gracefully — so it works
281+ offline in pre-commit ** and** with full type fidelity in a warehouse-connected
282+ CI job.
283+
284+ > 💡 ** Tip:** run ` dbt docs generate ` in CI before validating to unlock Tier 1
285+ > (real types). Without it, you still get accurate column-presence checks from
286+ > Tier 2. The workflow ` init ` generates includes this step already, commented
287+ > out — it needs your warehouse adapter and credentials filled in, which
288+ > can't be guessed, so it isn't active by default.
289+
259290## 🚦 What gets flagged
260291
261292| Severity | Meaning | Example |
262293| ---| ---| ---|
263- | 🚨 ** Critical** | Blocks the build | API requires a column the dbt model no longer produces |
294+ | 🚨 ** Critical** | Blocks the build | Destination requires a column the dbt model no longer produces |
264295| ⚠️ ** Warning** | Worth a look, non-blocking | A real type mismatch, or a missing column on a model we couldn't fully resolve |
265296
266297``` bash
@@ -291,6 +322,13 @@ source:
291322 disable_manifest: false
292323
293324target:
325+ # A reverse-ETL destination...
326+ hubspot:
327+ type: "hubspot"
328+ object_type: "contacts"
329+ fields: [email, lifecyclestage, lifetime_value]
330+
331+ # ...and/or a code target.
294332 fastapi:
295333 # GitHub repo:
296334 type: "github"
@@ -308,7 +346,7 @@ target:
308346# Optional: explicit mapping for when names don't line up by convention.
309347mapping:
310348 tables:
311- # target (Pydantic) table : source (dbt) model
349+ # target table : source (dbt) model
312350 user_analytics: user_analytics_summary
313351 columns:
314352 user_analytics:
@@ -318,6 +356,10 @@ mapping:
318356 # not dbt) -- see "When do I need mapping?" below.
319357 exclude:
320358 - feed_interaction
359+ # Missing columns that should fail the build rather than warn.
360+ critical_columns:
361+ contacts:
362+ - email
321363
322364validation:
323365 fail_on: [" missing_tables" , " missing_required_columns" ]
@@ -387,8 +429,8 @@ To make that switch:
3874293. Replace `secrets.GITHUB_TOKEN` with `secrets.API_REPO_TOKEN` (or
388430 whatever you named it) in the workflow's `env:` block.
389431
390- Skip all of this for a `local` target — `init` omits the whole `env:` block
391- since a local target never talks to the GitHub API at all.
432+ Skip all of this for a `local` or CRM target — `init` omits the whole `env:`
433+ block, since neither talks to the GitHub API at all.
392434
393435# ## When do I need `mapping`?
394436
@@ -478,6 +520,7 @@ jobs:
478520 - run : contract-validator validate --output github
479521 env :
480522 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
523+ HUBSPOT_ACCESS_TOKEN : ${{ secrets.HUBSPOT_ACCESS_TOKEN }}
481524` ` `
482525
483526` GITHUB_TOKEN` here is only needed if `target` is a `github` repo (`init`
@@ -497,7 +540,7 @@ contract-validator setup-precommit --install-hooks
497540` ` ` yaml
498541repos:
499542 - repo: https://github.com/OGsiji/data-contract-validator
500- rev: v1.1 .0
543+ rev: v1.3 .0
501544 hooks:
502545 - id: contract-validation
503546` ` `
@@ -510,15 +553,16 @@ contract-validator validate --output json # machine-readable for CI
510553contract-validator validate --output github # GitHub Actions annotations
511554` ` `
512555
513- # # 🚀 Supported frameworks
556+ # # 🚀 Supported sources and destinations
514557
515558**Source:** dbt (all adapters — Snowflake, BigQuery, Redshift, Postgres, …).
516- **Targets :** FastAPI (Pydantic v2 + SQLModel), HubSpot CRM .
559+ **Destinations :** HubSpot CRM, FastAPI (Pydantic v2 + SQLModel).
517560
518561The extractor architecture is intentionally pluggable (`BaseExtractor` →
519- ` Dict[str, Schema]` with canonical types), so additional sources/targets can be
520- added without touching the validator. [Open an issue](https://github.com/OGsiji/data-contract-validator/issues)
521- to request one.
562+ ` Dict[str, Schema]` with canonical types), so additional sources and
563+ destinations can be added without touching the validator.
564+ [Open an issue](https://github.com/OGsiji/data-contract-validator/issues) to
565+ request one.
522566
523567# # 🛠️ Development & testing
524568
@@ -556,10 +600,11 @@ class MyExtractor(BaseExtractor):
556600
557601# # 🗺️ Roadmap
558602
603+ - More reverse-ETL destinations (Salesforce, Braze, Customer.io)
559604- Real compatibility semantics (nullability, additive vs. breaking changes)
560605- Reporter/logging abstraction (quiet/embeddable core)
561606- A canonical, language-neutral contract artifact + baseline/snapshot diffing
562- - More targets (Django, SQLAlchemy, GraphQL, OpenAPI)
607+ - More code targets (Django, SQLAlchemy, GraphQL, OpenAPI)
563608
564609# # 📄 License
565610
0 commit comments