@sinclair/typebox 0.30.0-dev-2 → 0.30.0-dev-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,7 +140,7 @@ class TypeCompilerUnknownTypeError extends Error {
140
140
  exports.TypeCompilerUnknownTypeError = TypeCompilerUnknownTypeError;
141
141
  class TypeCompilerDereferenceError extends Error {
142
142
  constructor(schema) {
143
- super(`TypeCompiler: Unable to dereference schema with $id '${schema.$ref}'`);
143
+ super(`TypeCompiler: Unable to dereference type with $id '${schema.$ref}'`);
144
144
  this.schema = schema;
145
145
  }
146
146
  }
package/errors/errors.js CHANGED
@@ -131,7 +131,7 @@ class ValueErrorsUnknownTypeError extends Error {
131
131
  exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
132
132
  class ValueErrorsDereferenceError extends Error {
133
133
  constructor(schema) {
134
- super(`ValueErrors: Unable to dereference schema with $id '${schema.$ref}'`);
134
+ super(`ValueErrors: Unable to dereference type with $id '${schema.$ref}'`);
135
135
  this.schema = schema;
136
136
  }
137
137
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.30.0-dev-2",
3
+ "version": "0.30.0-dev-3",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -198,13 +198,13 @@ function receive(value: T) { // ... as a Static Type
198
198
 
199
199
  ## Types
200
200
 
201
- TypeBox types are JSON schema fragments that can be composed into more complex types. Each fragment is structured such that a JSON schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox provides a set of Standard types which are used create JSON schema compliant schematics as well as an Extended type set used to create schematics for constructs native to JavaScript.
201
+ TypeBox types are JSON schema fragments that compose into complex types. Each fragment is structured such that a JSON schema compliant validator can runtime assert a value the same way TypeScript will statically assert a type. TypeBox provides a set of Standard types which are used create JSON schema compliant schematics as well as an Extended type set used to create schematics for constructs native to JavaScript.
202
202
 
203
203
  <a name='types-standard'></a>
204
204
 
205
205
  ### Standard Types
206
206
 
207
- The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 6 specification.
207
+ The following table lists the Standard TypeBox types. These types are fully compatible with the JSON Schema Draft 7 specification.
208
208
 
209
209
  ```typescript
210
210
  ┌────────────────────────────────┬─────────────────────────────┬────────────────────────────────┐
@@ -668,7 +668,7 @@ Object properties can be modified with `readonly` or `optional`. The following t
668
668
 
669
669
  ### Generic Types
670
670
 
671
- Generic types can be created with generic functions constrained to type `TSchema`. The following creates a generic `Vector<T>` type.
671
+ Generic types are created with generic functions. All TypeBox types extend the sub type `TSchema` so it is common to constrain function arguments to this type. The following creates a generic `Vector<T>` type.
672
672
 
673
673
  ```typescript
674
674
  import { Type, Static, TSchema } from '@sinclair/typebox'
@@ -692,7 +692,7 @@ type NumberVector = Static<typeof NumberVector> // type NumberVector = {
692
692
  // }
693
693
  ```
694
694
 
695
- The following creates a generic `Nullable<T>` type.
695
+ Generic types can be used to create aliases for more complex types. The following creates a `Nullable<T>` type.
696
696
 
697
697
  ```typescript
698
698
  const Nullable = <T extends TSchema>(schema: T) => Type.Union([schema, Type.Null()])
@@ -711,24 +711,26 @@ type T = Static<typeof T> // type T = string | null
711
711
 
712
712
  ### Reference Types
713
713
 
714
- Reference types are supported with `Type.Ref`. The target type must specify a valid `$id`.
714
+ Reference types are supported with `Ref`.
715
715
 
716
716
  ```typescript
717
717
  const T = Type.String({ $id: 'T' }) // const T = {
718
- // $id: 'T',
719
- // type: 'string'
718
+ // $id: 'T',
719
+ // type: 'string'
720
720
  // }
721
721
 
722
- const R = Type.Ref(T) // const R = {
723
- // $ref: 'T'
722
+ const R = Type.Ref<typeof T>('T') // const R = {
723
+ // $ref: 'T'
724
724
  // }
725
+
726
+ type R = Static<typeof R> // type R = string
725
727
  ```
726
728
 
727
729
  <a name='types-recursive'></a>
728
730
 
729
731
  ### Recursive Types
730
732
 
731
- Recursive types are supported with `Type.Recursive`.
733
+ Recursive types are supported with `Recursive`. Recursive type inference is also supported.
732
734
 
733
735
  ```typescript
734
736
  const Node = Type.Recursive(This => Type.Object({ // const Node = {
@@ -765,7 +767,7 @@ function test(node: Node) {
765
767
 
766
768
  ### Conditional Types
767
769
 
768
- TypeBox supports conditional types with `Type.Extends`. This type will perform a structural assignment check for the first two parameters and return a `true` or `false` type from the second two parameters. The types `Type.Exclude` and `Type.Extract` are also supported.
770
+ TypeBox supports conditional types with `Extends`. This type performs a structural assignment check against the first two parameters and returns either the `true` or `false` type as given from the second two parameters. The conditional types `Exclude` and `Extract` are also supported.
769
771
 
770
772
  ```typescript
771
773
  // TypeScript
@@ -778,17 +780,17 @@ type T2 = Exclude<(1 | 2 | 3), 1> // type T2 = 2 | 3
778
780
 
779
781
  // TypeBox
780
782
 
781
- const T0 = Type.Extends( // const T0: TLiteral<false>
782
- Type.String(),
783
- Type.Number(),
784
- Type.Literal(true),
783
+ const T0 = Type.Extends( // const T0: TLiteral<false> = {
784
+ Type.String(), // type: 'boolean',
785
+ Type.Number(), // const: false
786
+ Type.Literal(true), // }
785
787
  Type.Literal(false)
786
788
  )
787
789
 
788
- const T1 = Type.Extract( // const T1: TLiteral<1>
789
- Type.Union([
790
- Type.Literal(1),
791
- Type.Literal(2),
790
+ const T1 = Type.Extract( // const T1: TLiteral<1> = {
791
+ Type.Union([ // type: 'number',
792
+ Type.Literal(1), // const: 1
793
+ Type.Literal(2), // }
792
794
  Type.Literal(3)
793
795
  ]),
794
796
  Type.Literal(1)
@@ -797,42 +799,38 @@ const T1 = Type.Extract( // const T1: TLiteral<1>
797
799
  const T2 = Type.Exclude( // const T2: TUnion<[
798
800
  Type.Union([ // TLiteral<2>,
799
801
  Type.Literal(1), // TLiteral<3>
800
- Type.Literal(2), // ]>
801
- Type.Literal(3)
802
- ]),
803
- Type.Literal(1)
804
- )
802
+ Type.Literal(2), // ]> = {
803
+ Type.Literal(3) // anyOf: [{
804
+ ]), // type: 'number',
805
+ Type.Literal(1) // const: 2
806
+ ) // }, {
807
+ // type: 'number',
808
+ // const: 3
809
+ // }]
810
+ // }
805
811
  ```
806
812
 
807
813
  <a name='types-template-literal'></a>
808
814
 
809
815
  ### Template Literal Types
810
816
 
811
- TypeBox supports template literal types with `Type.TemplateLiteral`. This type implements an embedded DSL syntax to match the TypeScript template literal syntax. This type can also be composed by passing an array of union and literal types as parameters. The following example shows the DSL syntax.
817
+ TypeBox supports template literal types with `TemplateLiteral`. This type provides an embedded DSL syntax that is similar to the TypeScript template literal syntax. These type can also be composed by passing a tuple of exterior union and literal types. The following example shows the DSL syntax.
812
818
 
813
819
  ```typescript
814
820
  // TypeScript
815
821
 
816
- type P = `/post/${string}/user/${number}` // type P = `/post/${string}/user/${number}`
817
-
818
- type T = `option${'A'|'B'}` // type T = 'optionA' | 'optionB'
822
+ type T = `option${'A'|'B'|'C'}` // type T = 'optionA' | 'optionB' | 'optionC'
819
823
 
820
824
  type R = Record<T, string> // type R = {
821
825
  // optionA: string
822
826
  // optionB: string
827
+ // optionC: string
823
828
  // }
824
829
 
825
830
  // TypeBox
826
831
 
827
- const P = Type.TemplateLiteral('/post/${string}/user/${number}')
828
-
829
- // const P = {
830
- // type: 'string',
831
- // pattern: '^/post/(.*)/user/(0|[1-9][0-9]*)$'
832
- // }
833
-
834
- const T = Type.TemplateLiteral('option${A|B}') // const T = {
835
- // pattern: '^option(A|B)$',
832
+ const T = Type.TemplateLiteral('option${A|B|C}') // const T = {
833
+ // pattern: '^option(A|B|C)$',
836
834
  // type: 'string'
837
835
  // }
838
836
 
@@ -846,6 +844,9 @@ const R = Type.Record(T, Type.String()) // const R = {
846
844
  // optionB: {
847
845
  // type: 'string'
848
846
  // }
847
+ // optionC: {
848
+ // type: 'string'
849
+ // }
849
850
  // }
850
851
  // }
851
852
  ```
@@ -854,7 +855,7 @@ const R = Type.Record(T, Type.String()) // const R = {
854
855
 
855
856
  ### Indexed Access Types
856
857
 
857
- TypeBox supports indexed access types using `Type.Index`. This type provides a consistent way to access interior property and array element types without having to extract them from the underlying schema representation. Indexed access types are supported for object, array, tuple, union and intersect types.
858
+ TypeBox supports indexed access types using `Index`. This type provides a consistent way of accessing interior property and array element types without having to extract them from the underlying schema representation. Indexed access types are supported for object, array, tuple, union and intersect types.
858
859
 
859
860
  ```typescript
860
861
  const T = Type.Object({ // const T = {
@@ -889,7 +890,7 @@ const C = Type.Index(T, Type.KeyOf(T)) // const C = {
889
890
 
890
891
  ### Negated Types
891
892
 
892
- TypeBox has support for type negation with `Type.Not`. This type will always infer as `unknown`.
893
+ TypeBox has support for type negation with `Not`. This type will always infer as `unknown`.
893
894
 
894
895
  ```typescript
895
896
  const T = Type.Not(Type.String()) // const T = {
@@ -900,7 +901,7 @@ type T = Static<typeof T> // type T = unknown
900
901
  //
901
902
  // where T could be any type other than string
902
903
  ```
903
- This type can be useful for certain forms of type narrowing. For example, consider a type that represents a `number` but not the values `1, 2, 3`. The example below shows an imaginary TypeScript syntax to express such a type followed by the TypeBox representation.
904
+ Type negation can be useful for certain forms of type narrowing. For example, consider a type that represents a `number` but not the numbers `1, 2, 3`. The example below shows an imaginary TypeScript syntax to express such a type followed by the TypeBox representation.
904
905
 
905
906
  ```typescript
906
907
  // TypeScript
@@ -926,7 +927,6 @@ const T = Type.Intersect([ // const T = {
926
927
 
927
928
  type T = Static<typeof T> // type T = number
928
929
  ```
929
-
930
930
  This type can be used with constraints to create schematics that would otherwise be difficult to express.
931
931
  ```typescript
932
932
  const Even = Type.Number({ multipleOf: 2 })
@@ -937,7 +937,7 @@ const Odd = Type.Intersect([Type.Number(), Type.Not(Even)])
937
937
 
938
938
  ### Rest Types
939
939
 
940
- Rest parameters are supported with `Type.Rest`. This function is used to extract interior type elements from tuples which enables them to compose with the JavaScript spread operator `...`. This type can be used for tuple concatenation as well as for variadic functions.
940
+ Rest parameters are supported with `Rest`. This function is used to extract interior type elements from tuples which enables them to compose with the JavaScript spread operator `...`. This type can be used for tuple concatenation as well function parameter assignment.
941
941
 
942
942
  ```typescript
943
943
  // TypeScript
@@ -975,7 +975,7 @@ const F = Type.Function(Type.Rest(C), Type.Void()) // const F: TFunction<[
975
975
 
976
976
  ### Unsafe Types
977
977
 
978
- Use `Type.Unsafe` to create custom schematics with user defined inference rules.
978
+ TypeBox supports the creation of user defined schematics with user defined inference rules using the Unsafe type.
979
979
 
980
980
  ```typescript
981
981
  const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
@@ -985,7 +985,7 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
985
985
  type T = Static<typeof T> // type T = string
986
986
  ```
987
987
 
988
- The `Type.Unsafe` type can be useful to express specific OpenAPI schema representations.
988
+ This type can be useful to create various extended schematics, such as those used by OpenAPI.
989
989
 
990
990
  ```typescript
991
991
  import { Type, Static, TSchema } from '@sinclair/typebox'
@@ -1020,12 +1020,12 @@ type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
1020
1020
 
1021
1021
  ### Type Guards
1022
1022
 
1023
- TypeBox provides a `TypeGuard` module that can be used for reflection and asserting values as types.
1023
+ TypeBox provides a TypeGuard module to assert JavaScript values are valid TypeBox types.
1024
1024
 
1025
1025
  ```typescript
1026
- import { Type, TypeGuard } from '@sinclair/typebox'
1026
+ import { Type, Kind, TypeGuard } from '@sinclair/typebox'
1027
1027
 
1028
- const T = Type.String()
1028
+ const T = { [Kind]: 'String', type: 'string' }
1029
1029
 
1030
1030
  if(TypeGuard.TString(T)) {
1031
1031
 
@@ -1037,7 +1037,7 @@ if(TypeGuard.TString(T)) {
1037
1037
 
1038
1038
  ### Strict
1039
1039
 
1040
- TypeBox types contain various symbol properties that are used for reflection, composition and compilation. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Type.Strict` function that will omit these properties if necessary.
1040
+ TypeBox types contain various symbol properties that are used for reflection, composition and compilation. These properties are not strictly valid JSON schema; so in some cases it may be desirable to omit them. TypeBox provides a `Strict` function that will omit these properties if necessary.
1041
1041
 
1042
1042
  ```typescript
1043
1043
  const T = Type.Object({ // const T = {
@@ -1110,7 +1110,7 @@ const R = Value.Check(T, { x: 1 }) // const R = true
1110
1110
 
1111
1111
  ### Convert
1112
1112
 
1113
- Use the Convert function to convert a value into its target type if a reasonable conversion is possible.
1113
+ Use the Convert function to convert a value into its target type if a reasonable conversion is possible. This function may return an invalid value and should be checked before use. It's return type is `unknown`.
1114
1114
 
1115
1115
  ```typescript
1116
1116
  const T = Type.Object({ x: Type.Number() })
@@ -1262,7 +1262,7 @@ ValuePointer.Set(A, '/z', 1) // const A' = { x: 1, y: 1,
1262
1262
 
1263
1263
  ## TypeCheck
1264
1264
 
1265
- TypeBox types target JSON Schema draft 6 so are compatible with any validator that supports this specification. TypeBox also provides a built in type checking compiler designed specifically for high performance compilation and value assertion.
1265
+ TypeBox types target JSON Schema draft 7 so are compatible with any validator that supports this specification. TypeBox also provides a built in type checking compiler designed specifically for high performance compilation and value assertion.
1266
1266
 
1267
1267
  The following sections detail using Ajv and TypeBox's compiler infrastructure.
1268
1268
 
package/value/cast.js CHANGED
@@ -74,7 +74,7 @@ class ValueCastUnknownTypeError extends Error {
74
74
  exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
75
75
  class ValueCastDereferenceError extends Error {
76
76
  constructor(schema) {
77
- super(`ValueCast: Unable to dereference schema with $id '${schema.$ref}'`);
77
+ super(`ValueCast: Unable to dereference type with $id '${schema.$ref}'`);
78
78
  this.schema = schema;
79
79
  }
80
80
  }
package/value/check.js CHANGED
@@ -44,7 +44,7 @@ class ValueCheckUnknownTypeError extends Error {
44
44
  exports.ValueCheckUnknownTypeError = ValueCheckUnknownTypeError;
45
45
  class ValueCheckDereferenceError extends Error {
46
46
  constructor(schema) {
47
- super(`ValueCheck: Unable to dereference schema with $id '${schema.$ref}'`);
47
+ super(`ValueCheck: Unable to dereference type with $id '${schema.$ref}'`);
48
48
  this.schema = schema;
49
49
  }
50
50
  }
package/value/convert.js CHANGED
@@ -44,7 +44,7 @@ class ValueConvertUnknownTypeError extends Error {
44
44
  exports.ValueConvertUnknownTypeError = ValueConvertUnknownTypeError;
45
45
  class ValueConvertDereferenceError extends Error {
46
46
  constructor(schema) {
47
- super(`ValueConvert: Unable to dereference schema with $id '${schema.$ref}'`);
47
+ super(`ValueConvert: Unable to dereference type with $id '${schema.$ref}'`);
48
48
  this.schema = schema;
49
49
  }
50
50
  }
package/value/create.js CHANGED
@@ -71,7 +71,7 @@ class ValueCreateTempateLiteralTypeError extends Error {
71
71
  exports.ValueCreateTempateLiteralTypeError = ValueCreateTempateLiteralTypeError;
72
72
  class ValueCreateDereferenceError extends Error {
73
73
  constructor(schema) {
74
- super(`ValueCreate: Unable to dereference schema with $id '${schema.$ref}'`);
74
+ super(`ValueCreate: Unable to dereference type with $id '${schema.$ref}'`);
75
75
  this.schema = schema;
76
76
  }
77
77
  }