-
Notifications
You must be signed in to change notification settings - Fork 1
chore(greenhouse): makes route search validation Zod v4–ready (filter by prefix before parse) and upgrade zod to v4 #1446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
| const VIOLATIONS_KNOWN_KEYS = ["searchTerm", "violationGroup"] as const | ||
|
|
||
| function validateViolationsSearch(search: Record<string, unknown>): z.infer<typeof searchSchema> { | ||
| const filtered = filterSearchParamsByPrefix(search, [...VIOLATIONS_KNOWN_KEYS], [ACTIVE_FILTERS_PREFIX]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, I think that is the correct way to get the allowed url search params and then performing validation 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can slightly improve it. Known keys are already in the schema maintaining a separate list introduces two sources of truths, meaning for any new key we'll have to add it in two places. Could you try to extract known keys from schema? as in using.
Object.keys(searchSchema.shape)
something like this
const searchSchema = z
.object({
service: z.string().optional(),
searchTerm: z.string().optional(),
})
.catchall(z.string().or(z.array(z.string())).optional())
function validateViolationsSearch(search: Record<string, unknown>): z.infer<typeof searchSchema> {
const filtered = filterSearchParamsByPrefix(search, Object.keys(searchSchema.shape), [ACTIVE_FILTERS_PREFIX])
return searchSchema.parse(filtered)
}
| const PLUGIN_PRESETS_KNOWN_KEYS = ["searchTerm"] as const | ||
|
|
||
| function validatePluginPresetsSearch(search: Record<string, unknown>): PluginPresetSearchParams { | ||
| const filtered = filterSearchParamsByPrefix(search, [...PLUGIN_PRESETS_KNOWN_KEYS], [SELECTED_FILTER_PREFIX]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above let's try to remove two sources of truths for known keys.
| const SERVICES_KNOWN_KEYS = ["service", "searchTerm"] as const | ||
|
|
||
| function validateServicesSearch(search: Record<string, unknown>): ServicesSearchParams { | ||
| const filtered = filterSearchParamsByPrefix(search, [...SERVICES_KNOWN_KEYS], [SELECTED_FILTER_PREFIX]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above let's try to remove two sources of truths for known keys.
| const VULNERABILITIES_KNOWN_KEYS = ["vulnerability", "searchTerm"] as const | ||
|
|
||
| function validateVulnerabilitiesSearch(search: Record<string, unknown>): VulnerabilitiesSearchParams { | ||
| const filtered = filterSearchParamsByPrefix(search, [...VULNERABILITIES_KNOWN_KEYS], [SELECTED_FILTER_PREFIX]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above let's try to remove two sources of truths for known keys.
| const ALERTS_KNOWN_KEYS = ["searchTerm", "showDetailsFor", "predefinedFilter"] as const | ||
|
|
||
| function validateAlertsSearch(search: Record<string, unknown>): z.infer<typeof searchSchema> { | ||
| const filtered = filterSearchParamsByPrefix( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above let's try to remove two sources of truths for known keys.
taymoor89
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this zod update touches code as well let's add changeset to update patch version of the apps changed.
Summary
Route search validation no longer relies on Zod’s
ctx.pathinz.preprocess(removed in Zod v4). A small helper filters URL search params by known keys and allowed prefixes before parsing with Zod, so the same behavior works with Zod v3 and v4. Upgraded Zod from v3 to v4 (exact version 4.3.6).Changes Made
utils.ts), supernova (lib/utils.ts), doop (lib/helpers.ts), and greenhouse (lib/helpers.ts)./servicesand/vulnerabilitiesroutes to validate search via the helper + schema (no preprocess usingctx.path)./alertsroute to use the helper with ACTIVE_FILTERS_PREFIX and PAUSED_FILTERS_PREFIX./violationsroute to use the helper with ACTIVE_FILTERS_PREFIX./admin/plugin-presetsroute to use the helper with SELECTED_FILTER_PREFIX (Zod v4–compatible).getFiltersForUrlasRecord<string, string | string[]>and adjusted implementation so search param types align with the route schema.instanceof Errorand fallback for empty message) and in doop/supernova validate-search return values.ctx.path.Related Issues
Screenshots (if applicable)
none
Testing Instructions
pnpm ipnpm TASKChecklist
PR Manifesto
Review the PR Manifesto for best practises.