-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from e-roy:server-side-validation-and-sanitize…
…d-input server side validation and sanitized input
- Loading branch information
Showing
9 changed files
with
175 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// lib/sanitize-content.ts | ||
import { escape } from "html-escaper"; | ||
|
||
export const sanitizeContent = (content: string): string => { | ||
return escape(content); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// lib/validate/common.ts | ||
import { z } from "zod"; | ||
import { JSONValue } from "ai"; | ||
|
||
export const safetySettingSchema = z.object({ | ||
harassment: z.number(), | ||
hateSpeech: z.number(), | ||
sexuallyExplicit: z.number(), | ||
dangerousContent: z.number(), | ||
}); | ||
|
||
const jsonValueSchema: z.ZodSchema<JSONValue> = z.lazy(() => | ||
z.union([ | ||
z.null(), | ||
z.string(), | ||
z.number(), | ||
z.boolean(), | ||
z.array(jsonValueSchema), | ||
z.record(jsonValueSchema), | ||
]) | ||
); | ||
|
||
const functionCallSchema = z.object({ | ||
arguments: z.string().optional(), | ||
name: z.string().optional(), | ||
}); | ||
|
||
const toolCallSchema = z.object({ | ||
id: z.string(), | ||
type: z.string(), | ||
function: z.object({ | ||
name: z.string(), | ||
arguments: z.string(), | ||
}), | ||
}); | ||
|
||
export const messageSchema = z.object({ | ||
id: z.string().optional(), | ||
tool_call_id: z.string().optional(), | ||
createdAt: z.date().optional(), | ||
content: z.string(), | ||
ui: z.any().optional(), | ||
role: z.enum(["system", "user", "assistant", "function", "data", "tool"]), | ||
name: z.string().optional(), | ||
function_call: functionCallSchema.optional(), | ||
data: jsonValueSchema.optional(), | ||
tool_calls: z.array(toolCallSchema).optional(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// lib/validate/pro-request-schema.ts | ||
import { z } from "zod"; | ||
import { messageSchema, safetySettingSchema } from "./common"; | ||
|
||
export const proRequestSchema = z.object({ | ||
messages: z.array(messageSchema), | ||
general_settings: z.object({ | ||
temperature: z.number(), | ||
maxLength: z.number(), | ||
topP: z.number(), | ||
topK: z.number(), | ||
}), | ||
safety_settings: safetySettingSchema.optional(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// lib/validate/vision-request-schema.ts | ||
import { z } from "zod"; | ||
import { safetySettingSchema } from "./common"; | ||
|
||
export const visionRequestSchema = z.object({ | ||
message: z.string(), | ||
media: z.array(z.string()), | ||
media_types: z.array(z.string()), | ||
general_settings: z.object({ | ||
temperature: z.number(), | ||
maxLength: z.number(), | ||
topP: z.number(), | ||
topK: z.number(), | ||
}), | ||
safety_settings: safetySettingSchema.optional(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters