@sinclair/typebox 0.30.0-dev-1 → 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.
- package/compiler/compiler.js +1 -1
- package/errors/errors.js +1 -1
- package/package.json +1 -1
- package/readme.md +50 -50
- package/value/cast.d.ts +1 -2
- package/value/cast.js +2 -3
- package/value/check.js +1 -1
- package/value/convert.d.ts +2 -3
- package/value/convert.js +3 -4
- package/value/create.d.ts +2 -4
- package/value/create.js +2 -4
- package/value/mutate.d.ts +1 -1
- package/value/mutate.js +1 -1
- package/value/value.d.ts +5 -5
- package/value/value.js +2 -2
package/compiler/compiler.js
CHANGED
|
@@ -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
|
|
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
|
|
134
|
+
super(`ValueErrors: Unable to dereference type with $id '${schema.$ref}'`);
|
|
135
135
|
this.schema = schema;
|
|
136
136
|
}
|
|
137
137
|
}
|
package/package.json
CHANGED
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
|
|
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
|
|
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
|
|
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
|
|
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 `
|
|
714
|
+
Reference types are supported with `Ref`.
|
|
715
715
|
|
|
716
716
|
```typescript
|
|
717
717
|
const T = Type.String({ $id: 'T' }) // const T = {
|
|
718
|
-
//
|
|
719
|
-
//
|
|
718
|
+
// $id: 'T',
|
|
719
|
+
// type: 'string'
|
|
720
720
|
// }
|
|
721
721
|
|
|
722
|
-
const R = Type.Ref(T)
|
|
723
|
-
//
|
|
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 `
|
|
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 `
|
|
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 `
|
|
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
|
|
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
|
|
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 `
|
|
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 `
|
|
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
|
-
|
|
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 `
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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 =
|
|
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 `
|
|
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
|
|
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.d.ts
CHANGED
|
@@ -24,8 +24,7 @@ export declare class ValueCastDereferenceError extends Error {
|
|
|
24
24
|
readonly schema: Types.TRef | Types.TThis;
|
|
25
25
|
constructor(schema: Types.TRef | Types.TThis);
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
27
|
+
/** Casts a value into a given type and references. The return value will retain as much information of the original value as possible. */
|
|
29
28
|
export declare function Cast<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): Types.Static<T>;
|
|
30
29
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
31
30
|
export declare function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
package/value/cast.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Cast = exports.
|
|
30
|
+
exports.Cast = exports.ValueCastDereferenceError = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const ValueCreate = require("./create");
|
|
33
33
|
const ValueCheck = require("./check");
|
|
@@ -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
|
|
77
|
+
super(`ValueCast: Unable to dereference type with $id '${schema.$ref}'`);
|
|
78
78
|
this.schema = schema;
|
|
79
79
|
}
|
|
80
80
|
}
|
|
@@ -359,7 +359,6 @@ function Visit(schema, references, value) {
|
|
|
359
359
|
return TKind(schema_, references_, value);
|
|
360
360
|
}
|
|
361
361
|
}
|
|
362
|
-
exports.Visit = Visit;
|
|
363
362
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
364
363
|
function Cast(...args) {
|
|
365
364
|
return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]);
|
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
|
|
47
|
+
super(`ValueCheck: Unable to dereference type with $id '${schema.$ref}'`);
|
|
48
48
|
this.schema = schema;
|
|
49
49
|
}
|
|
50
50
|
}
|
package/value/convert.d.ts
CHANGED
|
@@ -7,8 +7,7 @@ export declare class ValueConvertDereferenceError extends Error {
|
|
|
7
7
|
readonly schema: Types.TRef | Types.TThis;
|
|
8
8
|
constructor(schema: Types.TRef | Types.TThis);
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
/** Converts any type mismatched values to their target type if a conversion is possible. */
|
|
10
|
+
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
12
11
|
export declare function Convert<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): unknown;
|
|
13
|
-
/** Converts any type mismatched values to their target type if a conversion is possible. */
|
|
12
|
+
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
14
13
|
export declare function Convert<T extends Types.TSchema>(schema: T, value: unknown): unknown;
|
package/value/convert.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Convert = exports.
|
|
30
|
+
exports.Convert = exports.ValueConvertDereferenceError = exports.ValueConvertUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const ValueClone = require("./clone");
|
|
33
33
|
const ValueCheck = require("./check");
|
|
@@ -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
|
|
47
|
+
super(`ValueConvert: Unable to dereference type with $id '${schema.$ref}'`);
|
|
48
48
|
this.schema = schema;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -351,8 +351,7 @@ function Visit(schema, references, value) {
|
|
|
351
351
|
return TUserDefined(schema_, references_, value);
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
|
-
|
|
355
|
-
/** Converts any type mismatched values to their target type if a conversion is possible. */
|
|
354
|
+
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
356
355
|
function Convert(...args) {
|
|
357
356
|
return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]);
|
|
358
357
|
}
|
package/value/create.d.ts
CHANGED
|
@@ -28,9 +28,7 @@ export declare class ValueCreateRecursiveInstantiationError extends Error {
|
|
|
28
28
|
readonly recursiveMaxDepth: number;
|
|
29
29
|
constructor(schema: Types.TSchema, recursiveMaxDepth: number);
|
|
30
30
|
}
|
|
31
|
-
/** Creates a value from the given schema
|
|
32
|
-
export declare function Visit(schema: Types.TSchema, references: Types.TSchema[]): unknown;
|
|
33
|
-
/** Creates a value from the given schema */
|
|
31
|
+
/** Creates a value from the given schema and references */
|
|
34
32
|
export declare function Create<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
|
|
35
33
|
/** Creates a value from the given schema */
|
|
36
|
-
export declare function Create<T extends Types.TSchema>(schema: T
|
|
34
|
+
export declare function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
package/value/create.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Create = exports.
|
|
30
|
+
exports.Create = exports.ValueCreateRecursiveInstantiationError = exports.ValueCreateDereferenceError = exports.ValueCreateTempateLiteralTypeError = exports.ValueCreateIntersectTypeError = exports.ValueCreateNotTypeError = exports.ValueCreateNeverTypeError = exports.ValueCreateUnknownTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const ValueCheck = require("./check");
|
|
33
33
|
const ValueGuard = require("./guard");
|
|
@@ -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
|
|
74
|
+
super(`ValueCreate: Unable to dereference type with $id '${schema.$ref}'`);
|
|
75
75
|
this.schema = schema;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
@@ -431,7 +431,6 @@ function TKind(schema, references) {
|
|
|
431
431
|
throw new Error('ValueCreate: User defined types must specify a default value');
|
|
432
432
|
}
|
|
433
433
|
}
|
|
434
|
-
/** Creates a value from the given schema. If the schema specifies a default value, then that value is returned. */
|
|
435
434
|
function Visit(schema, references) {
|
|
436
435
|
const references_ = ValueGuard.IsString(schema.$id) ? [...references, schema] : references;
|
|
437
436
|
const schema_ = schema;
|
|
@@ -502,7 +501,6 @@ function Visit(schema, references) {
|
|
|
502
501
|
return TKind(schema_, references_);
|
|
503
502
|
}
|
|
504
503
|
}
|
|
505
|
-
exports.Visit = Visit;
|
|
506
504
|
// --------------------------------------------------------------------------
|
|
507
505
|
// State
|
|
508
506
|
// --------------------------------------------------------------------------
|
package/value/mutate.d.ts
CHANGED
|
@@ -7,5 +7,5 @@ export declare class ValueMutateInvalidRootMutationError extends Error {
|
|
|
7
7
|
export type Mutable = {
|
|
8
8
|
[key: string]: unknown;
|
|
9
9
|
} | unknown[];
|
|
10
|
-
/** Performs a deep mutable value assignment while retaining internal references
|
|
10
|
+
/** Performs a deep mutable value assignment while retaining internal references */
|
|
11
11
|
export declare function Mutate(current: Mutable, next: Mutable): void;
|
package/value/mutate.js
CHANGED
|
@@ -118,7 +118,7 @@ function IsMismatchedValue(current, next) {
|
|
|
118
118
|
// --------------------------------------------------------------------------
|
|
119
119
|
// Mutate
|
|
120
120
|
// --------------------------------------------------------------------------
|
|
121
|
-
/** Performs a deep mutable value assignment while retaining internal references
|
|
121
|
+
/** Performs a deep mutable value assignment while retaining internal references */
|
|
122
122
|
function Mutate(current, next) {
|
|
123
123
|
if (IsNonMutableValue(current) || IsNonMutableValue(next))
|
|
124
124
|
throw new ValueMutateInvalidRootMutationError();
|
package/value/value.d.ts
CHANGED
|
@@ -8,17 +8,17 @@ export declare namespace Value {
|
|
|
8
8
|
function Cast<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): Types.Static<T>;
|
|
9
9
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
10
10
|
function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
|
11
|
-
/** Creates a value from the given type */
|
|
11
|
+
/** Creates a value from the given type and references */
|
|
12
12
|
function Create<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
|
|
13
13
|
/** Creates a value from the given type */
|
|
14
14
|
function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
15
|
-
/** Returns true if the value matches the given type
|
|
15
|
+
/** Returns true if the value matches the given type and references */
|
|
16
16
|
function Check<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): value is Types.Static<T>;
|
|
17
|
-
/** Returns true if the value matches the given type
|
|
17
|
+
/** Returns true if the value matches the given type */
|
|
18
18
|
function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>;
|
|
19
|
-
/** Converts any type mismatched values to their target type if a conversion is possible
|
|
19
|
+
/** Converts any type mismatched values to their target type if a reasonable conversion is possible */
|
|
20
20
|
function Convert<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): unknown;
|
|
21
|
-
/** Converts any type mismatched values to their target type if a conversion is
|
|
21
|
+
/** Converts any type mismatched values to their target type if a reasonable conversion is possibl. */
|
|
22
22
|
function Convert<T extends Types.TSchema>(schema: T, value: unknown): unknown;
|
|
23
23
|
/** Returns a structural clone of the given value */
|
|
24
24
|
function Clone<T>(value: T): T;
|
package/value/value.js
CHANGED
|
@@ -51,12 +51,12 @@ var Value;
|
|
|
51
51
|
return ValueCreate.Create.apply(ValueCreate, args);
|
|
52
52
|
}
|
|
53
53
|
Value.Create = Create;
|
|
54
|
-
/** Returns true if the value matches the given type
|
|
54
|
+
/** Returns true if the value matches the given type */
|
|
55
55
|
function Check(...args) {
|
|
56
56
|
return ValueCheck.Check.apply(ValueCheck, args);
|
|
57
57
|
}
|
|
58
58
|
Value.Check = Check;
|
|
59
|
-
/** Converts any type mismatched values to their target type if a conversion is possible
|
|
59
|
+
/** Converts any type mismatched values to their target type if a reasonable conversion is possible */
|
|
60
60
|
function Convert(...args) {
|
|
61
61
|
return ValueConvert.Convert.apply(ValueConvert, args);
|
|
62
62
|
}
|