Skip to content
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

Mock API should match real API logic around resource selector combinations #264

Closed
david-crespo opened this issue Aug 22, 2024 · 1 comment

Comments

@david-crespo
Copy link
Collaborator

In Nexus, if you try to select something by { path: { router: '<id>' }, query: { project, vpc } }, the API will 400 because parent selectors are not allowed when the thing is specified by ID. Our validators don't error because the OpenAPI schema can't represent the valid combinations properly. But we might be able to do something sneaky here. Worst case, we can do something by hand on the console side. But ew.

function validateParams<S extends ZodSchema>(schema: S, req: Request, pathParams: PathParams) {
const rawParams = new URLSearchParams(new URL(req.url).search)
const params: [string, unknown][] = []
// Ensure numeric params like \`limit\` are parsed as numbers
for (const [name, value] of rawParams) {
params.push([name, isNaN(Number(value)) ? value : Number(value)])
}
const result = schema.safeParse({
path: pathParams,
query: Object.fromEntries(params),
})
if (result.success) {
return { params: result.data }
}
// if any of the errors come from path params, just 404 — the resource cannot
// exist if there's no valid name
const status = result.error.issues.some((e) => e.path[0] === 'path') ? 404 : 400
const error_code = status === 404 ? 'NotFound' : 'InvalidRequest'
const message = 'Zod error for params: ' + JSON.stringify(result.error)
return { paramsErr: json({ error_code, message }, { status }) }
}

@david-crespo
Copy link
Collaborator Author

Decided to do this manually in the console since we already have a manually-implemented set of DB lookup functions that are a good spot for it. oxidecomputer/console#2396

@david-crespo david-crespo closed this as not planned Won't fix, can't repro, duplicate, stale Aug 26, 2024
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

No branches or pull requests

1 participant