Add session join schema and enhance error serialization

Introduces a Zod schema for the Minecraft session join endpoint, validating accessToken, selectedProfile, and serverId. Updates ValidationError to support serialization via YggdrasilError and SessionError for improved error formatting.
This commit is contained in:
Gilles Lazures 2025-12-24 04:54:53 +01:00
parent 2519d8078a
commit 3d0f5c54af
2 changed files with 41 additions and 6 deletions

View File

@ -1,5 +1,6 @@
const path = require("node:path")
const DefaultError = require("./DefaultError") const DefaultError = require("./DefaultError")
const YggdrasilError = require("./YggdrasilError")
const SessionError = require("./SessionError")
const logger = require("../modules/logger") const logger = require("../modules/logger")
class ValidationError extends DefaultError { class ValidationError extends DefaultError {
@ -34,11 +35,23 @@ class ValidationError extends DefaultError {
serialize() { serialize() {
if (this.config.errorFormat === "YggdrasilError") { if (this.config.errorFormat === "YggdrasilError") {
return { const err = new YggdrasilError(
error: this.config.errorName || "IllegalArgumentException", this.code,
errorMessage: this.message, this.config.errorName || "IllegalArgumentException",
cause: JSON.stringify(this.formattedErrors) this.message,
} JSON.stringify(this.formattedErrors)
)
return err.serialize()
}
if (this.config.errorFormat === "SessionError") {
const err = new SessionError(
this.code,
this.config.errorName || "Forbidden",
this.message,
this.context.path || ""
)
return err.serialize()
} }
return { return {

View File

@ -0,0 +1,22 @@
const z = require("zod")
module.exports = {
POST: {
body: z.object({
accessToken: z.string()
.min(1, { message: "Access Token is required." }),
selectedProfile: z.string()
.length(32, { message: "Selected Profile must be a valid UUID without dashes." })
.regex(/^[0-9a-fA-F]+$/, { message: "Selected Profile must be hexadecimal." }),
serverId: z.string()
.min(1, { message: "Server ID is required." })
.max(42, { message: "Server ID is too long." })
}),
error: {
code: 400,
message: "Missing or invalid parameters.",
errorFormat: "SessionError",
errorName: "Forbidden"
}
}
}