@sinclair/typebox 0.34.34 → 0.34.36

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.
@@ -109,10 +109,26 @@ function FromImport(schema, references, value) {
109
109
  const target = schema.$defs[schema.$ref];
110
110
  return Visit(target, [...references, ...definitions], value);
111
111
  }
112
+ // ------------------------------------------------------------------
113
+ // Intersect
114
+ // ------------------------------------------------------------------
115
+ function IntersectAssign(correct, value) {
116
+ // trust correct on mismatch | value on non-object
117
+ if (((0, index_1.IsObject)(correct) && !(0, index_1.IsObject)(value)) || (!(0, index_1.IsObject)(correct) && (0, index_1.IsObject)(value)))
118
+ return correct;
119
+ if (!(0, index_1.IsObject)(correct) || !(0, index_1.IsObject)(value))
120
+ return value;
121
+ return globalThis.Object.getOwnPropertyNames(correct).reduce((result, key) => {
122
+ const property = key in value ? IntersectAssign(correct[key], value[key]) : correct[key];
123
+ return { ...result, [key]: property };
124
+ }, {});
125
+ }
112
126
  function FromIntersect(schema, references, value) {
113
- const created = (0, index_4.Create)(schema, references);
114
- const mapped = (0, index_1.IsObject)(created) && (0, index_1.IsObject)(value) ? { ...created, ...value } : value;
115
- return (0, index_5.Check)(schema, references, mapped) ? mapped : (0, index_4.Create)(schema, references);
127
+ if ((0, index_5.Check)(schema, references, value))
128
+ return value;
129
+ const correct = (0, index_4.Create)(schema, references);
130
+ const assigned = IntersectAssign(correct, value);
131
+ return (0, index_5.Check)(schema, references, assigned) ? assigned : correct;
116
132
  }
117
133
  function FromNever(schema, references, value) {
118
134
  throw new ValueCastError(schema, 'Never types cannot be cast');
@@ -8,15 +8,14 @@ const index_2 = require("../check/index");
8
8
  const index_3 = require("../clone/index");
9
9
  const index_4 = require("../deref/index");
10
10
  const index_5 = require("../../type/template-literal/index");
11
- const index_6 = require("../../type/patterns/index");
12
- const index_7 = require("../../type/registry/index");
13
- const index_8 = require("../../type/symbols/index");
14
- const index_9 = require("../../type/error/index");
11
+ const index_6 = require("../../type/registry/index");
12
+ const index_7 = require("../../type/symbols/index");
13
+ const index_8 = require("../../type/error/index");
15
14
  const guard_1 = require("../guard/guard");
16
15
  // ------------------------------------------------------------------
17
16
  // Errors
18
17
  // ------------------------------------------------------------------
19
- class ValueCreateError extends index_9.TypeBoxError {
18
+ class ValueCreateError extends index_8.TypeBoxError {
20
19
  constructor(schema, message) {
21
20
  super(message);
22
21
  this.schema = schema;
@@ -239,17 +238,9 @@ function FromPromise(schema, references) {
239
238
  }
240
239
  }
241
240
  function FromRecord(schema, references) {
242
- const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
243
241
  if ((0, index_1.HasPropertyKey)(schema, 'default')) {
244
242
  return FromDefault(schema.default);
245
243
  }
246
- else if (!(keyPattern === index_6.PatternStringExact || keyPattern === index_6.PatternNumberExact)) {
247
- const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
248
- const Acc = {};
249
- for (const key of propertyKeys)
250
- Acc[key] = Visit(valueSchema, references);
251
- return Acc;
252
- }
253
244
  else {
254
245
  return {};
255
246
  }
@@ -398,7 +389,7 @@ function FromKind(schema, references) {
398
389
  function Visit(schema, references) {
399
390
  const references_ = (0, index_4.Pushref)(schema, references);
400
391
  const schema_ = schema;
401
- switch (schema_[index_8.Kind]) {
392
+ switch (schema_[index_7.Kind]) {
402
393
  case 'Any':
403
394
  return FromAny(schema_, references_);
404
395
  case 'Argument':
@@ -466,7 +457,7 @@ function Visit(schema, references) {
466
457
  case 'Void':
467
458
  return FromVoid(schema_, references_);
468
459
  default:
469
- if (!index_7.TypeRegistry.Has(schema_[index_8.Kind]))
460
+ if (!index_6.TypeRegistry.Has(schema_[index_7.Kind]))
470
461
  throw new ValueCreateError(schema_, 'Unknown type');
471
462
  return FromKind(schema_, references_);
472
463
  }
@@ -103,10 +103,26 @@ function FromImport(schema, references, value) {
103
103
  const target = schema.$defs[schema.$ref];
104
104
  return Visit(target, [...references, ...definitions], value);
105
105
  }
106
+ // ------------------------------------------------------------------
107
+ // Intersect
108
+ // ------------------------------------------------------------------
109
+ function IntersectAssign(correct, value) {
110
+ // trust correct on mismatch | value on non-object
111
+ if ((IsObject(correct) && !IsObject(value)) || (!IsObject(correct) && IsObject(value)))
112
+ return correct;
113
+ if (!IsObject(correct) || !IsObject(value))
114
+ return value;
115
+ return globalThis.Object.getOwnPropertyNames(correct).reduce((result, key) => {
116
+ const property = key in value ? IntersectAssign(correct[key], value[key]) : correct[key];
117
+ return { ...result, [key]: property };
118
+ }, {});
119
+ }
106
120
  function FromIntersect(schema, references, value) {
107
- const created = Create(schema, references);
108
- const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
109
- return Check(schema, references, mapped) ? mapped : Create(schema, references);
121
+ if (Check(schema, references, value))
122
+ return value;
123
+ const correct = Create(schema, references);
124
+ const assigned = IntersectAssign(correct, value);
125
+ return Check(schema, references, assigned) ? assigned : correct;
110
126
  }
111
127
  function FromNever(schema, references, value) {
112
128
  throw new ValueCastError(schema, 'Never types cannot be cast');
@@ -3,7 +3,6 @@ import { Check } from '../check/index.mjs';
3
3
  import { Clone } from '../clone/index.mjs';
4
4
  import { Deref, Pushref } from '../deref/index.mjs';
5
5
  import { TemplateLiteralGenerate, IsTemplateLiteralFinite } from '../../type/template-literal/index.mjs';
6
- import { PatternStringExact, PatternNumberExact } from '../../type/patterns/index.mjs';
7
6
  import { TypeRegistry } from '../../type/registry/index.mjs';
8
7
  import { Kind } from '../../type/symbols/index.mjs';
9
8
  import { TypeBoxError } from '../../type/error/index.mjs';
@@ -233,17 +232,9 @@ function FromPromise(schema, references) {
233
232
  }
234
233
  }
235
234
  function FromRecord(schema, references) {
236
- const [keyPattern, valueSchema] = Object.entries(schema.patternProperties)[0];
237
235
  if (HasPropertyKey(schema, 'default')) {
238
236
  return FromDefault(schema.default);
239
237
  }
240
- else if (!(keyPattern === PatternStringExact || keyPattern === PatternNumberExact)) {
241
- const propertyKeys = keyPattern.slice(1, keyPattern.length - 1).split('|');
242
- const Acc = {};
243
- for (const key of propertyKeys)
244
- Acc[key] = Visit(valueSchema, references);
245
- return Acc;
246
- }
247
238
  else {
248
239
  return {};
249
240
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.34",
3
+ "version": "0.34.36",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -1738,6 +1738,7 @@ The following is a list of community packages that offer general tooling, extend
1738
1738
  | [ts2typebox](https://github.com/xddq/ts2typebox) | Creating TypeBox code from Typescript types |
1739
1739
  | [typebox-cli](https://github.com/gsuess/typebox-cli) | Generate Schema with TypeBox from the CLI |
1740
1740
  | [typebox-form-parser](https://github.com/jtlapp/typebox-form-parser) | Parses form and query data based on TypeBox schemas |
1741
+ | [typebox-schema-faker](https://github.com/iam-medvedev/typebox-schema-faker) | Generate fake data from TypeBox schemas for testing, prototyping and development |
1741
1742
 
1742
1743
 
1743
1744
  <a name='benchmark'></a>