@sinclair/typebox 0.31.18 → 0.31.20

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.
@@ -75,6 +75,7 @@ export declare class ValueErrorsUnknownTypeError extends Types.TypeBoxError {
75
75
  readonly schema: Types.TSchema;
76
76
  constructor(schema: Types.TSchema);
77
77
  }
78
+ export declare function EscapeKey(key: string): string;
78
79
  export declare class ValueErrorIterator {
79
80
  private readonly iterator;
80
81
  constructor(iterator: IterableIterator<ValueError>);
package/errors/errors.js CHANGED
@@ -27,7 +27,7 @@ THE SOFTWARE.
27
27
 
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.Errors = exports.ValueErrorIterator = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
30
+ exports.Errors = exports.ValueErrorIterator = exports.EscapeKey = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
31
31
  const guard_1 = require("../value/guard");
32
32
  const system_1 = require("../system/system");
33
33
  const deref_1 = require("../value/deref");
@@ -113,6 +113,13 @@ class ValueErrorsUnknownTypeError extends Types.TypeBoxError {
113
113
  }
114
114
  exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
115
115
  // --------------------------------------------------------------------------
116
+ // EscapeKey
117
+ // --------------------------------------------------------------------------
118
+ function EscapeKey(key) {
119
+ return key.replace(/~/g, '~0').replace(/\//g, '~1'); // RFC6901 Path
120
+ }
121
+ exports.EscapeKey = EscapeKey;
122
+ // --------------------------------------------------------------------------
116
123
  // Guards
117
124
  // --------------------------------------------------------------------------
118
125
  function IsDefined(value) {
@@ -338,12 +345,12 @@ function* TObject(schema, references, path, value) {
338
345
  for (const requiredKey of requiredKeys) {
339
346
  if (unknownKeys.includes(requiredKey))
340
347
  continue;
341
- yield Create(ValueErrorType.ObjectRequiredProperty, schema.properties[requiredKey], `${path}/${requiredKey}`, undefined);
348
+ yield Create(ValueErrorType.ObjectRequiredProperty, schema.properties[requiredKey], `${path}/${EscapeKey(requiredKey)}`, undefined);
342
349
  }
343
350
  if (schema.additionalProperties === false) {
344
351
  for (const valueKey of unknownKeys) {
345
352
  if (!knownKeys.includes(valueKey)) {
346
- yield Create(ValueErrorType.ObjectAdditionalProperties, schema, `${path}/${valueKey}`, value[valueKey]);
353
+ yield Create(ValueErrorType.ObjectAdditionalProperties, schema, `${path}/${EscapeKey(valueKey)}`, value[valueKey]);
347
354
  }
348
355
  }
349
356
  }
@@ -351,20 +358,20 @@ function* TObject(schema, references, path, value) {
351
358
  for (const valueKey of unknownKeys) {
352
359
  if (knownKeys.includes(valueKey))
353
360
  continue;
354
- yield* Visit(schema.additionalProperties, references, `${path}/${valueKey}`, value[valueKey]);
361
+ yield* Visit(schema.additionalProperties, references, `${path}/${EscapeKey(valueKey)}`, value[valueKey]);
355
362
  }
356
363
  }
357
364
  for (const knownKey of knownKeys) {
358
365
  const property = schema.properties[knownKey];
359
366
  if (schema.required && schema.required.includes(knownKey)) {
360
- yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
367
+ yield* Visit(property, references, `${path}/${EscapeKey(knownKey)}`, value[knownKey]);
361
368
  if (Types.ExtendsUndefined.Check(schema) && !(knownKey in value)) {
362
- yield Create(ValueErrorType.ObjectRequiredProperty, property, `${path}/${knownKey}`, undefined);
369
+ yield Create(ValueErrorType.ObjectRequiredProperty, property, `${path}/${EscapeKey(knownKey)}`, undefined);
363
370
  }
364
371
  }
365
372
  else {
366
373
  if (system_1.TypeSystemPolicy.IsExactOptionalProperty(value, knownKey)) {
367
- yield* Visit(property, references, `${path}/${knownKey}`, value[knownKey]);
374
+ yield* Visit(property, references, `${path}/${EscapeKey(knownKey)}`, value[knownKey]);
368
375
  }
369
376
  }
370
377
  }
@@ -386,19 +393,19 @@ function* TRecord(schema, references, path, value) {
386
393
  const regex = new RegExp(patternKey);
387
394
  for (const [propertyKey, propertyValue] of Object.entries(value)) {
388
395
  if (regex.test(propertyKey))
389
- yield* Visit(patternSchema, references, `${path}/${propertyKey}`, propertyValue);
396
+ yield* Visit(patternSchema, references, `${path}/${EscapeKey(propertyKey)}`, propertyValue);
390
397
  }
391
398
  if (typeof schema.additionalProperties === 'object') {
392
399
  for (const [propertyKey, propertyValue] of Object.entries(value)) {
393
400
  if (!regex.test(propertyKey))
394
- yield* Visit(schema.additionalProperties, references, `${path}/${propertyKey}`, propertyValue);
401
+ yield* Visit(schema.additionalProperties, references, `${path}/${EscapeKey(propertyKey)}`, propertyValue);
395
402
  }
396
403
  }
397
404
  if (schema.additionalProperties === false) {
398
405
  for (const [propertyKey, propertyValue] of Object.entries(value)) {
399
406
  if (regex.test(propertyKey))
400
407
  continue;
401
- return yield Create(ValueErrorType.ObjectAdditionalProperties, schema, `${path}/${propertyKey}`, propertyValue);
408
+ return yield Create(ValueErrorType.ObjectAdditionalProperties, schema, `${path}/${EscapeKey(propertyKey)}`, propertyValue);
402
409
  }
403
410
  }
404
411
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.31.18",
3
+ "version": "0.31.20",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/typebox.d.ts CHANGED
@@ -337,6 +337,7 @@ export type TRecordFromUnionLiteralString<K extends TLiteralString, T extends TS
337
337
  export type TRecordFromUnionLiteralNumber<K extends TLiteralNumber, T extends TSchema> = {
338
338
  [_ in K['const']]: T;
339
339
  };
340
+ export type TRecordFromEnumKey<K extends TEnum, T extends TSchema> = Ensure<TRecord<K, T>>;
340
341
  export type TRecordFromUnionRest<K extends TSchema[], T extends TSchema> = K extends [infer L, ...infer R] ? (L extends TUnion<infer S> ? TRecordFromUnionRest<S, T> & TRecordFromUnionRest<AssertRest<R>, T> : L extends TLiteralString ? TRecordFromUnionLiteralString<L, T> & TRecordFromUnionRest<AssertRest<R>, T> : L extends TLiteralNumber ? TRecordFromUnionLiteralNumber<L, T> & TRecordFromUnionRest<AssertRest<R>, T> : {}) : {};
341
342
  export type TRecordFromUnion<K extends TSchema[], T extends TSchema> = Ensure<TObject<AssertProperties<Evaluate<TRecordFromUnionRest<K, T>>>>>;
342
343
  export type TRecordFromTemplateLiteralKeyInfinite<K extends TTemplateLiteral, T extends TSchema> = Ensure<TRecord<K, T>>;
@@ -353,7 +354,7 @@ export type TRecordFromLiteralNumberKey<K extends TLiteralNumber, T extends TSch
353
354
  export type TRecordFromStringKey<K extends TString, T extends TSchema> = Ensure<TRecord<K, T>>;
354
355
  export type TRecordFromNumberKey<K extends TNumber, T extends TSchema> = Ensure<TRecord<K, T>>;
355
356
  export type TRecordFromIntegerKey<K extends TInteger, T extends TSchema> = Ensure<TRecord<K, T>>;
356
- export type TRecordResolve<K extends TSchema, T extends TSchema> = K extends TUnion<infer S> ? TRecordFromUnion<S, T> : K extends TTemplateLiteral ? TRecordFromTemplateLiteralKey<K, T> : K extends TLiteralString ? TRecordFromLiteralStringKey<K, T> : K extends TLiteralNumber ? TRecordFromLiteralNumberKey<K, T> : K extends TString ? TRecordFromStringKey<K, T> : K extends TNumber ? TRecordFromNumberKey<K, T> : K extends TInteger ? TRecordFromIntegerKey<K, T> : TNever;
357
+ export type TRecordResolve<K extends TSchema, T extends TSchema> = K extends TEnum<infer _> ? TRecordFromEnumKey<K, T> : K extends TUnion<infer S> ? TRecordFromUnion<S, T> : K extends TTemplateLiteral ? TRecordFromTemplateLiteralKey<K, T> : K extends TLiteralString ? TRecordFromLiteralStringKey<K, T> : K extends TLiteralNumber ? TRecordFromLiteralNumberKey<K, T> : K extends TString ? TRecordFromStringKey<K, T> : K extends TNumber ? TRecordFromNumberKey<K, T> : K extends TInteger ? TRecordFromIntegerKey<K, T> : TNever;
357
358
  export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchema> extends TSchema {
358
359
  [Kind]: 'Record';
359
360
  static: Record<Assert<Static<K>, string | number>, Static<T, this['params']>>;