From 5488874476cb31a8d57b75a09282f6edb524807e Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Wed, 30 Apr 2025 19:11:43 -0500 Subject: [PATCH] delete the null eater --- src/api/util/handlers/route.ts | 3 +-- src/util/schemas/Validator.ts | 39 +--------------------------------- 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/src/api/util/handlers/route.ts b/src/api/util/handlers/route.ts index 2c98783a..d09bbe55 100644 --- a/src/api/util/handlers/route.ts +++ b/src/api/util/handlers/route.ts @@ -28,7 +28,6 @@ import { ajv, getPermission, getRights, - normalizeBody, } from "@spacebar/util"; import { AnyValidateFunction } from "ajv/dist/core"; import { NextFunction, Request, Response } from "express"; @@ -121,7 +120,7 @@ export function route(opts: RouteOptions) { } if (validate) { - const valid = validate(normalizeBody(req.body)); + const valid = validate(req.body); if (!valid) { const fields: Record< string, diff --git a/src/util/schemas/Validator.ts b/src/util/schemas/Validator.ts index 1de511d3..9a7c1ebb 100644 --- a/src/util/schemas/Validator.ts +++ b/src/util/schemas/Validator.ts @@ -46,44 +46,7 @@ export const ajv = new Ajv({ addFormats(ajv); export function validateSchema(schema: string, data: G): G { - const valid = ajv.validate(schema, normalizeBody(data)); + const valid = ajv.validate(schema, data); if (!valid) throw ajv.errors; return data; } - -// Normalizer is introduced to workaround https://github.com/ajv-validator/ajv/issues/1287 -// this removes null values as ajv doesn't treat them as undefined -// normalizeBody allows to handle circular structures without issues -// taken from https://github.com/serverless/serverless/blob/master/lib/classes/ConfigSchemaHandler/index.js#L30 (MIT license) -export const normalizeBody = (body: object = {}) => { - const normalizedObjectsSet = new WeakSet(); - const normalizeObject = (object: object) => { - if (normalizedObjectsSet.has(object)) return; - normalizedObjectsSet.add(object); - if (Array.isArray(object)) { - for (const [, value] of object.entries()) { - if (typeof value === "object") normalizeObject(value); - } - } else { - for (const [key, value] of Object.entries(object)) { - if (value == null) { - if ( - key === "icon" || - key === "avatar" || - key === "banner" || - key === "splash" || - key === "discovery_splash" - ) - continue; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - //@ts-ignore - delete object[key]; - } else if (typeof value === "object") { - normalizeObject(value); - } - } - } - }; - normalizeObject(body); - return body; -};