@depup/typebox 1.3.22-depup.0 → 1.3.27-depup.0

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.
Files changed (38) hide show
  1. package/README.md +2 -2
  2. package/build/compile/validator.d.mts +1 -1
  3. package/build/compile/validator.mjs +1 -1
  4. package/build/format/iri.mjs +5 -1
  5. package/build/schema/compile.d.mts +3 -2
  6. package/build/schema/compile.mjs +8 -8
  7. package/build/schema/engine/_context.d.mts +1 -6
  8. package/build/schema/engine/_context.mjs +9 -14
  9. package/build/schema/engine/_functions.mjs +1 -1
  10. package/build/schema/engine/_stack.d.mts +6 -2
  11. package/build/schema/engine/_stack.mjs +135 -32
  12. package/build/schema/engine/additionalProperties.mjs +1 -3
  13. package/build/schema/engine/allOf.mjs +2 -2
  14. package/build/schema/engine/anyOf.mjs +2 -2
  15. package/build/schema/engine/if.mjs +2 -2
  16. package/build/schema/engine/oneOf.mjs +2 -2
  17. package/build/schema/engine/propertyNames.mjs +1 -3
  18. package/build/schema/engine/ref.mjs +2 -2
  19. package/build/schema/engine/schema.mjs +6 -0
  20. package/build/schema/engine/unevaluatedItems.mjs +2 -2
  21. package/build/schema/engine/unevaluatedProperties.mjs +2 -2
  22. package/build/schema/errors.d.mts +2 -2
  23. package/build/schema/errors.mjs +7 -13
  24. package/build/schema/intern/index.d.mts +1 -0
  25. package/build/schema/intern/index.mjs +1 -0
  26. package/build/schema/intern/intern.d.mts +14 -0
  27. package/build/schema/intern/intern.mjs +259 -0
  28. package/build/schema/parse.d.mts +1 -0
  29. package/build/schema/parse.mjs +10 -5
  30. package/build/schema/resolve/resolve.d.mts +34 -3
  31. package/build/schema/resolve/resolve.mjs +198 -73
  32. package/build/schema/schema.d.mts +1 -0
  33. package/build/schema/schema.mjs +1 -0
  34. package/build/value/errors/errors.d.mts +2 -12
  35. package/build/value/errors/errors.mjs +2 -7
  36. package/changes.json +1 -1
  37. package/package.json +3 -3
  38. package/readme.md +9 -5
@@ -1,24 +1,18 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Arguments } from '../system/arguments/index.mjs';
3
- import { Settings } from '../system/settings/index.mjs';
4
- import { Get as LocaleGet } from '../system/locale/_config.mjs';
5
- import { Guard } from '../guard/index.mjs';
3
+ import { Get as GetLocalizationFunction } from '../system/locale/_config.mjs';
6
4
  import * as Engine from './engine/index.mjs';
7
- /** Checks a value and returns validation errors */
5
+ /** Returns an array of validation errors for the given value. */
8
6
  export function Errors(...args) {
9
7
  const [context, schema, value] = Arguments.Match(args, {
10
8
  3: (context, schema, value) => [context, schema, value],
11
9
  2: (schema, value) => [{}, schema, value]
12
10
  });
13
- const settings = Settings.Get();
14
- const locale = LocaleGet();
15
- const errors = [];
16
11
  const stack = new Engine.Stack(context, schema);
17
- const errorContext = new Engine.ErrorContext(error => {
18
- if (Guard.IsGreaterEqualThan(errors.length, settings.maxErrors))
19
- return;
20
- return errors.push({ ...error, message: locale(error) });
21
- });
12
+ const errorContext = new Engine.ErrorContext();
22
13
  const result = Engine.ErrorSchema(stack, errorContext, '#', '', schema, value);
23
- return [result, errors];
14
+ const errors = errorContext.GetErrors();
15
+ const locale = GetLocalizationFunction();
16
+ const localized = errors.map(error => ({ ...error, message: locale(error) }));
17
+ return [result, localized];
24
18
  }
@@ -0,0 +1 @@
1
+ export * from './intern.mjs';
@@ -0,0 +1 @@
1
+ export * from './intern.mjs';
@@ -0,0 +1,14 @@
1
+ import { type XStatic } from '../static/index.mjs';
2
+ import * as S from '../types/index.mjs';
3
+ export interface XIntern<Type extends unknown = unknown> {
4
+ '~unsafe': Type;
5
+ $ref: string;
6
+ $defs: Record<string, S.XSchemaObject>;
7
+ }
8
+ /**
9
+ * [Experimental] Performs a Common Subexpression Elimination (CSE) transform on the given
10
+ * schema. This function restructures the schema such that each distinct sub-schema is stored
11
+ * exactly once in a $defs object and keyed by content hash. This function can be used to
12
+ * both compress and optimize schemas prior to compilation.
13
+ */
14
+ export declare function Intern<const Schema extends S.XSchema>(schema: Schema): XIntern<XStatic<Schema>>;
@@ -0,0 +1,259 @@
1
+ import { Hashing, Memory } from '../../system/index.mjs';
2
+ import { Guard } from '../../guard/index.mjs';
3
+ import { Resolve } from '../resolve/index.mjs';
4
+ import * as S from '../types/index.mjs';
5
+ // ----------------------------------------------------------------
6
+ // UnsupportedKeyword
7
+ // ----------------------------------------------------------------
8
+ function UnsupportedKeyword(keyword) {
9
+ throw Error(`UnsupportedKeyword '${keyword}'`);
10
+ }
11
+ // ----------------------------------------------------------------
12
+ // UnresolvableRef
13
+ // ----------------------------------------------------------------
14
+ function UnresolvableRef(ref) {
15
+ throw Error(`UnresolvableRef '${ref}'`);
16
+ }
17
+ // ----------------------------------------------------------------
18
+ // HashKey
19
+ // ----------------------------------------------------------------
20
+ function HashKey(schema) {
21
+ return `x-${Hashing.Hash(schema)}`;
22
+ }
23
+ // ----------------------------------------------------------------
24
+ // AdditionalItems
25
+ // ----------------------------------------------------------------
26
+ function FromAdditionalItems(context, schema) {
27
+ return FromSchema(context, schema.additionalItems);
28
+ }
29
+ // ----------------------------------------------------------------
30
+ // AdditionalProperties
31
+ // ----------------------------------------------------------------
32
+ function FromAdditionalProperties(context, schema) {
33
+ return FromSchema(context, schema.additionalProperties);
34
+ }
35
+ // ----------------------------------------------------------------
36
+ // AllOf
37
+ // ----------------------------------------------------------------
38
+ function FromAllOf(context, schema) {
39
+ return schema.allOf.map((inner) => FromSchema(context, inner));
40
+ }
41
+ // ----------------------------------------------------------------
42
+ // AnyOf
43
+ // ----------------------------------------------------------------
44
+ function FromAnyOf(context, schema) {
45
+ return schema.anyOf.map((inner) => FromSchema(context, inner));
46
+ }
47
+ // ----------------------------------------------------------------
48
+ // Contains
49
+ // ----------------------------------------------------------------
50
+ function FromContains(context, schema) {
51
+ return FromSchema(context, schema.contains);
52
+ }
53
+ // ----------------------------------------------------------------
54
+ // DependentSchemas
55
+ // ----------------------------------------------------------------
56
+ function FromDependentSchemas(context, schema) {
57
+ return Guard.Keys(schema.dependentSchemas).reduce((result, key) => ({ ...result, [key]: FromSchema(context, schema.dependentSchemas[key]) }), {});
58
+ }
59
+ // ----------------------------------------------------------------
60
+ // Else
61
+ // ----------------------------------------------------------------
62
+ function FromElse(context, schema) {
63
+ return FromSchema(context, schema.else);
64
+ }
65
+ // ----------------------------------------------------------------
66
+ // If
67
+ // ----------------------------------------------------------------
68
+ function FromIf(context, schema) {
69
+ return FromSchema(context, schema.if);
70
+ }
71
+ // ----------------------------------------------------------------
72
+ // Items
73
+ // ----------------------------------------------------------------
74
+ function FromItems(context, schema) {
75
+ return S.IsItemsSized(schema) ? FromItemsSized(context, schema) : FromItemsUnsized(context, schema);
76
+ }
77
+ // ----------------------------------------------------------------
78
+ // ItemsSized
79
+ // ----------------------------------------------------------------
80
+ function FromItemsSized(context, schema) {
81
+ return schema.items.map((inner) => FromSchema(context, inner));
82
+ }
83
+ // ----------------------------------------------------------------
84
+ // ItemsUnsized
85
+ // ----------------------------------------------------------------
86
+ function FromItemsUnsized(context, schema) {
87
+ return FromSchema(context, schema.items);
88
+ }
89
+ // ----------------------------------------------------------------
90
+ // Not
91
+ // ----------------------------------------------------------------
92
+ function FromNot(context, schema) {
93
+ return FromSchema(context, schema.not);
94
+ }
95
+ // ----------------------------------------------------------------
96
+ // OneOf
97
+ // ----------------------------------------------------------------
98
+ function FromOneOf(context, schema) {
99
+ return schema.oneOf.map((inner) => FromSchema(context, inner));
100
+ }
101
+ // ----------------------------------------------------------------
102
+ // PatternProperties
103
+ // ----------------------------------------------------------------
104
+ function FromPatternProperties(context, schema) {
105
+ return Guard.Keys(schema.patternProperties).reduce((result, key) => ({ ...result, [key]: FromSchema(context, schema.patternProperties[key]) }), {});
106
+ }
107
+ // ----------------------------------------------------------------
108
+ // PrefixItems
109
+ // ----------------------------------------------------------------
110
+ function FromPrefixItems(context, schema) {
111
+ return schema.prefixItems.map((inner) => FromSchema(context, inner));
112
+ }
113
+ // ----------------------------------------------------------------
114
+ // Properties
115
+ // ----------------------------------------------------------------
116
+ function FromProperties(context, schema) {
117
+ return Guard.Keys(schema.properties).reduce((result, key) => ({ ...result, [key]: FromSchema(context, schema.properties[key]) }), {});
118
+ }
119
+ // ----------------------------------------------------------------
120
+ // PropertyNames
121
+ // ----------------------------------------------------------------
122
+ function FromPropertyNames(context, schema) {
123
+ return FromSchema(context, schema.propertyNames);
124
+ }
125
+ // ----------------------------------------------------------------
126
+ // Ref
127
+ // ----------------------------------------------------------------
128
+ function ResolveRef(context, schema, ref) {
129
+ return Resolve.Ref(context, schema, Resolve.DefaultBase, ref) ?? UnresolvableRef(ref);
130
+ }
131
+ function FromRef(context, schema) {
132
+ // Resolve target
133
+ const target = ResolveRef(context.context, context.schema, schema.$ref);
134
+ // Check if target is resolving, if not, resolve
135
+ const resolving = context.resolving.get(target);
136
+ if (Guard.IsUndefined(resolving))
137
+ return FromSchema(context, target);
138
+ // Target is mid-intern, so this is a cycle (point at its reserved placeholder)
139
+ resolving.used = true;
140
+ return { $ref: `#/$defs/${resolving.key}` };
141
+ }
142
+ // ----------------------------------------------------------------
143
+ // Then
144
+ // ----------------------------------------------------------------
145
+ function FromThen(context, schema) {
146
+ return FromSchema(context, schema.then);
147
+ }
148
+ // ----------------------------------------------------------------
149
+ // UnevaluatedItems
150
+ // ----------------------------------------------------------------
151
+ function FromUnevaluatedItems(context, schema) {
152
+ return FromSchema(context, schema.unevaluatedItems);
153
+ }
154
+ // ----------------------------------------------------------------
155
+ // UnevaluatedProperties
156
+ // ----------------------------------------------------------------
157
+ function FromUnevaluatedProperties(context, schema) {
158
+ return FromSchema(context, schema.unevaluatedProperties);
159
+ }
160
+ // ----------------------------------------------------------------
161
+ // SchemaObject
162
+ // ----------------------------------------------------------------
163
+ function FromSchemaObject(context, schema) {
164
+ // Reference schemas cannot contain other keywords
165
+ if (S.IsRef(schema))
166
+ return FromRef(context, schema);
167
+ // Check if the schema has already been resolved
168
+ const existing = resolved.get(schema);
169
+ if (!Guard.IsUndefined(existing))
170
+ return existing;
171
+ // These keywords are unsupported
172
+ if (S.IsDynamicRef(schema))
173
+ UnsupportedKeyword('$dynamicRef');
174
+ if (S.IsRecursiveRef(schema))
175
+ UnsupportedKeyword('$recursiveRef');
176
+ // Reserve a placeholder key in case a nested ref cycles back to this schema
177
+ const reservation = { key: `x-ref-${context.resolving.size}`, used: false };
178
+ context.resolving.set(schema, reservation);
179
+ // Intern each subschema
180
+ const remapped = {
181
+ ...(S.IsRefine(schema) ? { ['~refine']: schema['~refine'] } : {}),
182
+ ...(S.IsAdditionalItems(schema) ? { additionalItems: FromAdditionalItems(context, schema) } : {}),
183
+ ...(S.IsAdditionalProperties(schema) ? { additionalProperties: FromAdditionalProperties(context, schema) } : {}),
184
+ ...(S.IsAllOf(schema) ? { allOf: FromAllOf(context, schema) } : {}),
185
+ ...(S.IsAnyOf(schema) ? { anyOf: FromAnyOf(context, schema) } : {}),
186
+ ...(S.IsContains(schema) ? { contains: FromContains(context, schema) } : {}),
187
+ ...(S.IsDependentSchemas(schema) ? { dependentSchemas: FromDependentSchemas(context, schema) } : {}),
188
+ ...(S.IsElse(schema) ? { else: FromElse(context, schema) } : {}),
189
+ ...(S.IsIf(schema) ? { if: FromIf(context, schema) } : {}),
190
+ ...(S.IsItems(schema) ? { items: FromItems(context, schema) } : {}),
191
+ ...(S.IsNot(schema) ? { not: FromNot(context, schema) } : {}),
192
+ ...(S.IsOneOf(schema) ? { oneOf: FromOneOf(context, schema) } : {}),
193
+ ...(S.IsPatternProperties(schema) ? { patternProperties: FromPatternProperties(context, schema) } : {}),
194
+ ...(S.IsPrefixItems(schema) ? { prefixItems: FromPrefixItems(context, schema) } : {}),
195
+ ...(S.IsProperties(schema) ? { properties: FromProperties(context, schema) } : {}),
196
+ ...(S.IsPropertyNames(schema) ? { propertyNames: FromPropertyNames(context, schema) } : {}),
197
+ ...(S.IsThen(schema) ? { then: FromThen(context, schema) } : {}),
198
+ ...(S.IsUnevaluatedItems(schema) ? { unevaluatedItems: FromUnevaluatedItems(context, schema) } : {}),
199
+ ...(S.IsUnevaluatedProperties(schema) ? { unevaluatedProperties: FromUnevaluatedProperties(context, schema) } : {})
200
+ };
201
+ context.resolving.delete(schema);
202
+ // Finalize and register the result
203
+ const interned = Memory.Discard(Memory.Assign(schema, remapped), ['$id']);
204
+ const key = reservation.used ? reservation.key : HashKey(interned);
205
+ registry.set(key, interned);
206
+ // Result
207
+ const result = { $ref: `#/$defs/${key}` };
208
+ resolved.set(schema, result);
209
+ return result;
210
+ }
211
+ // ----------------------------------------------------------------
212
+ // SchemaBoolean
213
+ // ----------------------------------------------------------------
214
+ function FromSchemaBoolean(_context, schema) {
215
+ // Finalize and register the result
216
+ const key = HashKey(schema);
217
+ registry.set(key, schema);
218
+ // Result
219
+ const result = { $ref: `#/$defs/${key}` };
220
+ resolved.set(schema, result);
221
+ return result;
222
+ }
223
+ // ----------------------------------------------------------------
224
+ // Schema
225
+ // ----------------------------------------------------------------
226
+ function FromSchema(context, schema) {
227
+ return S.IsSchemaBoolean(schema) ? FromSchemaBoolean(context, schema) : FromSchemaObject(context, schema);
228
+ }
229
+ // ----------------------------------------------------------------
230
+ // BooleanEntry
231
+ // ----------------------------------------------------------------
232
+ function BooleanEntry(schema) {
233
+ const key = HashKey(schema);
234
+ return { $ref: `#/$defs/${key}`, $defs: { [key]: schema } };
235
+ }
236
+ // ----------------------------------------------------------------
237
+ // Module-level accumulator state
238
+ // ----------------------------------------------------------------
239
+ const registry = new Map();
240
+ const resolved = new Map();
241
+ /**
242
+ * [Experimental] Performs a Common Subexpression Elimination (CSE) transform on the given
243
+ * schema. This function restructures the schema such that each distinct sub-schema is stored
244
+ * exactly once in a $defs object and keyed by content hash. This function can be used to
245
+ * both compress and optimize schemas prior to compilation.
246
+ */
247
+ export function Intern(schema) {
248
+ registry.clear();
249
+ resolved.clear();
250
+ if (S.IsSchemaBoolean(schema))
251
+ return BooleanEntry(schema);
252
+ const context = S.IsDefs(schema) ? schema.$defs : {};
253
+ const entry = S.IsRef(schema) ? ResolveRef(context, schema, schema.$ref) : schema;
254
+ if (S.IsSchemaBoolean(entry))
255
+ return BooleanEntry(entry);
256
+ const ref_context = { schema, context, resolving: new Map() };
257
+ const result = FromSchema(ref_context, entry);
258
+ return { $ref: result.$ref, $defs: Object.fromEntries(registry) };
259
+ }
@@ -7,6 +7,7 @@ export declare class ParseError {
7
7
  errors: TLocalizedValidationError[];
8
8
  constructor(schema: Schema.XSchema, value: unknown, errors: TLocalizedValidationError[]);
9
9
  }
10
+ export declare function ThrowParseError(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema, value: unknown): never;
10
11
  /** Parses a value against the provided schema */
11
12
  export declare function Parse<const Schema extends Schema.XSchema>(schema: Schema, value: unknown): Static<Schema>;
12
13
  /** Parses a value against the provided schema */
@@ -13,15 +13,20 @@ export class ParseError {
13
13
  this.errors = errors;
14
14
  }
15
15
  }
16
+ // ------------------------------------------------------------------
17
+ // ThrowParseError
18
+ // ------------------------------------------------------------------
19
+ export function ThrowParseError(context, schema, value) {
20
+ const result = Errors(context, schema, value);
21
+ throw new ParseError(schema, value, result[1]);
22
+ }
16
23
  /** Parses a value against the provided schema */
17
24
  export function Parse(...args) {
18
25
  const [context, schema, value] = Arguments.Match(args, {
19
26
  3: (context, schema, value) => [context, schema, value],
20
27
  2: (schema, value) => [{}, schema, value]
21
28
  });
22
- if (!Check(context, schema, value)) {
23
- const [_result, errors] = Errors(context, schema, value);
24
- throw new ParseError(schema, value, errors);
25
- }
26
- return value;
29
+ if (Check(context, schema, value))
30
+ return value;
31
+ ThrowParseError(context, schema, value);
27
32
  }
@@ -1,4 +1,35 @@
1
1
  import * as Schema from '../types/index.mjs';
2
- export declare const DefaultBase: URL;
3
- export declare function Ref(context: Record<string, Schema.XSchema>, schema: Schema.XSchemaObject, ref: string): Schema.XSchema | undefined;
4
- export declare function DynamicRef(context: Record<string, Schema.XSchema>, root: Schema.XSchemaObject, base: Schema.XSchemaObject, dynamicRef: Schema.XDynamicRef, dynamicAnchors: Schema.XDynamicAnchor[]): Schema.XSchema | undefined;
2
+ export declare const DefaultBase = "https://json-schema.org";
3
+ export interface StackFrame {
4
+ context: Record<string, Schema.XSchema>;
5
+ root: Schema.XSchemaObject;
6
+ lexicalSchema: Schema.XSchemaObject;
7
+ lexicalBase: string;
8
+ referenceBase: string;
9
+ resourceBase: string;
10
+ ids: Schema.XId[];
11
+ recursiveAnchors: Schema.XRecursiveAnchor[];
12
+ dynamicAnchors: Schema.XDynamicAnchor[];
13
+ inRetrievedFrame: boolean;
14
+ }
15
+ export interface RetrievedResource {
16
+ target: Schema.XSchemaObject;
17
+ base: string;
18
+ root: Schema.XSchemaObject;
19
+ }
20
+ export interface ResolvedResource {
21
+ target: Schema.XSchemaObject;
22
+ resource: Schema.XId;
23
+ }
24
+ export interface RefResult {
25
+ schema: Schema.XSchema | undefined;
26
+ retrievedResource?: RetrievedResource;
27
+ resolvedResource?: ResolvedResource;
28
+ }
29
+ export declare function Base(schema: Schema.XSchemaObject, base: string, target: Schema.XSchema): string | undefined;
30
+ export declare function Resource(context: Record<string, Schema.XSchema>, schema: Schema.XSchemaObject, base: string, ref: string): Schema.XId | undefined;
31
+ export declare function Ref(remotes: Record<string, Schema.XSchema>, schema: Schema.XSchemaObject, base: string, ref: string, applySchemaId?: boolean): Schema.XSchema | undefined;
32
+ export declare function DynamicRef(context: Record<string, Schema.XSchema>, root: Schema.XSchemaObject, base: string, schema: Schema.XSchemaObject, dynamicRef: Schema.XDynamicRef, dynamicAnchors: Schema.XDynamicAnchor[]): Schema.XSchema | undefined;
33
+ export declare function ResolveRef(stackframe: StackFrame, ref: Schema.XRef): RefResult;
34
+ export declare function ResolveRecursiveRef(stackframe: StackFrame, recursiveRef: Schema.XRecursiveRef): Schema.XSchema | undefined;
35
+ export declare function ResolveDynamicRef(stackframe: StackFrame, dynamicRef: Schema.XDynamicRef): Schema.XSchema | undefined;