Repository navigation
refactor(gui): validate .wind through the generated schema only - #375
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
The GUI carried two validators for one contract, together ~60% of its JavaScript:
src/lib/validation.ts(ajv) compilesschemas/wind-schema.json, generated from the Pydantic models.src/lib/schemas.ts(zod) hand-mirrored the same.windshapes.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
WindDefinitiontype was already imported from the generatedsrc/types/wind-schema.ts, so zod's inferred type was already redundant.validateWindDefinition. ajv's error shape ({field, message}[]) already matchesValidationError's second parameter, so the errors surface unchanged.schemas.tskeeps the error classes and helpers, which is most of it, and gains a note saying validation deliberately lives elsewhere.ajvmoved todependencies— it's imported by runtime code and was the only such package still indevDependencies(zodandopenapi-fetchwere both already correct).The one constraint that had to be carried over
The engine bounds
windAngleto [1, 89] in a Pydantic validator, not a field constraint, sogenerate_schema.pycan't emit it —wind-schema.jsonhas onlyexclusiveMinimum: 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.validateWindDefinitionnow applies that bound explicitly, cited tofiberpath/planning/validators.py. I checked every other constraint zod enforced — mandrel, tow, layer bounds, and theschemaVersion^1\.\d+$pattern — and all of them are already in the generated schema.windAnglewas the only gap. (Theprofile/endDiametermutual exclusion is a Pydanticmodel_validatorthat 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:
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.
tsc --noEmitclean ·svelte-check0 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.tsagainst the canonical schema) and 7 parity tests added.