feat: Create layerValidator.ts with layer validation functions

This commit is contained in:
2026-06-22 20:57:24 +00:00
parent d0599c67b3
commit 53b074bcfc
+121
View File
@@ -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<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 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;
}