@sinclair/typebox 0.25.24 → 0.26.0-dev

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.
Files changed (46) hide show
  1. package/compiler/compiler.d.ts +9 -4
  2. package/compiler/compiler.js +160 -122
  3. package/errors/errors.d.ts +56 -46
  4. package/errors/errors.js +234 -153
  5. package/package.json +1 -6
  6. package/readme.md +294 -207
  7. package/system/system.d.ts +9 -6
  8. package/system/system.js +17 -17
  9. package/typebox.d.ts +386 -162
  10. package/typebox.js +1710 -229
  11. package/value/cast.d.ts +2 -2
  12. package/value/cast.js +121 -188
  13. package/value/check.d.ts +1 -1
  14. package/value/check.js +156 -111
  15. package/value/convert.d.ts +13 -0
  16. package/value/convert.js +345 -0
  17. package/value/create.d.ts +6 -2
  18. package/value/create.js +149 -97
  19. package/{hash → value}/hash.js +39 -14
  20. package/value/index.d.ts +1 -0
  21. package/value/index.js +3 -1
  22. package/value/value.d.ts +2 -8
  23. package/value/value.js +20 -14
  24. package/conditional/conditional.d.ts +0 -17
  25. package/conditional/conditional.js +0 -91
  26. package/conditional/index.d.ts +0 -2
  27. package/conditional/index.js +0 -45
  28. package/conditional/structural.d.ts +0 -11
  29. package/conditional/structural.js +0 -685
  30. package/custom/custom.d.ts +0 -12
  31. package/custom/custom.js +0 -55
  32. package/custom/index.d.ts +0 -1
  33. package/custom/index.js +0 -44
  34. package/format/format.d.ts +0 -12
  35. package/format/format.js +0 -55
  36. package/format/index.d.ts +0 -1
  37. package/format/index.js +0 -44
  38. package/guard/extends.d.ts +0 -10
  39. package/guard/extends.js +0 -50
  40. package/guard/guard.d.ts +0 -60
  41. package/guard/guard.js +0 -440
  42. package/guard/index.d.ts +0 -2
  43. package/guard/index.js +0 -45
  44. package/hash/index.d.ts +0 -1
  45. package/hash/index.js +0 -44
  46. /package/{hash → value}/hash.d.ts +0 -0
package/value/value.js CHANGED
@@ -29,39 +29,45 @@ THE SOFTWARE.
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Value = void 0;
31
31
  const index_1 = require("../errors/index");
32
- const index_2 = require("../hash/index");
32
+ const hash_1 = require("./hash");
33
33
  const equal_1 = require("./equal");
34
34
  const cast_1 = require("./cast");
35
35
  const clone_1 = require("./clone");
36
+ const convert_1 = require("./convert");
36
37
  const create_1 = require("./create");
37
38
  const check_1 = require("./check");
38
39
  const delta_1 = require("./delta");
39
40
  /** Provides functions to perform structural updates to JavaScript values */
40
41
  var Value;
