Merge pull request #1653 from elysiajs/revert-1634-fix/record-response-validation

Revert "🔧 fix: t.Record validation failure in response schemas"
This commit is contained in:
SaltyAom
2026-01-03 21:30:43 +07:00
committed by GitHub
2 changed files with 22 additions and 88 deletions
+22 -59
View File
@@ -80,39 +80,6 @@ export const isOptional = (
return !!schema && OptionalKind in schema
}
export const hasPatternProperties = (
_schema: TAnySchema | TypeCheck<any> | ElysiaTypeCheck<any>
): boolean => {
if (!_schema) return false
// @ts-expect-error private property
const schema: TAnySchema = (_schema as TypeCheck<any>)?.schema ?? _schema
if ('patternProperties' in schema) return true
if (schema[Kind] === 'Import' && _schema.References)
return _schema.References().some(hasPatternProperties)
if (schema.anyOf) return schema.anyOf.some(hasPatternProperties)
if (schema.oneOf) return schema.oneOf.some(hasPatternProperties)
if (schema.someOf) return schema.someOf.some(hasPatternProperties)
if (schema.allOf) return schema.allOf.some(hasPatternProperties)
if (schema.not) return hasPatternProperties(schema.not)
if (schema.type === 'object' && schema.properties) {
const properties = schema.properties as Record<string, TAnySchema>
for (const key of Object.keys(properties)) {
if (hasPatternProperties(properties[key])) return true
}
}
if (schema.type === 'array' && schema.items && !Array.isArray(schema.items))
return hasPatternProperties(schema.items)
return false
}
export const hasAdditionalProperties = (
_schema: TAnySchema | TypeCheck<any> | ElysiaTypeCheck<any>
): boolean => {
@@ -541,7 +508,7 @@ export const getSchemaValidator = <
let mirror: Function
if (normalize === true || normalize === 'exactMirror')
try {
mirror = createMirror(schema as TAnySchema, {
mirror = createMirror(schema as TSchema, {
TypeCompiler,
sanitize: sanitize?.(),
modules
@@ -718,15 +685,14 @@ export const getSchemaValidator = <
})
])
if (schema.type === 'object' && hasAdditional && !('patternProperties' in schema))
if (schema.type === 'object' && hasAdditional)
schema.additionalProperties = false
}
} else {
if (
schema.type === 'object' &&
('additionalProperties' in schema === false ||
forceAdditionalProperties) &&
!('patternProperties' in schema)
forceAdditionalProperties)
)
schema.additionalProperties = additionalProperties
else
@@ -736,7 +702,6 @@ export const getSchemaValidator = <
to(schema) {
if (!schema.properties) return schema;
if ("additionalProperties" in schema) return schema;
if ("patternProperties" in schema) return schema;
return t.Object(schema.properties, {
...schema,
@@ -800,10 +765,10 @@ export const getSchemaValidator = <
if (validator?.schema?.config) delete validator.schema.config
}
if (normalize && !hasPatternProperties(schema)) {
if (normalize && schema.additionalProperties === false) {
if (normalize === true || normalize === 'exactMirror') {
try {
validator.Clean = createMirror(schema as TAnySchema, {
validator.Clean = createMirror(schema, {
TypeCompiler,
sanitize: sanitize?.(),
modules
@@ -939,27 +904,25 @@ export const getSchemaValidator = <
if (compiled?.schema?.config) delete compiled.schema.config
}
if (normalize && !hasPatternProperties(schema)) {
if (normalize === true || normalize === 'exactMirror') {
try {
compiled.Clean = createMirror(schema as TAnySchema, {
TypeCompiler,
sanitize: sanitize?.(),
modules
})
} catch (error) {
console.warn(
'Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues'
)
console.dir(schema, {
depth: null
})
if (normalize === true || normalize === 'exactMirror') {
try {
compiled.Clean = createMirror(schema, {
TypeCompiler,
sanitize: sanitize?.(),
modules
})
} catch (error) {
console.warn(
'Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues'
)
console.dir(schema, {
depth: null
})
compiled.Clean = createCleaner(schema)
}
} else if (normalize === 'typebox')
compiled.Clean = createCleaner(schema)
}
}
} else if (normalize === 'typebox')
compiled.Clean = createCleaner(schema)
} else {
compiled = {
provider: 'standard',
-29
View File
@@ -562,33 +562,4 @@ describe('Response Validator', () => {
expect(result.join('')).toContain('data: {"name":"Name"}')
})
it('validate Record with nested objects', async () => {
const app = new Elysia().get('/', () => ({
list: [{
toto: { bar: 1 },
foo: { link: 'first' }
}],
one: {
toto: { bar: 0 },
foo: { link: 'second' }
}
}), {
response: {
200: t.Object({
list: t.Array(t.Object({
toto: t.Object({ bar: t.Integer() }),
foo: t.Record(t.String(), t.String())
})),
one: t.Object({
toto: t.Object({ bar: t.Integer() }),
foo: t.Record(t.String(), t.String())
})
})
}
})
const res = await app.handle(req('/'))
expect(res.status).toBe(200)
})
})