Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 20, 2023
1 parent 6e79451 commit 9394d2a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 35 deletions.
70 changes: 35 additions & 35 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, StaticDecode, StaticEncode, Static } from '@sinclair/typebox'

const TBItem = Type.Transform(
Type.Object({
isHybrid: Type.Boolean(),
}),
)
.Decode((value) => ({ isHybrid: value.isHybrid ? 1 : 0 }))
.Encode((value) => ({ isHybrid: value.isHybrid === 1 ? true : false }))

let decoded = Value.Decode(TBItem, { isHybrid: true })
let encoded = Value.Encode(TBItem, { isHybrid: 1 })
console.log('decoded', decoded)
console.log('encoded', encoded)

const TBIntersect = Type.Intersect([
Type.Object({
model: Type.String(),
}),
Type.Object({
features: Type.Array(TBItem),
}),
])

const aencoded = Value.Encode(TBIntersect, {
model: 'Prius',
features: [
{
isHybrid: 1,
},
{
isHybrid: 1,
},
],
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
})

console.log(aencoded)
type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------

const C = TypeCompiler.Compile(T)

console.log(C.Code())

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------

console.log(C.Check(V))
57 changes: 57 additions & 0 deletions test/runtime/value/transform/intersect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,61 @@ describe('value/transform/Intersect', () => {
it('Should throw on exterior value type decode', () => {
Assert.Throws(() => Encoder.Decode(T4, null))
})
// --------------------------------------------------------
// https://github.com/sinclairzx81/typebox/discussions/672
// --------------------------------------------------------
// prettier-ignore
{
const A = Type.Object({ isHybrid: Type.Boolean() })
const T = Type.Transform(A)
.Decode((value) => ({ isHybrid: value.isHybrid ? 1 : 0 }))
.Encode((value) => ({ isHybrid: value.isHybrid === 1 ? true : false }))
const I = Type.Intersect([
Type.Object({ model: Type.String() }),
Type.Object({ features: Type.Array(T) }),
])
it('Should decode nested 1', () => {
const value = Value.Decode(T, { isHybrid: true })
Assert.IsEqual(value, { isHybrid: 1 })
})
// prettier-ignore
it('Should decode nested 2', () => {
const value = Value.Decode(I, {
model: 'Prius',
features: [
{ isHybrid: true },
{ isHybrid: false }
],
})
Assert.IsEqual(value, {
model: 'Prius',
features: [
{ isHybrid: 1 },
{ isHybrid: 0 }
],
})
})
it('should encode nested 1', () => {
let value = Value.Encode(T, { isHybrid: 1 })
Assert.IsEqual(value, { isHybrid: true })
})
// prettier-ignore
it('Should encode nested 2', () => {
const value = Value.Encode(I, {
model: 'Prius',
features: [
{ isHybrid: 1 },
{ isHybrid: 0 }
],
})
Assert.IsEqual(value, {
model: 'Prius',
features: [
{ isHybrid: true },
{ isHybrid: false }

],
})
})
}
})

0 comments on commit 9394d2a

Please sign in to comment.