@sinclair/typebox 0.34.33 → 0.34.35

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.
@@ -15,6 +15,7 @@ import { TRef } from '../ref/index';
15
15
  import { TTuple } from '../tuple/index';
16
16
  import { TUnion } from '../union/index';
17
17
  import { Static } from '../static/index';
18
+ import { TRecursive } from '../recursive/index';
18
19
  type TInferArray<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<Array<TInfer<ModuleProperties, Type>>>);
19
20
  type TInferAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<AsyncIterableIterator<TInfer<ModuleProperties, Type>>>);
20
21
  type TInferConstructor<ModuleProperties extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema> = Ensure<new (...args: TInferTuple<ModuleProperties, Parameters>) => TInfer<ModuleProperties, InstanceType>>;
@@ -42,7 +43,7 @@ type TInferRecord<ModuleProperties extends TProperties, Key extends TSchema, Typ
42
43
  }>;
43
44
  type TInferRef<ModuleProperties extends TProperties, Ref extends string> = (Ref extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Ref]> : unknown);
44
45
  type TInferUnion<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = never> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferUnion<ModuleProperties, R, Result | TInfer<ModuleProperties, L>> : Result);
45
- type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
46
+ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Type extends TRecursive<infer Schema extends TSchema> ? TInfer<ModuleProperties, Schema> : Static<Type>);
46
47
  /** Inference Path for Imports. This type is used to compute TImport `static` */
47
48
  export type TInferFromModuleKey<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Key]> : never);
48
49
  export {};
@@ -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');
@@ -15,6 +15,7 @@ import { TRef } from '../ref/index.mjs';
15
15
  import { TTuple } from '../tuple/index.mjs';
16
16
  import { TUnion } from '../union/index.mjs';
17
17
  import { Static } from '../static/index.mjs';
18
+ import { TRecursive } from '../recursive/index.mjs';
18
19
  type TInferArray<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<Array<TInfer<ModuleProperties, Type>>>);
19
20
  type TInferAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (Ensure<AsyncIterableIterator<TInfer<ModuleProperties, Type>>>);
20
21
  type TInferConstructor<ModuleProperties extends TProperties, Parameters extends TSchema[], InstanceType extends TSchema> = Ensure<new (...args: TInferTuple<ModuleProperties, Parameters>) => TInfer<ModuleProperties, InstanceType>>;
@@ -42,7 +43,7 @@ type TInferRecord<ModuleProperties extends TProperties, Key extends TSchema, Typ
42
43
  }>;
43
44
  type TInferRef<ModuleProperties extends TProperties, Ref extends string> = (Ref extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Ref]> : unknown);
44
45
  type TInferUnion<ModuleProperties extends TProperties, Types extends TSchema[], Result extends unknown = never> = (Types extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TInferUnion<ModuleProperties, R, Result | TInfer<ModuleProperties, L>> : Result);
45
- type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Static<Type>);
46
+ type TInfer<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TInferArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TInferAsyncIterator<ModuleProperties, Type> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TInferConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TInferFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TInferIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TInferIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TInferObject<ModuleProperties, Properties> : Type extends TRecord<infer Key extends TSchema, infer Type extends TSchema> ? TInferRecord<ModuleProperties, Key, Type> : Type extends TRef<infer Ref extends string> ? TInferRef<ModuleProperties, Ref> : Type extends TTuple<infer Types extends TSchema[]> ? TInferTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Static<Type> : Type extends TUnion<infer Types extends TSchema[]> ? TInferUnion<ModuleProperties, Types> : Type extends TRecursive<infer Schema extends TSchema> ? TInfer<ModuleProperties, Schema> : Static<Type>);
46
47
  /** Inference Path for Imports. This type is used to compute TImport `static` */
47
48
  export type TInferFromModuleKey<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TInfer<ModuleProperties, ModuleProperties[Key]> : never);
48
49
  export {};
@@ -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');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.34.33",
3
+ "version": "0.34.35",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",