diff --git a/examples/index.ts b/examples/index.ts index 40a1ceff2..ed52156bd 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -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 + +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)) diff --git a/test/runtime/value/transform/intersect.ts b/test/runtime/value/transform/intersect.ts index 0d7f39043..7e0bcc825 100644 --- a/test/runtime/value/transform/intersect.ts +++ b/test/runtime/value/transform/intersect.ts @@ -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 } + + ], + }) + }) + } })