Skip to content

refactor(gui): validate .wind through the generated schema only - #375

Merged
CameronBrooks11 merged 1 commit into
mainfrom
refactor/single-validator
Sep 10, 2026
Merged

CameronBrooks11 merged 1 commit into
mainfrom
refactor/single-validator

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

Closes #370.

Why this isn't the change #370 originally asked for

#370 was opened about zod growing 15.6 KiB. Measuring properly reframed it. I decoded the build's sourcemap mappings to attribute real minified bytes per module (source sizes overstate large libraries):

module minified share
ajv 101.0 KiB 30.3%
zod 80.8 KiB 24.2%
app source 75.6 KiB 22.7%
svelte 48.6 KiB 14.6%

The GUI carried two validators for one contract, together ~60% of its JavaScript:

  • src/lib/validation.ts (ajv) compiles schemas/wind-schema.json, generated from the Pydantic models.
  • src/lib/schemas.ts (zod) hand-mirrored the same .wind shapes.

That is a drift risk as well as weight, and it had already bitten — from #345's commit message: "#352 added frictionLambda to the Zod gate for validation parity but never threaded it through the converters, a latent silent-strip masked only by that guard."

What changed

Only two files consumed the zod schemas. The WindDefinition type was already imported from the generated src/types/wind-schema.ts, so zod's inferred type was already redundant.

  • File open gate → validateWindDefinition. ajv's error shape ({field, message}[]) already matches ValidationError's second parameter, so the errors surface unchanged.
  • Backend health → a small hand-written type guard. It comes over the Tauri bridge, not the OpenAPI surface, so it has no generated type; three fields didn't warrant a schema library.
  • schemas.ts keeps the error classes and helpers, which is most of it, and gains a note saying validation deliberately lives elsewhere.
  • ajv moved to dependencies — it's imported by runtime code and was the only such package still in devDependencies (zod and openapi-fetch were both already correct).

The one constraint that had to be carried over

The engine bounds windAngle to [1, 89] in a Pydantic validator, not a field constraint, so generate_schema.py can't emit it — wind-schema.json has only exclusiveMinimum: 0. ajv alone would have accepted a file the planner then rejects with a 422, so this would have been a silent loosening of the gate.

validateWindDefinition now applies that bound explicitly, cited to fiberpath/planning/validators.py. I checked every other constraint zod enforced — mandrel, tow, layer bounds, and the schemaVersion ^1\.\d+$ pattern — and all of them are already in the generated schema. windAngle was the only gap. (The profile/endDiameter mutual exclusion is a Pydantic model_validator that zod didn't enforce either, so no change there.)

Confirmed against the engine, with the mutation asserted so a no-op edit couldn't masquerade as a pass:

$ fiberpath plan <vk_nosecone with windAngle=120>
Planning failed: Layer 1: wind angle 120.0° must be between 1.0° and 89.0°
exit=1

Verification

New tests cover the parity bound at 0.5 / 89.5 / 90 / 120 (reject) and 1 / 45 / 89 (accept). Mutation-tested: disabling the parity check fails exactly those 4 rejection cases and nothing else, so the tests are genuinely exercising it rather than passing incidentally.

before after
JS bundle 337.39 KiB 255.10 KiB
gzip 102.83 KiB 80.05 KiB
headroom vs 346.62 KiB budget ~9 KiB 91.5 KiB

tsc --noEmit clean · svelte-check 0 errors · 327 tests pass · coverage 78.2 / 68.7 / 80.1 / 83.3 against thresholds of 60 / 50 / 65 / 60.

Test count moves 339 → 327: the 19 zod schema tests are removed (that contract is now covered by validation.test.ts against the canonical schema) and 7 parity tests added.

The GUI carried two validators for one contract. src/lib/validation.ts
compiles schemas/wind-schema.json, which is generated from the Pydantic
models; src/lib/schemas.ts hand-mirrored the same shapes in zod. Between
them they were ~60% of the bundle (ajv 101 KiB, zod 81 KiB of 333 KiB
attributed by decoding the build's sourcemap).

The duplication was also a live drift risk, and had already bitten: per
#345, "#352 added frictionLambda to the Zod gate for validation parity
but never threaded it through the converters".

Drop zod and route the two consumers through validateWindDefinition:
the file open gate, and a small hand-written guard for the backend-health
response, which comes over the Tauri bridge and so has no generated type.
The WindDefinition *type* was already imported from the generated
src/types/wind-schema.ts, so nothing changes there.

One constraint had to be carried over. The engine bounds windAngle to
[1, 89] in a Pydantic validator, which JSON Schema cannot express, so
wind-schema.json has only exclusiveMinimum: 0 and ajv alone would accept
a file the planner rejects with a 422. validateWindDefinition now applies
that bound explicitly, cited to fiberpath/planning/validators.py, with
tests. Every other constraint zod checked is already in the schema.

Move ajv to dependencies: it is imported by runtime code and was the only
such package left in devDependencies.

Bundle 337.39 -> 255.10 KiB (-82.3 KiB, -24%); headroom against the
346.62 KiB budget goes from ~9 KiB to 91.5 KiB.

Closes #370.
@CameronBrooks11
CameronBrooks11 merged commit ed9015c into main Sep 10, 2026
18 checks passed
@CameronBrooks11
CameronBrooks11 deleted the refactor/single-validator branch September 10, 2026 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zod 4.5.4 costs ~15.6 KiB of bundle, leaving ~9 KiB of budget headroom

1 participant