mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-26 12:16:13 +01:00
🔧 fix: Chat Middleware, Zod Conversion, Auto-Save and S3 URL Refresh (#6720)
* 🔧 feat: Add configurable S3 URL refresh expiry time
* fix: Set default width and height for URLIcon component in case container style results in NaN
* refactor: Enhance auto-save functionality with debounced restore methods
* feat: Add support for additionalProperties in JSON schema conversion to Zod
* test: Add tests for additionalProperties handling in JSON schema to Zod conversion
* chore: Reorder import statements for better readability in ask route
* fix: Handle additional successful response status code (200) in SSE error handler
* fix: add missing rate limiting middleware for bedrock and agent chat routes
* fix: update moderation middleware to check feature flag before processing requests
* fix: add moderation middleware to chat routes for text moderation
* Revert "refactor: Enhance auto-save functionality with debounced restore methods"
This reverts commit d2e4134d1f.
* refactor: Move base64 encoding/decoding functions to top-level scope and optimize input handling
This commit is contained in:
parent
95ecd05046
commit
953e9732d9
12 changed files with 300 additions and 73 deletions
|
|
@ -1,4 +1,3 @@
|
|||
/* eslint-disable jest/no-conditional-expect */
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// zod.spec.ts
|
||||
import { z } from 'zod';
|
||||
|
|
@ -468,6 +467,156 @@ describe('convertJsonSchemaToZod', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('additionalProperties handling', () => {
|
||||
it('should allow any additional properties when additionalProperties is true', () => {
|
||||
const schema: JsonSchemaType = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
},
|
||||
additionalProperties: true,
|
||||
};
|
||||
const zodSchema = convertJsonSchemaToZod(schema);
|
||||
|
||||
// Should accept the defined property
|
||||
expect(zodSchema?.parse({ name: 'John' })).toEqual({ name: 'John' });
|
||||
|
||||
// Should also accept additional properties of any type
|
||||
expect(zodSchema?.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
|
||||
expect(zodSchema?.parse({ name: 'John', isActive: true })).toEqual({
|
||||
name: 'John',
|
||||
isActive: true,
|
||||
});
|
||||
expect(zodSchema?.parse({ name: 'John', tags: ['tag1', 'tag2'] })).toEqual({
|
||||
name: 'John',
|
||||
tags: ['tag1', 'tag2'],
|
||||
});
|
||||
});
|
||||
|
||||
it('should validate additional properties according to schema when additionalProperties is an object', () => {
|
||||
const schema: JsonSchemaType = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
},
|
||||
additionalProperties: { type: 'number' },
|
||||
};
|
||||
const zodSchema = convertJsonSchemaToZod(schema);
|
||||
|
||||
// Should accept the defined property
|
||||
expect(zodSchema?.parse({ name: 'John' })).toEqual({ name: 'John' });
|
||||
|
||||
// Should accept additional properties that match the additionalProperties schema
|
||||
expect(zodSchema?.parse({ name: 'John', age: 30, score: 100 })).toEqual({
|
||||
name: 'John',
|
||||
age: 30,
|
||||
score: 100,
|
||||
});
|
||||
|
||||
// Should reject additional properties that don't match the additionalProperties schema
|
||||
expect(() => zodSchema?.parse({ name: 'John', isActive: true })).toThrow();
|
||||
expect(() => zodSchema?.parse({ name: 'John', tags: ['tag1', 'tag2'] })).toThrow();
|
||||
});
|
||||
|
||||
it('should strip additional properties when additionalProperties is false or not specified', () => {
|
||||
const schema: JsonSchemaType = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
age: { type: 'number' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
const zodSchema = convertJsonSchemaToZod(schema);
|
||||
|
||||
// Should accept the defined properties
|
||||
expect(zodSchema?.parse({ name: 'John', age: 30 })).toEqual({ name: 'John', age: 30 });
|
||||
|
||||
// Current implementation strips additional properties when additionalProperties is false
|
||||
const objWithExtra = { name: 'John', age: 30, isActive: true };
|
||||
expect(zodSchema?.parse(objWithExtra)).toEqual({ name: 'John', age: 30 });
|
||||
|
||||
// Test with additionalProperties not specified (should behave the same)
|
||||
const schemaWithoutAdditionalProps: JsonSchemaType = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
age: { type: 'number' },
|
||||
},
|
||||
};
|
||||
const zodSchemaWithoutAdditionalProps = convertJsonSchemaToZod(schemaWithoutAdditionalProps);
|
||||
|
||||
expect(zodSchemaWithoutAdditionalProps?.parse({ name: 'John', age: 30 })).toEqual({
|
||||
name: 'John',
|
||||
age: 30,
|
||||
});
|
||||
|
||||
// Current implementation strips additional properties when additionalProperties is not specified
|
||||
const objWithExtra2 = { name: 'John', age: 30, isActive: true };
|
||||
expect(zodSchemaWithoutAdditionalProps?.parse(objWithExtra2)).toEqual({
|
||||
name: 'John',
|
||||
age: 30,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle complex nested objects with additionalProperties', () => {
|
||||
const schema: JsonSchemaType = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
user: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
profile: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
bio: { type: 'string' },
|
||||
},
|
||||
additionalProperties: true,
|
||||
},
|
||||
},
|
||||
additionalProperties: { type: 'string' },
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
};
|
||||
const zodSchema = convertJsonSchemaToZod(schema);
|
||||
|
||||
const validData = {
|
||||
user: {
|
||||
name: 'John',
|
||||
profile: {
|
||||
bio: 'Developer',
|
||||
location: 'New York', // Additional property allowed in profile
|
||||
website: 'https://example.com', // Additional property allowed in profile
|
||||
},
|
||||
role: 'admin', // Additional property of type string allowed in user
|
||||
level: 'senior', // Additional property of type string allowed in user
|
||||
},
|
||||
};
|
||||
|
||||
expect(zodSchema?.parse(validData)).toEqual(validData);
|
||||
|
||||
// Current implementation strips additional properties at the top level
|
||||
// when additionalProperties is false
|
||||
const dataWithExtraTopLevel = {
|
||||
user: { name: 'John' },
|
||||
extraField: 'not allowed', // This should be stripped
|
||||
};
|
||||
expect(zodSchema?.parse(dataWithExtraTopLevel)).toEqual({ user: { name: 'John' } });
|
||||
|
||||
// Should reject additional properties in user that don't match the string type
|
||||
expect(() =>
|
||||
zodSchema?.parse({
|
||||
user: {
|
||||
name: 'John',
|
||||
age: 30, // Not a string
|
||||
},
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('empty object handling', () => {
|
||||
it('should return undefined for empty object schemas when allowEmptyObject is false', () => {
|
||||
const emptyObjectSchemas = [
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ export type JsonSchemaType = {
|
|||
properties?: Record<string, JsonSchemaType>;
|
||||
required?: string[];
|
||||
description?: string;
|
||||
additionalProperties?: boolean | JsonSchemaType;
|
||||
};
|
||||
|
||||
function isEmptyObjectSchema(jsonSchema?: JsonSchemaType): boolean {
|
||||
|
|
@ -72,7 +73,20 @@ export function convertJsonSchemaToZod(
|
|||
} else {
|
||||
objectSchema = objectSchema.partial();
|
||||
}
|
||||
zodSchema = objectSchema;
|
||||
|
||||
// Handle additionalProperties for open-ended objects
|
||||
if (schema.additionalProperties === true) {
|
||||
// This allows any additional properties with any type
|
||||
zodSchema = objectSchema.passthrough();
|
||||
} else if (typeof schema.additionalProperties === 'object') {
|
||||
// For specific additional property types
|
||||
const additionalSchema = convertJsonSchemaToZod(
|
||||
schema.additionalProperties as JsonSchemaType,
|
||||
);
|
||||
zodSchema = objectSchema.catchall(additionalSchema as z.ZodType);
|
||||
} else {
|
||||
zodSchema = objectSchema;
|
||||
}
|
||||
} else {
|
||||
zodSchema = z.unknown();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue