1- import { NextRequest , NextResponse } from 'next/server'
2- import { createLogger } from '@/lib/logs/console/logger'
3- import { generateRequestId } from '@/lib/utils'
4- import { validateJson } from '@/lib/guardrails/validate_json'
5- import { validateRegex } from '@/lib/guardrails/validate_regex'
1+ import { type NextRequest , NextResponse } from 'next/server'
62import { validateHallucination } from '@/lib/guardrails/validate_hallucination'
3+ import { validateJson } from '@/lib/guardrails/validate_json'
74import { validatePII } from '@/lib/guardrails/validate_pii'
5+ import { validateRegex } from '@/lib/guardrails/validate_regex'
6+ import { createLogger } from '@/lib/logs/console/logger'
7+ import { generateRequestId } from '@/lib/utils'
88
99const logger = createLogger ( 'GuardrailsValidateAPI' )
1010
@@ -14,7 +14,20 @@ export async function POST(request: NextRequest) {
1414
1515 try {
1616 const body = await request . json ( )
17- const { validationType, input, regex, knowledgeBaseId, threshold, topK, model, apiKey, workflowId, piiEntityTypes, piiMode, piiLanguage } = body
17+ const {
18+ validationType,
19+ input,
20+ regex,
21+ knowledgeBaseId,
22+ threshold,
23+ topK,
24+ model,
25+ apiKey,
26+ workflowId,
27+ piiEntityTypes,
28+ piiMode,
29+ piiLanguage,
30+ } = body
1831
1932 // Validate required fields
2033 if ( ! validationType ) {
@@ -44,7 +57,12 @@ export async function POST(request: NextRequest) {
4457 }
4558
4659 // Validate validationType
47- if ( validationType !== 'json' && validationType !== 'regex' && validationType !== 'hallucination' && validationType !== 'pii' ) {
60+ if (
61+ validationType !== 'json' &&
62+ validationType !== 'regex' &&
63+ validationType !== 'hallucination' &&
64+ validationType !== 'pii'
65+ ) {
4866 return NextResponse . json ( {
4967 success : true ,
5068 output : {
@@ -142,18 +160,19 @@ export async function POST(request: NextRequest) {
142160}
143161
144162/**
145- * Convert input to strfing for validation
163+ * Convert input to string for validation
146164 */
147165function convertInputToString ( input : any ) : string {
148166 if ( typeof input === 'string' ) {
149167 return input
150- } else if ( input === null || input === undefined ) {
168+ }
169+ if ( input === null || input === undefined ) {
151170 return ''
152- } else if ( typeof input === 'object' ) {
171+ }
172+ if ( typeof input === 'object' ) {
153173 return JSON . stringify ( input )
154- } else {
155- return String ( input )
156174 }
175+ return String ( input )
157176}
158177
159178/**
@@ -184,15 +203,17 @@ async function executeValidation(
184203 // Use TypeScript validators for all validation types
185204 if ( validationType === 'json' ) {
186205 return validateJson ( inputStr )
187- } else if ( validationType === 'regex' ) {
206+ }
207+ if ( validationType === 'regex' ) {
188208 if ( ! regex ) {
189209 return {
190210 passed : false ,
191211 error : 'Regex pattern is required' ,
192212 }
193213 }
194214 return validateRegex ( inputStr , regex )
195- } else if ( validationType === 'hallucination' ) {
215+ }
216+ if ( validationType === 'hallucination' ) {
196217 if ( ! knowledgeBaseId ) {
197218 return {
198219 passed : false ,
@@ -204,14 +225,15 @@ async function executeValidation(
204225 return await validateHallucination ( {
205226 userInput : inputStr ,
206227 knowledgeBaseId,
207- threshold : threshold != null ? parseFloat ( threshold ) : 3 , // Default threshold is 3 (confidence score, scores < 3 fail)
208- topK : topK ? parseInt ( topK ) : 10 , // Default topK is 10
228+ threshold : threshold != null ? Number . parseFloat ( threshold ) : 3 , // Default threshold is 3 (confidence score, scores < 3 fail)
229+ topK : topK ? Number . parseInt ( topK ) : 10 , // Default topK is 10
209230 model : model ,
210231 apiKey,
211232 workflowId,
212233 requestId,
213234 } )
214- } else if ( validationType === 'pii' ) {
235+ }
236+ if ( validationType === 'pii' ) {
215237 // PII validation using Presidio
216238 return await validatePII ( {
217239 text : inputStr ,
@@ -220,13 +242,9 @@ async function executeValidation(
220242 language : piiLanguage || 'en' ,
221243 requestId,
222244 } )
223- } else {
224- return {
225- passed : false ,
226- error : 'Unknown validation type' ,
227- }
245+ }
246+ return {
247+ passed : false ,
248+ error : 'Unknown validation type' ,
228249 }
229250}
230-
231-
232-
0 commit comments