Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 47 additions & 34 deletions src/type-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
TUnsafe,
TypeRegistry,
TInteger,
IntegerOptions
IntegerOptions,
Unsafe
} from '@sinclair/typebox'
import { TypeSystem } from '@sinclair/typebox/system'
import {
Type,
type SchemaOptions,
Expand Down Expand Up @@ -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, '')

Expand All @@ -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, '')

Expand All @@ -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)
}
})

Expand Down Expand Up @@ -232,33 +232,30 @@ type ElysiaFile = (
options?: Partial<ElysiaTypeOptions.Files> | undefined
) => TUnsafe<File>

const File: ElysiaFile =
(TypeRegistry.Get('Files') as unknown as ElysiaFile) ??
Copy link
Copy Markdown
Contributor Author

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"

Copy link
Copy Markdown
Contributor Author

@bogeychan bogeychan Dec 25, 2024

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 when type-system.ts gets executed twice.

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))
Expand Down Expand Up @@ -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'))
Copy link
Copy Markdown
Contributor Author

@bogeychan bogeychan Dec 25, 2024

Choose a reason for hiding this comment

The 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]
Expand Down Expand Up @@ -744,6 +745,18 @@ t.Date = ElysiaType.Date

t.UnionEnum = ElysiaType.UnionEnum

function getOrSetType<TSchema = unknown, TReturn = unknown>(
Copy link
Copy Markdown
Contributor Author

@bogeychan bogeychan Dec 25, 2024

Choose a reason for hiding this comment

The 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 TypeSystem and instead switch to TypeRegistry and FormatRegistry.

This approach doesn't throw any TypeSystemDuplicateTypeKind-Error.

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 {
Expand Down