mirror of
https://github.com/spacebarchat/server.git
synced 2026-11-13 03:19:11 +00:00
Identify: log invalid values
This commit is contained in:
1 parent
9062198c08
commit
c944a20aec
4 files changed
+19
No files matched your search
Binary file not shown.
Binary file not shown.
@@ -64,6 +64,7 @@ import {
|
||||
ChannelType,
|
||||
DefaultUserGuildSettings,
|
||||
DMChannel,
|
||||
getInvalidSchemaValues,
|
||||
IdentifySchema,
|
||||
PrivateStatus,
|
||||
PrivateUserProjection,
|
||||
@@ -94,6 +95,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
|
||||
data.d = validateSchema<IdentifySchema>("IdentifySchema", data.d);
|
||||
} catch (e) {
|
||||
console.error("AJV validation of Identify schema failed:", e);
|
||||
console.error("Provided values:");
|
||||
if (Array.isArray(e))
|
||||
for (const invalidSchemaValue of getInvalidSchemaValues(data.d, e)) {
|
||||
console.error(`${invalidSchemaValue.instancePath}:`, invalidSchemaValue.value);
|
||||
}
|
||||
|
||||
check.call(this, IdentifySchema, data.d);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import Ajv, { SchemaObject } from "ajv";
|
||||
import addFormats from "ajv-formats";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import instance from "@spacebar/api/routes/policies/instance";
|
||||
|
||||
const SchemaPath = path.join(__dirname, "..", "..", "assets", "schemas.json");
|
||||
const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }).replaceAll("#/definitions/", ""));
|
||||
@@ -67,3 +68,14 @@ export function validateSchema<G extends object>(schema: string, data: G): G {
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export function getInvalidSchemaValues(data: object, errors: typeof ajv.errors) {
|
||||
if (errors == null) return [];
|
||||
return errors.map((e) => getInvalidSchemaValue(data, e.instancePath));
|
||||
}
|
||||
|
||||
function getInvalidSchemaValue(data: object, instancePath: string) {
|
||||
const actualInstancePath = instancePath.startsWith("/") ? instancePath.slice(1) : instancePath;
|
||||
if (actualInstancePath.includes("/")) return getInvalidSchemaValue(data, actualInstancePath.split("/")[0]);
|
||||
return { instancePath, value: data };
|
||||
}
|
||||
Reference in new issue
Block a user