@sinclair/typebox 0.32.34 → 0.32.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.
- package/build/cjs/errors/function.js +1 -1
- package/build/cjs/type/patterns/patterns.d.ts +2 -0
- package/build/cjs/type/patterns/patterns.js +3 -1
- package/build/cjs/type/record/record.d.ts +4 -1
- package/build/cjs/type/record/record.js +11 -1
- package/build/cjs/value/convert/convert.js +3 -0
- package/build/esm/errors/function.mjs +1 -1
- package/build/esm/type/patterns/patterns.d.mts +2 -0
- package/build/esm/type/patterns/patterns.mjs +2 -0
- package/build/esm/type/record/record.d.mts +4 -1
- package/build/esm/type/record/record.mjs +13 -3
- package/build/esm/value/convert/convert.mjs +3 -0
- package/package.json +1 -1
|
@@ -100,7 +100,7 @@ function DefaultErrorFunction(error) {
|
|
|
100
100
|
case errors_1.ValueErrorType.ObjectMinProperties:
|
|
101
101
|
return `Expected object to have at least ${error.schema.minProperties} properties`;
|
|
102
102
|
case errors_1.ValueErrorType.ObjectRequiredProperty:
|
|
103
|
-
return '
|
|
103
|
+
return 'Expected required property';
|
|
104
104
|
case errors_1.ValueErrorType.Promise:
|
|
105
105
|
return 'Expected Promise';
|
|
106
106
|
case errors_1.ValueErrorType.RegExp:
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare const PatternBoolean = "(true|false)";
|
|
2
2
|
export declare const PatternNumber = "(0|[1-9][0-9]*)";
|
|
3
3
|
export declare const PatternString = "(.*)";
|
|
4
|
+
export declare const PatternNever = "(?!.*)";
|
|
4
5
|
export declare const PatternBooleanExact = "^(true|false)$";
|
|
5
6
|
export declare const PatternNumberExact = "^(0|[1-9][0-9]*)$";
|
|
6
7
|
export declare const PatternStringExact = "^(.*)$";
|
|
8
|
+
export declare const PatternNeverExact = "^(?!.*)$";
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = void 0;
|
|
4
|
+
exports.PatternNeverExact = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternNever = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = void 0;
|
|
5
5
|
exports.PatternBoolean = '(true|false)';
|
|
6
6
|
exports.PatternNumber = '(0|[1-9][0-9]*)';
|
|
7
7
|
exports.PatternString = '(.*)';
|
|
8
|
+
exports.PatternNever = '(?!.*)';
|
|
8
9
|
exports.PatternBooleanExact = `^${exports.PatternBoolean}$`;
|
|
9
10
|
exports.PatternNumberExact = `^${exports.PatternNumber}$`;
|
|
10
11
|
exports.PatternStringExact = `^${exports.PatternString}$`;
|
|
12
|
+
exports.PatternNeverExact = `^${exports.PatternNever}$`;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { TSchema } from '../schema/index';
|
|
2
2
|
import type { Static } from '../static/index';
|
|
3
3
|
import type { Evaluate, Ensure, Assert } from '../helpers/index';
|
|
4
|
+
import { type TAny } from '../any/index';
|
|
4
5
|
import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index';
|
|
5
6
|
import { type TLiteral, type TLiteralValue } from '../literal/index';
|
|
6
7
|
import { type TNever } from '../never/index';
|
|
@@ -33,6 +34,8 @@ type TFromLiteralKey<K extends TLiteralValue, T extends TSchema> = (Ensure<TObje
|
|
|
33
34
|
}>>);
|
|
34
35
|
type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TRegExp, T>>);
|
|
35
36
|
type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
|
|
37
|
+
type TFromAnyKey<_ extends TAny, T extends TSchema> = (Ensure<TRecord<TAny, T>>);
|
|
38
|
+
type TFromNeverKey<_ extends TNever, T extends TSchema> = (Ensure<TRecord<TNever, T>>);
|
|
36
39
|
type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
37
40
|
type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
38
41
|
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<{
|
|
@@ -47,7 +50,7 @@ export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchem
|
|
|
47
50
|
};
|
|
48
51
|
additionalProperties: TAdditionalProperties;
|
|
49
52
|
}
|
|
50
|
-
export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TRegExp ? TFromRegExpKey<K, T> : K extends TString ? TFromStringKey<K, T> : TNever;
|
|
53
|
+
export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TRegExp ? TFromRegExpKey<K, T> : K extends TString ? TFromStringKey<K, T> : K extends TAny ? TFromAnyKey<K, T> : K extends TNever ? TFromNeverKey<K, T> : TNever;
|
|
51
54
|
/** `[Json]` Creates a Record type */
|
|
52
55
|
export declare function Record<K extends TSchema, T extends TSchema>(K: K, T: T, options?: ObjectOptions): TRecordOrObject<K, T>;
|
|
53
56
|
export {};
|
|
@@ -64,6 +64,14 @@ function FromStringKey(K, T, options) {
|
|
|
64
64
|
return RecordCreateFromPattern(pattern, T, options);
|
|
65
65
|
}
|
|
66
66
|
// prettier-ignore
|
|
67
|
+
function FromAnyKey(K, T, options) {
|
|
68
|
+
return RecordCreateFromPattern(index_5.PatternStringExact, T, options);
|
|
69
|
+
}
|
|
70
|
+
// prettier-ignore
|
|
71
|
+
function FromNeverKey(K, T, options) {
|
|
72
|
+
return RecordCreateFromPattern(index_5.PatternNeverExact, T, options);
|
|
73
|
+
}
|
|
74
|
+
// prettier-ignore
|
|
67
75
|
function FromIntegerKey(_, T, options) {
|
|
68
76
|
return RecordCreateFromPattern(index_5.PatternNumberExact, T, options);
|
|
69
77
|
}
|
|
@@ -84,5 +92,7 @@ function Record(K, T, options = {}) {
|
|
|
84
92
|
(0, kind_1.IsNumber)(K) ? FromNumberKey(K, T, options) :
|
|
85
93
|
(0, kind_1.IsRegExp)(K) ? FromRegExpKey(K, T, options) :
|
|
86
94
|
(0, kind_1.IsString)(K) ? FromStringKey(K, T, options) :
|
|
87
|
-
(0,
|
|
95
|
+
(0, kind_1.IsAny)(K) ? FromAnyKey(K, T, options) :
|
|
96
|
+
(0, kind_1.IsNever)(K) ? FromNeverKey(K, T, options) :
|
|
97
|
+
(0, index_2.Never)(options));
|
|
88
98
|
}
|
|
@@ -155,6 +155,9 @@ function FromObject(schema, references, value) {
|
|
|
155
155
|
return result;
|
|
156
156
|
}
|
|
157
157
|
function FromRecord(schema, references, value) {
|
|
158
|
+
const isConvertable = (0, index_5.IsObject)(value);
|
|
159
|
+
if (!isConvertable)
|
|
160
|
+
return value;
|
|
158
161
|
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
159
162
|
const property = schema.patternProperties[propertyKey];
|
|
160
163
|
const result = {};
|
|
@@ -94,7 +94,7 @@ export function DefaultErrorFunction(error) {
|
|
|
94
94
|
case ValueErrorType.ObjectMinProperties:
|
|
95
95
|
return `Expected object to have at least ${error.schema.minProperties} properties`;
|
|
96
96
|
case ValueErrorType.ObjectRequiredProperty:
|
|
97
|
-
return '
|
|
97
|
+
return 'Expected required property';
|
|
98
98
|
case ValueErrorType.Promise:
|
|
99
99
|
return 'Expected Promise';
|
|
100
100
|
case ValueErrorType.RegExp:
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare const PatternBoolean = "(true|false)";
|
|
2
2
|
export declare const PatternNumber = "(0|[1-9][0-9]*)";
|
|
3
3
|
export declare const PatternString = "(.*)";
|
|
4
|
+
export declare const PatternNever = "(?!.*)";
|
|
4
5
|
export declare const PatternBooleanExact = "^(true|false)$";
|
|
5
6
|
export declare const PatternNumberExact = "^(0|[1-9][0-9]*)$";
|
|
6
7
|
export declare const PatternStringExact = "^(.*)$";
|
|
8
|
+
export declare const PatternNeverExact = "^(?!.*)$";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export const PatternBoolean = '(true|false)';
|
|
2
2
|
export const PatternNumber = '(0|[1-9][0-9]*)';
|
|
3
3
|
export const PatternString = '(.*)';
|
|
4
|
+
export const PatternNever = '(?!.*)';
|
|
4
5
|
export const PatternBooleanExact = `^${PatternBoolean}$`;
|
|
5
6
|
export const PatternNumberExact = `^${PatternNumber}$`;
|
|
6
7
|
export const PatternStringExact = `^${PatternString}$`;
|
|
8
|
+
export const PatternNeverExact = `^${PatternNever}$`;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { TSchema } from '../schema/index.mjs';
|
|
2
2
|
import type { Static } from '../static/index.mjs';
|
|
3
3
|
import type { Evaluate, Ensure, Assert } from '../helpers/index.mjs';
|
|
4
|
+
import { type TAny } from '../any/index.mjs';
|
|
4
5
|
import { type TObject, type TProperties, type TAdditionalProperties, type ObjectOptions } from '../object/index.mjs';
|
|
5
6
|
import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
|
|
6
7
|
import { type TNever } from '../never/index.mjs';
|
|
@@ -33,6 +34,8 @@ type TFromLiteralKey<K extends TLiteralValue, T extends TSchema> = (Ensure<TObje
|
|
|
33
34
|
}>>);
|
|
34
35
|
type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TRegExp, T>>);
|
|
35
36
|
type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
|
|
37
|
+
type TFromAnyKey<_ extends TAny, T extends TSchema> = (Ensure<TRecord<TAny, T>>);
|
|
38
|
+
type TFromNeverKey<_ extends TNever, T extends TSchema> = (Ensure<TRecord<TNever, T>>);
|
|
36
39
|
type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
37
40
|
type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
38
41
|
type RecordStatic<K extends TSchema, T extends TSchema, P extends unknown[]> = (Evaluate<{
|
|
@@ -47,7 +50,7 @@ export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchem
|
|
|
47
50
|
};
|
|
48
51
|
additionalProperties: TAdditionalProperties;
|
|
49
52
|
}
|
|
50
|
-
export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TRegExp ? TFromRegExpKey<K, T> : K extends TString ? TFromStringKey<K, T> : TNever;
|
|
53
|
+
export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TRegExp ? TFromRegExpKey<K, T> : K extends TString ? TFromStringKey<K, T> : K extends TAny ? TFromAnyKey<K, T> : K extends TNever ? TFromNeverKey<K, T> : TNever;
|
|
51
54
|
/** `[Json]` Creates a Record type */
|
|
52
55
|
export declare function Record<K extends TSchema, T extends TSchema>(K: K, T: T, options?: ObjectOptions): TRecordOrObject<K, T>;
|
|
53
56
|
export {};
|
|
@@ -2,7 +2,7 @@ import { Object } from '../object/index.mjs';
|
|
|
2
2
|
import { Never } from '../never/index.mjs';
|
|
3
3
|
import { Union } from '../union/index.mjs';
|
|
4
4
|
import { IsTemplateLiteralFinite } from '../template-literal/index.mjs';
|
|
5
|
-
import { PatternStringExact, PatternNumberExact } from '../patterns/index.mjs';
|
|
5
|
+
import { PatternStringExact, PatternNumberExact, PatternNeverExact } from '../patterns/index.mjs';
|
|
6
6
|
import { IndexPropertyKeys } from '../indexed/index.mjs';
|
|
7
7
|
import { Kind, Hint } from '../symbols/index.mjs';
|
|
8
8
|
import { CloneType } from '../clone/type.mjs';
|
|
@@ -13,7 +13,7 @@ import { IsUndefined } from '../guard/value.mjs';
|
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
// TypeGuard
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
|
-
import { IsInteger, IsLiteral, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion } from '../guard/kind.mjs';
|
|
16
|
+
import { IsInteger, IsLiteral, IsAny, IsNever, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion } from '../guard/kind.mjs';
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
18
|
// RecordCreateFromPattern
|
|
19
19
|
// ------------------------------------------------------------------
|
|
@@ -60,6 +60,14 @@ function FromStringKey(K, T, options) {
|
|
|
60
60
|
return RecordCreateFromPattern(pattern, T, options);
|
|
61
61
|
}
|
|
62
62
|
// prettier-ignore
|
|
63
|
+
function FromAnyKey(K, T, options) {
|
|
64
|
+
return RecordCreateFromPattern(PatternStringExact, T, options);
|
|
65
|
+
}
|
|
66
|
+
// prettier-ignore
|
|
67
|
+
function FromNeverKey(K, T, options) {
|
|
68
|
+
return RecordCreateFromPattern(PatternNeverExact, T, options);
|
|
69
|
+
}
|
|
70
|
+
// prettier-ignore
|
|
63
71
|
function FromIntegerKey(_, T, options) {
|
|
64
72
|
return RecordCreateFromPattern(PatternNumberExact, T, options);
|
|
65
73
|
}
|
|
@@ -80,5 +88,7 @@ export function Record(K, T, options = {}) {
|
|
|
80
88
|
IsNumber(K) ? FromNumberKey(K, T, options) :
|
|
81
89
|
IsRegExp(K) ? FromRegExpKey(K, T, options) :
|
|
82
90
|
IsString(K) ? FromStringKey(K, T, options) :
|
|
83
|
-
|
|
91
|
+
IsAny(K) ? FromAnyKey(K, T, options) :
|
|
92
|
+
IsNever(K) ? FromNeverKey(K, T, options) :
|
|
93
|
+
Never(options));
|
|
84
94
|
}
|
|
@@ -151,6 +151,9 @@ function FromObject(schema, references, value) {
|
|
|
151
151
|
return result;
|
|
152
152
|
}
|
|
153
153
|
function FromRecord(schema, references, value) {
|
|
154
|
+
const isConvertable = IsObject(value);
|
|
155
|
+
if (!isConvertable)
|
|
156
|
+
return value;
|
|
154
157
|
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
155
158
|
const property = schema.patternProperties[propertyKey];
|
|
156
159
|
const result = {};
|