From 53b074bcfceb5df97561d23814e3ddf7e06c747a Mon Sep 17 00:00:00 2001 From: Leopoldadmin Date: Mon, 22 Jun 2026 20:57:24 +0000 Subject: [PATCH] feat: Create layerValidator.ts with layer validation functions --- backend/src/layerValidator.ts | 121 ++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 backend/src/layerValidator.ts diff --git a/backend/src/layerValidator.ts b/backend/src/layerValidator.ts new file mode 100644 index 0000000..1e5b999 --- /dev/null +++ b/backend/src/layerValidator.ts @@ -0,0 +1,121 @@ +import { FastifyRequest, FastifyReply } from 'fastify'; + +// Type definitions +interface Layer { + id?: string; + project_id?: string; + name?: string; + visible?: number; + locked?: number; + color?: string; + line_type?: string; + transparency?: number; + sort_order?: number; + created_at?: string; +} + +// Validate layer creation data +export async function validateLayerCreation(request: FastifyRequest, reply: FastifyReply) { + const { name, visible, locked, color, line_type, transparency, sort_order } = request.body as Layer; + + // Validate name + if (name !== undefined && (typeof name !== 'string' || name.trim().length === 0)) { + return reply.status(400).send({ error: 'Invalid layer name' }); + } + + // Validate visible + if (visible !== undefined && typeof visible !== 'number') { + return reply.status(400).send({ error: 'Visible must be a number' }); + } + + // Validate locked + if (locked !== undefined && typeof locked !== 'number') { + return reply.status(400).send({ error: 'Locked must be a number' }); + } + + // Validate color + if (color !== undefined && typeof color !== 'string') { + return reply.status(400).send({ error: 'Color must be a string' }); + } + + // Validate line_type + if (line_type !== undefined && typeof line_type !== 'string') { + return reply.status(400).send({ error: 'Line type must be a string' }); + } + + // Validate transparency + if (transparency !== undefined && (typeof transparency !== 'number' || transparency < 0 || transparency > 1)) { + return reply.status(400).send({ error: 'Transparency must be a number between 0 and 1' }); + } + + // Validate sort_order + if (sort_order !== undefined && typeof sort_order !== 'number') { + return reply.status(400).send({ error: 'Sort order must be a number' }); + } + + // If all validations pass, continue to the next handler + return; +} + +// Validate layer update data +export async function validateLayerUpdate(request: FastifyRequest, reply: FastifyReply) { + const { name, visible, locked, color, line_type, transparency, sort_order } = request.body as Partial; + + // Validate name + if (name !== undefined && (typeof name !== 'string' || name.trim().length === 0)) { + return reply.status(400).send({ error: 'Invalid layer name' }); + } + + // Validate visible + if (visible !== undefined && typeof visible !== 'number') { + return reply.status(400).send({ error: 'Visible must be a number' }); + } + + // Validate locked + if (locked !== undefined && typeof locked !== 'number') { + return reply.status(400).send({ error: 'Locked must be a number' }); + } + + // Validate color + if (color !== undefined && typeof color !== 'string') { + return reply.status(400).send({ error: 'Color must be a string' }); + } + + // Validate line_type + if (line_type !== undefined && typeof line_type !== 'string') { + return reply.status(400).send({ error: 'Line type must be a string' }); + } + + // Validate transparency + if (transparency !== undefined && (typeof transparency !== 'number' || transparency < 0 || transparency > 1)) { + return reply.status(400).send({ error: 'Transparency must be a number between 0 and 1' }); + } + + // Validate sort_order + if (sort_order !== undefined && typeof sort_order !== 'number') { + return reply.status(400).send({ error: 'Sort order must be a number' }); + } + + // If all validations pass, continue to the next handler + return; +} + +// Validate layer reorder data +export async function validateLayerReorder(request: FastifyRequest, reply: FastifyReply) { + const { layerIds } = request.body as { layerIds: string[] }; + + // Validate layerIds + if (!Array.isArray(layerIds)) { + return reply.status(400).send({ error: 'Layer IDs must be an array' }); + } + + // Validate each layer ID + for (const id of layerIds) { + if (typeof id !== 'string' || id.trim().length === 0) { + return reply.status(400).send({ error: 'Each layer ID must be a non-empty string' }); + } + } + + // If all validations pass, continue to the next handler + return; +} \ No newline at end of file