Skip to content

Commit 941abce

Browse files
authored
Version 1.0.37 (#1406)
* Advanced Json Schema Inference * ChangeLog * Version
1 parent a3d9bc7 commit 941abce

92 files changed

Lines changed: 1171 additions & 483 deletions

Some content is hidden

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

changelog/1.0.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---
44

55
### Version Updates
6+
- [Revision 1.0.37](https://github.com/sinclairzx81/typebox/pull/1406)
7+
- Json Schema Inference: Union TypeName and UnevaluatedProperties
68
- [Revision 1.0.36](https://github.com/sinclairzx81/typebox/pull/1405)
79
- Add Immutable Type for Tuple and Array
810
- Documentation Updates Literal | Immutable
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Setup
22

3-
TypeBox requires no special configuration to use, but it is recommended to configure tsconfig.json with `strict: true`.
3+
TypeBox needs TypeScript to be configured with the following settings.
44

55
```typescript
66
// file: tsconfig.json
7-
87
{
98
"compilerOptions": {
10-
"strict": true
9+
"strict": true, // Required for Type Inference
10+
"target": "ES2018", // Minimum ES Target
1111
}
1212
}
1313
```

docs/docs/overview/2_setup.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<h1>Setup</h1>
2-
<p>TypeBox requires no special configuration to use, but it is recommended to configure tsconfig.json with <code>strict: true</code>.</p>
2+
<p>TypeBox needs TypeScript to be configured with the following settings.</p>
33
<pre><code class="language-typescript">// file: tsconfig.json
4-
54
{
65
&quot;compilerOptions&quot;: {
7-
&quot;strict&quot;: true
6+
&quot;strict&quot;: true, // Required for Type Inference
7+
&quot;target&quot;: &quot;ES2018&quot;, // Minimum ES Target
88
}
99
}
1010
</code></pre>

example/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Type from 'typebox'
1010
// ------------------------------------------------------------------
1111
System.Settings.Set({ enumerableKind: false })
1212

13-
1413
// ------------------------------------------------------------------
1514
// Type
1615
// ------------------------------------------------------------------

src/schema/build.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { Guard } from '../guard/index.ts'
3636
import { Format } from '../format/index.ts'
3737

3838
import * as Engine from './engine/index.ts'
39-
import { type XSchemaLike } from './types/index.ts'
39+
import { type XSchema } from './types/index.ts'
4040

4141
// ------------------------------------------------------------------
4242
// CreateCode
@@ -88,19 +88,19 @@ export interface EvaluateResult {
8888
// ------------------------------------------------------------------
8989
export class BuildResult {
9090
constructor(
91-
private readonly context: Record<PropertyKey, XSchemaLike>,
92-
private readonly schema: XSchemaLike,
91+
private readonly context: Record<PropertyKey, XSchema>,
92+
private readonly schema: XSchema,
9393
private readonly external: Engine.TExternal,
9494
private readonly functions: string[],
9595
private readonly call: string,
9696
private readonly useUnevaluated: boolean
9797
) { }
9898
/** Returns the Context used for this build */
99-
public Context(): Record<PropertyKey, XSchemaLike> {
99+
public Context(): Record<PropertyKey, XSchema> {
100100
return this.context
101101
}
102102
/** Returns the Schema used for this build */
103-
public Schema(): XSchemaLike {
103+
public Schema(): XSchema {
104104
return this.schema
105105
}
106106
/** Returns true if this build requires a Unevaluated context */
@@ -130,12 +130,12 @@ export class BuildResult {
130130
// Build
131131
// ------------------------------------------------------------------
132132
/** Builds a schema into a optimized runtime validator */
133-
export function Build(schema: XSchemaLike): BuildResult
133+
export function Build(schema: XSchema): BuildResult
134134
/** Builds a schema into a optimized runtime validator */
135-
export function Build(context: Record<PropertyKey, XSchemaLike>, schema: XSchemaLike): BuildResult
135+
export function Build(context: Record<PropertyKey, XSchema>, schema: XSchema): BuildResult
136136
/** Builds a schema into a optimized runtime validator */
137137
export function Build(...args: unknown[]): BuildResult {
138-
const [context, schema] = Arguments.Match<[Record<PropertyKey, XSchemaLike>, XSchemaLike]>(args, {
138+
const [context, schema] = Arguments.Match<[Record<PropertyKey, XSchema>, XSchema]>(args, {
139139
2: (context, schema) => [context, schema],
140140
1: (schema) => [{}, schema]
141141
})

src/schema/check.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ THE SOFTWARE.
3131

3232
import { Arguments } from '../system/arguments/index.ts'
3333
import { CheckSchema, CheckContext } from './engine/index.ts'
34-
import { XSchemaLike } from './types/index.ts'
34+
import { XSchema } from './types/index.ts'
3535

3636
// ------------------------------------------------------------------
3737
// Check
3838
// ------------------------------------------------------------------
3939
/** Checks a value against the provided schema */
40-
export function Check(schema: XSchemaLike, value: unknown): boolean
40+
export function Check(schema: XSchema, value: unknown): boolean
4141
/** Checks a value against the provided schema */
42-
export function Check(context: Record<PropertyKey, XSchemaLike>, schema: XSchemaLike, value: unknown): boolean
42+
export function Check(context: Record<PropertyKey, XSchema>, schema: XSchema, value: unknown): boolean
4343
/** Checks a value against the provided schema */
4444
export function Check(...args: unknown[]): boolean {
45-
const [context, schema, value] = Arguments.Match<[Record<PropertyKey, XSchemaLike>, XSchemaLike, unknown]>(args, {
45+
const [context, schema, value] = Arguments.Match<[Record<PropertyKey, XSchema>, XSchema, unknown]>(args, {
4646
3: (context, schema, value) => [context, schema, value],
4747
2: (schema, value) => [{}, schema, value]
4848
})

src/schema/deref/deref.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import * as S from '../types/index.ts'
3434
// ------------------------------------------------------------------
3535
// Rebase
3636
// ------------------------------------------------------------------
37-
export function Rebase(baseUri: string, schema: S.XSchema): string {
37+
export function Rebase(baseUri: string, schema: S.XSchemaObject): string {
3838
if (!S.IsId(schema)) return baseUri
3939
if (IsUri(schema.$id)) return schema.$id
4040
if (!IsUri(schema.$id) && IsUri(baseUri)) {
@@ -51,7 +51,7 @@ function IsHashPointer(value: string): boolean {
5151
function IsHash(value: string): boolean {
5252
return value.length > 0 && value[0] === '#'
5353
}
54-
function Select(schema: S.XSchema, baseUri: string, $ref: string): S.XSchemaLike | undefined {
54+
function Select(schema: S.XSchemaObject, baseUri: string, $ref: string): S.XSchema | undefined {
5555
if (IsUri(baseUri)) {
5656
const [base, ref] = [new URL(baseUri), new URL($ref, baseUri)]
5757
const matched = base.pathname === ref.pathname
@@ -62,14 +62,14 @@ function Select(schema: S.XSchema, baseUri: string, $ref: string): S.XSchemaLike
6262
}
6363
if (IsHashPointer($ref)) {
6464
const target = Pointer.Get(schema, $ref.slice(1))
65-
if (S.IsSchemaLike(target)) return target
65+
if (S.IsSchema(target)) return target
6666
}
6767
return undefined
6868
}
6969
// ------------------------------------------------------------------
7070
// Traversal
7171
// ------------------------------------------------------------------
72-
function FromObject(schema: S.XSchema, baseUrl: string, ref: S.XRef): S.XSchemaLike | undefined {
72+
function FromObject(schema: S.XSchemaObject, baseUrl: string, ref: S.XRef): S.XSchema | undefined {
7373
// ----------------------------------------------------------------
7474
// TypeBox: Fast TCyclic Deref
7575
// ----------------------------------------------------------------
@@ -81,18 +81,18 @@ function FromObject(schema: S.XSchema, baseUrl: string, ref: S.XRef): S.XSchemaL
8181
// ----------------------------------------------------------------
8282
const rebase = Rebase(baseUrl, schema)
8383
const target = Select(schema, rebase, ref.$ref)
84-
if (S.IsSchemaLike(target)) return target
84+
if (S.IsSchema(target)) return target
8585

8686
// Keep searching ...
87-
return Object.values(schema).reduce<S.XSchemaLike | undefined>(
87+
return Object.values(schema).reduce<S.XSchema | undefined>(
8888
(result, subschema) => result || FromValue(subschema, rebase, ref),
8989
undefined
9090
)
9191
}
92-
function FromArray(schema: unknown[], baseUri: string, ref: S.XRef): S.XSchemaLike | undefined {
93-
return schema.reduce((result, value) => result || FromValue(value, baseUri, ref), undefined) as S.XSchema | undefined
92+
function FromArray(schema: unknown[], baseUri: string, ref: S.XRef): S.XSchema | undefined {
93+
return schema.reduce((result, value) => result || FromValue(value, baseUri, ref), undefined) as S.XSchemaObject | undefined
9494
}
95-
function FromValue(schema: unknown, base: string, ref: S.XRef): S.XSchemaLike | undefined {
95+
function FromValue(schema: unknown, base: string, ref: S.XRef): S.XSchema | undefined {
9696
return (
9797
(Guard.IsObject(schema) && FromObject(schema, base, ref)) ||
9898
(Guard.IsArray(schema) && FromArray(schema, base, ref)) ||

src/schema/engine/_context.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export function HasUnevaluated(context: Record<PropertyKey, unknown>, schema: un
6161
// ------------------------------------------------------------------
6262
export class BaseContext {
6363
constructor(
64-
protected readonly context: Record<PropertyKey, S.XSchemaLike>,
65-
protected readonly schema: S.XSchemaLike,
64+
protected readonly context: Record<PropertyKey, S.XSchema>,
65+
protected readonly schema: S.XSchema,
6666
) { }
6767
public GetContext(): Record<PropertyKey, object | boolean> {
6868
return this.context
@@ -75,7 +75,7 @@ export class BaseContext {
7575
// BuildContext
7676
// ------------------------------------------------------------------
7777
export class BuildContext extends BaseContext {
78-
constructor(context: Record<PropertyKey, S.XSchemaLike>, schema: S.XSchemaLike, private readonly hasUnevaluated: boolean) {
78+
constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema, private readonly hasUnevaluated: boolean) {
7979
super(context, schema)
8080
}
8181
public UseUnevaluated(): boolean {
@@ -100,7 +100,7 @@ export class BuildContext extends BaseContext {
100100
export class CheckContext extends BaseContext {
101101
private readonly indices: Set<number>
102102
private readonly keys: Set<string>
103-
constructor(context: Record<PropertyKey, S.XSchemaLike>, schema: S.XSchemaLike) {
103+
constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema) {
104104
super(context, schema)
105105
this.indices = new Set()
106106
this.keys = new Set()
@@ -135,7 +135,7 @@ export class CheckContext extends BaseContext {
135135
// ------------------------------------------------------------------
136136
export type ErrorContextCallback = (error: TValidationError) => unknown
137137
export class ErrorContext extends CheckContext {
138-
constructor(context: Record<PropertyKey, S.XSchemaLike>, schema: S.XSchemaLike, private readonly callback: ErrorContextCallback) {
138+
constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema, private readonly callback: ErrorContextCallback) {
139139
super(context, schema)
140140
}
141141
public AddError(error: TValidationError): false {
@@ -148,7 +148,7 @@ export class ErrorContext extends CheckContext {
148148
// ------------------------------------------------------------------
149149
export class AccumulatedErrorContext extends ErrorContext {
150150
private readonly errors: TValidationError[]
151-
constructor(context: Record<PropertyKey, S.XSchemaLike>, schema: S.XSchemaLike) {
151+
constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema) {
152152
super(context, schema, error => this.errors.push(error))
153153
this.errors = []
154154
}

src/schema/engine/_functions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ const functions: Map<string, string> = new Map()
3939
// ------------------------------------------------------------------
4040
// CreateCallExpression
4141
// ------------------------------------------------------------------
42-
function CreateCallExpression(context: BuildContext, schema: S.XSchemaLike, hash: string, value: string): string {
42+
function CreateCallExpression(context: BuildContext, schema: S.XSchema, hash: string, value: string): string {
4343
return context.UseUnevaluated()
4444
? E.Call(`check_${hash}`, ['context', value])
4545
: E.Call(`check_${hash}`, [value])
4646
}
4747
// ------------------------------------------------------------------
4848
// CreateFunctionExpression
4949
// ------------------------------------------------------------------
50-
function CreateFunctionExpression(context: BuildContext, schema: S.XSchemaLike, hash: string): string {
50+
function CreateFunctionExpression(context: BuildContext, schema: S.XSchema, hash: string): string {
5151
const expression = BuildSchema(context, schema, 'value')
5252
return context.UseUnevaluated()
5353
? E.ConstDeclaration(`check_${hash}`, E.ArrowFunction(['context', 'value'], expression))
@@ -68,7 +68,7 @@ export function GetFunctions(): string[] {
6868
// ------------------------------------------------------------------
6969
// CreateFunction
7070
// ------------------------------------------------------------------
71-
export function CreateFunction(context: BuildContext, schema: S.XSchemaLike, value: string): string {
71+
export function CreateFunction(context: BuildContext, schema: S.XSchema, value: string): string {
7272
const hash = Hashing.Hash(schema)
7373
const call = CreateCallExpression(context, schema, hash, value)
7474
if (functions.has(hash)) return call

src/schema/engine/_reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import { BuildSchema } from './schema.ts'
6767
// })()
6868
//
6969
// ------------------------------------------------------------------
70-
export function Reducer(context: BuildContext, schemas: S.XSchemaLike[], value: string, check: string): string {
70+
export function Reducer(context: BuildContext, schemas: S.XSchema[], value: string, check: string): string {
7171
const results = E.ConstDeclaration('results', '[]')
7272
const context_n = schemas.map((_schema, index) => E.ConstDeclaration(`context_${index}`, context.Clone()))
7373
const condition_n = schemas.map((schema, index) => E.ConstDeclaration(`condition_${index}`, E.Call(E.ArrowFunction(['context'], BuildSchema(context, schema, value)), [`context_${index}`])))

0 commit comments

Comments
 (0)