Skip to content

Commit 29fcf52

Browse files
author
waleed
committed
ack PR comments
1 parent 0438d1b commit 29fcf52

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

apps/sim/app/api/guardrails/validate/route.ts

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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'
62
import { validateHallucination } from '@/lib/guardrails/validate_hallucination'
3+
import { validateJson } from '@/lib/guardrails/validate_json'
74
import { 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

99
const 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
*/
147165
function 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-

apps/sim/lib/guardrails/validate_pii.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { spawn } from 'child_process'
2-
import path from 'path'
32
import fs from 'fs'
3+
import path from 'path'
44
import { createLogger } from '@/lib/logs/console/logger'
55

66
const logger = createLogger('PIIValidator')
@@ -31,7 +31,7 @@ export interface PIIValidationResult {
3131

3232
/**
3333
* Validate text for PII using Microsoft Presidio
34-
*
34+
*
3535
* Supports two modes:
3636
* - block: Fails validation if any PII is detected
3737
* - mask: Passes validation and returns masked text with PII replaced
@@ -97,11 +97,7 @@ async function executePythonPIIDetection(
9797

9898
const timeout = setTimeout(() => {
9999
python.kill()
100-
resolve({
101-
passed: false,
102-
error: 'PII validation timeout',
103-
detectedEntities: [],
104-
})
100+
reject(new Error('PII validation timeout'))
105101
}, DEFAULT_TIMEOUT)
106102

107103
// Write input to stdin as JSON
@@ -179,11 +175,11 @@ async function executePythonPIIDetection(
179175
logger.error(`[${requestId}] Failed to spawn Python process`, {
180176
error: error.message,
181177
})
182-
resolve({
183-
passed: false,
184-
error: `Failed to execute Python: ${error.message}. Make sure Python 3 and Presidio are installed.`,
185-
detectedEntities: [],
186-
})
178+
reject(
179+
new Error(
180+
`Failed to execute Python: ${error.message}. Make sure Python 3 and Presidio are installed.`
181+
)
182+
)
187183
})
188184
})
189185
}
@@ -206,18 +202,18 @@ export const SUPPORTED_PII_ENTITIES = {
206202
PHONE_NUMBER: 'Phone number',
207203
MEDICAL_LICENSE: 'Medical license number',
208204
URL: 'URL',
209-
205+
210206
// USA
211207
US_BANK_NUMBER: 'US bank account number',
212208
US_DRIVER_LICENSE: 'US driver license',
213209
US_ITIN: 'US Individual Taxpayer Identification Number',
214210
US_PASSPORT: 'US passport number',
215211
US_SSN: 'US Social Security Number',
216-
212+
217213
// UK
218214
UK_NHS: 'UK NHS number',
219215
UK_NINO: 'UK National Insurance Number',
220-
216+
221217
// Other countries
222218
ES_NIF: 'Spanish NIF number',
223219
ES_NIE: 'Spanish NIE number',
@@ -244,4 +240,3 @@ export const SUPPORTED_PII_ENTITIES = {
244240
} as const
245241

246242
export type PIIEntityType = keyof typeof SUPPORTED_PII_ENTITIES
247-

0 commit comments

Comments
 (0)