-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e79451
commit 9394d2a
Showing
2 changed files
with
92 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters