@sinclair/typebox 0.32.0-dev-24 → 0.32.0-dev-26

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.
@@ -124,8 +124,8 @@ export class ValueErrorIterator {
124
124
  // --------------------------------------------------------------------------
125
125
  // Create
126
126
  // --------------------------------------------------------------------------
127
- function Create(type, schema, path, value) {
128
- return { type, schema, path, value, message: GetErrorFunction()(schema, type) };
127
+ function Create(errorType, schema, path, value) {
128
+ return { type: errorType, schema, path, value, message: GetErrorFunction()({ errorType, path, schema, value }) };
129
129
  }
130
130
  // --------------------------------------------------------------------------
131
131
  // Types
@@ -1,9 +1,19 @@
1
1
  import { TSchema } from '../type/schema/index.mjs';
2
2
  import { ValueErrorType } from './errors.mjs';
3
3
  /** Creates an error message using en-US as the default locale */
4
- export declare function DefaultErrorFunction(schema: TSchema, errorType: ValueErrorType): string;
5
- export type ErrorFunction = (schema: TSchema, type: ValueErrorType) => string;
6
- /** Sets the error function used to generate error messages */
4
+ export declare function DefaultErrorFunction(error: ErrorFunctionParameter): string;
5
+ export type ErrorFunctionParameter = {
6
+ /** The type of validation error */
7
+ errorType: ValueErrorType;
8
+ /** The path of the error */
9
+ path: string;
10
+ /** The schema associated with the error */
11
+ schema: TSchema;
12
+ /** The value associated with the error */
13
+ value: unknown;
14
+ };
15
+ export type ErrorFunction = (parameter: ErrorFunctionParameter) => string;
16
+ /** Sets the error function used to generate error messages. */
7
17
  export declare function SetErrorFunction(callback: ErrorFunction): void;
8
18
  /** Gets the error function used to generate error messages */
9
19
  export declare function GetErrorFunction(): ErrorFunction;
@@ -1,18 +1,18 @@
1
1
  import { Kind } from '../type/symbols/index.mjs';
2
2
  import { ValueErrorType } from './errors.mjs';
3
3
  /** Creates an error message using en-US as the default locale */
4
- export function DefaultErrorFunction(schema, errorType) {
5
- switch (errorType) {
4
+ export function DefaultErrorFunction(error) {
5
+ switch (error.errorType) {
6
6
  case ValueErrorType.ArrayContains:
7
7
  return 'Expected array to contain at least one matching value';
8
8
  case ValueErrorType.ArrayMaxContains:
9
- return `Expected array to contain no more than ${schema.maxContains} matching values`;
9
+ return `Expected array to contain no more than ${error.schema.maxContains} matching values`;
10
10
  case ValueErrorType.ArrayMinContains:
11
- return `Expected array to contain at least ${schema.minContains} matching values`;
11
+ return `Expected array to contain at least ${error.schema.minContains} matching values`;
12
12
  case ValueErrorType.ArrayMaxItems:
13
- return `Expected array length to be less or equal to ${schema.maxItems}`;
13
+ return `Expected array length to be less or equal to ${error.schema.maxItems}`;
14
14
  case ValueErrorType.ArrayMinItems:
15
- return `Expected array length to be greater or equal to ${schema.minItems}`;
15
+ return `Expected array length to be greater or equal to ${error.schema.minItems}`;
16
16
  case ValueErrorType.ArrayUniqueItems:
17
17
  return 'Expected array elements to be unique';
18
18
  case ValueErrorType.Array:
@@ -20,43 +20,43 @@ export function DefaultErrorFunction(schema, errorType) {
20
20
  case ValueErrorType.AsyncIterator:
21
21
  return 'Expected AsyncIterator';
22
22
  case ValueErrorType.BigIntExclusiveMaximum:
23
- return `Expected bigint to be less than ${schema.exclusiveMaximum}`;
23
+ return `Expected bigint to be less than ${error.schema.exclusiveMaximum}`;
24
24
  case ValueErrorType.BigIntExclusiveMinimum:
25
- return `Expected bigint to be greater than ${schema.exclusiveMinimum}`;
25
+ return `Expected bigint to be greater than ${error.schema.exclusiveMinimum}`;
26
26
  case ValueErrorType.BigIntMaximum:
27
- return `Expected bigint to be less or equal to ${schema.maximum}`;
27
+ return `Expected bigint to be less or equal to ${error.schema.maximum}`;
28
28
  case ValueErrorType.BigIntMinimum:
29
- return `Expected bigint to be greater or equal to ${schema.minimum}`;
29
+ return `Expected bigint to be greater or equal to ${error.schema.minimum}`;
30
30
  case ValueErrorType.BigIntMultipleOf:
31
- return `Expected bigint to be a multiple of ${schema.multipleOf}`;
31
+ return `Expected bigint to be a multiple of ${error.schema.multipleOf}`;
32
32
  case ValueErrorType.BigInt:
33
33
  return 'Expected bigint';
34
34
  case ValueErrorType.Boolean:
35
35
  return 'Expected boolean';
36
36
  case ValueErrorType.DateExclusiveMinimumTimestamp:
37
- return `Expected Date timestamp to be greater than ${schema.exclusiveMinimumTimestamp}`;
37
+ return `Expected Date timestamp to be greater than ${error.schema.exclusiveMinimumTimestamp}`;
38
38
  case ValueErrorType.DateExclusiveMaximumTimestamp:
39
- return `Expected Date timestamp to be less than ${schema.exclusiveMaximumTimestamp}`;
39
+ return `Expected Date timestamp to be less than ${error.schema.exclusiveMaximumTimestamp}`;
40
40
  case ValueErrorType.DateMinimumTimestamp:
41
- return `Expected Date timestamp to be greater or equal to ${schema.minimumTimestamp}`;
41
+ return `Expected Date timestamp to be greater or equal to ${error.schema.minimumTimestamp}`;
42
42
  case ValueErrorType.DateMaximumTimestamp:
43
- return `Expected Date timestamp to be less or equal to ${schema.maximumTimestamp}`;
43
+ return `Expected Date timestamp to be less or equal to ${error.schema.maximumTimestamp}`;
44
44
  case ValueErrorType.DateMultipleOfTimestamp:
45
- return `Expected Date timestamp to be a multiple of ${schema.multipleOfTimestamp}`;
45
+ return `Expected Date timestamp to be a multiple of ${error.schema.multipleOfTimestamp}`;
46
46
  case ValueErrorType.Date:
47
47
  return 'Expected Date';
48
48
  case ValueErrorType.Function:
49
49
  return 'Expected function';
50
50
  case ValueErrorType.IntegerExclusiveMaximum:
51
- return `Expected integer to be less than ${schema.exclusiveMaximum}`;
51
+ return `Expected integer to be less than ${error.schema.exclusiveMaximum}`;
52
52
  case ValueErrorType.IntegerExclusiveMinimum:
53
- return `Expected integer to be greater than ${schema.exclusiveMinimum}`;
53
+ return `Expected integer to be greater than ${error.schema.exclusiveMinimum}`;
54
54
  case ValueErrorType.IntegerMaximum:
55
- return `Expected integer to be less or equal to ${schema.maximum}`;
55
+ return `Expected integer to be less or equal to ${error.schema.maximum}`;
56
56
  case ValueErrorType.IntegerMinimum:
57
- return `Expected integer to be greater or equal to ${schema.minimum}`;
57
+ return `Expected integer to be greater or equal to ${error.schema.minimum}`;
58
58
  case ValueErrorType.IntegerMultipleOf:
59
- return `Expected integer to be a multiple of ${schema.multipleOf}`;
59
+ return `Expected integer to be a multiple of ${error.schema.multipleOf}`;
60
60
  case ValueErrorType.Integer:
61
61
  return 'Expected integer';
62
62
  case ValueErrorType.IntersectUnevaluatedProperties:
@@ -66,7 +66,7 @@ export function DefaultErrorFunction(schema, errorType) {
66
66
  case ValueErrorType.Iterator:
67
67
  return 'Expected Iterator';
68
68
  case ValueErrorType.Literal:
69
- return `Expected ${typeof schema.const === 'string' ? `'${schema.const}'` : schema.const}`;
69
+ return `Expected ${typeof error.schema.const === 'string' ? `'${error.schema.const}'` : error.schema.const}`;
70
70
  case ValueErrorType.Never:
71
71
  return 'Never';
72
72
  case ValueErrorType.Not:
@@ -74,15 +74,15 @@ export function DefaultErrorFunction(schema, errorType) {
74
74
  case ValueErrorType.Null:
75
75
  return 'Expected null';
76
76
  case ValueErrorType.NumberExclusiveMaximum:
77
- return `Expected number to be less than ${schema.exclusiveMaximum}`;
77
+ return `Expected number to be less than ${error.schema.exclusiveMaximum}`;
78
78
  case ValueErrorType.NumberExclusiveMinimum:
79
- return `Expected number to be greater than ${schema.exclusiveMinimum}`;
79
+ return `Expected number to be greater than ${error.schema.exclusiveMinimum}`;
80
80
  case ValueErrorType.NumberMaximum:
81
- return `Expected number to be less or equal to ${schema.maximum}`;
81
+ return `Expected number to be less or equal to ${error.schema.maximum}`;
82
82
  case ValueErrorType.NumberMinimum:
83
- return `Expected number to be greater or equal to ${schema.minimum}`;
83
+ return `Expected number to be greater or equal to ${error.schema.minimum}`;
84
84
  case ValueErrorType.NumberMultipleOf:
85
- return `Expected number to be a multiple of ${schema.multipleOf}`;
85
+ return `Expected number to be a multiple of ${error.schema.multipleOf}`;
86
86
  case ValueErrorType.Number:
87
87
  return 'Expected number';
88
88
  case ValueErrorType.Object:
@@ -90,35 +90,35 @@ export function DefaultErrorFunction(schema, errorType) {
90
90
  case ValueErrorType.ObjectAdditionalProperties:
91
91
  return 'Unexpected property';
92
92
  case ValueErrorType.ObjectMaxProperties:
93
- return `Expected object to have no more than ${schema.maxProperties} properties`;
93
+ return `Expected object to have no more than ${error.schema.maxProperties} properties`;
94
94
  case ValueErrorType.ObjectMinProperties:
95
- return `Expected object to have at least ${schema.minProperties} properties`;
95
+ return `Expected object to have at least ${error.schema.minProperties} properties`;
96
96
  case ValueErrorType.ObjectRequiredProperty:
97
97
  return 'Required property';
98
98
  case ValueErrorType.Promise:
99
99
  return 'Expected Promise';
100
100
  case ValueErrorType.StringFormatUnknown:
101
- return `Unknown format '${schema.format}'`;
101
+ return `Unknown format '${error.schema.format}'`;
102
102
  case ValueErrorType.StringFormat:
103
- return `Expected string to match '${schema.format}' format`;
103
+ return `Expected string to match '${error.schema.format}' format`;
104
104
  case ValueErrorType.StringMaxLength:
105
- return `Expected string length less or equal to ${schema.maxLength}`;
105
+ return `Expected string length less or equal to ${error.schema.maxLength}`;
106
106
  case ValueErrorType.StringMinLength:
107
- return `Expected string length greater or equal to ${schema.minLength}`;
107
+ return `Expected string length greater or equal to ${error.schema.minLength}`;
108
108
  case ValueErrorType.StringPattern:
109
- return `Expected string to match '${schema.pattern}'`;
109
+ return `Expected string to match '${error.schema.pattern}'`;
110
110
  case ValueErrorType.String:
111
111
  return 'Expected string';
112
112
  case ValueErrorType.Symbol:
113
113
  return 'Expected symbol';
114
114
  case ValueErrorType.TupleLength:
115
- return `Expected tuple to have ${schema.maxItems || 0} elements`;
115
+ return `Expected tuple to have ${error.schema.maxItems || 0} elements`;
116
116
  case ValueErrorType.Tuple:
117
117
  return 'Expected tuple';
118
118
  case ValueErrorType.Uint8ArrayMaxByteLength:
119
- return `Expected byte length less or equal to ${schema.maxByteLength}`;
119
+ return `Expected byte length less or equal to ${error.schema.maxByteLength}`;
120
120
  case ValueErrorType.Uint8ArrayMinByteLength:
121
- return `Expected byte length greater or equal to ${schema.minByteLength}`;
121
+ return `Expected byte length greater or equal to ${error.schema.minByteLength}`;
122
122
  case ValueErrorType.Uint8Array:
123
123
  return 'Expected Uint8Array';
124
124
  case ValueErrorType.Undefined:
@@ -128,14 +128,14 @@ export function DefaultErrorFunction(schema, errorType) {
128
128
  case ValueErrorType.Void:
129
129
  return 'Expected void';
130
130
  case ValueErrorType.Kind:
131
- return `Expected kind '${schema[Kind]}'`;
131
+ return `Expected kind '${error.schema[Kind]}'`;
132
132
  default:
133
133
  return 'Unknown error type';
134
134
  }
135
135
  }
136
136
  /** Manages error message providers */
137
137
  let errorFunction = DefaultErrorFunction;
138
- /** Sets the error function used to generate error messages */
138
+ /** Sets the error function used to generate error messages. */
139
139
  export function SetErrorFunction(callback) {
140
140
  errorFunction = callback;
141
141
  }
@@ -22,7 +22,7 @@ export { Extends, type TExtends, type ExtendsFromMappedResult, type ExtendsFromM
22
22
  export { Extract, type TExtract, type TExtractFromMappedResult } from './type/extract/index.mjs';
23
23
  export { Function, type TFunction } from './type/function/index.mjs';
24
24
  export { Increment, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index.mjs';
25
- export { Index, IndexPropertyKeys, IndexFromMappedKey, type TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey } from './type/indexed/index.mjs';
25
+ export { Index, IndexPropertyKeys, IndexFromMappedKey, IndexFromMappedResult, type TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey, type TIndexFromMappedResult } from './type/indexed/index.mjs';
26
26
  export { InstanceType, type TInstanceType } from './type/instance-type/index.mjs';
27
27
  export { Integer, type TInteger, type IntegerOptions } from './type/integer/index.mjs';
28
28
  export { Intersect, IntersectEvaluated, type TIntersect, type TIntersectEvaluated, type IntersectOptions } from './type/intersect/index.mjs';
@@ -28,7 +28,7 @@ export { Extends, ExtendsCheck, ExtendsResult, ExtendsUndefinedCheck } from './t
28
28
  export { Extract } from './type/extract/index.mjs';
29
29
  export { Function } from './type/function/index.mjs';
30
30
  export { Increment } from './type/helpers/index.mjs';
31
- export { Index, IndexPropertyKeys, IndexFromMappedKey } from './type/indexed/index.mjs';
31
+ export { Index, IndexPropertyKeys, IndexFromMappedKey, IndexFromMappedResult } from './type/indexed/index.mjs';
32
32
  export { InstanceType } from './type/instance-type/index.mjs';
33
33
  export { Integer } from './type/integer/index.mjs';
34
34
  export { Intersect, IntersectEvaluated } from './type/intersect/index.mjs';
@@ -1,3 +1,4 @@
1
1
  export * from './indexed-from-mapped-key.mjs';
2
+ export * from './indexed-from-mapped-result.mjs';
2
3
  export * from './indexed-property-keys.mjs';
3
4
  export * from './indexed.mjs';
@@ -1,3 +1,4 @@
1
1
  export * from './indexed-from-mapped-key.mjs';
2
+ export * from './indexed-from-mapped-result.mjs';
2
3
  export * from './indexed-property-keys.mjs';
3
4
  export * from './indexed.mjs';
@@ -0,0 +1,12 @@
1
+ import type { TSchema, SchemaOptions } from '../schema/index.mjs';
2
+ import type { TProperties } from '../object/index.mjs';
3
+ import { type TMappedResult } from '../mapped/index.mjs';
4
+ import { type TIndexPropertyKeys } from './indexed-property-keys.mjs';
5
+ import { type TIndex } from './index.mjs';
6
+ type TFromProperties<T extends TSchema, P extends TProperties> = ({
7
+ [K2 in keyof P]: TIndex<T, TIndexPropertyKeys<P[K2]>>;
8
+ });
9
+ type TFromMappedResult<T extends TSchema, K extends TMappedResult> = (TFromProperties<T, K['properties']>);
10
+ export type TIndexFromMappedResult<T extends TSchema, R extends TMappedResult, P extends TProperties = TFromMappedResult<T, R>> = (TMappedResult<P>);
11
+ export declare function IndexFromMappedResult<T extends TSchema, R extends TMappedResult, P extends TProperties = TFromMappedResult<T, R>>(T: T, R: R, options: SchemaOptions): TMappedResult<P>;
12
+ export {};
@@ -0,0 +1,18 @@
1
+ import { MappedResult } from '../mapped/index.mjs';
2
+ import { IndexPropertyKeys } from './indexed-property-keys.mjs';
3
+ import { Index } from './index.mjs';
4
+ // prettier-ignore
5
+ function FromProperties(T, P, options) {
6
+ return globalThis.Object.getOwnPropertyNames(P).reduce((Acc, K2) => {
7
+ return { ...Acc, [K2]: Index(T, IndexPropertyKeys(P[K2]), options) };
8
+ }, {});
9
+ }
10
+ // prettier-ignore
11
+ function FromMappedResult(T, R, options) {
12
+ return FromProperties(T, R.properties, options);
13
+ }
14
+ // prettier-ignore
15
+ export function IndexFromMappedResult(T, R, options) {
16
+ const P = FromMappedResult(T, R, options);
17
+ return MappedResult(P);
18
+ }
@@ -4,7 +4,7 @@ import { type Assert } from '../helpers/index.mjs';
4
4
  import { type TNever } from '../never/index.mjs';
5
5
  import { type TRecursive } from '../recursive/index.mjs';
6
6
  import { type TIntersect } from '../intersect/index.mjs';
7
- import { type TMappedKey } from '../mapped/index.mjs';
7
+ import { TMappedResult, type TMappedKey } from '../mapped/index.mjs';
8
8
  import { type TUnion } from '../union/index.mjs';
9
9
  import { type TTuple } from '../tuple/index.mjs';
10
10
  import { type TArray } from '../array/index.mjs';
@@ -12,6 +12,7 @@ import { type TIntersectEvaluated } from '../intersect/index.mjs';
12
12
  import { type TUnionEvaluated } from '../union/index.mjs';
13
13
  import { type TIndexPropertyKeys } from './indexed-property-keys.mjs';
14
14
  import { type TIndexFromMappedKey } from './indexed-from-mapped-key.mjs';
15
+ import { type TIndexFromMappedResult } from './indexed-from-mapped-result.mjs';
15
16
  type TFromRest<T extends TSchema[], K extends PropertyKey, Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, K, [...Acc, Assert<TFromKey<L, K>, TSchema>]> : Acc);
16
17
  type TFromIntersectRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? TFromIntersectRest<R, [...Acc]> : TFromIntersectRest<R, [...Acc, L]> : Acc);
17
18
  type TFromIntersect<T extends TSchema[], K extends PropertyKey> = (TIntersectEvaluated<TFromIntersectRest<TFromRest<T, K>>>);
@@ -27,6 +28,8 @@ type FromSchema<T extends TSchema, K extends PropertyKey[]> = (TUnionEvaluated<T
27
28
  declare function FromSchema<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): FromSchema<T, K>;
28
29
  export type TIndex<T extends TSchema, K extends PropertyKey[]> = (FromSchema<T, K>);
29
30
  /** `[Json]` Returns an Indexed property type for the given keys */
31
+ export declare function Index<T extends TSchema, K extends TMappedResult>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedResult<T, K>;
32
+ /** `[Json]` Returns an Indexed property type for the given keys */
30
33
  export declare function Index<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedKey<T, K>;
31
34
  /** `[Json]` Returns an Indexed property type for the given keys */
32
35
  export declare function Index<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TIndex<T, I>;
@@ -4,11 +4,11 @@ import { UnionEvaluated } from '../union/index.mjs';
4
4
  import { CloneType } from '../clone/type.mjs';
5
5
  import { IndexPropertyKeys } from './indexed-property-keys.mjs';
6
6
  import { IndexFromMappedKey } from './indexed-from-mapped-key.mjs';
7
+ import { IndexFromMappedResult } from './indexed-from-mapped-result.mjs';
7
8
  // ------------------------------------------------------------------
8
9
  // TypeGuard
9
10
  // ------------------------------------------------------------------
10
- // prettier-ignore
11
- import { IsArray, IsIntersect, IsObject, IsMappedKey, IsNever, IsSchema, IsTuple, IsUnion } from '../guard/type.mjs';
11
+ import { IsArray, IsIntersect, IsObject, IsMappedKey, IsMappedResult, IsNever, IsSchema, IsTuple, IsUnion } from '../guard/type.mjs';
12
12
  // prettier-ignore
13
13
  function FromRest(T, K, Acc = []) {
14
14
  const [L, ...R] = T;
@@ -83,7 +83,8 @@ function FromSchema(T, K) {
83
83
  /** `[Json]` Returns an Indexed property type for the given keys */
84
84
  export function Index(T, K, options = {}) {
85
85
  // prettier-ignore
86
- return (IsMappedKey(K) ? CloneType(IndexFromMappedKey(T, K, options)) :
87
- IsSchema(K) ? CloneType(FromSchema(T, IndexPropertyKeys(K)), options) :
88
- CloneType(FromSchema(T, K), options));
86
+ return (IsMappedResult(K) ? CloneType(IndexFromMappedResult(T, K, options)) :
87
+ IsMappedKey(K) ? CloneType(IndexFromMappedKey(T, K, options)) :
88
+ IsSchema(K) ? CloneType(FromSchema(T, IndexPropertyKeys(K)), options) :
89
+ CloneType(FromSchema(T, K), options));
89
90
  }
@@ -8,7 +8,7 @@ import { type TEnum, type TEnumKey, type TEnumValue } from '../enum/index.mjs';
8
8
  import { type TExclude, type TExcludeFromMappedResult } from '../exclude/index.mjs';
9
9
  import { type TExtends, type TExtendsFromMappedKey, type TExtendsFromMappedResult } from '../extends/index.mjs';
10
10
  import { type TExtract, type TExtractFromMappedResult } from '../extract/index.mjs';
11
- import { TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey } from '../indexed/index.mjs';
11
+ import { TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey, type TIndexFromMappedResult } from '../indexed/index.mjs';
12
12
  import { type IntegerOptions, type TInteger } from '../integer/index.mjs';
13
13
  import { Intersect, type IntersectOptions } from '../intersect/index.mjs';
14
14
  import { type TCapitalize, type TUncapitalize, type TLowercase, type TUppercase } from '../intrinsic/index.mjs';
@@ -93,6 +93,8 @@ export declare class JsonTypeBuilder {
93
93
  /** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
94
94
  Extract<L extends TSchema, R extends TSchema>(type: L, union: R, options?: SchemaOptions): TExtract<L, R>;
95
95
  /** `[Json]` Returns an Indexed property type for the given keys */
96
+ Index<T extends TSchema, K extends TMappedResult>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedResult<T, K>;
97
+ /** `[Json]` Returns an Indexed property type for the given keys */
96
98
  Index<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedKey<T, K>;
97
99
  /** `[Json]` Returns an Indexed property type for the given keys */
98
100
  Index<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TIndex<T, I>;
@@ -128,8 +128,8 @@ exports.ValueErrorIterator = ValueErrorIterator;
128
128
  // --------------------------------------------------------------------------
129
129
  // Create
130
130
  // --------------------------------------------------------------------------
131
- function Create(type, schema, path, value) {
132
- return { type, schema, path, value, message: (0, function_1.GetErrorFunction)()(schema, type) };
131
+ function Create(errorType, schema, path, value) {
132
+ return { type: errorType, schema, path, value, message: (0, function_1.GetErrorFunction)()({ errorType, path, schema, value }) };
133
133
  }
134
134
  // --------------------------------------------------------------------------
135
135
  // Types
@@ -1,9 +1,19 @@
1
1
  import { TSchema } from '../type/schema/index';
2
2
  import { ValueErrorType } from './errors';
3
3
  /** Creates an error message using en-US as the default locale */
4
- export declare function DefaultErrorFunction(schema: TSchema, errorType: ValueErrorType): string;
5
- export type ErrorFunction = (schema: TSchema, type: ValueErrorType) => string;
6
- /** Sets the error function used to generate error messages */
4
+ export declare function DefaultErrorFunction(error: ErrorFunctionParameter): string;
5
+ export type ErrorFunctionParameter = {
6
+ /** The type of validation error */
7
+ errorType: ValueErrorType;
8
+ /** The path of the error */
9
+ path: string;
10
+ /** The schema associated with the error */
11
+ schema: TSchema;
12
+ /** The value associated with the error */
13
+ value: unknown;
14
+ };
15
+ export type ErrorFunction = (parameter: ErrorFunctionParameter) => string;
16
+ /** Sets the error function used to generate error messages. */
7
17
  export declare function SetErrorFunction(callback: ErrorFunction): void;
8
18
  /** Gets the error function used to generate error messages */
9
19
  export declare function GetErrorFunction(): ErrorFunction;
@@ -5,18 +5,18 @@ exports.GetErrorFunction = exports.SetErrorFunction = exports.DefaultErrorFuncti
5
5
  const index_1 = require("../type/symbols/index");
6
6
  const errors_1 = require("./errors");
7
7
  /** Creates an error message using en-US as the default locale */
8
- function DefaultErrorFunction(schema, errorType) {
9
- switch (errorType) {
8
+ function DefaultErrorFunction(error) {
9
+ switch (error.errorType) {
10
10
  case errors_1.ValueErrorType.ArrayContains:
11
11
  return 'Expected array to contain at least one matching value';
12
12
  case errors_1.ValueErrorType.ArrayMaxContains:
13
- return `Expected array to contain no more than ${schema.maxContains} matching values`;
13
+ return `Expected array to contain no more than ${error.schema.maxContains} matching values`;
14
14
  case errors_1.ValueErrorType.ArrayMinContains:
15
- return `Expected array to contain at least ${schema.minContains} matching values`;
15
+ return `Expected array to contain at least ${error.schema.minContains} matching values`;
16
16
  case errors_1.ValueErrorType.ArrayMaxItems:
17
- return `Expected array length to be less or equal to ${schema.maxItems}`;
17
+ return `Expected array length to be less or equal to ${error.schema.maxItems}`;
18
18
  case errors_1.ValueErrorType.ArrayMinItems:
19
- return `Expected array length to be greater or equal to ${schema.minItems}`;
19
+ return `Expected array length to be greater or equal to ${error.schema.minItems}`;
20
20
  case errors_1.ValueErrorType.ArrayUniqueItems:
21
21
  return 'Expected array elements to be unique';
22
22
  case errors_1.ValueErrorType.Array:
@@ -24,43 +24,43 @@ function DefaultErrorFunction(schema, errorType) {
24
24
  case errors_1.ValueErrorType.AsyncIterator:
25
25
  return 'Expected AsyncIterator';
26
26
  case errors_1.ValueErrorType.BigIntExclusiveMaximum:
27
- return `Expected bigint to be less than ${schema.exclusiveMaximum}`;
27
+ return `Expected bigint to be less than ${error.schema.exclusiveMaximum}`;
28
28
  case errors_1.ValueErrorType.BigIntExclusiveMinimum:
29
- return `Expected bigint to be greater than ${schema.exclusiveMinimum}`;
29
+ return `Expected bigint to be greater than ${error.schema.exclusiveMinimum}`;
30
30
  case errors_1.ValueErrorType.BigIntMaximum:
31
- return `Expected bigint to be less or equal to ${schema.maximum}`;
31
+ return `Expected bigint to be less or equal to ${error.schema.maximum}`;
32
32
  case errors_1.ValueErrorType.BigIntMinimum:
33
- return `Expected bigint to be greater or equal to ${schema.minimum}`;
33
+ return `Expected bigint to be greater or equal to ${error.schema.minimum}`;
34
34
  case errors_1.ValueErrorType.BigIntMultipleOf:
35
- return `Expected bigint to be a multiple of ${schema.multipleOf}`;
35
+ return `Expected bigint to be a multiple of ${error.schema.multipleOf}`;
36
36
  case errors_1.ValueErrorType.BigInt:
37
37
  return 'Expected bigint';
38
38
  case errors_1.ValueErrorType.Boolean:
39
39
  return 'Expected boolean';
40
40
  case errors_1.ValueErrorType.DateExclusiveMinimumTimestamp:
41
- return `Expected Date timestamp to be greater than ${schema.exclusiveMinimumTimestamp}`;
41
+ return `Expected Date timestamp to be greater than ${error.schema.exclusiveMinimumTimestamp}`;
42
42
  case errors_1.ValueErrorType.DateExclusiveMaximumTimestamp:
43
- return `Expected Date timestamp to be less than ${schema.exclusiveMaximumTimestamp}`;
43
+ return `Expected Date timestamp to be less than ${error.schema.exclusiveMaximumTimestamp}`;
44
44
  case errors_1.ValueErrorType.DateMinimumTimestamp:
45
- return `Expected Date timestamp to be greater or equal to ${schema.minimumTimestamp}`;
45
+ return `Expected Date timestamp to be greater or equal to ${error.schema.minimumTimestamp}`;
46
46
  case errors_1.ValueErrorType.DateMaximumTimestamp:
47
- return `Expected Date timestamp to be less or equal to ${schema.maximumTimestamp}`;
47
+ return `Expected Date timestamp to be less or equal to ${error.schema.maximumTimestamp}`;
48
48
  case errors_1.ValueErrorType.DateMultipleOfTimestamp:
49
- return `Expected Date timestamp to be a multiple of ${schema.multipleOfTimestamp}`;
49
+ return `Expected Date timestamp to be a multiple of ${error.schema.multipleOfTimestamp}`;
50
50
  case errors_1.ValueErrorType.Date:
51
51
  return 'Expected Date';
52
52
  case errors_1.ValueErrorType.Function:
53
53
  return 'Expected function';
54
54
  case errors_1.ValueErrorType.IntegerExclusiveMaximum:
55
- return `Expected integer to be less than ${schema.exclusiveMaximum}`;
55
+ return `Expected integer to be less than ${error.schema.exclusiveMaximum}`;
56
56
  case errors_1.ValueErrorType.IntegerExclusiveMinimum:
57
- return `Expected integer to be greater than ${schema.exclusiveMinimum}`;
57
+ return `Expected integer to be greater than ${error.schema.exclusiveMinimum}`;
58
58
  case errors_1.ValueErrorType.IntegerMaximum:
59
- return `Expected integer to be less or equal to ${schema.maximum}`;
59
+ return `Expected integer to be less or equal to ${error.schema.maximum}`;
60
60
  case errors_1.ValueErrorType.IntegerMinimum:
61
- return `Expected integer to be greater or equal to ${schema.minimum}`;
61
+ return `Expected integer to be greater or equal to ${error.schema.minimum}`;
62
62
  case errors_1.ValueErrorType.IntegerMultipleOf:
63
- return `Expected integer to be a multiple of ${schema.multipleOf}`;
63
+ return `Expected integer to be a multiple of ${error.schema.multipleOf}`;
64
64
  case errors_1.ValueErrorType.Integer:
65
65
  return 'Expected integer';
66
66
  case errors_1.ValueErrorType.IntersectUnevaluatedProperties:
@@ -70,7 +70,7 @@ function DefaultErrorFunction(schema, errorType) {
70
70
  case errors_1.ValueErrorType.Iterator:
71
71
  return 'Expected Iterator';
72
72
  case errors_1.ValueErrorType.Literal:
73
- return `Expected ${typeof schema.const === 'string' ? `'${schema.const}'` : schema.const}`;
73
+ return `Expected ${typeof error.schema.const === 'string' ? `'${error.schema.const}'` : error.schema.const}`;
74
74
  case errors_1.ValueErrorType.Never:
75
75
  return 'Never';
76
76
  case errors_1.ValueErrorType.Not:
@@ -78,15 +78,15 @@ function DefaultErrorFunction(schema, errorType) {
78
78
  case errors_1.ValueErrorType.Null:
79
79
  return 'Expected null';
80
80
  case errors_1.ValueErrorType.NumberExclusiveMaximum:
81
- return `Expected number to be less than ${schema.exclusiveMaximum}`;
81
+ return `Expected number to be less than ${error.schema.exclusiveMaximum}`;
82
82
  case errors_1.ValueErrorType.NumberExclusiveMinimum:
83
- return `Expected number to be greater than ${schema.exclusiveMinimum}`;
83
+ return `Expected number to be greater than ${error.schema.exclusiveMinimum}`;
84
84
  case errors_1.ValueErrorType.NumberMaximum:
85
- return `Expected number to be less or equal to ${schema.maximum}`;
85
+ return `Expected number to be less or equal to ${error.schema.maximum}`;
86
86
  case errors_1.ValueErrorType.NumberMinimum:
87
- return `Expected number to be greater or equal to ${schema.minimum}`;
87
+ return `Expected number to be greater or equal to ${error.schema.minimum}`;
88
88
  case errors_1.ValueErrorType.NumberMultipleOf:
89
- return `Expected number to be a multiple of ${schema.multipleOf}`;
89
+ return `Expected number to be a multiple of ${error.schema.multipleOf}`;
90
90
  case errors_1.ValueErrorType.Number:
91
91
  return 'Expected number';
92
92
  case errors_1.ValueErrorType.Object:
@@ -94,35 +94,35 @@ function DefaultErrorFunction(schema, errorType) {
94
94
  case errors_1.ValueErrorType.ObjectAdditionalProperties:
95
95
  return 'Unexpected property';
96
96
  case errors_1.ValueErrorType.ObjectMaxProperties:
97
- return `Expected object to have no more than ${schema.maxProperties} properties`;
97
+ return `Expected object to have no more than ${error.schema.maxProperties} properties`;
98
98
  case errors_1.ValueErrorType.ObjectMinProperties:
99
- return `Expected object to have at least ${schema.minProperties} properties`;
99
+ return `Expected object to have at least ${error.schema.minProperties} properties`;
100
100
  case errors_1.ValueErrorType.ObjectRequiredProperty:
101
101
  return 'Required property';
102
102
  case errors_1.ValueErrorType.Promise:
103
103
  return 'Expected Promise';
104
104
  case errors_1.ValueErrorType.StringFormatUnknown:
105
- return `Unknown format '${schema.format}'`;
105
+ return `Unknown format '${error.schema.format}'`;
106
106
  case errors_1.ValueErrorType.StringFormat:
107
- return `Expected string to match '${schema.format}' format`;
107
+ return `Expected string to match '${error.schema.format}' format`;
108
108
  case errors_1.ValueErrorType.StringMaxLength:
109
- return `Expected string length less or equal to ${schema.maxLength}`;
109
+ return `Expected string length less or equal to ${error.schema.maxLength}`;
110
110
  case errors_1.ValueErrorType.StringMinLength:
111
- return `Expected string length greater or equal to ${schema.minLength}`;
111
+ return `Expected string length greater or equal to ${error.schema.minLength}`;
112
112
  case errors_1.ValueErrorType.StringPattern:
113
- return `Expected string to match '${schema.pattern}'`;
113
+ return `Expected string to match '${error.schema.pattern}'`;
114
114
  case errors_1.ValueErrorType.String:
115
115
  return 'Expected string';
116
116
  case errors_1.ValueErrorType.Symbol:
117
117
  return 'Expected symbol';
118
118
  case errors_1.ValueErrorType.TupleLength:
119
- return `Expected tuple to have ${schema.maxItems || 0} elements`;
119
+ return `Expected tuple to have ${error.schema.maxItems || 0} elements`;
120
120
  case errors_1.ValueErrorType.Tuple:
121
121
  return 'Expected tuple';
122
122
  case errors_1.ValueErrorType.Uint8ArrayMaxByteLength:
123
- return `Expected byte length less or equal to ${schema.maxByteLength}`;
123
+ return `Expected byte length less or equal to ${error.schema.maxByteLength}`;
124
124
  case errors_1.ValueErrorType.Uint8ArrayMinByteLength:
125
- return `Expected byte length greater or equal to ${schema.minByteLength}`;
125
+ return `Expected byte length greater or equal to ${error.schema.minByteLength}`;
126
126
  case errors_1.ValueErrorType.Uint8Array:
127
127
  return 'Expected Uint8Array';
128
128
  case errors_1.ValueErrorType.Undefined:
@@ -132,7 +132,7 @@ function DefaultErrorFunction(schema, errorType) {
132
132
  case errors_1.ValueErrorType.Void:
133
133
  return 'Expected void';
134
134
  case errors_1.ValueErrorType.Kind:
135
- return `Expected kind '${schema[index_1.Kind]}'`;
135
+ return `Expected kind '${error.schema[index_1.Kind]}'`;
136
136
  default:
137
137
  return 'Unknown error type';
138
138
  }
@@ -140,7 +140,7 @@ function DefaultErrorFunction(schema, errorType) {
140
140
  exports.DefaultErrorFunction = DefaultErrorFunction;
141
141
  /** Manages error message providers */
142
142
  let errorFunction = DefaultErrorFunction;
143
- /** Sets the error function used to generate error messages */
143
+ /** Sets the error function used to generate error messages. */
144
144
  function SetErrorFunction(callback) {
145
145
  errorFunction = callback;
146
146
  }
@@ -22,7 +22,7 @@ export { Extends, type TExtends, type ExtendsFromMappedResult, type ExtendsFromM
22
22
  export { Extract, type TExtract, type TExtractFromMappedResult } from './type/extract/index';
23
23
  export { Function, type TFunction } from './type/function/index';
24
24
  export { Increment, type Assert, type AssertType, type AssertRest, type AssertProperties, type Ensure, type Evaluate, type TupleToIntersect, type TupleToUnion, type UnionToTuple } from './type/helpers/index';
25
- export { Index, IndexPropertyKeys, IndexFromMappedKey, type TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey } from './type/indexed/index';
25
+ export { Index, IndexPropertyKeys, IndexFromMappedKey, IndexFromMappedResult, type TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey, type TIndexFromMappedResult } from './type/indexed/index';
26
26
  export { InstanceType, type TInstanceType } from './type/instance-type/index';
27
27
  export { Integer, type TInteger, type IntegerOptions } from './type/integer/index';
28
28
  export { Intersect, IntersectEvaluated, type TIntersect, type TIntersectEvaluated, type IntersectOptions } from './type/intersect/index';
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Capitalize = exports.IntrinsicFromMappedKey = exports.Intrinsic = exports.Iterator = exports.IntersectEvaluated = exports.Intersect = exports.Integer = exports.InstanceType = exports.IndexFromMappedKey = exports.IndexPropertyKeys = exports.Index = exports.Increment = exports.Function = exports.Extract = exports.ExtendsUndefinedCheck = exports.ExtendsResult = exports.ExtendsCheck = exports.Extends = exports.Exclude = exports.Enum = exports.Deref = exports.Date = exports.ConstructorParameters = exports.Constructor = exports.Const = exports.Composite = exports.Boolean = exports.BigInt = exports.Awaited = exports.AsyncIterator = exports.Array = exports.Any = exports.TypeBoxError = exports.CloneRest = exports.CloneType = exports.ValueGuard = exports.TypeGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternString = exports.PatternNumberExact = exports.PatternNumber = exports.PatternBooleanExact = exports.PatternBoolean = exports.TransformKind = exports.OptionalKind = exports.ReadonlyKind = exports.Hint = exports.Kind = void 0;
5
- exports.Union = exports.Undefined = exports.Uint8Array = exports.Tuple = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.Transform = exports.TemplateLiteralExpressionGenerate = exports.TemplateLiteralGenerate = exports.TemplateLiteralParseExact = exports.TemplateLiteralParse = exports.IsTemplateLiteralExpressionFinite = exports.IsTemplateLiteralFinite = exports.TemplateLiteral = exports.Symbol = exports.String = exports.Strict = exports.ReturnType = exports.Rest = exports.Required = exports.RegExp = exports.Ref = exports.Recursive = exports.Record = exports.ReadonlyOptional = exports.ReadonlyFromMappedResult = exports.Readonly = exports.Promise = exports.Pick = exports.PartialFromMappedResult = exports.Partial = exports.Parameters = exports.OptionalFromMappedResult = exports.Optional = exports.Omit = exports.Object = exports.Number = exports.Null = exports.Not = exports.Never = exports.MappedResult = exports.MappedKey = exports.Mapped = exports.Literal = exports.KeyOfPattern = exports.KeyOfPropertyKeys = exports.KeyOf = exports.Uppercase = exports.Uncapitalize = exports.Lowercase = void 0;
6
- exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.Type = exports.Void = exports.Unsafe = exports.Unknown = exports.UnionEvaluated = void 0;
4
+ exports.IntrinsicFromMappedKey = exports.Intrinsic = exports.Iterator = exports.IntersectEvaluated = exports.Intersect = exports.Integer = exports.InstanceType = exports.IndexFromMappedResult = exports.IndexFromMappedKey = exports.IndexPropertyKeys = exports.Index = exports.Increment = exports.Function = exports.Extract = exports.ExtendsUndefinedCheck = exports.ExtendsResult = exports.ExtendsCheck = exports.Extends = exports.Exclude = exports.Enum = exports.Deref = exports.Date = exports.ConstructorParameters = exports.Constructor = exports.Const = exports.Composite = exports.Boolean = exports.BigInt = exports.Awaited = exports.AsyncIterator = exports.Array = exports.Any = exports.TypeBoxError = exports.CloneRest = exports.CloneType = exports.ValueGuard = exports.TypeGuard = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternString = exports.PatternNumberExact = exports.PatternNumber = exports.PatternBooleanExact = exports.PatternBoolean = exports.TransformKind = exports.OptionalKind = exports.ReadonlyKind = exports.Hint = exports.Kind = void 0;
5
+ exports.Undefined = exports.Uint8Array = exports.Tuple = exports.TransformEncodeBuilder = exports.TransformDecodeBuilder = exports.Transform = exports.TemplateLiteralExpressionGenerate = exports.TemplateLiteralGenerate = exports.TemplateLiteralParseExact = exports.TemplateLiteralParse = exports.IsTemplateLiteralExpressionFinite = exports.IsTemplateLiteralFinite = exports.TemplateLiteral = exports.Symbol = exports.String = exports.Strict = exports.ReturnType = exports.Rest = exports.Required = exports.RegExp = exports.Ref = exports.Recursive = exports.Record = exports.ReadonlyOptional = exports.ReadonlyFromMappedResult = exports.Readonly = exports.Promise = exports.Pick = exports.PartialFromMappedResult = exports.Partial = exports.Parameters = exports.OptionalFromMappedResult = exports.Optional = exports.Omit = exports.Object = exports.Number = exports.Null = exports.Not = exports.Never = exports.MappedResult = exports.MappedKey = exports.Mapped = exports.Literal = exports.KeyOfPattern = exports.KeyOfPropertyKeys = exports.KeyOf = exports.Uppercase = exports.Uncapitalize = exports.Lowercase = exports.Capitalize = void 0;
6
+ exports.JavaScriptTypeBuilder = exports.JsonTypeBuilder = exports.Type = exports.Void = exports.Unsafe = exports.Unknown = exports.UnionEvaluated = exports.Union = void 0;
7
7
  // ------------------------------------------------------------------
8
8
  // Infrastructure
9
9
  // ------------------------------------------------------------------
@@ -77,6 +77,7 @@ var index_24 = require("./type/indexed/index");
77
77
  Object.defineProperty(exports, "Index", { enumerable: true, get: function () { return index_24.Index; } });
78
78
  Object.defineProperty(exports, "IndexPropertyKeys", { enumerable: true, get: function () { return index_24.IndexPropertyKeys; } });
79
79
  Object.defineProperty(exports, "IndexFromMappedKey", { enumerable: true, get: function () { return index_24.IndexFromMappedKey; } });
80
+ Object.defineProperty(exports, "IndexFromMappedResult", { enumerable: true, get: function () { return index_24.IndexFromMappedResult; } });
80
81
  var index_25 = require("./type/instance-type/index");
81
82
  Object.defineProperty(exports, "InstanceType", { enumerable: true, get: function () { return index_25.InstanceType; } });
82
83
  var index_26 = require("./type/integer/index");
@@ -1,3 +1,4 @@
1
1
  export * from './indexed-from-mapped-key';
2
+ export * from './indexed-from-mapped-result';
2
3
  export * from './indexed-property-keys';
3
4
  export * from './indexed';
@@ -16,5 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  __exportStar(require("./indexed-from-mapped-key"), exports);
19
+ __exportStar(require("./indexed-from-mapped-result"), exports);
19
20
  __exportStar(require("./indexed-property-keys"), exports);
20
21
  __exportStar(require("./indexed"), exports);
@@ -0,0 +1,12 @@
1
+ import type { TSchema, SchemaOptions } from '../schema/index';
2
+ import type { TProperties } from '../object/index';
3
+ import { type TMappedResult } from '../mapped/index';
4
+ import { type TIndexPropertyKeys } from './indexed-property-keys';
5
+ import { type TIndex } from './index';
6
+ type TFromProperties<T extends TSchema, P extends TProperties> = ({
7
+ [K2 in keyof P]: TIndex<T, TIndexPropertyKeys<P[K2]>>;
8
+ });
9
+ type TFromMappedResult<T extends TSchema, K extends TMappedResult> = (TFromProperties<T, K['properties']>);
10
+ export type TIndexFromMappedResult<T extends TSchema, R extends TMappedResult, P extends TProperties = TFromMappedResult<T, R>> = (TMappedResult<P>);
11
+ export declare function IndexFromMappedResult<T extends TSchema, R extends TMappedResult, P extends TProperties = TFromMappedResult<T, R>>(T: T, R: R, options: SchemaOptions): TMappedResult<P>;
12
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.IndexFromMappedResult = void 0;
5
+ const index_1 = require("../mapped/index");
6
+ const indexed_property_keys_1 = require("./indexed-property-keys");
7
+ const index_2 = require("./index");
8
+ // prettier-ignore
9
+ function FromProperties(T, P, options) {
10
+ return globalThis.Object.getOwnPropertyNames(P).reduce((Acc, K2) => {
11
+ return { ...Acc, [K2]: (0, index_2.Index)(T, (0, indexed_property_keys_1.IndexPropertyKeys)(P[K2]), options) };
12
+ }, {});
13
+ }
14
+ // prettier-ignore
15
+ function FromMappedResult(T, R, options) {
16
+ return FromProperties(T, R.properties, options);
17
+ }
18
+ // prettier-ignore
19
+ function IndexFromMappedResult(T, R, options) {
20
+ const P = FromMappedResult(T, R, options);
21
+ return (0, index_1.MappedResult)(P);
22
+ }
23
+ exports.IndexFromMappedResult = IndexFromMappedResult;
@@ -4,7 +4,7 @@ import { type Assert } from '../helpers/index';
4
4
  import { type TNever } from '../never/index';
5
5
  import { type TRecursive } from '../recursive/index';
6
6
  import { type TIntersect } from '../intersect/index';
7
- import { type TMappedKey } from '../mapped/index';
7
+ import { TMappedResult, type TMappedKey } from '../mapped/index';
8
8
  import { type TUnion } from '../union/index';
9
9
  import { type TTuple } from '../tuple/index';
10
10
  import { type TArray } from '../array/index';
@@ -12,6 +12,7 @@ import { type TIntersectEvaluated } from '../intersect/index';
12
12
  import { type TUnionEvaluated } from '../union/index';
13
13
  import { type TIndexPropertyKeys } from './indexed-property-keys';
14
14
  import { type TIndexFromMappedKey } from './indexed-from-mapped-key';
15
+ import { type TIndexFromMappedResult } from './indexed-from-mapped-result';
15
16
  type TFromRest<T extends TSchema[], K extends PropertyKey, Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, K, [...Acc, Assert<TFromKey<L, K>, TSchema>]> : Acc);
16
17
  type TFromIntersectRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? TFromIntersectRest<R, [...Acc]> : TFromIntersectRest<R, [...Acc, L]> : Acc);
17
18
  type TFromIntersect<T extends TSchema[], K extends PropertyKey> = (TIntersectEvaluated<TFromIntersectRest<TFromRest<T, K>>>);
@@ -27,6 +28,8 @@ type FromSchema<T extends TSchema, K extends PropertyKey[]> = (TUnionEvaluated<T
27
28
  declare function FromSchema<T extends TSchema, K extends PropertyKey[]>(T: T, K: [...K]): FromSchema<T, K>;
28
29
  export type TIndex<T extends TSchema, K extends PropertyKey[]> = (FromSchema<T, K>);
29
30
  /** `[Json]` Returns an Indexed property type for the given keys */
31
+ export declare function Index<T extends TSchema, K extends TMappedResult>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedResult<T, K>;
32
+ /** `[Json]` Returns an Indexed property type for the given keys */
30
33
  export declare function Index<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedKey<T, K>;
31
34
  /** `[Json]` Returns an Indexed property type for the given keys */
32
35
  export declare function Index<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TIndex<T, I>;
@@ -8,10 +8,10 @@ const index_3 = require("../union/index");
8
8
  const type_1 = require("../clone/type");
9
9
  const indexed_property_keys_1 = require("./indexed-property-keys");
10
10
  const indexed_from_mapped_key_1 = require("./indexed-from-mapped-key");
11
+ const indexed_from_mapped_result_1 = require("./indexed-from-mapped-result");
11
12
  // ------------------------------------------------------------------
12
13
  // TypeGuard
13
14
  // ------------------------------------------------------------------
14
- // prettier-ignore
15
15
  const type_2 = require("../guard/type");
16
16
  // prettier-ignore
17
17
  function FromRest(T, K, Acc = []) {
@@ -87,8 +87,9 @@ function FromSchema(T, K) {
87
87
  /** `[Json]` Returns an Indexed property type for the given keys */
88
88
  function Index(T, K, options = {}) {
89
89
  // prettier-ignore
90
- return ((0, type_2.IsMappedKey)(K) ? (0, type_1.CloneType)((0, indexed_from_mapped_key_1.IndexFromMappedKey)(T, K, options)) :
91
- (0, type_2.IsSchema)(K) ? (0, type_1.CloneType)(FromSchema(T, (0, indexed_property_keys_1.IndexPropertyKeys)(K)), options) :
92
- (0, type_1.CloneType)(FromSchema(T, K), options));
90
+ return ((0, type_2.IsMappedResult)(K) ? (0, type_1.CloneType)((0, indexed_from_mapped_result_1.IndexFromMappedResult)(T, K, options)) :
91
+ (0, type_2.IsMappedKey)(K) ? (0, type_1.CloneType)((0, indexed_from_mapped_key_1.IndexFromMappedKey)(T, K, options)) :
92
+ (0, type_2.IsSchema)(K) ? (0, type_1.CloneType)(FromSchema(T, (0, indexed_property_keys_1.IndexPropertyKeys)(K)), options) :
93
+ (0, type_1.CloneType)(FromSchema(T, K), options));
93
94
  }
94
95
  exports.Index = Index;
@@ -8,7 +8,7 @@ import { type TEnum, type TEnumKey, type TEnumValue } from '../enum/index';
8
8
  import { type TExclude, type TExcludeFromMappedResult } from '../exclude/index';
9
9
  import { type TExtends, type TExtendsFromMappedKey, type TExtendsFromMappedResult } from '../extends/index';
10
10
  import { type TExtract, type TExtractFromMappedResult } from '../extract/index';
11
- import { TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey } from '../indexed/index';
11
+ import { TIndex, type TIndexPropertyKeys, type TIndexFromMappedKey, type TIndexFromMappedResult } from '../indexed/index';
12
12
  import { type IntegerOptions, type TInteger } from '../integer/index';
13
13
  import { Intersect, type IntersectOptions } from '../intersect/index';
14
14
  import { type TCapitalize, type TUncapitalize, type TLowercase, type TUppercase } from '../intrinsic/index';
@@ -93,6 +93,8 @@ export declare class JsonTypeBuilder {
93
93
  /** `[Json]` Constructs a type by extracting from type all union members that are assignable to union */
94
94
  Extract<L extends TSchema, R extends TSchema>(type: L, union: R, options?: SchemaOptions): TExtract<L, R>;
95
95
  /** `[Json]` Returns an Indexed property type for the given keys */
96
+ Index<T extends TSchema, K extends TMappedResult>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedResult<T, K>;
97
+ /** `[Json]` Returns an Indexed property type for the given keys */
96
98
  Index<T extends TSchema, K extends TMappedKey>(T: T, K: K, options?: SchemaOptions): TIndexFromMappedKey<T, K>;
97
99
  /** `[Json]` Returns an Indexed property type for the given keys */
98
100
  Index<T extends TSchema, K extends TSchema, I extends PropertyKey[] = TIndexPropertyKeys<K>>(T: T, K: K, options?: SchemaOptions): TIndex<T, I>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.32.0-dev-24",
3
+ "version": "0.32.0-dev-26",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -913,13 +913,13 @@ const T = Type.Object({ // const T: TObject<{
913
913
 
914
914
  const A = Type.Index(T, ['x']) // type A = T['x']
915
915
  //
916
- // ... evaluates as
916
+ // ... evaluated as
917
917
  //
918
918
  // const A: TNumber
919
919
 
920
920
  const B = Type.Index(T, ['x', 'y']) // type B = T['x' | 'y']
921
921
  //
922
- // ... evaluates as
922
+ // ... evaluated as
923
923
  //
924
924
  // const B: TUnion<[
925
925
  // TNumber,
@@ -928,7 +928,7 @@ const B = Type.Index(T, ['x', 'y']) // type B = T['x' | 'y']
928
928
 
929
929
  const C = Type.Index(T, Type.KeyOf(T)) // type C = T[keyof T]
930
930
  //
931
- // ... evaluates as
931
+ // ... evaluated as
932
932
  //
933
933
  // const C: TUnion<[
934
934
  // TNumber,
@@ -1619,12 +1619,12 @@ The following example shows an inline error function that intercepts errors for
1619
1619
  ```typescript
1620
1620
  import { SetErrorFunction, DefaultErrorFunction, ValueErrorType } from '@sinclair/typebox/errors'
1621
1621
 
1622
- SetErrorFunction((schema, errorType) => { // i18n override
1623
- switch(errorType) {
1622
+ SetErrorFunction((error) => { // i18n override
1623
+ switch(error.errorType) {
1624
1624
  /* en-US */ case ValueErrorType.String: return 'Expected string'
1625
1625
  /* fr-FR */ case ValueErrorType.Number: return 'Nombre attendu'
1626
1626
  /* ko-KR */ case ValueErrorType.Boolean: return '예상 부울'
1627
- /* en-US */ default: return DefaultErrorFunction(schema, errorType)
1627
+ /* en-US */ default: return DefaultErrorFunction(error)
1628
1628
  }
1629
1629
  })
1630
1630
  const T = Type.Object({ // const T: TObject<{
@@ -1795,11 +1795,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
1795
1795
  ┌──────────────────────┬────────────┬────────────┬─────────────┐
1796
1796
  │ (index) │ Compiled │ Minified │ Compression │
1797
1797
  ├──────────────────────┼────────────┼────────────┼─────────────┤
1798
- │ typebox/compiler │ '119.9 kb' │ ' 52.5 kb' │ '2.29 x' │
1799
- │ typebox/errors │ ' 55.5 kb' │ ' 25.2 kb' │ '2.21 x' │
1798
+ │ typebox/compiler │ '120.7 kb' │ ' 53.0 kb' │ '2.28 x' │
1799
+ │ typebox/errors │ ' 55.8 kb' │ ' 25.5 kb' │ '2.19 x' │
1800
1800
  │ typebox/system │ ' 4.7 kb' │ ' 2.0 kb' │ '2.33 x' │
1801
- │ typebox/value │ '146.8 kb' │ ' 61.9 kb' │ '2.37 x' │
1802
- │ typebox │ ' 90.7 kb' │ ' 37.7 kb' │ '2.40 x' │
1801
+ │ typebox/value │ '147.6 kb' │ ' 62.5 kb' │ '2.36 x' │
1802
+ │ typebox │ ' 91.3 kb' │ ' 38.0 kb' │ '2.40 x' │
1803
1803
  └──────────────────────┴────────────┴────────────┴─────────────┘
1804
1804
  ```
1805
1805