@sinclair/typebox 0.33.2 → 0.33.4

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 (60) hide show
  1. package/build/cjs/compiler/compiler.d.ts +2 -2
  2. package/build/cjs/compiler/compiler.js +1 -1
  3. package/build/cjs/value/assert/assert.d.ts +15 -0
  4. package/build/cjs/value/assert/assert.js +55 -0
  5. package/build/cjs/value/assert/index.d.ts +1 -0
  6. package/build/cjs/value/assert/index.js +18 -0
  7. package/build/cjs/value/cast/cast.js +1 -1
  8. package/build/cjs/value/clean/clean.js +6 -6
  9. package/build/cjs/value/clone/clone.js +21 -11
  10. package/build/cjs/value/convert/convert.d.ts +2 -2
  11. package/build/cjs/value/convert/convert.js +15 -18
  12. package/build/cjs/value/create/create.js +5 -1
  13. package/build/cjs/value/default/default.js +14 -16
  14. package/build/cjs/value/delta/delta.d.ts +1 -5
  15. package/build/cjs/value/delta/delta.js +37 -33
  16. package/build/cjs/value/equal/equal.js +3 -3
  17. package/build/cjs/value/hash/hash.js +1 -1
  18. package/build/cjs/value/index.d.ts +2 -0
  19. package/build/cjs/value/index.js +2 -0
  20. package/build/cjs/value/mutate/mutate.js +4 -4
  21. package/build/cjs/value/parse/index.d.ts +1 -0
  22. package/build/cjs/value/parse/index.js +18 -0
  23. package/build/cjs/value/parse/parse.d.ts +6 -0
  24. package/build/cjs/value/parse/parse.js +29 -0
  25. package/build/cjs/value/transform/decode.js +8 -4
  26. package/build/cjs/value/transform/encode.js +9 -5
  27. package/build/cjs/value/transform/has.js +5 -1
  28. package/build/cjs/value/value/value.d.ts +10 -2
  29. package/build/cjs/value/value/value.js +38 -26
  30. package/build/esm/compiler/compiler.d.mts +2 -2
  31. package/build/esm/compiler/compiler.mjs +1 -1
  32. package/build/esm/value/assert/assert.d.mts +15 -0
  33. package/build/esm/value/assert/assert.mjs +49 -0
  34. package/build/esm/value/assert/index.d.mts +1 -0
  35. package/build/esm/value/assert/index.mjs +1 -0
  36. package/build/esm/value/cast/cast.mjs +2 -2
  37. package/build/esm/value/clean/clean.mjs +7 -7
  38. package/build/esm/value/clone/clone.mjs +22 -12
  39. package/build/esm/value/convert/convert.d.mts +2 -2
  40. package/build/esm/value/convert/convert.mjs +16 -19
  41. package/build/esm/value/create/create.mjs +5 -1
  42. package/build/esm/value/default/default.mjs +14 -16
  43. package/build/esm/value/delta/delta.d.mts +1 -5
  44. package/build/esm/value/delta/delta.mjs +36 -31
  45. package/build/esm/value/equal/equal.mjs +4 -4
  46. package/build/esm/value/hash/hash.mjs +2 -2
  47. package/build/esm/value/index.d.mts +2 -0
  48. package/build/esm/value/index.mjs +2 -0
  49. package/build/esm/value/mutate/mutate.mjs +5 -5
  50. package/build/esm/value/parse/index.d.mts +1 -0
  51. package/build/esm/value/parse/index.mjs +1 -0
  52. package/build/esm/value/parse/parse.d.mts +6 -0
  53. package/build/esm/value/parse/parse.mjs +25 -0
  54. package/build/esm/value/transform/decode.mjs +9 -5
  55. package/build/esm/value/transform/encode.mjs +10 -6
  56. package/build/esm/value/transform/has.mjs +5 -1
  57. package/build/esm/value/value/value.d.mts +10 -2
  58. package/build/esm/value/value/value.mjs +11 -1
  59. package/package.json +1 -1
  60. package/readme.md +114 -130
@@ -17,7 +17,7 @@ class ValueMutateError extends index_4.TypeBoxError {
17
17
  }
18
18
  exports.ValueMutateError = ValueMutateError;
19
19
  function ObjectType(root, path, current, next) {
20
- if (!(0, index_1.IsStandardObject)(current)) {
20
+ if (!(0, index_1.IsObject)(current)) {
21
21
  index_2.ValuePointer.Set(root, path, (0, index_3.Clone)(next));
22
22
  }
23
23
  else {
@@ -69,7 +69,7 @@ function Visit(root, path, current, next) {
69
69
  return ArrayType(root, path, current, next);
70
70
  if ((0, index_1.IsTypedArray)(next))
71
71
  return TypedArrayType(root, path, current, next);
72
- if ((0, index_1.IsStandardObject)(next))
72
+ if ((0, index_1.IsObject)(next))
73
73
  return ObjectType(root, path, current, next);
74
74
  if ((0, index_1.IsValueType)(next))
75
75
  return ValueType(root, path, current, next);
@@ -82,8 +82,8 @@ function IsNonMutableValue(value) {
82
82
  }
83
83
  function IsMismatchedValue(current, next) {
84
84
  // prettier-ignore
85
- return (((0, index_1.IsStandardObject)(current) && (0, index_1.IsArray)(next)) ||
86
- ((0, index_1.IsArray)(current) && (0, index_1.IsStandardObject)(next)));
85
+ return (((0, index_1.IsObject)(current) && (0, index_1.IsArray)(next)) ||
86
+ ((0, index_1.IsArray)(current) && (0, index_1.IsObject)(next)));
87
87
  }
88
88
  // ------------------------------------------------------------------
89
89
  // Mutate
@@ -0,0 +1 @@
1
+ export * from './parse';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ __exportStar(require("./parse"), exports);
@@ -0,0 +1,6 @@
1
+ import { TSchema } from '../../type/schema/index';
2
+ import { StaticDecode } from '../../type/static/index';
3
+ /** Parses a value or throws an `AssertError` if invalid. */
4
+ export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
5
+ /** Parses a value or throws an `AssertError` if invalid. */
6
+ export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, value: unknown): R;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Parse = Parse;
5
+ const index_1 = require("../transform/index");
6
+ const assert_1 = require("../assert/assert");
7
+ const default_1 = require("../default/default");
8
+ const convert_1 = require("../convert/convert");
9
+ const clean_1 = require("../clean/clean");
10
+ const index_2 = require("../clone/index");
11
+ // prettier-ignore
12
+ const ParseReducer = [
13
+ (_schema, _references, value) => (0, index_2.Clone)(value),
14
+ (schema, references, value) => (0, default_1.Default)(schema, references, value),
15
+ (schema, references, value) => (0, clean_1.Clean)(schema, references, value),
16
+ (schema, references, value) => (0, convert_1.Convert)(schema, references, value),
17
+ (schema, references, value) => { (0, assert_1.Assert)(schema, references, value); return value; },
18
+ (schema, references, value) => ((0, index_1.HasTransform)(schema, references) ? (0, index_1.TransformDecode)(schema, references, value) : value),
19
+ ];
20
+ // ------------------------------------------------------------------
21
+ // ParseValue
22
+ // ------------------------------------------------------------------
23
+ function ParseValue(schema, references, value) {
24
+ return ParseReducer.reduce((value, reducer) => reducer(schema, references, value), value);
25
+ }
26
+ /** Parses a value or throws an `AssertError` if invalid. */
27
+ function Parse(...args) {
28
+ return args.length === 3 ? ParseValue(args[0], args[1], args[2]) : ParseValue(args[0], [], args[1]);
29
+ }
@@ -61,7 +61,7 @@ function FromArray(schema, references, path, value) {
61
61
  }
62
62
  // prettier-ignore
63
63
  function FromIntersect(schema, references, path, value) {
64
- if (!(0, index_6.IsStandardObject)(value) || (0, index_6.IsValueType)(value))
64
+ if (!(0, index_6.IsObject)(value) || (0, index_6.IsValueType)(value))
65
65
  return Default(schema, path, value);
66
66
  const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
67
67
  const knownKeys = knownEntries.map(entry => entry[0]);
@@ -87,7 +87,7 @@ function FromNot(schema, references, path, value) {
87
87
  }
88
88
  // prettier-ignore
89
89
  function FromObject(schema, references, path, value) {
90
- if (!(0, index_6.IsStandardObject)(value))
90
+ if (!(0, index_6.IsObject)(value))
91
91
  return Default(schema, path, value);
92
92
  const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
93
93
  const knownProperties = { ...value };
@@ -109,7 +109,7 @@ function FromObject(schema, references, path, value) {
109
109
  }
110
110
  // prettier-ignore
111
111
  function FromRecord(schema, references, path, value) {
112
- if (!(0, index_6.IsStandardObject)(value))
112
+ if (!(0, index_6.IsObject)(value))
113
113
  return Default(schema, path, value);
114
114
  const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
115
115
  const knownKeys = new RegExp(pattern);
@@ -157,9 +157,13 @@ function FromUnion(schema, references, path, value) {
157
157
  }
158
158
  return Default(schema, path, value);
159
159
  }
160
+ function AddReference(references, schema) {
161
+ references.push(schema);
162
+ return references;
163
+ }
160
164
  // prettier-ignore
161
165
  function Visit(schema, references, path, value) {
162
- const references_ = typeof schema.$id === 'string' ? [...references, schema] : references;
166
+ const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
163
167
  const schema_ = schema;
164
168
  switch (schema[index_1.Kind]) {
165
169
  case 'Array':
@@ -62,7 +62,7 @@ function FromArray(schema, references, path, value) {
62
62
  // prettier-ignore
63
63
  function FromIntersect(schema, references, path, value) {
64
64
  const defaulted = Default(schema, path, value);
65
- if (!(0, index_6.IsStandardObject)(value) || (0, index_6.IsValueType)(value))
65
+ if (!(0, index_6.IsObject)(value) || (0, index_6.IsValueType)(value))
66
66
  return defaulted;
67
67
  const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
68
68
  const knownKeys = knownEntries.map(entry => entry[0]);
@@ -90,7 +90,7 @@ function FromNot(schema, references, path, value) {
90
90
  // prettier-ignore
91
91
  function FromObject(schema, references, path, value) {
92
92
  const defaulted = Default(schema, path, value);
93
- if (!(0, index_6.IsStandardObject)(defaulted))
93
+ if (!(0, index_6.IsObject)(defaulted))
94
94
  return defaulted;
95
95
  const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
96
96
  const knownProperties = { ...defaulted };
@@ -113,7 +113,7 @@ function FromObject(schema, references, path, value) {
113
113
  // prettier-ignore
114
114
  function FromRecord(schema, references, path, value) {
115
115
  const defaulted = Default(schema, path, value);
116
- if (!(0, index_6.IsStandardObject)(value))
116
+ if (!(0, index_6.IsObject)(value))
117
117
  return defaulted;
118
118
  const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
119
119
  const knownKeys = new RegExp(pattern);
@@ -123,7 +123,7 @@ function FromRecord(schema, references, path, value) {
123
123
  knownProperties[key] = Visit(schema.patternProperties[pattern], references, `${path}/${key}`, knownProperties[key]);
124
124
  }
125
125
  if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
126
- return Default(schema, path, knownProperties);
126
+ return knownProperties;
127
127
  }
128
128
  const unknownKeys = Object.getOwnPropertyNames(knownProperties);
129
129
  const additionalProperties = schema.additionalProperties;
@@ -169,9 +169,13 @@ function FromUnion(schema, references, path, value) {
169
169
  }
170
170
  return Default(schema, path, value);
171
171
  }
172
+ function AddReference(references, schema) {
173
+ references.push(schema);
174
+ return references;
175
+ }
172
176
  // prettier-ignore
173
177
  function Visit(schema, references, path, value) {
174
- const references_ = typeof schema.$id === 'string' ? [...references, schema] : references;
178
+ const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
175
179
  const schema_ = schema;
176
180
  switch (schema[index_1.Kind]) {
177
181
  case 'Array':
@@ -76,9 +76,13 @@ function FromTuple(schema, references) {
76
76
  function FromUnion(schema, references) {
77
77
  return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
78
78
  }
79
+ function AddReference(references, schema) {
80
+ references.push(schema);
81
+ return references;
82
+ }
79
83
  // prettier-ignore
80
84
  function Visit(schema, references) {
81
- const references_ = (0, index_3.IsString)(schema.$id) ? [...references, schema] : references;
85
+ const references_ = (0, index_3.IsString)(schema.$id) ? AddReference(references, schema) : references;
82
86
  const schema_ = schema;
83
87
  if (schema.$id && visited.has(schema.$id))
84
88
  return false;
@@ -3,6 +3,10 @@ import { Edit } from '../delta/index';
3
3
  import { ValueErrorIterator } from '../../errors/index';
4
4
  import type { TSchema } from '../../type/schema/index';
5
5
  import type { Static, StaticDecode, StaticEncode } from '../../type/static/index';
6
+ /** Asserts a value matches the given type or throws an `AssertError` if invalid. */
7
+ export declare function Assert<T extends TSchema, R = Static<T>>(schema: T, references: TSchema[], value: unknown): asserts value is R;
8
+ /** Asserts a value matches the given type or throws an `AssertError` if invalid. */
9
+ export declare function Assert<T extends TSchema, R = Static<T>>(schema: T, value: unknown): asserts value is R;
6
10
  /** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
7
11
  export declare function Cast<T extends TSchema>(schema: T, references: TSchema[], value: unknown): Static<T>;
8
12
  /** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
@@ -19,9 +23,9 @@ export declare function Check<T extends TSchema>(schema: T, value: unknown): val
19
23
  export declare function Clean(schema: TSchema, references: TSchema[], value: unknown): unknown;
20
24
  /** `[Mutable]` Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first. */
21
25
  export declare function Clean(schema: TSchema, value: unknown): unknown;
22
- /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
26
+ /** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
23
27
  export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
24
- /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
28
+ /** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
25
29
  export declare function Convert(schema: TSchema, value: unknown): unknown;
26
30
  /** Returns a structural clone of the given value */
27
31
  export declare function Clone<T>(value: T): T;
@@ -37,6 +41,10 @@ export declare function Default(schema: TSchema, value: unknown): unknown;
37
41
  export declare function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, references: TSchema[], value: unknown): R;
38
42
  /** Encodes a value or throws if error */
39
43
  export declare function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, value: unknown): R;
44
+ /** Parses a value or throws an `AssertError` if invalid. */
45
+ export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
46
+ /** Parses a value or throws an `AssertError` if invalid. */
47
+ export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, value: unknown): R;
40
48
  /** Returns an iterator for each error in this value. */
41
49
  export declare function Errors<T extends TSchema>(schema: T, references: TSchema[], value: unknown): ValueErrorIterator;
42
50
  /** Returns an iterator for each error in this value. */
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Assert = Assert;
4
5
  exports.Cast = Cast;
5
6
  exports.Create = Create;
6
7
  exports.Check = Check;
@@ -10,6 +11,7 @@ exports.Clone = Clone;
10
11
  exports.Decode = Decode;
11
12
  exports.Default = Default;
12
13
  exports.Encode = Encode;
14
+ exports.Parse = Parse;
13
15
  exports.Errors = Errors;
14
16
  exports.Equal = Equal;
15
17
  exports.Diff = Diff;
@@ -17,41 +19,47 @@ exports.Hash = Hash;
17
19
  exports.Patch = Patch;
18
20
  exports.Mutate = Mutate;
19
21
  const index_1 = require("../transform/index");
20
- const index_2 = require("../mutate/index");
21
- const index_3 = require("../hash/index");
22
- const index_4 = require("../equal/index");
23
- const index_5 = require("../cast/index");
24
- const index_6 = require("../clone/index");
25
- const index_7 = require("../convert/index");
26
- const index_8 = require("../create/index");
27
- const index_9 = require("../clean/index");
28
- const index_10 = require("../check/index");
29
- const index_11 = require("../default/index");
30
- const index_12 = require("../delta/index");
31
- const index_13 = require("../../errors/index");
22
+ const index_2 = require("../assert/index");
23
+ const index_3 = require("../mutate/index");
24
+ const index_4 = require("../hash/index");
25
+ const index_5 = require("../equal/index");
26
+ const index_6 = require("../cast/index");
27
+ const index_7 = require("../clone/index");
28
+ const index_8 = require("../convert/index");
29
+ const index_9 = require("../create/index");
30
+ const index_10 = require("../clean/index");
31
+ const index_11 = require("../check/index");
32
+ const index_12 = require("../parse/index");
33
+ const index_13 = require("../default/index");
34
+ const index_14 = require("../delta/index");
35
+ const index_15 = require("../../errors/index");
36
+ /** Asserts a value matches the given type or throws an `AssertError` if invalid. */
37
+ function Assert(...args) {
38
+ return index_2.Assert.apply(index_2.Assert, args);
39
+ }
32
40
  /** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
33
41
  function Cast(...args) {
34
- return index_5.Cast.apply(index_5.Cast, args);
42
+ return index_6.Cast.apply(index_6.Cast, args);
35
43
  }
36
44
  /** Creates a value from the given type */
37
45
  function Create(...args) {
38
- return index_8.Create.apply(index_8.Create, args);
46
+ return index_9.Create.apply(index_9.Create, args);
39
47
  }
40
48
  /** Returns true if the value matches the given type */
41
49
  function Check(...args) {
42
- return index_10.Check.apply(index_10.Check, args);
50
+ return index_11.Check.apply(index_11.Check, args);
43
51
  }
44
52
  /** `[Mutable]` Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first. */
45
53
  function Clean(...args) {
46
- return index_9.Clean.apply(index_9.Clean, args);
54
+ return index_10.Clean.apply(index_10.Clean, args);
47
55
  }
48
- /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
56
+ /** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
49
57
  function Convert(...args) {
50
- return index_7.Convert.apply(index_7.Convert, args);
58
+ return index_8.Convert.apply(index_8.Convert, args);
51
59
  }
52
60
  /** Returns a structural clone of the given value */
53
61
  function Clone(value) {
54
- return (0, index_6.Clone)(value);
62
+ return (0, index_7.Clone)(value);
55
63
  }
56
64
  /** Decodes a value or throws if error */
57
65
  function Decode(...args) {
@@ -62,7 +70,7 @@ function Decode(...args) {
62
70
  }
63
71
  /** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
64
72
  function Default(...args) {
65
- return index_11.Default.apply(index_11.Default, args);
73
+ return index_13.Default.apply(index_13.Default, args);
66
74
  }
67
75
  /** Encodes a value or throws if error */
68
76
  function Encode(...args) {
@@ -72,27 +80,31 @@ function Encode(...args) {
72
80
  throw new index_1.TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First());
73
81
  return encoded;
74
82
  }
83
+ /** Parses a value or throws an `AssertError` if invalid. */
84
+ function Parse(...args) {
85
+ return index_12.Parse.apply(index_12.Parse, args);
86
+ }
75
87
  /** Returns an iterator for each error in this value. */
76
88
  function Errors(...args) {
77
- return index_13.Errors.apply(index_13.Errors, args);
89
+ return index_15.Errors.apply(index_15.Errors, args);
78
90
  }
79
91
  /** Returns true if left and right values are structurally equal */
80
92
  function Equal(left, right) {
81
- return (0, index_4.Equal)(left, right);
93
+ return (0, index_5.Equal)(left, right);
82
94
  }
83
95
  /** Returns edits to transform the current value into the next value */
84
96
  function Diff(current, next) {
85
- return (0, index_12.Diff)(current, next);
97
+ return (0, index_14.Diff)(current, next);
86
98
  }
87
99
  /** Returns a FNV1A-64 non cryptographic hash of the given value */
88
100
  function Hash(value) {
89
- return (0, index_3.Hash)(value);
101
+ return (0, index_4.Hash)(value);
90
102
  }
91
103
  /** Returns a new value with edits applied to the given value */
92
104
  function Patch(current, edits) {
93
- return (0, index_12.Patch)(current, edits);
105
+ return (0, index_14.Patch)(current, edits);
94
106
  }
95
107
  /** `[Mutable]` Performs a deep mutable value assignment while retaining internal references. */
96
108
  function Mutate(current, next) {
97
- (0, index_2.Mutate)(current, next);
109
+ (0, index_3.Mutate)(current, next);
98
110
  }
@@ -17,9 +17,9 @@ export declare class TypeCheck<T extends TSchema> {
17
17
  /** Returns true if the value matches the compiled type. */
18
18
  Check(value: unknown): value is Static<T>;
19
19
  /** Decodes a value or throws if error */
20
- Decode(value: unknown): StaticDecode<T>;
20
+ Decode<R = StaticDecode<T>>(value: unknown): R;
21
21
  /** Encodes a value or throws if error */
22
- Encode(value: unknown): StaticEncode<T>;
22
+ Encode<R = StaticEncode<T>>(value: unknown): R;
23
23
  }
24
24
  export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
25
25
  readonly schema: TSchema;
@@ -44,7 +44,7 @@ export class TypeCheck {
44
44
  Decode(value) {
45
45
  if (!this.checkFunc(value))
46
46
  throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
47
- return this.hasTransform ? TransformDecode(this.schema, this.references, value) : value;
47
+ return (this.hasTransform ? TransformDecode(this.schema, this.references, value) : value);
48
48
  }
49
49
  /** Encodes a value or throws if error */
50
50
  Encode(value) {
@@ -0,0 +1,15 @@
1
+ import { ValueErrorIterator, ValueError } from '../../errors/index.mjs';
2
+ import { TypeBoxError } from '../../type/error/error.mjs';
3
+ import { TSchema } from '../../type/schema/index.mjs';
4
+ import { Static } from '../../type/static/index.mjs';
5
+ export declare class AssertError extends TypeBoxError {
6
+ #private;
7
+ error: ValueError | undefined;
8
+ constructor(iterator: ValueErrorIterator);
9
+ /** Returns an iterator for each error in this value. */
10
+ Errors(): ValueErrorIterator;
11
+ }
12
+ /** Asserts a value matches the given type or throws an `AssertError` if invalid */
13
+ export declare function Assert<T extends TSchema>(schema: T, references: TSchema[], value: unknown): asserts value is Static<T>;
14
+ /** Asserts a value matches the given type or throws an `AssertError` if invalid */
15
+ export declare function Assert<T extends TSchema>(schema: T, value: unknown): asserts value is Static<T>;
@@ -0,0 +1,49 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _AssertError_instances, _AssertError_iterator, _AssertError_Iterator;
13
+ import { Errors, ValueErrorIterator } from '../../errors/index.mjs';
14
+ import { TypeBoxError } from '../../type/error/error.mjs';
15
+ import { Check } from '../check/check.mjs';
16
+ // ------------------------------------------------------------------
17
+ // AssertError
18
+ // ------------------------------------------------------------------
19
+ export class AssertError extends TypeBoxError {
20
+ constructor(iterator) {
21
+ const error = iterator.First();
22
+ super(error === undefined ? 'Invalid Value' : error.message);
23
+ _AssertError_instances.add(this);
24
+ _AssertError_iterator.set(this, void 0);
25
+ __classPrivateFieldSet(this, _AssertError_iterator, iterator, "f");
26
+ this.error = error;
27
+ }
28
+ /** Returns an iterator for each error in this value. */
29
+ Errors() {
30
+ return new ValueErrorIterator(__classPrivateFieldGet(this, _AssertError_instances, "m", _AssertError_Iterator).call(this));
31
+ }
32
+ }
33
+ _AssertError_iterator = new WeakMap(), _AssertError_instances = new WeakSet(), _AssertError_Iterator = function* _AssertError_Iterator() {
34
+ if (this.error)
35
+ yield this.error;
36
+ yield* __classPrivateFieldGet(this, _AssertError_iterator, "f");
37
+ };
38
+ // ------------------------------------------------------------------
39
+ // AssertValue
40
+ // ------------------------------------------------------------------
41
+ function AssertValue(schema, references, value) {
42
+ if (Check(schema, references, value))
43
+ return;
44
+ throw new AssertError(Errors(schema, references, value));
45
+ }
46
+ /** Asserts a value matches the given type or throws an `AssertError` if invalid */
47
+ export function Assert(...args) {
48
+ return args.length === 3 ? AssertValue(args[0], args[1], args[2]) : AssertValue(args[0], [], args[1]);
49
+ }
@@ -0,0 +1 @@
1
+ export * from './assert.mjs';
@@ -0,0 +1 @@
1
+ export * from './assert.mjs';
@@ -1,4 +1,4 @@
1
- import { IsStandardObject, IsArray, IsString, IsNumber, IsNull } from '../guard/index.mjs';
1
+ import { IsObject, IsArray, IsString, IsNumber, IsNull } from '../guard/index.mjs';
2
2
  import { TypeBoxError } from '../../type/error/index.mjs';
3
3
  import { Kind } from '../../type/symbols/index.mjs';
4
4
  import { Create } from '../create/index.mjs';
@@ -100,7 +100,7 @@ function FromConstructor(schema, references, value) {
100
100
  }
101
101
  function FromIntersect(schema, references, value) {
102
102
  const created = Create(schema, references);
103
- const mapped = IsStandardObject(created) && IsStandardObject(value) ? { ...created, ...value } : value;
103
+ const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
104
104
  return Check(schema, references, mapped) ? mapped : Create(schema, references);
105
105
  }
106
106
  function FromNever(schema, references, value) {
@@ -7,17 +7,17 @@ import { Kind } from '../../type/symbols/index.mjs';
7
7
  // ValueGuard
8
8
  // ------------------------------------------------------------------
9
9
  // prettier-ignore
10
- import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
10
+ import { HasPropertyKey, IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
11
11
  // ------------------------------------------------------------------
12
12
  // TypeGuard
13
13
  // ------------------------------------------------------------------
14
14
  // prettier-ignore
15
- import { IsSchema } from '../../type/guard/type.mjs';
15
+ import { IsKind } from '../../type/guard/kind.mjs';
16
16
  // ------------------------------------------------------------------
17
17
  // IsCheckable
18
18
  // ------------------------------------------------------------------
19
19
  function IsCheckable(schema) {
20
- return IsSchema(schema) && schema[Kind] !== 'Unsafe';
20
+ return IsKind(schema) && schema[Kind] !== 'Unsafe';
21
21
  }
22
22
  // ------------------------------------------------------------------
23
23
  // Types
@@ -31,7 +31,7 @@ function FromIntersect(schema, references, value) {
31
31
  const unevaluatedProperties = schema.unevaluatedProperties;
32
32
  const intersections = schema.allOf.map((schema) => Visit(schema, references, Clone(value)));
33
33
  const composite = intersections.reduce((acc, value) => (IsObject(value) ? { ...acc, ...value } : value), {});
34
- if (!IsObject(value) || !IsObject(composite) || !IsSchema(unevaluatedProperties))
34
+ if (!IsObject(value) || !IsObject(composite) || !IsKind(unevaluatedProperties))
35
35
  return composite;
36
36
  const knownkeys = KeyOfPropertyKeys(schema);
37
37
  for (const key of Object.getOwnPropertyNames(value)) {
@@ -48,11 +48,11 @@ function FromObject(schema, references, value) {
48
48
  return value; // Check IsArray for AllowArrayObject configuration
49
49
  const additionalProperties = schema.additionalProperties;
50
50
  for (const key of Object.getOwnPropertyNames(value)) {
51
- if (key in schema.properties) {
51
+ if (HasPropertyKey(schema.properties, key)) {
52
52
  value[key] = Visit(schema.properties[key], references, value[key]);
53
53
  continue;
54
54
  }
55
- if (IsSchema(additionalProperties) && Check(additionalProperties, references, value[key])) {
55
+ if (IsKind(additionalProperties) && Check(additionalProperties, references, value[key])) {
56
56
  value[key] = Visit(additionalProperties, references, value[key]);
57
57
  continue;
58
58
  }
@@ -72,7 +72,7 @@ function FromRecord(schema, references, value) {
72
72
  value[key] = Visit(propertySchema, references, value[key]);
73
73
  continue;
74
74
  }
75
- if (IsSchema(additionalProperties) && Check(additionalProperties, references, value[key])) {
75
+ if (IsKind(additionalProperties) && Check(additionalProperties, references, value[key])) {
76
76
  value[key] = Visit(additionalProperties, references, value[key]);
77
77
  continue;
78
78
  }
@@ -1,11 +1,11 @@
1
1
  // ------------------------------------------------------------------
2
2
  // ValueGuard
3
3
  // ------------------------------------------------------------------
4
- import { IsArray, IsDate, IsStandardObject, IsTypedArray, IsValueType } from '../guard/index.mjs';
4
+ import { IsArray, IsDate, IsMap, IsSet, IsObject, IsTypedArray, IsValueType } from '../guard/index.mjs';
5
5
  // ------------------------------------------------------------------
6
6
  // Clonable
7
7
  // ------------------------------------------------------------------
8
- function ObjectType(value) {
8
+ function FromObject(value) {
9
9
  const Acc = {};
10
10
  for (const key of Object.getOwnPropertyNames(value)) {
11
11
  Acc[key] = Clone(value[key]);
@@ -15,16 +15,22 @@ function ObjectType(value) {
15
15
  }
16
16
  return Acc;
17
17
  }
18
- function ArrayType(value) {
18
+ function FromArray(value) {
19
19
  return value.map((element) => Clone(element));
20
20
  }
21
- function TypedArrayType(value) {
21
+ function FromTypedArray(value) {
22
22
  return value.slice();
23
23
  }
24
- function DateType(value) {
24
+ function FromMap(value) {
25
+ return new Map(Clone([...value.entries()]));
26
+ }
27
+ function FromSet(value) {
28
+ return new Set(Clone([...value.entries()]));
29
+ }
30
+ function FromDate(value) {
25
31
  return new Date(value.toISOString());
26
32
  }
27
- function ValueType(value) {
33
+ function FromValue(value) {
28
34
  return value;
29
35
  }
30
36
  // ------------------------------------------------------------------
@@ -33,14 +39,18 @@ function ValueType(value) {
33
39
  /** Returns a clone of the given value */
34
40
  export function Clone(value) {
35
41
  if (IsArray(value))
36
- return ArrayType(value);
42
+ return FromArray(value);
37
43
  if (IsDate(value))
38
- return DateType(value);
39
- if (IsStandardObject(value))
40
- return ObjectType(value);
44
+ return FromDate(value);
41
45
  if (IsTypedArray(value))
42
- return TypedArrayType(value);
46
+ return FromTypedArray(value);
47
+ if (IsMap(value))
48
+ return FromMap(value);
49
+ if (IsSet(value))
50
+ return FromSet(value);
51
+ if (IsObject(value))
52
+ return FromObject(value);
43
53
  if (IsValueType(value))
44
- return ValueType(value);
54
+ return FromValue(value);
45
55
  throw new Error('ValueClone: Unable to clone value');
46
56
  }
@@ -1,5 +1,5 @@
1
1
  import type { TSchema } from '../../type/schema/index.mjs';
2
- /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
2
+ /** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
3
3
  export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
4
- /** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
4
+ /** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
5
5
  export declare function Convert(schema: TSchema, value: unknown): unknown;