Skip to content

Commit 4362a55

Browse files
committed
Next
1 parent 03a36dd commit 4362a55

63 files changed

Lines changed: 138 additions & 2429 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

design/website/docs/value/parse.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,4 @@ Example usage is shown below.
1010
const R = Value.Parse(Type.String(), 'hello') // const R: string = "hello"
1111

1212
const E = Value.Parse(Type.String(), 12345) // throws ParseError
13-
```
14-
15-
## Corrective Parse
16-
17-
TypeBox provides an optional corrective parsing mode that attempts to repair invalid values before failing. When enabled, the parser runs a pipeline consisting of Convert, Default, and Clean, then re-asserts the value after processing. This feature can be useful when parsing environment variables into target types.
18-
19-
> ⚠️ This feature can impact performance. It is not recommended for use in high throughput applications.
20-
21-
This feature can be enabled as follows:
22-
23-
```typescript
24-
import { Settings } from 'typebox/system'
25-
26-
// Corrective Parse: Enable
27-
28-
Settings.Set({ correctiveParse: true })
29-
30-
// Corrective Parse: Convert Value into the target type if reasonable conversion is possible.
31-
32-
const R = Value.Parse(Type.String(), 'hello') // const R: string = "hello"
33-
34-
const S = Value.Parse(Type.String(), 12345) // const S: string = "12345"
35-
36-
// Corrective Parse: Reset (optional)
37-
38-
Settings.Reset()
3913
```

example/index.ts

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,23 @@
1-
import Compile from 'typebox/compile'
2-
import System from 'typebox/system'
3-
import Guard from 'typebox/guard'
4-
import Format from 'typebox/format'
5-
import Schema from 'typebox/schema'
61
import Value from 'typebox/value'
72
import Type from 'typebox'
83

9-
// ------------------------------------------------------------------
10-
// Settings
11-
// ------------------------------------------------------------------
4+
function Decode<Type extends Type.TSchema>(type: Type, value: unknown): Type.StaticDecode<Type> {
5+
return Value.Pipeline(
6+
Value.Clone,
7+
Value.Convert,
8+
Value.Clean,
9+
Value.Assert
10+
)(type, value)
11+
}
12+
function Encode<Type extends Type.TSchema>(type: Type, value: unknown): Type.StaticEncode<Type> {
13+
return Value.Pipeline(
14+
Value.Clone,
15+
Value.Encode,
16+
Value.Convert,
17+
Value.Clean,
18+
Value.Assert,
19+
)(type, value)
20+
}
21+
22+
1223

13-
System.Settings.Set({ enumerableKind: false })
14-
15-
// ------------------------------------------------------------------
16-
// Guard
17-
// ------------------------------------------------------------------
18-
19-
const A = Guard.GraphemeCount('type-📦') // 6
20-
const B = Guard.HasPropertyKey({ x: 1 }, 'x') // true
21-
22-
// ------------------------------------------------------------------
23-
// Type
24-
// ------------------------------------------------------------------
25-
26-
const T = Type.Object({
27-
x: Type.Number(),
28-
y: Type.Number(),
29-
z: Type.Number()
30-
})
31-
32-
// ------------------------------------------------------------------
33-
// Script
34-
// ------------------------------------------------------------------
35-
36-
const S = Type.Script({ T }, `{
37-
[K in keyof T]: T[K] | null
38-
}`)
39-
40-
// ------------------------------------------------------------------
41-
// Infer
42-
// ------------------------------------------------------------------
43-
44-
type T = Type.Static<typeof T>
45-
type S = Type.Static<typeof S>
46-
47-
// ------------------------------------------------------------------
48-
// Parse
49-
// ------------------------------------------------------------------
50-
51-
const R = Value.Parse(T, { x: 1, y: 2, z: 3 })
52-
53-
// ------------------------------------------------------------------
54-
// Compile
55-
// ------------------------------------------------------------------
56-
const C = Compile(S)
57-
58-
const X = C.Parse({ x: 1, y: 2, z: 3 })
59-
60-
// ------------------------------------------------------------------
61-
// Format
62-
// ------------------------------------------------------------------
63-
64-
const E = Format.IsEmail('user@domain.com')
65-
66-
// ------------------------------------------------------------------
67-
// Schema
68-
// ------------------------------------------------------------------
69-
70-
const D = Schema.Parse({ const: 'hello' }, 'hello')

example/legacy/date.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ THE SOFTWARE.
2828

2929
import Type from 'typebox'
3030

31-
// ------------------------------------------------------------------
32-
// Definition
33-
// ------------------------------------------------------------------
34-
export class TDate extends Type.Base<globalThis.Date> {
35-
// required: Used by validation
36-
public override Check(value: unknown): value is globalThis.Date {
37-
return value instanceof globalThis.Date
38-
}
39-
// required: Used by validation
40-
public override Errors(value: unknown): object[] {
41-
return this.Check(value) ? [] : [{ message: 'must be Date' }]
42-
}
43-
// required: Used by type compositor
44-
public override Clone(): TDate {
45-
return new TDate()
46-
}
47-
// required: Used by value/create
48-
public override Create(): globalThis.Date {
49-
return new globalThis.Date(0)
50-
}
51-
}
52-
// ------------------------------------------------------------------
53-
// Factory
54-
// ------------------------------------------------------------------
55-
export function Date(): TDate {
56-
return new TDate()
57-
}
31+
// // ------------------------------------------------------------------
32+
// // Definition
33+
// // ------------------------------------------------------------------
34+
// export class TDate extends Type.Base<globalThis.Date> {
35+
// // required: Used by validation
36+
// public override Check(value: unknown): value is globalThis.Date {
37+
// return value instanceof globalThis.Date
38+
// }
39+
// // required: Used by validation
40+
// public override Errors(value: unknown): object[] {
41+
// return this.Check(value) ? [] : [{ message: 'must be Date' }]
42+
// }
43+
// // required: Used by type compositor
44+
// public override Clone(): TDate {
45+
// return new TDate()
46+
// }
47+
// // required: Used by value/create
48+
// public override Create(): globalThis.Date {
49+
// return new globalThis.Date(0)
50+
// }
51+
// }
52+
// // ------------------------------------------------------------------
53+
// // Factory
54+
// // ------------------------------------------------------------------
55+
// export function Date(): TDate {
56+
// return new TDate()
57+
// }

example/legacy/uint8array.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,30 @@ THE SOFTWARE.
2828

2929
import Type from 'typebox'
3030

31-
// ------------------------------------------------------------------
32-
// Definition
33-
// ------------------------------------------------------------------
34-
export class TUint8Array extends Type.Base<globalThis.Uint8Array> {
35-
// required: Used by validation
36-
public override Check(value: unknown): value is Uint8Array {
37-
return value instanceof Uint8Array
38-
}
39-
// required: Used by validation
40-
public override Errors(value: unknown): object[] {
41-
return !this.Check(value) ? [{ message: 'not a Uint8Array'}] : []
42-
}
43-
// required: Used by type compositor
44-
public override Clone(): TUint8Array {
45-
return new TUint8Array()
46-
}
47-
// required: Used by value/create
48-
public override Create(): globalThis.Uint8Array {
49-
return new globalThis.Uint8Array(0)
50-
}
51-
}
52-
// ------------------------------------------------------------------
53-
// Factory
54-
// ------------------------------------------------------------------
55-
export function Uint8Array(): TUint8Array {
56-
return new TUint8Array()
57-
}
31+
// // ------------------------------------------------------------------
32+
// // Definition
33+
// // ------------------------------------------------------------------
34+
// export class TUint8Array extends Type.Base<globalThis.Uint8Array> {
35+
// // required: Used by validation
36+
// public override Check(value: unknown): value is Uint8Array {
37+
// return value instanceof Uint8Array
38+
// }
39+
// // required: Used by validation
40+
// public override Errors(value: unknown): object[] {
41+
// return !this.Check(value) ? [{ message: 'not a Uint8Array'}] : []
42+
// }
43+
// // required: Used by type compositor
44+
// public override Clone(): TUint8Array {
45+
// return new TUint8Array()
46+
// }
47+
// // required: Used by value/create
48+
// public override Create(): globalThis.Uint8Array {
49+
// return new globalThis.Uint8Array(0)
50+
// }
51+
// }
52+
// // ------------------------------------------------------------------
53+
// // Factory
54+
// // ------------------------------------------------------------------
55+
// export function Uint8Array(): TUint8Array {
56+
// return new TUint8Array()
57+
// }

src/compile/code.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.

src/compile/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ THE SOFTWARE.
2929
// ------------------------------------------------------------------
3030
// Barrel
3131
// ------------------------------------------------------------------
32-
export * from './code.ts'
3332
export * from './compile.ts'
3433
export * from './validator.ts'
3534

3635
// ------------------------------------------------------------------
3736
// Default
3837
// ------------------------------------------------------------------
39-
import { Code } from './code.ts'
4038
import { Compile } from './compile.ts'
4139
import { Validator } from './validator.ts'
42-
export { Code, Compile, Validator }
40+
export { Compile, Validator }
4341
export default Compile

0 commit comments

Comments
 (0)