Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
76 changes: 63 additions & 13 deletions src/type-system/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { Type, Kind } from '@sinclair/typebox'
import type {
import {
ArrayOptions,
DateOptions,
IntegerOptions,
JavaScriptTypeBuilder,
Kind,
NumberOptions,
ObjectOptions,
SchemaOptions,
StringOptions,
TAnySchema,
TArray,
TBoolean,
TDate,
TEnum,
TEnumValue,
TInteger,
TNumber,
TObject,
TProperties,
TSchema,
TString,
NumberOptions,
JavaScriptTypeBuilder,
StringOptions,
TUnsafe,
Uint8ArrayOptions,
TEnum
Type,
Uint8ArrayOptions
} from '@sinclair/typebox'
import {
DefaultErrorFunction,
SetErrorFunction
} from '@sinclair/typebox/errors'

import {
compile,
Expand All @@ -32,22 +37,25 @@ import {
validateFile
} from './utils'
import {
AssertNumericEnum,
CookieValidatorOptions,
TFile,
TFiles,
ElysiaTransformDecodeBuilder,
FileOptions,
FilesOptions,
NonEmptyArray,
TArrayBuffer,
TFile,
TFiles,
TForm,
TMaybeNull,
TUnionEnum,
ElysiaTransformDecodeBuilder,
TArrayBuffer,
AssertNumericEnum
WrappedKind
} from './types'

import { ELYSIA_FORM_DATA, form } from '../utils'
import { ValidationError } from '../error'
import { parseDateTimeEmptySpace } from './format'
import { Value } from '@sinclair/typebox/value'

const t = Object.assign({}, Type) as unknown as Omit<
JavaScriptTypeBuilder,
Expand All @@ -73,6 +81,31 @@ createType<TArrayBuffer>(
(schema, value) => value instanceof ArrayBuffer
)

createType<TMaybeNull>(
'MaybeNull',
({ [Kind]: kind, [WrappedKind]: wrappedKind, ...schema }, value) => {
return (
value === null ||
Value.Check(
{
[Kind]: wrappedKind,
...schema
},
value
)
)
}
)

SetErrorFunction((error) => {
switch (error.schema[Kind]) {
case 'MaybeNull':
return `Expected '${error.schema.type ?? (error.schema as TMaybeNull)[WrappedKind]}' or 'null'`
default:
return DefaultErrorFunction(error)
}
})

const internalFiles = createType<FilesOptions, File[]>(
'Files',
(options, value) => {
Expand Down Expand Up @@ -153,7 +186,10 @@ export const ElysiaType = {
.Encode((value) => value) as any as TNumber
},

NumericEnum<T extends AssertNumericEnum<T>>(item: T, property?: SchemaOptions) {
NumericEnum<T extends AssertNumericEnum<T>>(
item: T,
property?: SchemaOptions
) {
const schema = Type.Enum(item, property)
const compiler = compile(schema)

Expand Down Expand Up @@ -492,12 +528,25 @@ export const ElysiaType = {
})
.Encode((value) => value) as unknown as TUnsafe<File[]>,

/**
* @deprecated Use MaybeNull instead which is OpenAPI 3.0 compliant. Will be removed in the next major release
*/
Nullable: <T extends TSchema>(schema: T, options?: SchemaOptions) =>
t.Union([schema, t.Null()], {
...options,
nullable: true
}),

MaybeNull: <T extends TSchema>({ [Kind]: kind, ...schema }: T) => {
return {
[Kind]: 'MaybeNull',
[WrappedKind]: kind,
default: undefined,
...schema,
nullable: true
} as unknown as TMaybeNull
},

/**
* Allow Optional, Nullable and Undefined
*/
Expand Down Expand Up @@ -660,6 +709,7 @@ t.Files = (arg) => {
}

t.Nullable = ElysiaType.Nullable
t.MaybeNull = ElysiaType.MaybeNull
t.MaybeEmpty = ElysiaType.MaybeEmpty
t.Cookie = ElysiaType.Cookie
t.Date = ElysiaType.Date
Expand Down
7 changes: 7 additions & 0 deletions src/type-system/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ export interface TUnionEnum<
enum: T
}

export const WrappedKind = Symbol('WrappedKind');

export interface TMaybeNull extends TSchema {
[Kind]: 'MaybeNull',
[WrappedKind]: string,
}

export interface TArrayBuffer extends Uint8ArrayOptions {}

export type TForm<T extends TProperties = TProperties> = TUnsafe<
Expand Down
Loading
Loading