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.
@@ -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;
@@ -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; // A-Z => 0-25
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 BuildBooleanSchema(_stack: Stack, _context: BuildContext, schema: boolean, _value: string): string;
4
- export declare function CheckBooleanSchema(_stack: Stack, _context: CheckContext, schema: boolean, _value: unknown): boolean;
5
- export declare function ErrorBooleanSchema(stack: Stack, context: ErrorContext, schemaPath: string, instancePath: string, schema: boolean, value: unknown): boolean;
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 BuildBooleanSchema(_stack, _context, schema, _value) {
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 CheckBooleanSchema(_stack, _context, schema, _value) {
12
+ export function CheckSchemaBoolean(_stack, _context, schema, _value) {
13
13
  return schema;
14
14
  }
15
15
  // ------------------------------------------------------------------
16
16
  // Error
17
17
  // ------------------------------------------------------------------
18
- export function ErrorBooleanSchema(stack, context, schemaPath, instancePath, schema, value) {
19
- return CheckBooleanSchema(stack, context, schema, value) || context.AddError({
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 { BuildBooleanSchema, CheckBooleanSchema, ErrorBooleanSchema } from './boolean.mjs';
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.IsBooleanSchema(schema))
129
- return BuildBooleanSchema(stack, context, schema, value);
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.IsBooleanSchema(schema) ? CheckBooleanSchema(stack, context, schema, value) : ((!Schema.IsType(schema) || CheckType(stack, context, schema, value)) &&
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.IsBooleanSchema(schema)) ? ErrorBooleanSchema(stack, context, schemaPath, instancePath, schema, value) : (!!(+(!Schema.IsType(schema) || ErrorType(stack, context, schemaPath, instancePath, schema, value)) &
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 IsBooleanSchema(value: unknown): value is XSchemaBoolean;
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 IsBooleanSchema(value) {
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) || IsBooleanSchema(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;
@@ -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 repaired = (T.IsArray(type) ? FromArray(context, type, value) :
66
- T.IsEnum(type) ? FromEnum(context, type, value) :
67
- T.IsIntersect(type) ? FromIntersect(context, type, value) :
68
- T.IsObject(type) ? FromObject(context, type, value) :
69
- T.IsRecord(type) ? FromRecord(context, type, value) :
70
- T.IsRef(type) ? FromRef(context, type, value) :
71
- T.IsTemplateLiteral(type) ? FromTemplateLiteral(context, type, value) :
72
- T.IsTuple(type) ? FromTuple(context, type, value) :
73
- T.IsUnion(type) ? FromUnion(context, type, value) :
74
- FromUnknown(context, type, value));
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
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.3.1",
4
+ "version": "1.3.2",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"