typebox 1.3.1 → 1.3.2
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/format/_idna.mjs +4 -1
- package/build/format/_puny.mjs +4 -1
- package/build/schema/engine/boolean.d.mts +3 -3
- package/build/schema/engine/boolean.mjs +4 -4
- package/build/schema/engine/schema.mjs +5 -5
- package/build/schema/types/schema.d.mts +1 -1
- package/build/schema/types/schema.mjs +2 -2
- package/build/type/types/enum.d.mts +2 -0
- package/build/type/types/enum.mjs +5 -0
- package/build/typebox.d.mts +1 -1
- package/build/typebox.mjs +1 -1
- package/build/value/repair/from_type.mjs +22 -10
- package/package.json +1 -1
package/build/format/_idna.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Unreachable } from '../system/unreachable/index.mjs';
|
|
1
2
|
import * as Puny from './_puny.mjs';
|
|
2
3
|
// ------------------------------------------------------------------
|
|
3
4
|
// Unicode General Category Helper (RFC 5892)
|
|
@@ -102,8 +103,10 @@ function IsVirama(cp) {
|
|
|
102
103
|
// IsUnicodeLabel
|
|
103
104
|
// ------------------------------------------------------------------
|
|
104
105
|
function IsUnicodeLabel(value) {
|
|
106
|
+
// deno-coverage-ignore-start - unable to reach via format guards
|
|
105
107
|
if (value.length === 0)
|
|
106
|
-
return false
|
|
108
|
+
return Unreachable(); // false
|
|
109
|
+
// deno-coverage-ignore-stop
|
|
107
110
|
// Use spread to handle surrogate pairs and provide O(1) neighbor access
|
|
108
111
|
const cps = [...value].map((c) => c.codePointAt(0));
|
|
109
112
|
const len = cps.length;
|
package/build/format/_puny.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Unreachable } from '../system/unreachable/index.mjs';
|
|
1
2
|
// ------------------------------------------------------------------
|
|
2
3
|
// PunyCode (RFC 3492)
|
|
3
4
|
// ------------------------------------------------------------------
|
|
@@ -52,8 +53,10 @@ export function Decode(value) {
|
|
|
52
53
|
digit = ch - 0x61; // a-z => 0-25
|
|
53
54
|
else if (ch >= 0x30 && ch <= 0x39)
|
|
54
55
|
digit = ch - 0x30 + 26; // 0-9 => 26-35
|
|
56
|
+
// deno-coverage-ignore-start - _idna.ts maps input to lowercase, so we can't reach here
|
|
55
57
|
else if (ch >= 0x41 && ch <= 0x5a)
|
|
56
|
-
digit = ch - 0x41
|
|
58
|
+
Unreachable(); // digit = ch - 0x41 // A-Z => 0-25
|
|
59
|
+
// deno-coverage-ignore-stop
|
|
57
60
|
else
|
|
58
61
|
throw new Error('Invalid punycode: bad digit character');
|
|
59
62
|
i += digit * w;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Stack } from './_stack.mjs';
|
|
2
2
|
import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
3
|
+
export declare function BuildSchemaBoolean(_stack: Stack, _context: BuildContext, schema: boolean, _value: string): string;
|
|
4
|
+
export declare function CheckSchemaBoolean(_stack: Stack, _context: CheckContext, schema: boolean, _value: unknown): boolean;
|
|
5
|
+
export declare function ErrorSchemaBoolean(stack: Stack, context: ErrorContext, schemaPath: string, instancePath: string, schema: boolean, value: unknown): boolean;
|
|
@@ -3,20 +3,20 @@ import { EmitGuard as E } from '../../guard/index.mjs';
|
|
|
3
3
|
// ------------------------------------------------------------------
|
|
4
4
|
// Build
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
|
-
export function
|
|
6
|
+
export function BuildSchemaBoolean(_stack, _context, schema, _value) {
|
|
7
7
|
return schema ? E.Constant(true) : E.Constant(false);
|
|
8
8
|
}
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Check
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
|
-
export function
|
|
12
|
+
export function CheckSchemaBoolean(_stack, _context, schema, _value) {
|
|
13
13
|
return schema;
|
|
14
14
|
}
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
16
|
// Error
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
|
-
export function
|
|
19
|
-
return
|
|
18
|
+
export function ErrorSchemaBoolean(stack, context, schemaPath, instancePath, schema, value) {
|
|
19
|
+
return CheckSchemaBoolean(stack, context, schema, value) || context.AddError({
|
|
20
20
|
keyword: 'boolean',
|
|
21
21
|
schemaPath,
|
|
22
22
|
instancePath,
|
|
@@ -6,7 +6,7 @@ import { BuildAdditionalItems, CheckAdditionalItems, ErrorAdditionalItems } from
|
|
|
6
6
|
import { BuildAdditionalProperties, CheckAdditionalProperties, ErrorAdditionalProperties } from './additionalProperties.mjs';
|
|
7
7
|
import { BuildAllOf, CheckAllOf, ErrorAllOf } from './allOf.mjs';
|
|
8
8
|
import { BuildAnyOf, CheckAnyOf, ErrorAnyOf } from './anyOf.mjs';
|
|
9
|
-
import {
|
|
9
|
+
import { BuildSchemaBoolean, CheckSchemaBoolean, ErrorSchemaBoolean } from './boolean.mjs';
|
|
10
10
|
import { BuildConst, CheckConst, ErrorConst } from './const.mjs';
|
|
11
11
|
import { BuildContains, CheckContains, ErrorContains } from './contains.mjs';
|
|
12
12
|
import { BuildDependencies, CheckDependencies, ErrorDependencies } from './dependencies.mjs';
|
|
@@ -125,8 +125,8 @@ export function BuildSchemaPushStack(stack, context, schema, value) {
|
|
|
125
125
|
export function BuildSchema(stack, context, schema, value) {
|
|
126
126
|
stack.Push(schema);
|
|
127
127
|
const conditions = [];
|
|
128
|
-
if (Schema.
|
|
129
|
-
return
|
|
128
|
+
if (Schema.IsSchemaBoolean(schema))
|
|
129
|
+
return BuildSchemaBoolean(stack, context, schema, value);
|
|
130
130
|
if (Schema.IsType(schema))
|
|
131
131
|
conditions.push(BuildType(stack, context, schema, value));
|
|
132
132
|
if (HasObjectKeywords(schema)) {
|
|
@@ -247,7 +247,7 @@ export function CheckSchemaPushStack(stack, context, schema, value) {
|
|
|
247
247
|
}
|
|
248
248
|
export function CheckSchema(stack, context, schema, value) {
|
|
249
249
|
stack.Push(schema);
|
|
250
|
-
const result = Schema.
|
|
250
|
+
const result = Schema.IsSchemaBoolean(schema) ? CheckSchemaBoolean(stack, context, schema, value) : ((!Schema.IsType(schema) || CheckType(stack, context, schema, value)) &&
|
|
251
251
|
(!(G.IsObject(value) && !G.IsArray(value)) || ((!Schema.IsRequired(schema) || CheckRequired(stack, context, schema, value)) &&
|
|
252
252
|
(!Schema.IsAdditionalProperties(schema) || CheckAdditionalProperties(stack, context, schema, value)) &&
|
|
253
253
|
(!Schema.IsDependencies(schema) || CheckDependencies(stack, context, schema, value)) &&
|
|
@@ -300,7 +300,7 @@ export function ErrorSchemaPushStack(stack, context, schemaPath, instancePath, s
|
|
|
300
300
|
}
|
|
301
301
|
export function ErrorSchema(stack, context, schemaPath, instancePath, schema, value) {
|
|
302
302
|
stack.Push(schema);
|
|
303
|
-
const result = (Schema.
|
|
303
|
+
const result = (Schema.IsSchemaBoolean(schema)) ? ErrorSchemaBoolean(stack, context, schemaPath, instancePath, schema, value) : (!!(+(!Schema.IsType(schema) || ErrorType(stack, context, schemaPath, instancePath, schema, value)) &
|
|
304
304
|
+(!(G.IsObject(value) && !G.IsArray(value)) || !!(+(!Schema.IsRequired(schema) || ErrorRequired(stack, context, schemaPath, instancePath, schema, value)) &
|
|
305
305
|
+(!Schema.IsAdditionalProperties(schema) || ErrorAdditionalProperties(stack, context, schemaPath, instancePath, schema, value)) &
|
|
306
306
|
+(!Schema.IsDependencies(schema) || ErrorDependencies(stack, context, schemaPath, instancePath, schema, value)) &
|
|
@@ -3,7 +3,7 @@ export type XSchemaObject = object;
|
|
|
3
3
|
export declare function IsSchemaObject(value: unknown): value is XSchemaObject;
|
|
4
4
|
export type XSchemaBoolean = boolean;
|
|
5
5
|
/** Returns true if this value is a boolean */
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function IsSchemaBoolean(value: unknown): value is XSchemaBoolean;
|
|
7
7
|
export type XSchema = XSchemaObject | XSchemaBoolean;
|
|
8
8
|
/** Returns true if this value is schema like */
|
|
9
9
|
export declare function IsSchema(value: unknown): value is XSchema;
|
|
@@ -5,10 +5,10 @@ export function IsSchemaObject(value) {
|
|
|
5
5
|
return Guard.IsObject(value) && !Guard.IsArray(value);
|
|
6
6
|
}
|
|
7
7
|
/** Returns true if this value is a boolean */
|
|
8
|
-
export function
|
|
8
|
+
export function IsSchemaBoolean(value) {
|
|
9
9
|
return Guard.IsBoolean(value);
|
|
10
10
|
}
|
|
11
11
|
/** Returns true if this value is schema like */
|
|
12
12
|
export function IsSchema(value) {
|
|
13
|
-
return IsSchemaObject(value) ||
|
|
13
|
+
return IsSchemaObject(value) || IsSchemaBoolean(value);
|
|
14
14
|
}
|
|
@@ -3,6 +3,8 @@ import { type TTypeScriptEnumLike } from '../engine/enum/typescript_enum_to_enum
|
|
|
3
3
|
import { type TTypeScriptEnumToEnumValues } from '../engine/enum/typescript_enum_to_enum_values.mjs';
|
|
4
4
|
export type StaticEnum<Values extends TEnumValue[]> = (Values[number]);
|
|
5
5
|
export type TEnumValue = string | number;
|
|
6
|
+
/** Returns true if the given value is a EnumValue */
|
|
7
|
+
export declare function IsEnumValue(value: unknown): value is TEnumValue;
|
|
6
8
|
/** Represents an Enum type. */
|
|
7
9
|
export interface TEnum<Values extends TEnumValue[] = TEnumValue[]> extends TSchema {
|
|
8
10
|
'~kind': 'Enum';
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Guard } from '../../guard/index.mjs';
|
|
2
3
|
import { Memory } from '../../system/memory/index.mjs';
|
|
3
4
|
import { IsKind } from './schema.mjs';
|
|
4
5
|
import { IsTypeScriptEnumLike } from '../engine/enum/typescript_enum_to_enum_values.mjs';
|
|
5
6
|
import { TypeScriptEnumToEnumValues } from '../engine/enum/typescript_enum_to_enum_values.mjs';
|
|
7
|
+
/** Returns true if the given value is a EnumValue */
|
|
8
|
+
export function IsEnumValue(value) {
|
|
9
|
+
return Guard.IsString(value) || Guard.IsNumber(value);
|
|
10
|
+
}
|
|
6
11
|
/** Creates an Enum type. */
|
|
7
12
|
export function Enum(value, options) {
|
|
8
13
|
const values = IsTypeScriptEnumLike(value) ? TypeScriptEnumToEnumValues(value) : value;
|
package/build/typebox.d.mts
CHANGED
|
@@ -37,7 +37,7 @@ export { Boolean, IsBoolean, type TBoolean } from './type/types/boolean.mjs';
|
|
|
37
37
|
export { Call, IsCall, type TCall } from './type/types/call.mjs';
|
|
38
38
|
export { Constructor, IsConstructor, type TConstructor } from './type/types/constructor.mjs';
|
|
39
39
|
export { Cyclic, IsCyclic, type TCyclic } from './type/types/cyclic.mjs';
|
|
40
|
-
export { Enum, IsEnum, type TEnum, type TEnumValue } from './type/types/enum.mjs';
|
|
40
|
+
export { Enum, IsEnum, IsEnumValue, type TEnum, type TEnumValue } from './type/types/enum.mjs';
|
|
41
41
|
export { Function, IsFunction, type TFunction } from './type/types/function.mjs';
|
|
42
42
|
export { Generic, IsGeneric, type TGeneric } from './type/types/generic.mjs';
|
|
43
43
|
export { Identifier, IsIdentifier, type TIdentifier } from './type/types/identifier.mjs';
|
package/build/typebox.mjs
CHANGED
|
@@ -55,7 +55,7 @@ export { Boolean, IsBoolean } from './type/types/boolean.mjs';
|
|
|
55
55
|
export { Call, IsCall } from './type/types/call.mjs';
|
|
56
56
|
export { Constructor, IsConstructor } from './type/types/constructor.mjs';
|
|
57
57
|
export { Cyclic, IsCyclic } from './type/types/cyclic.mjs';
|
|
58
|
-
export { Enum, IsEnum } from './type/types/enum.mjs';
|
|
58
|
+
export { Enum, IsEnum, IsEnumValue } from './type/types/enum.mjs';
|
|
59
59
|
export { Function, IsFunction } from './type/types/function.mjs';
|
|
60
60
|
export { Generic, IsGeneric } from './type/types/generic.mjs';
|
|
61
61
|
export { Identifier, IsIdentifier } from './type/types/identifier.mjs';
|
|
@@ -40,6 +40,17 @@ function AssertRepairableType(context, type, value) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
// ------------------------------------------------------------------
|
|
43
|
+
// CreateWhenUndefined
|
|
44
|
+
//
|
|
45
|
+
// If the value is 'undefined' AND the type is not TUndefined, then
|
|
46
|
+
// we know the value must be created. We handle this case for undefined
|
|
47
|
+
// only as it enables 'default' annotation to be initialized via Create
|
|
48
|
+
// before we applying subsequent Repair logic.
|
|
49
|
+
// ------------------------------------------------------------------
|
|
50
|
+
function CreateWhenUndefined(context, type, value) {
|
|
51
|
+
return (Guard.IsUndefined(value) && !T.IsUndefined(type)) ? Create(context, type) : value;
|
|
52
|
+
}
|
|
53
|
+
// ------------------------------------------------------------------
|
|
43
54
|
// FinalizeRepair
|
|
44
55
|
//
|
|
45
56
|
// When a type includes the ~refine modifier, a post-repair validation
|
|
@@ -62,15 +73,16 @@ function FinalizeRepair(context, type, repaired) {
|
|
|
62
73
|
export function FromType(context, type, value) {
|
|
63
74
|
AssertRepairableValue(context, type, value);
|
|
64
75
|
AssertRepairableType(context, type, value);
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
const candidate = CreateWhenUndefined(context, type, value);
|
|
77
|
+
const repaired = (T.IsArray(type) ? FromArray(context, type, candidate) :
|
|
78
|
+
T.IsEnum(type) ? FromEnum(context, type, candidate) :
|
|
79
|
+
T.IsIntersect(type) ? FromIntersect(context, type, candidate) :
|
|
80
|
+
T.IsObject(type) ? FromObject(context, type, candidate) :
|
|
81
|
+
T.IsRecord(type) ? FromRecord(context, type, candidate) :
|
|
82
|
+
T.IsRef(type) ? FromRef(context, type, candidate) :
|
|
83
|
+
T.IsTemplateLiteral(type) ? FromTemplateLiteral(context, type, candidate) :
|
|
84
|
+
T.IsTuple(type) ? FromTuple(context, type, candidate) :
|
|
85
|
+
T.IsUnion(type) ? FromUnion(context, type, candidate) :
|
|
86
|
+
FromUnknown(context, type, candidate));
|
|
75
87
|
return FinalizeRepair(context, type, repaired);
|
|
76
88
|
}
|