Migrated auth to Nitro

This commit is contained in:
lucast committed 2026-09-29 00:23:30 +02:00
1 parent 27feb1ce95
commit eb4e9f0700
16 files changed
+517 -129

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
type AuthField = {
name: string
email?: boolean
minimumLength?: number
}
function receivedType(value: unknown) {
if (value === null) return "null"
if (Array.isArray(value)) return "array"
return typeof value
}
/** Match the first-issue messages returned by the legacy Zod validation hook. */
export function getAuthValidationMessage(body: unknown, fields: AuthField[]) {
if (!body || typeof body !== "object" || Array.isArray(body)) {
return body === undefined ? "Required" : `Expected object, received ${receivedType(body)}`
}
for (const field of fields) {
const value = (body as Record<string, unknown>)[field.name]
if (typeof value !== "string") {
return value === undefined
? "Required"
: `Expected string, received ${receivedType(value)}`
}
if (
field.email &&
!/^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i.test(
value,
)
) {
return "Invalid email"
}
if (field.minimumLength && value.length < field.minimumLength) {
return `String must contain at least ${field.minimumLength} character(s)`
}
}
return null
}