Skip to content

Generate validation zod schemas and types from openapi spec#599

Open
correct-horse-battery-bench wants to merge 15 commits intomasterfrom
2001-task-generate-validation-schemas-against-openapi-spec-or-write-them-by-hand
Open

Generate validation zod schemas and types from openapi spec#599
correct-horse-battery-bench wants to merge 15 commits intomasterfrom
2001-task-generate-validation-schemas-against-openapi-spec-or-write-them-by-hand

Conversation

@correct-horse-battery-bench
Copy link
Copy Markdown
Contributor

@correct-horse-battery-bench correct-horse-battery-bench commented Mar 19, 2026

Issue: hashtopolis/server#2001

Currently only backend responses are validated as request outgoing validation needs a bit a refactoring how the calls are done. In addition, the pull request is already quite large therefore it makes sense to split this up.

Adds zod generated validation schemas and types using @hey-api/openapi-ts package and the zod plugin.

Currently strict mode is off which has some effect how types are inferred from zod but the solution will be to switch to strict mode at one point guaranteeing complete type safety within.

There are new scripts in package.json which generate the types from a running backend. Make sure that the correct backend branch is checked out to be sure that the openapi spec is correct (had to be adjusted somewhat on the branch).

As the openapi spec includes the json+api envelope and jsona parses them out and flattens attributes and includes relationships then the inferred zod type from the envelope will not correctly match the flattened object. Therefore a json-api types file has been added that maps the zod json+api type to the type that jsona actually returns. It also correctly adds included fields the the result type.
That the return type contains the correct relationship fields non optional could later be auto inferred when caling the client that does the request with certain included fields. Currently they can be passed manually so the J interfaces are matched in type.

Main Changes:

JAgent

  • cpuOnly number -> boolean
  • userId -> null -> number | null
  • ignoreErrors -> made optional as well

JHashList

  • brainFeatures string -> number

HealthCheckType
Was BRUTE_FORCE but api can return 0 and 3200 -> (md5 and bcrypt?)

JHealthCheck

  • added hashtypeId number

JTask

  • totalAssignedAgents -> activeAgents?

Certain fields are made optional on the jtypes as they refer to relationships which may or may not be present as we do not know which includes have been sent in the request.

…s not valid yet) use redocly and spectral for linting the openapi spec and json+api convention
@correct-horse-battery-bench correct-horse-battery-bench changed the title Generate types from (locally fixed openapi spec as the current spec i… Generate validation zod schemas and types from openapi spec Mar 23, 2026
@correct-horse-battery-bench
Copy link
Copy Markdown
Contributor Author

Potential Improvements for the future

  • When zod schema does not validate then show error toast
  • Maybe turn off zod validation (apart from error message) in prod until we are relatively sure all schemas are correct
  • turn on strict mode
  • also validate requests going out

@correct-horse-battery-bench
Copy link
Copy Markdown
Contributor Author

Should only be merged after the release so we have some time for testing locally to notice errors during validation

@correct-horse-battery-bench correct-horse-battery-bench marked this pull request as ready for review March 24, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TASK]: Generate validation schemas against Openapi spec or write them by hand

1 participant