Cut down schemas by 90%

This commit is contained in:
Rory& committed 2025-11-29 22:22:41 +01:00
1 parent 60a24f0e39
commit 2ebe2a57fb
16 files changed
+302 -82

No files matched your search

+7 -1
View File
@@ -87,7 +87,13 @@ export interface RouteOptions {
export function route(opts: RouteOptions) {
let validate: AnyValidateFunction | undefined;
if (opts.requestBody) {
validate = ajv.getSchema(opts.requestBody);
try {
validate = ajv.getSchema(opts.requestBody);
} catch (e) {
console.error("AJV getSchema failed!");
throw e;
}
if (!validate)
throw new Error(`Body schema ${opts.requestBody} not found`);
}
+19 -10
View File
@@ -21,20 +21,29 @@ import addFormats from "ajv-formats";
import fs from "fs";
import path from "path";
const SchemaPath = path.join(
__dirname,
"..",
"..",
"assets",
"schemas.json",
);
const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }));
const SchemaPath = path.join(__dirname, "..", "..", "assets", "schemas.json");
const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }).replaceAll("#/definitions/", ""));
// const schemas2 = {...schemas, definitions: {...schemas, }};
// console.log(schemas);
// for (const schemaName in schemas) {
// const schema = schemas[schemaName];
// if ("x-sb-defs" in schema) {
// console.log("[Validator] Adding definitions for schema", schemaName, ":", schema["x-sb-defs"]);
// for (const defKey of schema["x-sb-defs"]) {
// console.log(" - ", defKey, typeof schemas[defKey] === "object");
// schema.definitions = schema.definitions || {};
// if (schemas[defKey]) schema.definitions[defKey] = schemas[defKey];
// else console.warn("[Validator] Definition", defKey, "not found for schema", schemaName);
// }
// }
// }
export const ajv = new Ajv({
allErrors: true,
parseDate: true,
allowDate: true,
schemas,
schemas: schemas,
coerceTypes: true,
messages: true,
strict: true,
@@ -47,7 +56,7 @@ addFormats(ajv);
export function validateSchema<G extends object>(schema: string, data: G): G {
const valid = ajv.validate(schema, data);
if (!valid) {
console.log("[Validator] Validation error in ", schema)
console.log("[Validator] Validation error in ", schema);
throw ajv.errors;
}
return data;
@@ -1,11 +1,18 @@
import { ApplicationCommandHandlerType, ApplicationCommandOption, ApplicationCommandType, ApplicationIntegrationType, InteractionContextType } from "@spacebar/schemas";
import {
ApplicationCommandHandlerType,
ApplicationCommandOption,
ApplicationCommandType,
ApplicationIntegrationType,
InteractionContextType,
StringStringDictionary,
} from "@spacebar/schemas";
export interface ApplicationCommandCreateSchema {
type?: ApplicationCommandType;
name: string;
name_localizations?: Record<string, string>;
name_localizations?: StringStringDictionary;
description?: string;
description_localizations?: Record<string, string>;
description_localizations?: StringStringDictionary;
options?: ApplicationCommandOption[];
default_member_permissions?: string;
/*
@@ -1,4 +1,4 @@
import { ApplicationCommandOption, Snowflake } from "@spacebar/schemas";
import { ApplicationCommandOption, Snowflake, StringStringDictionary } from "@spacebar/schemas";
export interface ApplicationCommandSchema {
id?: Snowflake;
@@ -6,10 +6,10 @@ export interface ApplicationCommandSchema {
application_id: Snowflake;
guild_id?: Snowflake;
name: string;
name_localizations?: Record<string, string>;
name_localizations?: StringStringDictionary;
name_localized?: string | null;
description: string;
description_localizations?: Record<string, string>;
description_localizations?: StringStringDictionary;
description_localized?: string | null;
options?: ApplicationCommandOption[];
default_member_permissions: string | null;
+2 -1
View File
@@ -21,9 +21,10 @@ import { ErrorObject } from "ajv";
export interface FieldErrorResponse {
code: number;
message: string;
errors: Record<string, ObjectErrorContent>;
errors: ErrorList;
}
export type ErrorList = Record<string, ObjectErrorContent>;
export type ErrorContent = { code: string; message: string };
export type ObjectErrorContent = { _errors: ErrorContent[] };
+5 -6
View File
@@ -98,7 +98,7 @@ export class Server {
await new Promise<void>((res) => {
this.http = server.listen(this.options.port, () => res());
});
if(this.options.serverInitLogging) this.log("info", `[Server] started on ${this.options.host}:${this.options.port}`);
if (this.options.serverInitLogging) this.log("info", `[Server] started on ${this.options.host}:${this.options.port}`);
}
}
@@ -150,17 +150,16 @@ export class Server {
if (!path.length) path = "/"; // first root index.js file must have a / path
try {
var router = require(file);
let router = require(file);
if (router.router) router = router.router;
if (router.default) router = router.default;
if (!router || router?.prototype?.constructor?.name !== "router")
throw `File doesn't export any default router`;
if (!router || router?.prototype?.constructor?.name !== "router") throw `File doesn't export any default router`;
if (this.options.errorHandler) router.use(this.options.errorHandler);
this.app.use(path, <Router>router);
if(this.options.serverInitLogging) this.log("verbose", `[Server] Route ${path} registered`);
if (this.options.serverInitLogging) this.log("verbose", `[Server] Route ${path} registered`);
return router;
} catch (error) {
console.error(new Error(`[Server] Failed to register route ${path}: ${error}`));