41
42
  (function (Value) {
42
- function Cast(...args) {
43
- const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
44
- return cast_1.ValueCast.Cast(schema, references, value);
43
+ /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number, boolean and date values if a reasonable conversion is possible. */
44
+ function Cast(schema, value) {
45
+ return cast_1.ValueCast.Cast(schema, value);
45
46
  }
46
47
  Value.Cast = Cast;
47
- function Create(...args) {
48
- const [schema, references] = args.length === 2 ? [args[0], args[1]] : [args[0], []];
49
- return create_1.ValueCreate.Create(schema, references);
48
+ /** Creates a value from the given type */
49
+ function Create(schema) {
50
+ return create_1.ValueCreate.Create(schema);
50
51
  }
51
52
  Value.Create = Create;
52
- function Check(...args) {
53
- const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
54
- return check_1.ValueCheck.Check(schema, references, value);
53
+ /** Returns true if the value matches the given type. */
54
+ function Check(schema, value) {
55
+ return check_1.ValueCheck.Check(schema, value);
55
56
  }
56
57
  Value.Check = Check;
58
+ /** Converts any type mismatched values to their target type if a conversion is possible. */
59
+ function Convert(schema, value) {
60
+ return convert_1.ValueConvert.Convert(schema, value);
61
+ }
62
+ Value.Convert = Convert;
57
63
  /** Returns a structural clone of the given value */
58
64
  function Clone(value) {
59
65
  return clone_1.ValueClone.Clone(value);
60
66
  }
61
67
  Value.Clone = Clone;
62
- function* Errors(...args) {
63
- const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
64
- yield* index_1.ValueErrors.Errors(schema, references, value);
68
+ /** Returns an iterator for each error in this value. */
69
+ function Errors(schema, value) {
70
+ return index_1.ValueErrors.Errors(schema, value);
65
71
  }
66
72
  Value.Errors = Errors;
67
73
  /** Returns true if left and right values are structurally equal */
@@ -76,7 +82,7 @@ var Value;
76
82
  Value.Diff = Diff;
77
83
  /** Returns a FNV1A-64 non cryptographic hash of the given value */
78
84
  function Hash(value) {
79
- return index_2.ValueHash.Create(value);
85
+ return hash_1.ValueHash.Create(value);
80
86
  }
81
87
  Value.Hash = Hash;
82
88
  /** Returns a new value with edits applied to the given value */
@@ -1,17 +0,0 @@
1
- import * as Types from '../typebox';
2
- export type TExtends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema> = Types.Static<L> extends Types.Static<R> ? T : U;
3
- export interface TExclude<T extends Types.TUnion, U extends Types.TUnion> extends Types.TUnion<any[]> {
4
- static: Exclude<Types.Static<T, this['params']>, Types.Static<U, this['params']>>;
5
- }
6
- export interface TExtract<T extends Types.TSchema, U extends Types.TUnion> extends Types.TUnion<any[]> {
7
- static: Extract<Types.Static<T, this['params']>, Types.Static<U, this['params']>>;
8
- }
9
- /** Conditional type mapping for TypeBox types */
10
- export declare namespace Conditional {
11
- /** (Experimental) Creates a conditional expression type */
12
- function Extends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema>(left: L, right: R, ok: T, fail: U): TExtends<L, R, T, U>;
13
- /** (Experimental) Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers. */
14
- function Exclude<T extends Types.TUnion, U extends Types.TUnion>(unionType: T, excludedMembers: U, options?: Types.SchemaOptions): TExclude<T, U>;
15
- /** (Experimental) Constructs a type by extracting from Type all union members that are assignable to Union. */
16
- function Extract<T extends Types.TSchema, U extends Types.TUnion>(type: T, union: U, options?: Types.SchemaOptions): TExtract<T, U>;
17
- }
@@ -1,91 +0,0 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox/conditional
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
-
10
- Permission is hereby granted, free of charge, to any person obtaining a copy
11
- of this software and associated documentation files (the "Software"), to deal
12
- in the Software without restriction, including without limitation the rights
13
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- copies of the Software, and to permit persons to whom the Software is
15
- furnished to do so, subject to the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be included in
18
- all copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
- THE SOFTWARE.
27
-
28
- ---------------------------------------------------------------------------*/
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Conditional = void 0;
31
- const Types = require("../typebox");
32
- const structural_1 = require("./structural");
33
- const index_1 = require("../guard/index");
34
- /** Conditional type mapping for TypeBox types */
35
- var Conditional;
36
- (function (Conditional) {
37
- /** (Experimental) Creates a conditional expression type */
38
- function Extends(left, right, ok, fail) {
39
- switch (structural_1.Structural.Check(left, right)) {
40
- case structural_1.StructuralResult.Union:
41
- return Types.Type.Union([Clone(ok), Clone(fail)]);
42
- case structural_1.StructuralResult.True:
43
- return Clone(ok);
44
- case structural_1.StructuralResult.False:
45
- return Clone(fail);
46
- }
47
- }
48
- Conditional.Extends = Extends;
49
- /** (Experimental) Constructs a type by excluding from UnionType all union members that are assignable to ExcludedMembers. */
50
- function Exclude(unionType, excludedMembers, options = {}) {
51
- const anyOf = unionType.anyOf
52
- .filter((schema) => {
53
- const check = structural_1.Structural.Check(schema, excludedMembers);
54
- return !(check === structural_1.StructuralResult.True || check === structural_1.StructuralResult.Union);
55
- })
56
- .map((schema) => Clone(schema));
57
- return { ...options, [Types.Kind]: 'Union', anyOf };
58
- }
59
- Conditional.Exclude = Exclude;
60
- /** (Experimental) Constructs a type by extracting from Type all union members that are assignable to Union. */
61
- function Extract(type, union, options = {}) {
62
- if (index_1.TypeGuard.TUnion(type)) {
63
- const anyOf = type.anyOf.filter((schema) => structural_1.Structural.Check(schema, union) === structural_1.StructuralResult.True).map((schema) => Clone(schema));
64
- return { ...options, [Types.Kind]: 'Union', anyOf };
65
- }
66
- else {
67
- const anyOf = union.anyOf.filter((schema) => structural_1.Structural.Check(type, schema) === structural_1.StructuralResult.True).map((schema) => Clone(schema));
68
- return { ...options, [Types.Kind]: 'Union', anyOf };
69
- }
70
- }
71
- Conditional.Extract = Extract;
72
- function Clone(value) {
73
- const isObject = (object) => typeof object === 'object' && object !== null && !Array.isArray(object);
74
- const isArray = (object) => typeof object === 'object' && object !== null && Array.isArray(object);
75
- if (isObject(value)) {
76
- return Object.keys(value).reduce((acc, key) => ({
77
- ...acc,
78
- [key]: Clone(value[key]),
79
- }), Object.getOwnPropertySymbols(value).reduce((acc, key) => ({
80
- ...acc,
81
- [key]: Clone(value[key]),
82
- }), {}));
83
- }
84
- else if (isArray(value)) {
85
- return value.map((item) => Clone(item));
86
- }
87
- else {
88
- return value;
89
- }
90
- }
91
- })(Conditional = exports.Conditional || (exports.Conditional = {}));
@@ -1,2 +0,0 @@
1
- export * from './conditional';
2
- export * from './structural';
@@ -1,45 +0,0 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox/conditional
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2017-2023 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
9
-
10
- Permission is hereby granted, free of charge, to any person obtaining a copy
11
- of this software and associated documentation files (the "Software"), to deal
12
- in the Software without restriction, including without limitation the rights
13
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
- copies of the Software, and to permit persons to whom the Software is
15
- furnished to do so, subject to the following conditions:
16
-
17
- The above copyright notice and this permission notice shall be included in
18
- all copies or substantial portions of the Software.
19
-
20
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
- THE SOFTWARE.
27
-
28
- ---------------------------------------------------------------------------*/
29
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
30
- if (k2 === undefined) k2 = k;
31
- var desc = Object.getOwnPropertyDescriptor(m, k);
32
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
33
- desc = { enumerable: true, get: function() { return m[k]; } };
34
- }
35
- Object.defineProperty(o, k2, desc);
36
- }) : (function(o, m, k, k2) {
37
- if (k2 === undefined) k2 = k;
38
- o[k2] = m[k];
39
- }));
40
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
41
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
42
- };
43
- Object.defineProperty(exports, "__esModule", { value: true });
44
- __exportStar(require("./conditional"), exports);
45
- __exportStar(require("./structural"), exports);
@@ -1,11 +0,0 @@
1
- import * as Types from '../typebox';
2
- export declare enum StructuralResult {
3
- Union = 0,
4
- True = 1,
5
- False = 2
6
- }
7
- /** Performs structural equivalence checks against TypeBox types. */
8
- export declare namespace Structural {
9
- /** Structurally tests if the left schema extends the right. */
10
- function Check(left: Types.TSchema, right: Types.TSchema): StructuralResult;
11
- }