88 TUnsafe ,
99 TypeRegistry ,
1010 TInteger ,
11- IntegerOptions
11+ IntegerOptions ,
12+ Unsafe
1213} from '@sinclair/typebox'
13- import { TypeSystem } from '@sinclair/typebox/system'
1414import {
1515 Type ,
1616 type SchemaOptions ,
@@ -49,7 +49,7 @@ const _validateDate = fullFormats.date
4949const _validateDateTime = fullFormats [ 'date-time' ]
5050
5151if ( ! FormatRegistry . Has ( 'date' ) )
52- TypeSystem . Format ( 'date' , ( value : string ) => {
52+ FormatRegistry . Set ( 'date' , ( value : string ) => {
5353 // Remove quote from stringified date
5454 const temp = value . replace ( / " / g, '' )
5555
@@ -67,7 +67,7 @@ if (!FormatRegistry.Has('date'))
6767 } )
6868
6969if ( ! FormatRegistry . Has ( 'date-time' ) )
70- TypeSystem . Format ( 'date-time' , ( value : string ) => {
70+ FormatRegistry . Set ( 'date-time' , ( value : string ) => {
7171 // Remove quote from stringified date
7272 const temp = value . replace ( / " / g, '' )
7373
@@ -89,9 +89,9 @@ Object.entries(fullFormats).forEach((formatEntry) => {
8989
9090 if ( ! FormatRegistry . Has ( formatName ) ) {
9191 if ( formatValue instanceof RegExp )
92- TypeSystem . Format ( formatName , ( value ) => formatValue . test ( value ) )
92+ FormatRegistry . Set ( formatName , ( value ) => formatValue . test ( value ) )
9393 else if ( typeof formatValue === 'function' )
94- TypeSystem . Format ( formatName , formatValue )
94+ FormatRegistry . Set ( formatName , formatValue )
9595 }
9696} )
9797
@@ -232,33 +232,30 @@ type ElysiaFile = (
232232 options ?: Partial < ElysiaTypeOptions . Files > | undefined
233233) => TUnsafe < File >
234234
235- const File : ElysiaFile =
236- ( TypeRegistry . Get ( 'Files' ) as unknown as ElysiaFile ) ??
237- TypeSystem . Type < File , ElysiaTypeOptions . File > ( 'File' , validateFile )
235+ const File : ElysiaFile = getOrSetType < ElysiaTypeOptions . File , ElysiaFile > (
236+ 'File' ,
237+ validateFile
238+ )
238239
239240type ElysiaFiles = (
240241 options ?: Partial < ElysiaTypeOptions . Files > | undefined
241242) => TUnsafe < File [ ] >
242243
243- const Files : ElysiaFiles =
244- ( TypeRegistry . Get ( 'Files' ) as unknown as ElysiaFiles ) ??
245- TypeSystem . Type < File [ ] , ElysiaTypeOptions . Files > (
246- 'Files' ,
247- ( options , value ) => {
248- if ( ! Array . isArray ( value ) ) return validateFile ( options , value )
244+ const Files : ElysiaFiles = getOrSetType < ElysiaTypeOptions . Files , ElysiaFiles > (
245+ 'Files' ,
246+ ( options , value ) => {
247+ if ( ! Array . isArray ( value ) ) return validateFile ( options , value )
249248
250- if ( options . minItems && value . length < options . minItems )
251- return false
249+ if ( options . minItems && value . length < options . minItems ) return false
252250
253- if ( options . maxItems && value . length > options . maxItems )
254- return false
251+ if ( options . maxItems && value . length > options . maxItems ) return false
255252
256- for ( let i = 0 ; i < value . length ; i ++ )
257- if ( ! validateFile ( options , value [ i ] ) ) return false
253+ for ( let i = 0 ; i < value . length ; i ++ )
254+ if ( ! validateFile ( options , value [ i ] ) ) return false
258255
259- return true
260- }
261- )
256+ return true
257+ }
258+ )
262259
263260if ( ! FormatRegistry . Has ( 'numeric' ) )
264261 FormatRegistry . Set ( 'numeric' , ( value ) => ! ! value && ! isNaN ( + value ) )
@@ -313,14 +310,15 @@ if (!FormatRegistry.Has('ArrayString'))
313310 }
314311 } )
315312
316- TypeRegistry . Set < TUnionEnum > ( 'UnionEnum' , ( schema , value ) => {
317- return (
318- ( typeof value === 'number' ||
319- typeof value === 'string' ||
320- value === null ) &&
321- schema . enum . includes ( value as never )
322- )
323- } )
313+ if ( ! TypeRegistry . Has ( 'UnionEnum' ) )
314+ TypeRegistry . Set < TUnionEnum > ( 'UnionEnum' , ( schema , value ) => {
315+ return (
316+ ( typeof value === 'number' ||
317+ typeof value === 'string' ||
318+ value === null ) &&
319+ schema . enum . includes ( value as never )
320+ )
321+ } )
324322
325323type NonEmptyArray < T > = [ T , ...T [ ] ]
326324
@@ -754,6 +752,18 @@ t.Date = ElysiaType.Date
754752
755753t . UnionEnum = ElysiaType . UnionEnum
756754
755+ function getOrSetType < TSchema = unknown , TReturn = unknown > (
756+ kind : string ,
757+ func : TypeRegistry . TypeRegistryValidationFunction < TSchema >
758+ ) : TReturn {
759+ if ( ! TypeRegistry . Has ( kind ) ) {
760+ TypeRegistry . Set < TSchema > ( kind , func )
761+ }
762+
763+ return ( ( options = { } ) =>
764+ Unsafe ( { ...options , [ Kind ] : kind } ) ) as unknown as TReturn
765+ }
766+
757767export { t }
758768
759769export {
@@ -762,6 +772,7 @@ export {
762772 TypeSystemDuplicateFormat ,
763773 TypeSystemDuplicateTypeKind
764774} from '@sinclair/typebox/system'
775+ export { TypeRegistry , FormatRegistry } from '@sinclair/typebox'
765776export { TypeCompiler , TypeCheck } from '@sinclair/typebox/compiler'
766777
767778// type Template =
0 commit comments