feat(db): add validation stored procedure with THROW errors

This commit is contained in:
Joseph Doherty
2026-01-06 13:20:42 -05:00
parent e875244c8f
commit 2fd1cb1fe7
@@ -0,0 +1,51 @@
-- Migration: 048_CreateValidateSearchCriteriaProcedure
-- Stored procedure for strict validation with THROW errors
IF OBJECT_ID('dbo.usp_ValidateSearchCriteria', 'P') IS NOT NULL
DROP PROCEDURE dbo.usp_ValidateSearchCriteria;
GO
CREATE PROCEDURE dbo.usp_ValidateSearchCriteria(@SearchId INT)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Criteria VARCHAR(MAX);
DECLARE @ErrorMsg NVARCHAR(400);
-- Get criteria for the search
SELECT @Criteria = Criteria
FROM dbo.Search
WHERE ID = @SearchId;
-- Validate search exists
IF @@ROWCOUNT = 0
BEGIN
SET @ErrorMsg = CONCAT('Search ID ', @SearchId, ' not found');
THROW 50001, @ErrorMsg, 1;
END
-- Validate criteria not null
IF @Criteria IS NULL
BEGIN
SET @ErrorMsg = CONCAT('Search ID ', @SearchId, ' has no criteria');
THROW 50002, @ErrorMsg, 1;
END
-- Validate criteria not empty
IF @Criteria = ''
BEGIN
SET @ErrorMsg = CONCAT('Search ID ', @SearchId, ' has no criteria');
THROW 50002, @ErrorMsg, 1;
END
-- Validate JSON format
IF ISJSON(@Criteria) = 0
BEGIN
SET @ErrorMsg = CONCAT('Search ID ', @SearchId, ' has invalid JSON');
THROW 50003, @ErrorMsg, 1;
END
-- If we get here, validation passed
RETURN 0;
END
GO