Skip to content

Commit

Permalink
TypeGuard for Raw TUnsafe (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Mar 29, 2023
1 parent b9db762 commit e4d7378
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.26.7",
"version": "0.26.8",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
9 changes: 9 additions & 0 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,14 @@ export namespace TypeGuard {
IsOptionalString(schema.$id)
)
}
/** Returns true if the given schema is a raw TUnsafe */
export function TUnsafe(schema: unknown): schema is TUnsafe<unknown> {
// prettier-ignore
return (
TKind(schema) &&
schema[Kind] === 'Unsafe'
)
}
/** Returns true if the given schema is TVoid */
export function TVoid(schema: unknown): schema is TVoid {
// prettier-ignore
Expand Down Expand Up @@ -1165,6 +1173,7 @@ export namespace TypeGuard {
TUnion(schema) ||
TUint8Array(schema) ||
TUnknown(schema) ||
TUnsafe(schema) ||
TVoid(schema) ||
(TKind(schema) && TypeRegistry.Has(schema[Kind] as any)))
)
Expand Down
19 changes: 18 additions & 1 deletion test/runtime/compiler/partial.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type, Modifier } from '@sinclair/typebox'
import { TypeSystem } from '@sinclair/typebox/system'
import { Type, Kind, Modifier } from '@sinclair/typebox'
import { Ok, Fail } from './validate'
import { strictEqual } from 'assert'

Expand Down Expand Up @@ -49,4 +50,20 @@ describe('type/compiler/Partial', () => {
strictEqual(A.additionalProperties, false)
strictEqual(T.additionalProperties, false)
})
it('Should support partial properties of raw TUnsafe', () => {
// https://github.com/sinclairzx81/typebox/issues/364
const T = Type.Partial(Type.Object({ x: Type.Unsafe({ x: 1 }) }))
strictEqual(T.required, undefined)
})
it('Should not support partial properties of unknown TUnsafe', () => {
// https://github.com/sinclairzx81/typebox/issues/364
const T = Type.Partial(Type.Object({ x: Type.Unsafe({ [Kind]: 'UnknownPartialType', x: 1 }) }))
strictEqual(T.required![0], 'x')
})
it('Should support partial properties of custom TUnsafe', () => {
// https://github.com/sinclairzx81/typebox/issues/364
const U = TypeSystem.Type('CustomPartialType', () => true)
const T = Type.Partial(Type.Object({ x: U() }))
strictEqual(T.required, undefined)
})
})
1 change: 1 addition & 0 deletions test/runtime/type/guard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ import './uint8array'
import './undefined'
import './union'
import './unknown'
import './unsafe'
import './void'
32 changes: 32 additions & 0 deletions test/runtime/type/guard/unsafe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Kind, TypeGuard, TypeRegistry } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('type/guard/TUnsafe', () => {
it('Should guard raw TUnsafe', () => {
const T = Type.Unsafe({ x: 1 })
const R = TypeGuard.TUnsafe(T)
Assert.equal(R, true)
})
it('Should guard raw TUnsafe as TSchema', () => {
const T = Type.Unsafe({ x: 1 })
const R = TypeGuard.TSchema(T)
Assert.equal(R, true)
})
it('Should guard override TUnsafe as TSchema when registered', () => {
TypeRegistry.Set('type/guard/TUnsafe/Type1', () => true)
const T = Type.Unsafe({ [Kind]: 'type/guard/TUnsafe/Type1' })
const R = TypeGuard.TSchema(T)
Assert.equal(R, true)
})
it('Should not guard TUnsafe with unregistered kind', () => {
const T = Type.Unsafe({ [Kind]: 'type/guard/TUnsafe/Type2' })
const R = TypeGuard.TUnsafe(T)
Assert.equal(R, false)
})
it('Should not guard for TString', () => {
const T = Type.String()
const R = TypeGuard.TUnsafe(T)
Assert.equal(R, false)
})
})

0 comments on commit e4d7378

Please sign in to comment.