Cross-Platform Interop
Cross-Platform Interop
Section titled “Cross-Platform Interop”Purview.ZodSharp ships a cross-platform fixture pipeline that proves the C# implementation agrees with TypeScript/Zod. The TypeScript side runs on Bun; the C# side runs under the TUnit test suite.
The schema
Section titled “The schema”Both sides define the same user schema — TypeScript UserSchema in src/ts/schema.ts and the C# CrossPlatformUserSchema in src/tests/SystemTextJson.UnitTests/CrossPlatformFixtures.cs (mirrored by NewtonsoftJson.UnitTests):
| Field | Zod (TS) | Purview.ZodSharp (C#) |
|---|---|---|
name |
z.string().min(1) |
min-length 1 |
age |
z.number().int().min(0).max(120) |
0..120 integer |
email |
z.string().email().optional() |
optional email |
tags |
z.array(z.string()).default([]) |
string array, default [] |
Fixture generation
Section titled “Fixture generation”bun run generate-fixtures # runs src/ts/generate-fixtures.tsFor each of the eight fixture cases (three valid, five invalid) the script:
- Writes a JSON file to
src/ts/fixtures/{key}.json. - Runs
UserSchema.safeParse(value)and records the outcome insrc/ts/fixtures/manifest.json— the authoritative{ "valid": true|false }result for every fixture.
The validation loop
Section titled “The validation loop”bun run generate-fixtureswritessrc/ts/fixtures/*.jsonandmanifest.json(Zod is the authority on validity).just test(C# TUnit) reads the fixtures and manifest, asserts outcomes match, and writes validated C# output tosrc/tests/cross-platform/output/{systemtext,newtonsoft}-valid.json.bun run test(vitest) re-checks fixture validity under Zod and parses the C# output JSON — closing the TypeScript ↔ C# loop.
C# cross-platform tests (SystemTextCrossPlatformTests, NewtonsoftCrossPlatformTests) load every fixture and assert the C# outcome matches manifest.json, deserialize-and-validate valid fixtures, round-trip TS fixture → C# → JSON → C#, and serialize a validated CrossPlatformUser into src/tests/cross-platform/output/{systemtext,newtonsoft}-valid.json.
Vitest cross-platform tests (tests/ts/cross-platform.test.ts) re-check fixture validity under Zod, verify canonical serialization, and parse every JSON file in cross-platform/output with Zod to prove C# output is acceptable to TypeScript/Zod.
If the C# output directory is empty, the vitest suite emits a note instructing you to run the C# cross-platform tests first.
JSON Schema bridge
Section titled “JSON Schema bridge”The same interop goal is available without fixtures via JSON Schema:
- Export:
Z.ToJsonSchema(core package) → JSON Schema, orz.toJSONSchemaon the TypeScript side (Zod v4+). - Import:
Z.FromJsonSchema(in the System.Text.Json or Newtonsoft.Json package).
See JSON Schema Export and JSON Schema Import.
Directory layout
Section titled “Directory layout”src/ts/ TypeScript (Zod) schemas + fixture generationsrc/ts/fixtures/ generated JSON fixtures + manifest.jsontests/ts/ vitest cross-platform testssrc/tests/cross-platform/ shared output directory for C#-generated JSON