@sinclair/typebox 0.26.8 → 0.27.1

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.
@@ -363,10 +363,6 @@ var TypeCompiler;
363
363
  return yield `${CreateFunctionName(schema.$ref)}(${value})`;
364
364
  yield* Visit(target, references, value);
365
365
  }
366
- function* Self(schema, references, value) {
367
- const func = CreateFunctionName(schema.$ref);
368
- yield `${func}(${value})`;
369
- }
370
366
  function* String(schema, references, value) {
371
367
  yield `(typeof ${value} === 'string')`;
372
368
  if (IsNumber(schema.minLength))
@@ -384,6 +380,15 @@ var TypeCompiler;
384
380
  function* Symbol(schema, references, value) {
385
381
  yield `(typeof ${value} === 'symbol')`;
386
382
  }
383
+ function* TemplateLiteral(schema, references, value) {
384
+ yield `(typeof ${value} === 'string')`;
385
+ const local = PushLocal(`${new RegExp(schema.pattern)};`);
386
+ yield `${local}.test(${value})`;
387
+ }
388
+ function* This(schema, references, value) {
389
+ const func = CreateFunctionName(schema.$ref);
390
+ yield `${func}(${value})`;
391
+ }
387
392
  function* Tuple(schema, references, value) {
388
393
  yield `(Array.isArray(${value}))`;
389
394
  if (schema.items === undefined)
@@ -470,12 +475,14 @@ var TypeCompiler;
470
475
  return yield* Record(schema_, references_, value);
471
476
  case 'Ref':
472
477
  return yield* Ref(schema_, references_, value);
473
- case 'Self':
474
- return yield* Self(schema_, references_, value);
475
478
  case 'String':
476
479
  return yield* String(schema_, references_, value);
477
480
  case 'Symbol':
478
481
  return yield* Symbol(schema_, references_, value);
482
+ case 'TemplateLiteral':
483
+ return yield* TemplateLiteral(schema_, references_, value);
484
+ case 'This':
485
+ return yield* This(schema_, references_, value);
479
486
  case 'Tuple':
480
487
  return yield* Tuple(schema_, references_, value);
481
488
  case 'Undefined':
@@ -79,8 +79,8 @@ export declare class ValueErrorsUnknownTypeError extends Error {
79
79
  constructor(schema: Types.TSchema);
80
80
  }
81
81
  export declare class ValueErrorsDereferenceError extends Error {
82
- readonly schema: Types.TRef | Types.TSelf;
83
- constructor(schema: Types.TRef | Types.TSelf);
82
+ readonly schema: Types.TRef | Types.TThis;
83
+ constructor(schema: Types.TRef | Types.TThis);
84
84
  }
85
85
  /** Provides functionality to generate a sequence of errors against a TypeBox type. */
86
86
  export declare namespace ValueErrors {
package/errors/errors.js CHANGED
@@ -410,7 +410,7 @@ var ValueErrors;
410
410
  const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
411
411
  const regex = new RegExp(keyPattern);
412
412
  if (!globalThis.Object.getOwnPropertyNames(value).every((key) => regex.test(key))) {
413
- const numeric = keyPattern === '^(0|[1-9][0-9]*)$';
413
+ const numeric = keyPattern === Types.PatternNumberExact;
414
414
  const type = numeric ? ValueErrorType.RecordKeyNumeric : ValueErrorType.RecordKeyString;
415
415
  const message = numeric ? 'Expected all object property keys to be numeric' : 'Expected all object property keys to be strings';
416
416
  return yield { type, schema, path, value, message };
@@ -426,13 +426,6 @@ var ValueErrors;
426
426
  const target = references[index];
427
427
  yield* Visit(target, references, path, value);
428
428
  }
429
- function* Self(schema, references, path, value) {
430
- const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
431
- if (index === -1)
432
- throw new ValueErrorsDereferenceError(schema);
433
- const target = references[index];
434
- yield* Visit(target, references, path, value);
435
- }
436
429
  function* String(schema, references, path, value) {
437
430
  if (!IsString(value)) {
438
431
  return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
@@ -466,6 +459,22 @@ var ValueErrors;
466
459
  return yield { type: ValueErrorType.Symbol, schema, path, value, message: 'Expected symbol' };
467
460
  }
468
461
  }
462
+ function* TemplateLiteral(schema, references, path, value) {
463
+ if (!IsString(value)) {
464
+ return yield { type: ValueErrorType.String, schema, path, value, message: 'Expected string' };
465
+ }
466
+ const regex = new RegExp(schema.pattern);
467
+ if (!regex.test(value)) {
468
+ yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
469
+ }
470
+ }
471
+ function* This(schema, references, path, value) {
472
+ const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
473
+ if (index === -1)
474
+ throw new ValueErrorsDereferenceError(schema);
475
+ const target = references[index];
476
+ yield* Visit(target, references, path, value);
477
+ }
469
478
  function* Tuple(schema, references, path, value) {
470
479
  if (!globalThis.Array.isArray(value)) {
471
480
  return yield { type: ValueErrorType.Array, schema, path, value, message: 'Expected Array' };
@@ -566,12 +575,14 @@ var ValueErrors;
566
575
  return yield* Record(schema_, references_, path, value);
567
576
  case 'Ref':
568
577
  return yield* Ref(schema_, references_, path, value);
569
- case 'Self':
570
- return yield* Self(schema_, references_, path, value);
571
578
  case 'String':
572
579
  return yield* String(schema_, references_, path, value);
573
580
  case 'Symbol':
574
581
  return yield* Symbol(schema_, references_, path, value);
582
+ case 'TemplateLiteral':
583
+ return yield* TemplateLiteral(schema_, references_, path, value);
584
+ case 'This':
585
+ return yield* This(schema_, references_, path, value);
575
586
  case 'Tuple':
576
587
  return yield* Tuple(schema_, references_, path, value);
577
588
  case 'Undefined':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.26.8",
3
+ "version": "0.27.1",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
@@ -37,7 +37,7 @@
37
37
  "@types/chai": "^4.3.3",
38
38
  "@types/mocha": "^9.1.1",
39
39
  "@types/node": "^18.11.9",
40
- "ajv": "^8.11.2",
40
+ "ajv": "^8.12.0",
41
41
  "ajv-formats": "^2.1.1",
42
42
  "chai": "^4.3.6",
43
43
  "mocha": "^9.2.2",