@sinclair/typebox 0.27.4 → 0.27.6
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/package.json +1 -1
- package/readme.md +2 -2
- package/typebox.d.ts +5 -5
- package/typebox.js +16 -7
- package/value/create.js +8 -2
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -101,7 +101,7 @@ License MIT
|
|
|
101
101
|
- [TypeCheck](#typecheck)
|
|
102
102
|
- [Ajv](#typecheck-ajv)
|
|
103
103
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
104
|
-
- [TypeSystem](#
|
|
104
|
+
- [TypeSystem](#typesystem)
|
|
105
105
|
- [Types](#typesystem-types)
|
|
106
106
|
- [Formats](#typesystem-formats)
|
|
107
107
|
- [Policies](#typesystem-policies)
|
|
@@ -402,7 +402,7 @@ The following table lists the Standard TypeBox types. These types are fully comp
|
|
|
402
402
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
403
403
|
│ const T = Type.Record( │ type T = Record< │ const T = { │
|
|
404
404
|
│ Type.String(), │ string, │ type: 'object', │
|
|
405
|
-
│ Type.Number() │ number
|
|
405
|
+
│ Type.Number() │ number │ patternProperties: { │
|
|
406
406
|
│ ) │ > │ '^.*$': { │
|
|
407
407
|
│ │ │ type: 'number' │
|
|
408
408
|
│ │ │ } │
|
package/typebox.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare const Hint: unique symbol;
|
|
|
3
3
|
export declare const Kind: unique symbol;
|
|
4
4
|
export declare const PatternBoolean = "(true|false)";
|
|
5
5
|
export declare const PatternNumber = "(0|[1-9][0-9]*)";
|
|
6
|
-
export declare const PatternString = ".*";
|
|
6
|
+
export declare const PatternString = "(.*)";
|
|
7
7
|
export declare const PatternBooleanExact: string;
|
|
8
8
|
export declare const PatternNumberExact: string;
|
|
9
9
|
export declare const PatternStringExact: string;
|
|
@@ -183,7 +183,7 @@ export type TKeyOfTuple<T extends TSchema> = {
|
|
|
183
183
|
} extends infer U ? UnionToTuple<Exclude<{
|
|
184
184
|
[K in keyof U]: U[K];
|
|
185
185
|
}[keyof U], undefined>> : never;
|
|
186
|
-
export type TKeyOf<T extends TSchema = TSchema> = (T extends TComposite ? TKeyOfTuple<T> : T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : T extends TRecord<infer K> ? [K] : [
|
|
186
|
+
export type TKeyOf<T extends TSchema = TSchema> = (T extends TComposite ? TKeyOfTuple<T> : T extends TIntersect ? TKeyOfTuple<T> : T extends TUnion ? TKeyOfTuple<T> : T extends TObject ? TKeyOfTuple<T> : T extends TRecursive<infer S> ? TKeyOfTuple<S> : T extends TRecord<infer K> ? [K] : [
|
|
187
187
|
]) extends infer R ? TUnionResult<Assert<R, TSchema[]>> : never;
|
|
188
188
|
export type TLiteralValue = string | number | boolean;
|
|
189
189
|
export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
|
|
@@ -248,7 +248,7 @@ export type TOmitArray<T extends TSchema[], K extends keyof any> = Assert<{
|
|
|
248
248
|
[K2 in keyof T]: TOmit<Assert<T[K2], TSchema>, K>;
|
|
249
249
|
}, TSchema[]>;
|
|
250
250
|
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<Assert<Omit<T, K>, TProperties>>;
|
|
251
|
-
export type TOmit<T extends TSchema, K extends keyof any> = T extends TComposite<infer S> ? TComposite<TOmitArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
251
|
+
export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TComposite<infer S> ? TComposite<TOmitArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
252
252
|
export type TParameters<T extends TFunction> = TTuple<T['parameters']>;
|
|
253
253
|
export type TPartialObjectArray<T extends TObject[]> = Assert<{
|
|
254
254
|
[K in keyof T]: TPartial<Assert<T[K], TObject>>;
|
|
@@ -266,7 +266,7 @@ export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
|
266
266
|
export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
|
|
267
267
|
[K in keyof R]: Assert<R[K], TSchema> extends TSchema ? R[K] : never;
|
|
268
268
|
}) : never;
|
|
269
|
-
export type TPick<T extends TSchema, K extends keyof any> = T extends TComposite<infer S> ? TComposite<TPickArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
|
|
269
|
+
export type TPick<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TComposite<infer S> ? TComposite<TPickArray<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : T extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
|
|
270
270
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
271
271
|
[Kind]: 'Promise';
|
|
272
272
|
static: Promise<Static<T, this['params']>>;
|
|
@@ -277,7 +277,7 @@ export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
|
277
277
|
export type RecordTemplateLiteralObjectType<K extends TTemplateLiteral, T extends TSchema> = Ensure<TObject<Evaluate<{
|
|
278
278
|
[_ in Static<K>]: T;
|
|
279
279
|
}>>>;
|
|
280
|
-
export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<
|
|
280
|
+
export type RecordTemplateLiteralType<K extends TTemplateLiteral, T extends TSchema> = IsTemplateLiteralFinite<K> extends true ? RecordTemplateLiteralObjectType<K, T> : TRecord<K, T>;
|
|
281
281
|
export type RecordUnionLiteralType<K extends TUnion<TLiteral<string | number>[]>, T extends TSchema> = Static<K> extends string ? Ensure<TObject<{
|
|
282
282
|
[X in Static<K>]: T;
|
|
283
283
|
}>> : never;
|
package/typebox.js
CHANGED
|
@@ -39,7 +39,7 @@ exports.Kind = Symbol.for('TypeBox.Kind');
|
|
|
39
39
|
// --------------------------------------------------------------------------
|
|
40
40
|
exports.PatternBoolean = '(true|false)';
|
|
41
41
|
exports.PatternNumber = '(0|[1-9][0-9]*)';
|
|
42
|
-
exports.PatternString = '.*';
|
|
42
|
+
exports.PatternString = '(.*)';
|
|
43
43
|
exports.PatternBooleanExact = `^${exports.PatternBoolean}$`;
|
|
44
44
|
exports.PatternNumberExact = `^${exports.PatternNumber}$`;
|
|
45
45
|
exports.PatternStringExact = `^${exports.PatternString}$`;
|
|
@@ -1520,9 +1520,6 @@ class TemplateLiteralParserError extends Error {
|
|
|
1520
1520
|
exports.TemplateLiteralParserError = TemplateLiteralParserError;
|
|
1521
1521
|
var TemplateLiteralParser;
|
|
1522
1522
|
(function (TemplateLiteralParser) {
|
|
1523
|
-
function Unescape(value) {
|
|
1524
|
-
return value.replace(/\\/g, '');
|
|
1525
|
-
}
|
|
1526
1523
|
function IsNonEscaped(pattern, index, char) {
|
|
1527
1524
|
return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92;
|
|
1528
1525
|
}
|
|
@@ -1647,7 +1644,7 @@ var TemplateLiteralParser;
|
|
|
1647
1644
|
return Or(pattern);
|
|
1648
1645
|
if (IsPrecedenceAnd(pattern))
|
|
1649
1646
|
return And(pattern);
|
|
1650
|
-
return { type: 'const', const:
|
|
1647
|
+
return { type: 'const', const: pattern };
|
|
1651
1648
|
}
|
|
1652
1649
|
TemplateLiteralParser.Parse = Parse;
|
|
1653
1650
|
/** Parses a pattern and strips forward and trailing ^ and $ */
|
|
@@ -1662,10 +1659,22 @@ var TemplateLiteralParser;
|
|
|
1662
1659
|
var TemplateLiteralFinite;
|
|
1663
1660
|
(function (TemplateLiteralFinite) {
|
|
1664
1661
|
function IsNumber(expression) {
|
|
1665
|
-
|
|
1662
|
+
// prettier-ignore
|
|
1663
|
+
return (expression.type === 'or' &&
|
|
1664
|
+
expression.expr.length === 2 &&
|
|
1665
|
+
expression.expr[0].type === 'const' &&
|
|
1666
|
+
expression.expr[0].const === '0' &&
|
|
1667
|
+
expression.expr[1].type === 'const' &&
|
|
1668
|
+
expression.expr[1].const === '[1-9][0-9]*');
|
|
1666
1669
|
}
|
|
1667
1670
|
function IsBoolean(expression) {
|
|
1668
|
-
|
|
1671
|
+
// prettier-ignore
|
|
1672
|
+
return (expression.type === 'or' &&
|
|
1673
|
+
expression.expr.length === 2 &&
|
|
1674
|
+
expression.expr[0].type === 'const' &&
|
|
1675
|
+
expression.expr[0].const === 'true' &&
|
|
1676
|
+
expression.expr[1].type === 'const' &&
|
|
1677
|
+
expression.expr[1].const === 'false');
|
|
1669
1678
|
}
|
|
1670
1679
|
function IsString(expression) {
|
|
1671
1680
|
return expression.type === 'const' && expression.const === '.*';
|
package/value/create.js
CHANGED
|
@@ -49,7 +49,7 @@ class ValueCreateNeverTypeError extends Error {
|
|
|
49
49
|
exports.ValueCreateNeverTypeError = ValueCreateNeverTypeError;
|
|
50
50
|
class ValueCreateIntersectTypeError extends Error {
|
|
51
51
|
constructor(schema) {
|
|
52
|
-
super('ValueCreate:
|
|
52
|
+
super('ValueCreate: Intersect produced invalid value. Consider using a default value.');
|
|
53
53
|
this.schema = schema;
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -179,7 +179,13 @@ var ValueCreate;
|
|
|
179
179
|
return schema.default;
|
|
180
180
|
}
|
|
181
181
|
else {
|
|
182
|
-
|
|
182
|
+
// Note: The best we can do here is attempt to instance each sub type and apply through object assign. For non-object
|
|
183
|
+
// sub types, we just escape the assignment and just return the value. In the latter case, this is typically going to
|
|
184
|
+
// be a consequence of an illogical intersection.
|
|
185
|
+
const value = schema.allOf.reduce((acc, schema) => {
|
|
186
|
+
const next = Visit(schema, references);
|
|
187
|
+
return typeof next === 'object' ? { ...acc, ...next } : next;
|
|
188
|
+
}, {});
|
|
183
189
|
if (!check_1.ValueCheck.Check(schema, references, value))
|
|
184
190
|
throw new ValueCreateIntersectTypeError(schema);
|
|
185
191
|
return value;
|