-
-
Notifications
You must be signed in to change notification settings - Fork 500
ref: use Registry instead of TypeSystem #977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,9 @@ import { | |
| TUnsafe, | ||
| TypeRegistry, | ||
| TInteger, | ||
| IntegerOptions | ||
| IntegerOptions, | ||
| Unsafe | ||
| } from '@sinclair/typebox' | ||
| import { TypeSystem } from '@sinclair/typebox/system' | ||
| import { | ||
| Type, | ||
| type SchemaOptions, | ||
|
|
@@ -49,7 +49,7 @@ const _validateDate = fullFormats.date | |
| const _validateDateTime = fullFormats['date-time'] | ||
|
|
||
| if (!FormatRegistry.Has('date')) | ||
| TypeSystem.Format('date', (value: string) => { | ||
| FormatRegistry.Set('date', (value: string) => { | ||
| // Remove quote from stringified date | ||
| const temp = value.replace(/"/g, '') | ||
|
|
||
|
|
@@ -67,7 +67,7 @@ if (!FormatRegistry.Has('date')) | |
| }) | ||
|
|
||
| if (!FormatRegistry.Has('date-time')) | ||
| TypeSystem.Format('date-time', (value: string) => { | ||
| FormatRegistry.Set('date-time', (value: string) => { | ||
| // Remove quote from stringified date | ||
| const temp = value.replace(/"/g, '') | ||
|
|
||
|
|
@@ -89,9 +89,9 @@ Object.entries(fullFormats).forEach((formatEntry) => { | |
|
|
||
| if (!FormatRegistry.Has(formatName)) { | ||
| if (formatValue instanceof RegExp) | ||
| TypeSystem.Format(formatName, (value) => formatValue.test(value)) | ||
| FormatRegistry.Set(formatName, (value) => formatValue.test(value)) | ||
| else if (typeof formatValue === 'function') | ||
| TypeSystem.Format(formatName, formatValue) | ||
| FormatRegistry.Set(formatName, formatValue) | ||
| } | ||
| }) | ||
|
|
||
|
|
@@ -232,33 +232,30 @@ type ElysiaFile = ( | |
| options?: Partial<ElysiaTypeOptions.Files> | undefined | ||
| ) => TUnsafe<File> | ||
|
|
||
| const File: ElysiaFile = | ||
| (TypeRegistry.Get('Files') as unknown as ElysiaFile) ?? | ||
| TypeSystem.Type<File, ElysiaTypeOptions.File>('File', validateFile) | ||
| const File: ElysiaFile = getOrSetType<ElysiaTypeOptions.File, ElysiaFile>( | ||
| 'File', | ||
| validateFile | ||
| ) | ||
|
|
||
| type ElysiaFiles = ( | ||
| options?: Partial<ElysiaTypeOptions.Files> | undefined | ||
| ) => TUnsafe<File[]> | ||
|
|
||
| const Files: ElysiaFiles = | ||
| (TypeRegistry.Get('Files') as unknown as ElysiaFiles) ?? | ||
| TypeSystem.Type<File[], ElysiaTypeOptions.Files>( | ||
| 'Files', | ||
| (options, value) => { | ||
| if (!Array.isArray(value)) return validateFile(options, value) | ||
| const Files: ElysiaFiles = getOrSetType<ElysiaTypeOptions.Files, ElysiaFiles>( | ||
| 'Files', | ||
| (options, value) => { | ||
| if (!Array.isArray(value)) return validateFile(options, value) | ||
|
|
||
| if (options.minItems && value.length < options.minItems) | ||
| return false | ||
| if (options.minItems && value.length < options.minItems) return false | ||
|
|
||
| if (options.maxItems && value.length > options.maxItems) | ||
| return false | ||
| if (options.maxItems && value.length > options.maxItems) return false | ||
|
|
||
| for (let i = 0; i < value.length; i++) | ||
| if (!validateFile(options, value[i])) return false | ||
| for (let i = 0; i < value.length; i++) | ||
| if (!validateFile(options, value[i])) return false | ||
|
|
||
| return true | ||
| } | ||
| ) | ||
| return true | ||
| } | ||
| ) | ||
|
|
||
| if (!FormatRegistry.Has('numeric')) | ||
| FormatRegistry.Set('numeric', (value) => !!value && !isNaN(+value)) | ||
|
|
@@ -313,21 +310,25 @@ if (!FormatRegistry.Has('ArrayString')) | |
| } | ||
| }) | ||
|
|
||
| TypeRegistry.Set<TUnionEnum>('UnionEnum', (schema, value) => { | ||
| return ( | ||
| (typeof value === 'number' || | ||
| typeof value === 'string' || | ||
| value === null) && | ||
| schema.enum.includes(value as never) | ||
| ) | ||
| }) | ||
| if (!TypeRegistry.Has('UnionEnum')) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here was no "Has"-check.. In case a user wanna override it |
||
| TypeRegistry.Set<TUnionEnum>('UnionEnum', (schema, value) => { | ||
| return ( | ||
| (typeof value === 'number' || | ||
| typeof value === 'string' || | ||
| value === null) && | ||
| schema.enum.includes(value as never) | ||
| ) | ||
| }) | ||
|
|
||
| type NonEmptyArray<T> = [T, ...T[]] | ||
|
|
||
| export type TEnumValue = number | string | null | ||
|
|
||
| export interface TUnionEnum<T extends NonEmptyArray<TEnumValue> | Readonly<NonEmptyArray<TEnumValue>> = [TEnumValue]> | ||
| extends TSchema { | ||
| export interface TUnionEnum< | ||
| T extends | ||
| | NonEmptyArray<TEnumValue> | ||
| | Readonly<NonEmptyArray<TEnumValue>> = [TEnumValue] | ||
| > extends TSchema { | ||
| type?: 'number' | 'string' | 'null' | ||
| [Kind]: 'UnionEnum' | ||
| static: T[number] | ||
|
|
@@ -744,6 +745,18 @@ t.Date = ElysiaType.Date | |
|
|
||
| t.UnionEnum = ElysiaType.UnionEnum | ||
|
|
||
| function getOrSetType<TSchema = unknown, TReturn = unknown>( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function implementation is based on sinclare docs and TypeSystem.Type. It's not recommendet anymore to use This approach doesn't throw any |
||
| kind: string, | ||
| func: TypeRegistry.TypeRegistryValidationFunction<TSchema> | ||
| ): TReturn { | ||
| if (!TypeRegistry.Has(kind)) { | ||
| TypeRegistry.Set<TSchema>(kind, func) | ||
| } | ||
|
|
||
| return ((options = {}) => | ||
| Unsafe({ ...options, [Kind]: kind })) as unknown as TReturn | ||
| } | ||
|
|
||
| export { t } | ||
|
|
||
| export { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here was "Files" instead of "File"
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Furthermore,
TypeRegistry.Get('...')didn't work as expected because it returns the validation function instead of the schema-function with options, which means it breaks if someone overwrites it nowadays or whentype-system.tsgets executed twice.