11import type { Type } from './schema'
2- import { assert } from '../assert '
2+ import { isEmpty } from '../data '
33import { objectMap } from '../data/object'
4- import { isSchemaObjectFlat } from './utils'
54
65const _mapJsonSchemaType : Record < string , string > = {
76 string : 'string' ,
@@ -11,7 +10,7 @@ const _mapJsonSchemaType: Record<string, string> = {
1110}
1211
1312export function schemaExportJsonSchema < T > ( schema : Type < T > ) : Record < string , any > {
14- assert ( isSchemaObjectFlat ( schema ) , 'schema should be a flat object' )
13+ // assert(isSchemaObjectFlat(schema), 'schema should be a flat object')
1514
1615 function transformSchema ( schema : Type < any > ) : any {
1716 const type = _mapJsonSchemaType [ schema . type ] ?? schema . type ?? 'object'
@@ -37,25 +36,37 @@ export function schemaExportJsonSchema<T>(schema: Type<T>): Record<string, any>
3736 if ( schema . type === 'array' && schema . _type ) {
3837 properties [ key ] . items = transformSchema ( schema . _type )
3938 }
40- if ( schema . type === 'object' && schema . _object ) {
41- Object . assign ( properties [ key ] , transformSchema ( schema . _object ) )
39+ else if ( schema . type === 'object' && schema . _object ) {
40+ Object . assign ( properties [ key ] , transformSchema ( schema ) )
41+ properties [ key ] . additionalProperties = false
42+ }
43+ else if ( schema . type === 'record' && schema . _type ) {
44+ properties [ key ] . type = 'object'
45+ properties [ key ] . additionalProperties = transformSchema ( schema . _type )
4246 }
4347 // Handle union types (e.g., z.union)
44- if ( schema . type === 'union' && Array . isArray ( schema . _union ) ) {
48+ else if ( schema . type === 'union' && Array . isArray ( schema . _union ) ) {
4549 properties [ key ] . type = schema . _union . map ( ( s : any ) => _mapJsonSchemaType [ s . type ] ?? s . type ) // todo complex types
4650 }
4751 } )
4852
53+ if ( ! isEmpty ( properties ) ) {
54+ return {
55+ type,
56+ properties,
57+ additionalProperties : false ,
58+ ...( required . length > 0 ? { required } : { } ) ,
59+ }
60+ }
61+
4962 return {
5063 type,
51- additionalProperties : false ,
52- properties,
53- ...( required . length > 0 ? { required } : { } ) ,
5464 }
5565 }
5666
5767 return {
5868 $schema : 'http://json-schema.org/draft-07/schema#' ,
69+ additionalProperties : false ,
5970 ...transformSchema ( schema ) ,
6071 }
6172}
0 commit comments