Revision 0.8.0 (#9)

- Rename to TypeMap
This commit is contained in:
sinclairzx81
2025-01-26 03:27:38 +09:00
committed by GitHub
parent d24876b2ac
commit 5ce19b2f4f
52 changed files with 3769 additions and 1228 deletions

View File

@@ -1,2 +1,4 @@
import './zod'
import './valibot'
import './typebox-from-zod'
import './typebox-from-valibot'
import './valibot-from-typebox'
import './zod-from-typebox'

View File

@@ -1,38 +1,38 @@
import { Box } from '@sinclair/typebox-adapter'
import * as Types from '@sinclair/typebox'
import { TypeBox } from '@sinclair/typemap'
import { TypeGuard } from '@sinclair/typebox'
import { Assert } from './assert'
import * as t from '@sinclair/typebox'
import * as v from 'valibot'
describe('Valibot', () => {
describe('TypeBox from Valibot', () => {
// ----------------------------------------------------------------
// Metadata
// ----------------------------------------------------------------
it('Should map Description', () => {
const T = Box(v.pipe(v.number(), v.description('a number')))
const T = TypeBox(v.pipe(v.number(), v.description('a number')))
Assert.IsEqual(T.description, 'a number')
})
it('Should map Title', () => {
const T = Box(v.pipe(v.number(), v.title('a number')))
const T = TypeBox(v.pipe(v.number(), v.title('a number')))
Assert.IsEqual(T.title, 'a number')
})
it('Should map Metadata', () => {
const T = Box(v.pipe(v.number(), v.metadata({ x: 1, y: 2 })))
Assert.IsEqual(T.x, 1)
Assert.IsEqual(T.y, 2)
const T = TypeBox(v.pipe(v.number(), v.metadata({ x: 1, y: 2 })))
Assert.IsEqual(T.metadata.x, 1)
Assert.IsEqual(T.metadata.y, 2)
})
// ----------------------------------------------------------------
// Any
// ----------------------------------------------------------------
it('Should map Any', () => {
const T = Box(v.any())
const T = TypeBox(v.any())
Assert.IsTrue(TypeGuard.IsAny(T))
})
// ----------------------------------------------------------------
// Array
// ----------------------------------------------------------------
it('Should map Array', () => {
const T = Box(v.array(v.number()))
const T = TypeBox(v.array(v.number()))
Assert.IsTrue(TypeGuard.IsArray(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items))
})
@@ -40,26 +40,26 @@ describe('Valibot', () => {
// BigInt
// ----------------------------------------------------------------
it('Should map BigInt', () => {
const T = Box(v.bigint())
const T = TypeBox(v.bigint())
Assert.IsTrue(TypeGuard.IsBigInt(T))
})
// ----------------------------------------------------------------
// Date
// ----------------------------------------------------------------
it('Should map Date', () => {
const T = Box(v.date())
const T = TypeBox(v.date())
Assert.IsTrue(TypeGuard.IsDate(T))
})
// ----------------------------------------------------------------
// Effects
// ----------------------------------------------------------------
// it('Should map Effects (Transform)', () => {
// const T = Box(v.number().transform(x => x))
// const T = TypeBox(v.number().transform(x => x))
// Assert.IsTrue(TypeGuard.IsNumber(T))
// Assert.IsTrue(TypeGuard.IsTransform(T))
// })
// it('Should map Effects (Refine)', () => {
// const T = Box(v.number().refine(x => true))
// const T = TypeBox(v.number().refine(x => true))
// Assert.IsTrue(TypeGuard.IsNumber(T))
// Assert.IsTrue(TypeGuard.IsTransform(T))
// })
@@ -67,17 +67,17 @@ describe('Valibot', () => {
// Literal
// ----------------------------------------------------------------
it('Should map Literal (Number)', () => {
const T = Box(v.literal(42))
const T = TypeBox(v.literal(42))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 42)
})
it('Should map Literal (String)', () => {
const T = Box(v.literal('hello'))
const T = TypeBox(v.literal('hello'))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 'hello')
})
it('Should map Literal (Boolean)', () => {
const T = Box(v.literal(true))
const T = TypeBox(v.literal(true))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, true)
})
@@ -85,7 +85,7 @@ describe('Valibot', () => {
// Nullable
// ----------------------------------------------------------------
it('Should map Nullable', () => {
const T = Box(v.nullable(v.number()))
const T = TypeBox(v.nullable(v.number()))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsNull(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsNumber(T.anyOf[1]))
@@ -94,7 +94,7 @@ describe('Valibot', () => {
// Object
// ----------------------------------------------------------------
it('Should map Object', () => {
const T = Box(
const T = TypeBox(
v.object({
x: v.number(),
y: v.string(),
@@ -105,7 +105,7 @@ describe('Valibot', () => {
Assert.IsTrue(TypeGuard.IsString(T.properties.y))
})
it('Should map Object (Strict)', () => {
const T = Box(
const T = TypeBox(
v.strictObject({
x: v.number(),
y: v.string(),
@@ -120,7 +120,7 @@ describe('Valibot', () => {
// Optional
// ----------------------------------------------------------------
it('Should map Optional', () => {
const T = Box(
const T = TypeBox(
v.object({
x: v.optional(v.number()),
y: v.optional(v.number()),
@@ -133,7 +133,7 @@ describe('Valibot', () => {
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
})
it('Should map Optional (Partial)', () => {
const T = Box(
const T = TypeBox(
v.partial(
v.object({
x: v.number(),
@@ -151,19 +151,19 @@ describe('Valibot', () => {
// Promise
// ----------------------------------------------------------------
it('Should map Promise', () => {
const T = Box(v.promise())
Assert.IsEqual(T[Types.Kind], 'ValibotPromise')
const T = TypeBox(v.promise())
Assert.IsEqual(T[t.Kind], 'ValibotPromise')
})
// ----------------------------------------------------------------
// Record
// ----------------------------------------------------------------
it('Should map Record (String Key)', () => {
const T = Box(v.record(v.string(), v.number()))
const T = TypeBox(v.record(v.string(), v.number()))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[Types.PatternStringExact]))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternStringExact]))
})
it('Should map Record (Finite Union)', () => {
const T = Box(v.record(v.union([v.literal('x'), v.literal('y')]), v.number()))
const T = TypeBox(v.record(v.union([v.literal('x'), v.literal('y')]), v.number()))
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
@@ -172,35 +172,35 @@ describe('Valibot', () => {
// Never
// ----------------------------------------------------------------
it('Should map Never', () => {
const T = Box(v.never())
const T = TypeBox(v.never())
Assert.IsTrue(TypeGuard.IsNever(T))
})
// ----------------------------------------------------------------
// Null
// ----------------------------------------------------------------
it('Should map Null', () => {
const T = Box(v.null())
const T = TypeBox(v.null())
Assert.IsTrue(TypeGuard.IsNull(T))
})
// ----------------------------------------------------------------
// Number
// ----------------------------------------------------------------
it('Should map Number', () => {
const T = Box(v.number())
const T = TypeBox(v.number())
Assert.IsTrue(TypeGuard.IsNumber(T))
})
it('Should map Number (Integer)', () => {
const T = Box(v.pipe(v.number(), v.integer()))
const T = TypeBox(v.pipe(v.number(), v.integer()))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.multipleOf, 1)
})
it('Should map Number (Minimum)', () => {
const T = Box(v.pipe(v.number(), v.minValue(100)))
const T = TypeBox(v.pipe(v.number(), v.minValue(100)))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.minimum, 100)
})
it('Should map Number (Maximum)', () => {
const T = Box(v.pipe(v.number(), v.maxValue(100)))
const T = TypeBox(v.pipe(v.number(), v.maxValue(100)))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.maximum, 100)
})
@@ -208,194 +208,203 @@ describe('Valibot', () => {
// String
// ----------------------------------------------------------------
it('Should map String', () => {
const T = Box(v.string())
const T = TypeBox(v.string())
Assert.IsTrue(TypeGuard.IsString(T))
})
it('Should map String (Base64)', () => {
const T = Box(v.pipe(v.string(), v.base64()))
const T = TypeBox(v.pipe(v.string(), v.base64()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:base64')
Assert.IsEqual(T.format, 'base64')
})
it('Should map String (Bic)', () => {
const T = Box(v.pipe(v.string(), v.bic()))
const T = TypeBox(v.pipe(v.string(), v.bic()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:bic')
Assert.IsEqual(T.format, 'bic')
})
it('Should map String (CreditCard)', () => {
const T = Box(v.pipe(v.string(), v.creditCard()))
const T = TypeBox(v.pipe(v.string(), v.creditCard()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:credit_card')
Assert.IsEqual(T.format, 'credit_card')
})
it('Should map String (Cuid2)', () => {
const T = Box(v.pipe(v.string(), v.cuid2()))
const T = TypeBox(v.pipe(v.string(), v.cuid2()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:cuid2')
Assert.IsEqual(T.format, 'cuid2')
})
it('Should map String (Decimal)', () => {
const T = Box(v.pipe(v.string(), v.decimal()))
const T = TypeBox(v.pipe(v.string(), v.decimal()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:decimal')
Assert.IsEqual(T.format, 'decimal')
})
it('Should map String (Digits)', () => {
const T = Box(v.pipe(v.string(), v.digits()))
const T = TypeBox(v.pipe(v.string(), v.digits()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:digits')
Assert.IsEqual(T.format, 'digits')
})
it('Should map String (Email)', () => {
const T = Box(v.pipe(v.string(), v.email()))
const T = TypeBox(v.pipe(v.string(), v.email()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:email')
Assert.IsEqual(T.format, 'email')
})
it('Should map String (Emoji)', () => {
const T = Box(v.pipe(v.string(), v.emoji()))
const T = TypeBox(v.pipe(v.string(), v.emoji()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:emoji')
Assert.IsEqual(T.format, 'emoji')
})
it('Should map String (Empty)', () => {
const T = Box(v.pipe(v.string(), v.empty()))
const T = TypeBox(v.pipe(v.string(), v.empty()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.maxLength, 0)
})
it('Should map String (EndsWith)', () => {
const T = Box(v.pipe(v.string(), v.endsWith('hello')))
const T = TypeBox(v.pipe(v.string(), v.endsWith('hello')))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello$')
})
it('Should map String (Includes)', () => {
const T = Box(v.pipe(v.string(), v.includes('hello')))
const T = TypeBox(v.pipe(v.string(), v.includes('hello')))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello')
})
it('Should map String (Ipv4)', () => {
const T = Box(v.pipe(v.string(), v.ipv4()))
const T = TypeBox(v.pipe(v.string(), v.ipv4()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:ipv4')
Assert.IsEqual(T.format, 'ipv4')
})
it('Should map String (IpV6)', () => {
const T = Box(v.pipe(v.string(), v.ipv6()))
const T = TypeBox(v.pipe(v.string(), v.ipv6()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:ipv6')
Assert.IsEqual(T.format, 'ipv6')
})
it('Should map String (Ip)', () => {
const T = Box(v.pipe(v.string(), v.ip()))
const T = TypeBox(v.pipe(v.string(), v.ip()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:ip')
Assert.IsEqual(T.format, 'ip')
})
it('Should map String (IsoDate)', () => {
const T = Box(v.pipe(v.string(), v.isoDate()))
const T = TypeBox(v.pipe(v.string(), v.isoDate()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:iso_date')
Assert.IsEqual(T.format, 'iso_date')
})
it('Should map String (IsoDateTime)', () => {
const T = Box(v.pipe(v.string(), v.isoDateTime()))
const T = TypeBox(v.pipe(v.string(), v.isoDateTime()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:iso_date_time')
Assert.IsEqual(T.format, 'iso_date_time')
})
it('Should map String (IsoTime)', () => {
const T = Box(v.pipe(v.string(), v.isoTime()))
const T = TypeBox(v.pipe(v.string(), v.isoTime()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:iso_time')
Assert.IsEqual(T.format, 'iso_time')
})
it('Should map String (IsoTimeSecond)', () => {
const T = Box(v.pipe(v.string(), v.isoTimeSecond()))
const T = TypeBox(v.pipe(v.string(), v.isoTimeSecond()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:iso_time_second')
Assert.IsEqual(T.format, 'iso_time_second')
})
it('Should map String (IsoTimestamp)', () => {
const T = Box(v.pipe(v.string(), v.isoTimestamp()))
const T = TypeBox(v.pipe(v.string(), v.isoTimestamp()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:iso_timestamp')
Assert.IsEqual(T.format, 'iso_timestamp')
})
it('Should map String (IsoWeek)', () => {
const T = Box(v.pipe(v.string(), v.isoWeek()))
const T = TypeBox(v.pipe(v.string(), v.isoWeek()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:iso_week')
Assert.IsEqual(T.format, 'iso_week')
})
it('Should map String (Length)', () => {
const T = Box(v.pipe(v.string(), v.length(100)))
const T = TypeBox(v.pipe(v.string(), v.length(100)))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.maxLength, 100)
Assert.IsEqual(T.minLength, 100)
})
it('Should map String (Mac48)', () => {
const T = Box(v.pipe(v.string(), v.mac48()))
const T = TypeBox(v.pipe(v.string(), v.mac48()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:mac48')
Assert.IsEqual(T.format, 'mac48')
})
it('Should map String (Mac64)', () => {
const T = Box(v.pipe(v.string(), v.mac64()))
const T = TypeBox(v.pipe(v.string(), v.mac64()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:mac64')
Assert.IsEqual(T.format, 'mac64')
})
it('Should map String (Mac)', () => {
const T = Box(v.pipe(v.string(), v.mac()))
const T = TypeBox(v.pipe(v.string(), v.mac()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:mac')
Assert.IsEqual(T.format, 'mac')
})
it('Should map String (MaxLength)', () => {
const T = Box(v.pipe(v.string(), v.maxLength(100)))
const T = TypeBox(v.pipe(v.string(), v.maxLength(100)))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.maxLength, 100)
})
it('Should map String (MinLength)', () => {
const T = Box(v.pipe(v.string(), v.minLength(100)))
const T = TypeBox(v.pipe(v.string(), v.minLength(100)))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.minLength, 100)
})
it('Should map String (Nanoid)', () => {
const T = Box(v.pipe(v.string(), v.nanoid()))
const T = TypeBox(v.pipe(v.string(), v.nanoid()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:nanoid')
Assert.IsEqual(T.format, 'nanoid')
})
it('Should map String (Octal)', () => {
const T = Box(v.pipe(v.string(), v.octal()))
const T = TypeBox(v.pipe(v.string(), v.octal()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:octal')
Assert.IsEqual(T.format, 'octal')
})
it('Should map String (RegExp)', () => {
const T = Box(v.pipe(v.string(), v.regex(/abc/)))
const T = TypeBox(v.pipe(v.string(), v.regex(/abc/)))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'abc')
})
it('Should map String (StartsWith)', () => {
const T = Box(v.pipe(v.string(), v.startsWith('hello')))
const T = TypeBox(v.pipe(v.string(), v.startsWith('hello')))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, '^hello')
})
it('Should map String (Ulid)', () => {
const T = Box(v.pipe(v.string(), v.ulid()))
const T = TypeBox(v.pipe(v.string(), v.ulid()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:ulid')
Assert.IsEqual(T.format, 'ulid')
})
it('Should map String (Url)', () => {
const T = Box(v.pipe(v.string(), v.url()))
const T = TypeBox(v.pipe(v.string(), v.url()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:url')
Assert.IsEqual(T.format, 'url')
})
it('Should map String (Uuid)', () => {
const T = Box(v.pipe(v.string(), v.uuid()))
const T = TypeBox(v.pipe(v.string(), v.uuid()))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'valibot:uuid')
Assert.IsEqual(T.format, 'uuid')
})
// ----------------------------------------------------------------
// Symbol
// ----------------------------------------------------------------
it('Should map Symbol', () => {
const T = Box(v.symbol())
const T = TypeBox(v.symbol())
Assert.IsTrue(TypeGuard.IsSymbol(T))
})
// ----------------------------------------------------------------
// Tuple
// ----------------------------------------------------------------
it('Should map Tuple', () => {
const T = TypeBox(v.tuple([v.number(), v.string()]))
Assert.IsTrue(TypeGuard.IsTuple(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items![0]))
Assert.IsTrue(TypeGuard.IsString(T.items![1]))
})
// ----------------------------------------------------------------
// Undefined
// ----------------------------------------------------------------
it('Should map Undefined', () => {
const T = Box(v.undefined())
const T = TypeBox(v.undefined())
Assert.IsTrue(TypeGuard.IsUndefined(T))
})
// ----------------------------------------------------------------
// Union
// ----------------------------------------------------------------
it('Should map Union', () => {
const T = Box(v.union([v.string(), v.boolean()]))
const T = TypeBox(v.union([v.string(), v.boolean()]))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsString(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsBoolean(T.anyOf[1]))
@@ -404,14 +413,14 @@ describe('Valibot', () => {
// Unknown
// ----------------------------------------------------------------
it('Should map Unknown', () => {
const T = Box(v.unknown())
const T = TypeBox(v.unknown())
Assert.IsTrue(TypeGuard.IsUnknown(T))
})
// ----------------------------------------------------------------
// Void
// ----------------------------------------------------------------
it('Should map Void', () => {
const T = Box(v.void())
const T = TypeBox(v.void())
Assert.IsTrue(TypeGuard.IsVoid(T))
})
})

View File

@@ -1,33 +1,33 @@
import { Box } from '@sinclair/typebox-adapter'
import * as Types from '@sinclair/typebox'
import { TypeBox } from '@sinclair/typemap'
import { TypeGuard } from '@sinclair/typebox'
import { Assert } from './assert'
import * as t from '@sinclair/typebox'
import * as z from 'zod'
describe('Zod', () => {
describe('TypeBox From Zod', () => {
// ----------------------------------------------------------------
// Metadata
// ----------------------------------------------------------------
it('Should map Description', () => {
const T = Box(z.number().describe('a number'))
const T = TypeBox(z.number().describe('a number'))
Assert.IsEqual(T.description, 'a number')
})
it('Should map Default', () => {
const T = Box(z.number().default(12345))
const T = TypeBox(z.number().default(12345))
Assert.IsEqual(T.default, 12345)
})
// ----------------------------------------------------------------
// Any
// ----------------------------------------------------------------
it('Should map Any', () => {
const T = Box(z.any())
const T = TypeBox(z.any())
Assert.IsTrue(TypeGuard.IsAny(T))
})
// ----------------------------------------------------------------
// Array
// ----------------------------------------------------------------
it('Should map Array', () => {
const T = Box(z.array(z.number()))
const T = TypeBox(z.array(z.number()))
Assert.IsTrue(TypeGuard.IsArray(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items))
})
@@ -35,14 +35,14 @@ describe('Zod', () => {
// BigInt
// ----------------------------------------------------------------
it('Should map BigInt', () => {
const T = Box(z.bigint())
const T = TypeBox(z.bigint())
Assert.IsTrue(TypeGuard.IsBigInt(T))
})
// ----------------------------------------------------------------
// Date
// ----------------------------------------------------------------
it('Should map Date', () => {
const T = Box(z.date())
const T = TypeBox(z.date())
Assert.IsTrue(TypeGuard.IsDate(T))
})
// ----------------------------------------------------------------
@@ -52,7 +52,7 @@ describe('Zod', () => {
const A = z.object({ type: z.literal('A') })
const B = z.object({ type: z.literal('B') })
const C = z.object({ type: z.literal('C') })
const T = Box(z.discriminatedUnion('type', [A, B, C]))
const T = TypeBox(z.discriminatedUnion('type', [A, B, C]))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsEqual(T.discriminator, 'type')
Assert.IsTrue(T.anyOf[0].properties.type.const === 'A')
@@ -63,30 +63,40 @@ describe('Zod', () => {
// Effects
// ----------------------------------------------------------------
it('Should map Effects (Transform)', () => {
const T = Box(z.number().transform((x) => x))
const T = TypeBox(z.number().transform((x) => x))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsTrue(TypeGuard.IsTransform(T))
})
it('Should map Effects (Refine)', () => {
const T = Box(z.number().refine((x) => true))
const T = TypeBox(z.number().refine((x) => true))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsTrue(TypeGuard.IsTransform(T))
})
// ----------------------------------------------------------------
// Enum
// ----------------------------------------------------------------
it('Should map Enum', () => {
const T = TypeBox(z.enum(['a', 'b', 'c']))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsEqual(T.anyOf[0].const, 'a')
Assert.IsEqual(T.anyOf[1].const, 'b')
Assert.IsEqual(T.anyOf[2].const, 'c')
})
// ----------------------------------------------------------------
// Literal
// ----------------------------------------------------------------
it('Should map Literal (Number)', () => {
const T = Box(z.literal(42))
const T = TypeBox(z.literal(42))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 42)
})
it('Should map Literal (String)', () => {
const T = Box(z.literal('hello'))
const T = TypeBox(z.literal('hello'))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 'hello')
})
it('Should map Literal (Boolean)', () => {
const T = Box(z.literal(true))
const T = TypeBox(z.literal(true))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, true)
})
@@ -94,7 +104,7 @@ describe('Zod', () => {
// Nullable
// ----------------------------------------------------------------
it('Should map Nullable', () => {
const T = Box(z.number().nullable())
const T = TypeBox(z.number().nullable())
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsNull(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsNumber(T.anyOf[1]))
@@ -103,7 +113,7 @@ describe('Zod', () => {
// Object
// ----------------------------------------------------------------
it('Should map Object', () => {
const T = Box(
const T = TypeBox(
z.object({
x: z.number(),
y: z.string(),
@@ -114,7 +124,7 @@ describe('Zod', () => {
Assert.IsTrue(TypeGuard.IsString(T.properties.y))
})
it('Should map Object (Strict)', () => {
const T = Box(
const T = TypeBox(
z
.object({
x: z.number(),
@@ -131,7 +141,7 @@ describe('Zod', () => {
// Optional
// ----------------------------------------------------------------
it('Should map Optional', () => {
const T = Box(
const T = TypeBox(
z.object({
x: z.number().optional(),
y: z.number().optional(),
@@ -144,7 +154,7 @@ describe('Zod', () => {
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
})
it('Should map Optional (Readonly)', () => {
const T = Box(
const T = TypeBox(
z.object({
x: z.number().optional().readonly(),
y: z.number().optional().readonly(),
@@ -159,7 +169,7 @@ describe('Zod', () => {
Assert.IsTrue(TypeGuard.IsReadonly(T.properties.y))
})
it('Should map Optional (Partial)', () => {
const T = Box(
const T = TypeBox(
z
.object({
x: z.number(),
@@ -177,7 +187,7 @@ describe('Zod', () => {
// Promise
// ----------------------------------------------------------------
it('Should map Promise', () => {
const T = Box(z.promise(z.number()))
const T = TypeBox(z.promise(z.number()))
Assert.IsTrue(TypeGuard.IsPromise(T))
Assert.IsTrue(TypeGuard.IsNumber(T.item))
})
@@ -185,7 +195,7 @@ describe('Zod', () => {
// Readonly
// ----------------------------------------------------------------
it('Should map Readonly', () => {
const T = Box(
const T = TypeBox(
z.object({
x: z.number().readonly(),
y: z.number().readonly(),
@@ -198,7 +208,7 @@ describe('Zod', () => {
Assert.IsTrue(TypeGuard.IsReadonly(T.properties.y))
})
it('Should map Readonly (Optional)', () => {
const T = Box(
const T = TypeBox(
z.object({
x: z.number().readonly().optional(),
y: z.number().readonly().optional(),
@@ -216,22 +226,22 @@ describe('Zod', () => {
// Record
// ----------------------------------------------------------------
it('Should map Record (Key Implicit)', () => {
const T = Box(z.record(z.number()))
const T = TypeBox(z.record(z.number()))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[Types.PatternStringExact]))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternStringExact]))
})
it('Should map Record (Number Key)', () => {
const T = Box(z.record(z.number(), z.number()))
const T = TypeBox(z.record(z.number(), z.number()))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[Types.PatternNumberExact]))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternNumberExact]))
})
it('Should map Record (String Key)', () => {
const T = Box(z.record(z.string(), z.number()))
const T = TypeBox(z.record(z.string(), z.number()))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[Types.PatternStringExact]))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternStringExact]))
})
it('Should map Record (Finite Union)', () => {
const T = Box(z.record(z.union([z.literal('x'), z.literal('y')]), z.number()))
const T = TypeBox(z.record(z.union([z.literal('x'), z.literal('y')]), z.number()))
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
@@ -240,35 +250,35 @@ describe('Zod', () => {
// Never
// ----------------------------------------------------------------
it('Should map Never', () => {
const T = Box(z.never())
const T = TypeBox(z.never())
Assert.IsTrue(TypeGuard.IsNever(T))
})
// ----------------------------------------------------------------
// Null
// ----------------------------------------------------------------
it('Should map Null', () => {
const T = Box(z.null())
const T = TypeBox(z.null())
Assert.IsTrue(TypeGuard.IsNull(T))
})
// ----------------------------------------------------------------
// Number
// ----------------------------------------------------------------
it('Should map Number', () => {
const T = Box(z.number())
const T = TypeBox(z.number())
Assert.IsTrue(TypeGuard.IsNumber(T))
})
it('Should map Number (Integer)', () => {
const T = Box(z.number().int())
const T = TypeBox(z.number().int())
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.multipleOf, 1)
})
it('Should map Number (Minimum)', () => {
const T = Box(z.number().min(100))
const T = TypeBox(z.number().min(100))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.minimum, 100)
})
it('Should map Number (Maximum)', () => {
const T = Box(z.number().max(100))
const T = TypeBox(z.number().max(100))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.maximum, 100)
})
@@ -276,153 +286,153 @@ describe('Zod', () => {
// String
// ----------------------------------------------------------------
it('Should map String', () => {
const T = Box(z.string())
const T = TypeBox(z.string())
Assert.IsTrue(TypeGuard.IsString(T))
})
it('Should map String (Base64)', () => {
const T = Box(z.string().base64())
const T = TypeBox(z.string().base64())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:base64')
Assert.IsEqual(T.format, 'base64')
})
it('Should map String (Base64Url)', () => {
const T = Box(z.string().base64url())
const T = TypeBox(z.string().base64url())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:base64url')
Assert.IsEqual(T.format, 'base64url')
})
it('Should map String (Cidr V4)', () => {
const T = Box(z.string().cidr({ version: 'v4' }))
const T = TypeBox(z.string().cidr({ version: 'v4' }))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:cidrv4')
Assert.IsEqual(T.format, 'cidrv4')
})
it('Should map String (Cidr v6)', () => {
const T = Box(z.string().cidr({ version: 'v6' }))
const T = TypeBox(z.string().cidr({ version: 'v6' }))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:cidrv6')
Assert.IsEqual(T.format, 'cidrv6')
})
it('Should map String (Cidr)', () => {
const T = Box(z.string().cidr())
const T = TypeBox(z.string().cidr())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:cidr')
Assert.IsEqual(T.format, 'cidr')
})
it('Should map String (Cuid)', () => {
const T = Box(z.string().cuid())
const T = TypeBox(z.string().cuid())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:cuid')
Assert.IsEqual(T.format, 'cuid')
})
it('Should map String (Cuid2)', () => {
const T = Box(z.string().cuid2())
const T = TypeBox(z.string().cuid2())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:cuid2')
Assert.IsEqual(T.format, 'cuid2')
})
it('Should map String (Ulid)', () => {
const T = Box(z.string().ulid())
const T = TypeBox(z.string().ulid())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:ulid')
Assert.IsEqual(T.format, 'ulid')
})
it('Should map String (Email)', () => {
const T = Box(z.string().email())
const T = TypeBox(z.string().email())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:email')
Assert.IsEqual(T.format, 'email')
})
it('Should map String (Emoji)', () => {
const T = Box(z.string().emoji())
const T = TypeBox(z.string().emoji())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:emoji')
Assert.IsEqual(T.format, 'emoji')
})
it('Should map String (EndsWith)', () => {
const T = Box(z.string().endsWith('hello'))
const T = TypeBox(z.string().endsWith('hello'))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello$')
})
it('Should map String (Includes)', () => {
const T = Box(z.string().includes('hello'))
const T = TypeBox(z.string().includes('hello'))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello')
})
it('Should map String (IpV4)', () => {
const T = Box(z.string().ip({ version: 'v4' }))
const T = TypeBox(z.string().ip({ version: 'v4' }))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:ipv4')
Assert.IsEqual(T.format, 'ipv4')
})
it('Should map String (IpV6)', () => {
const T = Box(z.string().ip({ version: 'v6' }))
const T = TypeBox(z.string().ip({ version: 'v6' }))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:ipv6')
Assert.IsEqual(T.format, 'ipv6')
})
it('Should map String (Ip)', () => {
const T = Box(z.string().ip())
const T = TypeBox(z.string().ip())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:ip')
Assert.IsEqual(T.format, 'ip')
})
it('Should map String (Jwt)', () => {
const T = Box(z.string().jwt())
const T = TypeBox(z.string().jwt())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:jwt')
Assert.IsEqual(T.format, 'jwt')
})
it('Should map String (Length)', () => {
const T = Box(z.string().length(100))
const T = TypeBox(z.string().length(100))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.minLength, 100)
Assert.IsEqual(T.maxLength, 100)
})
it('Should map String (Min)', () => {
const T = Box(z.string().min(100))
const T = TypeBox(z.string().min(100))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.minLength, 100)
})
it('Should map String (Max)', () => {
const T = Box(z.string().max(100))
const T = TypeBox(z.string().max(100))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.maxLength, 100)
})
it('Should map String (Nanoid)', () => {
const T = Box(z.string().nanoid())
const T = TypeBox(z.string().nanoid())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:nanoid')
Assert.IsEqual(T.format, 'nanoid')
})
it('Should map String (RegExp)', () => {
const T = Box(z.string().regex(/abc/))
const T = TypeBox(z.string().regex(/abc/))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'abc')
})
it('Should map String (StartsWith)', () => {
const T = Box(z.string().startsWith('hello'))
const T = TypeBox(z.string().startsWith('hello'))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, '^hello')
})
it('Should map String (Time)', () => {
const T = Box(z.string().time())
const T = TypeBox(z.string().time())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:time')
Assert.IsEqual(T.format, 'time')
})
it('Should map String (Ulid)', () => {
const T = Box(z.string().ulid())
const T = TypeBox(z.string().ulid())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:ulid')
Assert.IsEqual(T.format, 'ulid')
})
it('Should map String (Url)', () => {
const T = Box(z.string().url())
const T = TypeBox(z.string().url())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:url')
Assert.IsEqual(T.format, 'url')
})
it('Should map String (Uuid)', () => {
const T = Box(z.string().uuid())
const T = TypeBox(z.string().uuid())
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'zod:uuid')
Assert.IsEqual(T.format, 'uuid')
})
// ----------------------------------------------------------------
// Symbol
// ----------------------------------------------------------------
it('Should map Symbol', () => {
const T = Box(z.symbol())
const T = TypeBox(z.symbol())
Assert.IsTrue(TypeGuard.IsSymbol(T))
})
// ----------------------------------------------------------------
// Tuple
// ----------------------------------------------------------------
it('Should map Tuple', () => {
const T = Box(z.tuple([z.number(), z.string()]))
const T = TypeBox(z.tuple([z.number(), z.string()]))
Assert.IsTrue(TypeGuard.IsTuple(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items![0]))
Assert.IsTrue(TypeGuard.IsString(T.items![1]))
@@ -431,14 +441,14 @@ describe('Zod', () => {
// Undefined
// ----------------------------------------------------------------
it('Should map Undefined', () => {
const T = Box(z.undefined())
const T = TypeBox(z.undefined())
Assert.IsTrue(TypeGuard.IsUndefined(T))
})
// ----------------------------------------------------------------
// Union
// ----------------------------------------------------------------
it('Should map Union', () => {
const T = Box(z.union([z.string(), z.boolean()]))
const T = TypeBox(z.union([z.string(), z.boolean()]))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsString(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsBoolean(T.anyOf[1]))
@@ -447,14 +457,14 @@ describe('Zod', () => {
// Unknown
// ----------------------------------------------------------------
it('Should map Unknown', () => {
const T = Box(z.unknown())
const T = TypeBox(z.unknown())
Assert.IsTrue(TypeGuard.IsUnknown(T))
})
// ----------------------------------------------------------------
// Void
// ----------------------------------------------------------------
it('Should map Void', () => {
const T = Box(z.void())
const T = TypeBox(z.void())
Assert.IsTrue(TypeGuard.IsVoid(T))
})
})

View File

@@ -0,0 +1,426 @@
import { TypeBox, Valibot } from '@sinclair/typemap'
import { TypeGuard } from '@sinclair/typebox'
import { Assert } from './assert'
import * as t from '@sinclair/typebox'
import * as v from 'valibot'
describe('Valibot from TypeBox', () => {
// ----------------------------------------------------------------
// Metadata
// ----------------------------------------------------------------
it('Should map Description', () => {
const T = TypeBox(Valibot(t.Number({ description: 'a number' })))
Assert.IsEqual(T.description, 'a number')
})
it('Should map Title', () => {
const T = TypeBox(Valibot(t.Number({ title: 'a number' })))
Assert.IsEqual(T.title, 'a number')
})
it('Should map Metadata', () => {
const T = TypeBox(Valibot(t.Number({ metadata: { x: 1, y: 2 } })))
Assert.IsEqual(T.metadata.x, 1)
Assert.IsEqual(T.metadata.y, 2)
})
// ----------------------------------------------------------------
// Any
// ----------------------------------------------------------------
it('Should map Any', () => {
const T = TypeBox(Valibot(t.Any()))
Assert.IsTrue(TypeGuard.IsAny(T))
})
// ----------------------------------------------------------------
// Array
// ----------------------------------------------------------------
it('Should map Array', () => {
const T = TypeBox(Valibot(t.Array(t.Number())))
Assert.IsTrue(TypeGuard.IsArray(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items))
})
// ----------------------------------------------------------------
// BigInt
// ----------------------------------------------------------------
it('Should map BigInt', () => {
const T = TypeBox(Valibot(t.BigInt()))
Assert.IsTrue(TypeGuard.IsBigInt(T))
})
// ----------------------------------------------------------------
// Date
// ----------------------------------------------------------------
it('Should map Date', () => {
const T = TypeBox(Valibot(t.Date()))
Assert.IsTrue(TypeGuard.IsDate(T))
})
// ----------------------------------------------------------------
// Effects
// ----------------------------------------------------------------
// it('Should map Effects (Transform)', () => {
// const T = TypeBox(v.number().transform(x => x))
// Assert.IsTrue(TypeGuard.IsNumber(T))
// Assert.IsTrue(TypeGuard.IsTransform(T))
// })
// it('Should map Effects (Refine)', () => {
// const T = TypeBox(v.number().refine(x => true))
// Assert.IsTrue(TypeGuard.IsNumber(T))
// Assert.IsTrue(TypeGuard.IsTransform(T))
// })
// ----------------------------------------------------------------
// Literal
// ----------------------------------------------------------------
it('Should map Literal (Number)', () => {
const T = TypeBox(Valibot(t.Literal(42)))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 42)
})
it('Should map Literal (String)', () => {
const T = TypeBox(Valibot(t.Literal('hello')))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 'hello')
})
it('Should map Literal (Boolean)', () => {
const T = TypeBox(Valibot(t.Literal(true)))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, true)
})
// ----------------------------------------------------------------
// Nullable
// ----------------------------------------------------------------
it('Should map Nullable', () => {
const T = TypeBox(Valibot(t.Union([t.Null(), t.Number()])))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsNull(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsNumber(T.anyOf[1]))
})
// ----------------------------------------------------------------
// Object
// ----------------------------------------------------------------
it('Should map Object', () => {
const T = TypeBox(
Valibot(
t.Object({
x: t.Number(),
y: t.String(),
}),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsString(T.properties.y))
})
it('Should map Object (Strict)', () => {
const T = TypeBox(
Valibot(
t.Object(
{
x: t.Number(),
y: t.String(),
},
{ additionalProperties: false },
),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsString(T.properties.y))
Assert.IsEqual(T.additionalProperties, false)
})
// ----------------------------------------------------------------
// Optional
// ----------------------------------------------------------------
it('Should map Optional', () => {
const T = TypeBox(
Valibot(
t.Object({
x: t.Optional(t.Number()),
y: t.Optional(t.Number()),
}),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
})
it('Should map Optional (Partial)', () => {
const T = TypeBox(
Valibot(
t.Partial(
t.Object({
x: t.Optional(t.Number()),
y: t.Optional(t.Number()),
}),
),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
})
// ----------------------------------------------------------------
// Promise
// ----------------------------------------------------------------
it('Should map Promise', () => {
const T = TypeBox(Valibot(t.Promise(t.String())))
Assert.IsEqual(T[t.Kind], 'ValibotPromise')
})
// ----------------------------------------------------------------
// Record
// ----------------------------------------------------------------
it('Should map Record (String Key)', () => {
const T = TypeBox(Valibot(t.Record(t.String(), t.Number())))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternStringExact]))
})
it('Should map Record (Finite Union)', () => {
const T = TypeBox(Valibot(t.Record(t.Union([t.Literal('x'), t.Literal('y')]), t.Number())))
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
})
// ----------------------------------------------------------------
// Never
// ----------------------------------------------------------------
it('Should map Never', () => {
const T = TypeBox(Valibot(t.Never()))
Assert.IsTrue(TypeGuard.IsNever(T))
})
// ----------------------------------------------------------------
// Null
// ----------------------------------------------------------------
it('Should map Null', () => {
const T = TypeBox(Valibot(t.Null()))
Assert.IsTrue(TypeGuard.IsNull(T))
})
// ----------------------------------------------------------------
// Number
// ----------------------------------------------------------------
it('Should map Number', () => {
const T = TypeBox(Valibot(t.Number()))
Assert.IsTrue(TypeGuard.IsNumber(T))
})
it('Should map Number (Integer)', () => {
const T = TypeBox(Valibot(t.Integer())) // remap as Number + Modulo
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.multipleOf, 1)
})
it('Should map Number (Minimum)', () => {
const T = TypeBox(Valibot(t.Number({ minimum: 100 })))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.minimum, 100)
})
it('Should map Number (Maximum)', () => {
const T = TypeBox(Valibot(t.Number({ maximum: 100 })))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.maximum, 100)
})
// ----------------------------------------------------------------
// String
// ----------------------------------------------------------------
it('Should map String', () => {
const T = TypeBox(Valibot(t.String()))
Assert.IsTrue(TypeGuard.IsString(T))
})
it('Should map String (Base64)', () => {
const T = TypeBox(Valibot(t.String({ format: 'base64' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'base64')
})
it('Should map String (Bic)', () => {
const T = TypeBox(Valibot(t.String({ format: 'bic' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'bic')
})
it('Should map String (CreditCard)', () => {
const T = TypeBox(Valibot(t.String({ format: 'credit_card' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'credit_card')
})
it('Should map String (Cuid2)', () => {
const T = TypeBox(Valibot(t.String({ format: 'cuid2' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'cuid2')
})
it('Should map String (Decimal)', () => {
const T = TypeBox(Valibot(t.String({ format: 'decimal' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'decimal')
})
it('Should map String (Digits)', () => {
const T = TypeBox(Valibot(t.String({ format: 'digits' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'digits')
})
it('Should map String (Email)', () => {
const T = TypeBox(Valibot(t.String({ format: 'email' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'email')
})
it('Should map String (Emoji)', () => {
const T = TypeBox(Valibot(t.String({ format: 'emoji' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'emoji')
})
it('Should map String (EndsWith)', () => {
const T = TypeBox(Valibot(t.String({ pattern: 'hello$' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello$')
})
it('Should map String (Includes)', () => {
const T = TypeBox(Valibot(t.String({ pattern: 'hello' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello')
})
it('Should map String (Ipv4)', () => {
const T = TypeBox(Valibot(t.String({ format: 'ipv4' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ipv4')
})
it('Should map String (IpV6)', () => {
const T = TypeBox(Valibot(t.String({ format: 'ipv6' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ipv6')
})
it('Should map String (Ip)', () => {
const T = TypeBox(Valibot(t.String({ format: 'ip' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ip')
})
it('Should map String (IsoDate)', () => {
const T = TypeBox(Valibot(t.String({ format: 'iso_date' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'iso_date')
})
it('Should map String (IsoDateTime)', () => {
const T = TypeBox(Valibot(t.String({ format: 'iso_date_time' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'iso_date_time')
})
it('Should map String (IsoTime)', () => {
const T = TypeBox(Valibot(t.String({ format: 'iso_time' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'iso_time')
})
it('Should map String (IsoTimeSecond)', () => {
const T = TypeBox(Valibot(t.String({ format: 'iso_time_second' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'iso_time_second')
})
it('Should map String (IsoTimestamp)', () => {
const T = TypeBox(Valibot(t.String({ format: 'iso_timestamp' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'iso_timestamp')
})
it('Should map String (IsoWeek)', () => {
const T = TypeBox(Valibot(t.String({ format: 'iso_week' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'iso_week')
})
it('Should map String (Mac48)', () => {
const T = TypeBox(Valibot(t.String({ format: 'mac48' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'mac48')
})
it('Should map String (Mac64)', () => {
const T = TypeBox(Valibot(t.String({ format: 'mac64' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'mac64')
})
it('Should map String (Mac)', () => {
const T = TypeBox(Valibot(t.String({ format: 'mac' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'mac')
})
it('Should map String (MaxLength)', () => {
const T = TypeBox(Valibot(t.String({ maxLength: 100 })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.maxLength, 100)
})
it('Should map String (MinLength)', () => {
const T = TypeBox(Valibot(t.String({ minLength: 100 })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.minLength, 100)
})
it('Should map String (Nanoid)', () => {
const T = TypeBox(Valibot(t.String({ format: 'nanoid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'nanoid')
})
it('Should map String (Octal)', () => {
const T = TypeBox(Valibot(t.String({ format: 'octal' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'octal')
})
it('Should map String (RegExp)', () => {
const T = TypeBox(Valibot(t.RegExp(/abc/)))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'abc')
})
it('Should map String (StartsWith)', () => {
const T = TypeBox(Valibot(t.String({ pattern: '^hello' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, '^hello')
})
it('Should map String (Ulid)', () => {
const T = TypeBox(Valibot(t.String({ format: 'ulid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ulid')
})
it('Should map String (Url)', () => {
const T = TypeBox(Valibot(t.String({ format: 'url' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'url')
})
it('Should map String (Uuid)', () => {
const T = TypeBox(Valibot(t.String({ format: 'uuid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'uuid')
})
// ----------------------------------------------------------------
// Symbol
// ----------------------------------------------------------------
it('Should map Symbol', () => {
const T = TypeBox(Valibot(t.Symbol()))
Assert.IsTrue(TypeGuard.IsSymbol(T))
})
// ----------------------------------------------------------------
// Tuple
// ----------------------------------------------------------------
it('Should map Tuple', () => {
const T = TypeBox(Valibot(t.Tuple([t.Number(), t.String()])))
Assert.IsTrue(TypeGuard.IsTuple(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items![0]))
Assert.IsTrue(TypeGuard.IsString(T.items![1]))
})
// ----------------------------------------------------------------
// Undefined
// ----------------------------------------------------------------
it('Should map Undefined', () => {
const T = TypeBox(Valibot(t.Undefined()))
Assert.IsTrue(TypeGuard.IsUndefined(T))
})
// ----------------------------------------------------------------
// Union
// ----------------------------------------------------------------
it('Should map Union', () => {
const T = TypeBox(Valibot(t.Union([t.String(), t.Boolean()])))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsString(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsBoolean(T.anyOf[1]))
})
// ----------------------------------------------------------------
// Unknown
// ----------------------------------------------------------------
it('Should map Unknown', () => {
const T = TypeBox(Valibot(t.Unknown()))
Assert.IsTrue(TypeGuard.IsUnknown(T))
})
// ----------------------------------------------------------------
// Void
// ----------------------------------------------------------------
it('Should map Void', () => {
const T = TypeBox(Valibot(t.Void()))
Assert.IsTrue(TypeGuard.IsVoid(T))
})
})

429
test/zod-from-typebox.ts Normal file
View File

@@ -0,0 +1,429 @@
import { TypeBox, Zod } from '@sinclair/typemap'
import { TypeGuard } from '@sinclair/typebox'
import { Assert } from './assert'
import * as t from '@sinclair/typebox'
import * as z from 'zod'
describe('Zod From TypeBox', () => {
// ----------------------------------------------------------------
// Metadata
// ----------------------------------------------------------------
it('Should map Description', () => {
const T = TypeBox(Zod(t.Number({ description: 'a number' })))
Assert.IsEqual(T.description, 'a number')
})
it('Should map Default', () => {
const T = TypeBox(Zod(t.Number({ default: 12345 })))
Assert.IsEqual(T.default, 12345)
})
// ----------------------------------------------------------------
// Any
// ----------------------------------------------------------------
it('Should map Any', () => {
const T = TypeBox(Zod(t.Any()))
Assert.IsTrue(TypeGuard.IsAny(T))
})
// ----------------------------------------------------------------
// Array
// ----------------------------------------------------------------
it('Should map Array', () => {
const T = TypeBox(Zod(t.Array(t.Number())))
Assert.IsTrue(TypeGuard.IsArray(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items))
})
// ----------------------------------------------------------------
// BigInt
// ----------------------------------------------------------------
it('Should map BigInt', () => {
const T = TypeBox(Zod(t.BigInt()))
Assert.IsTrue(TypeGuard.IsBigInt(T))
})
// ----------------------------------------------------------------
// Date
// ----------------------------------------------------------------
it('Should map Date', () => {
const T = TypeBox(Zod(t.Date()))
Assert.IsTrue(TypeGuard.IsDate(T))
})
// ----------------------------------------------------------------
// Effects
// ----------------------------------------------------------------
// it('Should map Effects (Transform)', () => {
// const T = TypeBox(z.number().transform((x) => x))
// Assert.IsTrue(TypeGuard.IsNumber(T))
// Assert.IsTrue(TypeGuard.IsTransform(T))
// })
// it('Should map Effects (Refine)', () => {
// const T = TypeBox(z.number().refine((x) => true))
// Assert.IsTrue(TypeGuard.IsNumber(T))
// Assert.IsTrue(TypeGuard.IsTransform(T))
// })
// ----------------------------------------------------------------
// Literal
// ----------------------------------------------------------------
it('Should map Literal (Number)', () => {
const T = TypeBox(Zod(t.Literal(42)))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 42)
})
it('Should map Literal (String)', () => {
const T = TypeBox(Zod(t.Literal('hello')))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, 'hello')
})
it('Should map Literal (Boolean)', () => {
const T = TypeBox(Zod(t.Literal(true)))
Assert.IsTrue(TypeGuard.IsLiteral(T))
Assert.IsEqual(T.const, true)
})
// ----------------------------------------------------------------
// Object
// ----------------------------------------------------------------
it('Should map Object', () => {
const T = TypeBox(
Zod(
t.Object({
x: t.Number(),
y: t.String(),
}),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsString(T.properties.y))
})
it('Should map Object (Strict)', () => {
const T = TypeBox(
Zod(
t.Object(
{
x: t.Number(),
y: t.String(),
},
{ additionalProperties: false },
),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsString(T.properties.y))
Assert.IsEqual(T.additionalProperties, false)
})
// ----------------------------------------------------------------
// Optional
// ----------------------------------------------------------------
it('Should map Optional', () => {
const T = TypeBox(
Zod(
t.Object({
x: t.Optional(t.Number()),
y: t.Optional(t.Number()),
}),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
})
it('Should map Optional (Readonly)', () => {
const T = TypeBox(
Zod(
t.Object({
x: t.ReadonlyOptional(t.Number()),
y: t.ReadonlyOptional(t.Number()),
}),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.x))
Assert.IsFalse(TypeGuard.IsReadonly(T.properties.x)) // Cannot Map for Readonly in Zod
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
Assert.IsFalse(TypeGuard.IsReadonly(T.properties.y)) // Cannot Map for Readonly in Zod
})
it('Should map Optional (Partial)', () => {
const T = TypeBox(
Zod(
t.Partial(
t.Object({
x: t.Number(),
y: t.Number(),
}),
),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
Assert.IsTrue(TypeGuard.IsOptional(T.properties.y))
})
// ----------------------------------------------------------------
// Promise
// ----------------------------------------------------------------
it('Should map Promise', () => {
const T = TypeBox(Zod(t.Promise(t.Number())))
Assert.IsTrue(TypeGuard.IsPromise(T))
Assert.IsTrue(TypeGuard.IsNumber(T.item))
})
// ----------------------------------------------------------------
// Readonly
// ----------------------------------------------------------------
it('Should map Readonly', () => {
const T = TypeBox(
Zod(
t.Object({
x: t.Readonly(t.Number()),
y: t.Readonly(t.Number()),
}),
),
)
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsFalse(TypeGuard.IsReadonly(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
Assert.IsFalse(TypeGuard.IsReadonly(T.properties.y))
})
// ----------------------------------------------------------------
// Record
// ----------------------------------------------------------------
it('Should map Record (Number Key)', () => {
const T = TypeBox(Zod(t.Record(t.Number(), t.Number())))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternNumberExact]))
})
it('Should map Record (String Key)', () => {
const T = TypeBox(Zod(t.Record(t.String(), t.Number())))
Assert.IsTrue(TypeGuard.IsRecord(T))
Assert.IsTrue(TypeGuard.IsNumber(T.patternProperties[t.PatternStringExact]))
})
it('Should map Record (Finite Union)', () => {
const T = TypeBox(Zod(t.Record(t.Union([t.Literal('x'), t.Literal('y')]), t.Number())))
Assert.IsTrue(TypeGuard.IsObject(T))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.x))
Assert.IsTrue(TypeGuard.IsNumber(T.properties.y))
})
// ----------------------------------------------------------------
// Never
// ----------------------------------------------------------------
it('Should map Never', () => {
const T = TypeBox(Zod(t.Never()))
Assert.IsTrue(TypeGuard.IsNever(T))
})
// ----------------------------------------------------------------
// Null
// ----------------------------------------------------------------
it('Should map Null', () => {
const T = TypeBox(Zod(t.Null()))
Assert.IsTrue(TypeGuard.IsNull(T))
})
// ----------------------------------------------------------------
// Number
// ----------------------------------------------------------------
it('Should map Number', () => {
const T = TypeBox(Zod(t.Number()))
Assert.IsTrue(TypeGuard.IsNumber(T))
})
it('Should map Number (Integer)', () => {
const T = TypeBox(Zod(t.Integer()))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.multipleOf, 1)
})
it('Should map Number (Minimum)', () => {
const T = TypeBox(Zod(t.Number({ minimum: 100 })))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.minimum, 100)
})
it('Should map Number (Maximum)', () => {
const T = TypeBox(Zod(t.Number({ maximum: 100 })))
Assert.IsTrue(TypeGuard.IsNumber(T))
Assert.IsEqual(T.maximum, 100)
})
// ----------------------------------------------------------------
// String
// ----------------------------------------------------------------
it('Should map String', () => {
const T = TypeBox(Zod(t.String()))
Assert.IsTrue(TypeGuard.IsString(T))
})
it('Should map String (Base64)', () => {
const T = TypeBox(Zod(t.String({ format: 'base64' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'base64')
})
it('Should map String (Base64Url)', () => {
const T = TypeBox(Zod(t.String({ format: 'base64url' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'base64url')
})
it('Should map String (Cidr V4)', () => {
const T = TypeBox(Zod(t.String({ format: 'cidrv4' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'cidrv4')
})
it('Should map String (Cidr v6)', () => {
const T = TypeBox(Zod(t.String({ format: 'cidrv6' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'cidrv6')
})
it('Should map String (Cidr)', () => {
const T = TypeBox(Zod(t.String({ format: 'cidr' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'cidr')
})
it('Should map String (Cuid)', () => {
const T = TypeBox(Zod(t.String({ format: 'cuid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'cuid')
})
it('Should map String (Cuid2)', () => {
const T = TypeBox(Zod(t.String({ format: 'cuid2' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'cuid2')
})
it('Should map String (Ulid)', () => {
const T = TypeBox(Zod(t.String({ format: 'ulid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ulid')
})
it('Should map String (Email)', () => {
const T = TypeBox(Zod(t.String({ format: 'email' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'email')
})
it('Should map String (Emoji)', () => {
const T = TypeBox(Zod(t.String({ format: 'emoji' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'emoji')
})
it('Should map String (EndsWith)', () => {
const T = TypeBox(Zod(t.String({ pattern: 'hello$' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello$')
})
it('Should map String (Includes)', () => {
const T = TypeBox(Zod(t.String({ pattern: 'hello' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'hello')
})
it('Should map String (IpV4)', () => {
const T = TypeBox(Zod(t.String({ format: 'ipv4' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ipv4')
})
it('Should map String (IpV6)', () => {
const T = TypeBox(Zod(t.String({ format: 'ipv6' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ipv6')
})
it('Should map String (Ip)', () => {
const T = TypeBox(Zod(t.String({ format: 'ip' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ip')
})
it('Should map String (Jwt)', () => {
const T = TypeBox(Zod(t.String({ format: 'jwt' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'jwt')
})
it('Should map String (Length)', () => {
const T = TypeBox(Zod(t.String({ minLength: 100, maxLength: 100 })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.minLength, 100)
Assert.IsEqual(T.maxLength, 100)
})
it('Should map String (Min)', () => {
const T = TypeBox(Zod(t.String({ minLength: 100 })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.minLength, 100)
})
it('Should map String (Max)', () => {
const T = TypeBox(Zod(t.String({ maxLength: 100 })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.maxLength, 100)
})
it('Should map String (Nanoid)', () => {
const T = TypeBox(Zod(t.String({ format: 'nanoid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'nanoid')
})
it('Should map String (RegExp)', () => {
const T = TypeBox(Zod(t.RegExp(/abc/)))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, 'abc')
})
it('Should map String (StartsWith)', () => {
const T = TypeBox(Zod(t.String({ pattern: '^hello' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.pattern, '^hello')
})
it('Should map String (Time)', () => {
const T = TypeBox(Zod(t.String({ format: 'time' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'time')
})
it('Should map String (Ulid)', () => {
const T = TypeBox(Zod(t.String({ format: 'ulid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'ulid')
})
it('Should map String (Url)', () => {
const T = TypeBox(Zod(t.String({ format: 'url' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'url')
})
it('Should map String (Uuid)', () => {
const T = TypeBox(Zod(t.String({ format: 'uuid' })))
Assert.IsTrue(TypeGuard.IsString(T))
Assert.IsEqual(T.format, 'uuid')
})
// ----------------------------------------------------------------
// Symbol
// ----------------------------------------------------------------
it('Should map Symbol', () => {
const T = TypeBox(Zod(t.Symbol()))
Assert.IsTrue(TypeGuard.IsSymbol(T))
})
// ----------------------------------------------------------------
// Tuple
// ----------------------------------------------------------------
it('Should map Tuple', () => {
const T = TypeBox(Zod(t.Tuple([t.Number(), t.String()])))
Assert.IsTrue(TypeGuard.IsTuple(T))
Assert.IsTrue(TypeGuard.IsNumber(T.items![0]))
Assert.IsTrue(TypeGuard.IsString(T.items![1]))
})
// ----------------------------------------------------------------
// Undefined
// ----------------------------------------------------------------
it('Should map Undefined', () => {
const T = TypeBox(Zod(t.Undefined()))
Assert.IsTrue(TypeGuard.IsUndefined(T))
})
// ----------------------------------------------------------------
// Union
// ----------------------------------------------------------------
it('Should map Union', () => {
const T = TypeBox(Zod(t.Union([t.String(), t.Boolean()])))
Assert.IsTrue(TypeGuard.IsUnion(T))
Assert.IsTrue(TypeGuard.IsString(T.anyOf[0]))
Assert.IsTrue(TypeGuard.IsBoolean(T.anyOf[1]))
})
// ----------------------------------------------------------------
// Unknown
// ----------------------------------------------------------------
it('Should map Unknown', () => {
const T = TypeBox(Zod(t.Unknown()))
Assert.IsTrue(TypeGuard.IsUnknown(T))
})
// ----------------------------------------------------------------
// Void
// ----------------------------------------------------------------
it('Should map Void', () => {
const T = TypeBox(Zod(t.Void()))
Assert.IsTrue(TypeGuard.IsVoid(T))
})
})