typebox 1.0.4 → 1.0.6

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 (39) hide show
  1. package/build/guard/guard.d.mts +2 -0
  2. package/build/guard/guard.mjs +14 -0
  3. package/build/schema/engine/_refine.mjs +1 -1
  4. package/build/schema/engine/_standard.mjs +1 -1
  5. package/build/schema/engine/additionalProperties.mjs +1 -1
  6. package/build/schema/engine/anyOf.mjs +1 -1
  7. package/build/schema/engine/boolean.mjs +1 -1
  8. package/build/schema/engine/const.mjs +1 -1
  9. package/build/schema/engine/contains.mjs +1 -1
  10. package/build/schema/engine/dependencies.mjs +1 -1
  11. package/build/schema/engine/dependentRequired.mjs +1 -1
  12. package/build/schema/engine/enum.mjs +1 -1
  13. package/build/schema/engine/exclusiveMaximum.mjs +1 -1
  14. package/build/schema/engine/exclusiveMinimum.mjs +1 -1
  15. package/build/schema/engine/format.mjs +1 -1
  16. package/build/schema/engine/if.mjs +2 -2
  17. package/build/schema/engine/maxContains.mjs +1 -1
  18. package/build/schema/engine/maxItems.mjs +1 -1
  19. package/build/schema/engine/maxLength.mjs +1 -1
  20. package/build/schema/engine/maxProperties.mjs +1 -1
  21. package/build/schema/engine/maximum.mjs +1 -1
  22. package/build/schema/engine/minContains.mjs +1 -1
  23. package/build/schema/engine/minItems.mjs +1 -1
  24. package/build/schema/engine/minLength.mjs +1 -1
  25. package/build/schema/engine/minProperties.mjs +1 -1
  26. package/build/schema/engine/minimum.mjs +1 -1
  27. package/build/schema/engine/multipleOf.mjs +1 -1
  28. package/build/schema/engine/not.mjs +1 -1
  29. package/build/schema/engine/oneOf.mjs +1 -1
  30. package/build/schema/engine/pattern.mjs +1 -1
  31. package/build/schema/engine/propertyNames.mjs +2 -2
  32. package/build/schema/engine/required.mjs +1 -1
  33. package/build/schema/engine/type.mjs +1 -1
  34. package/build/schema/engine/unevaluatedItems.mjs +1 -1
  35. package/build/schema/engine/unevaluatedProperties.mjs +1 -1
  36. package/build/schema/engine/uniqueItems.mjs +1 -1
  37. package/build/value/clone/clone.mjs +23 -2
  38. package/package.json +1 -1
  39. package/readme.md +1 -1
@@ -34,6 +34,8 @@ export declare function IsLessThan<Type extends number | bigint>(left: Type, rig
34
34
  export declare function IsLessEqualThan<Type extends number | bigint>(left: Type, right: Type): boolean;
35
35
  export declare function IsGreaterEqualThan<Type extends number | bigint>(left: Type, right: Type): boolean;
36
36
  export declare function IsMultipleOf(dividend: bigint | number, divisor: bigint | number): boolean;
37
+ /** Returns true if the value appears to be an instance of a class. */
38
+ export declare function IsClassInstance(value: unknown): boolean;
37
39
  export declare function IsValueLike(value: unknown): value is bigint | boolean | null | number | string | undefined;
38
40
  /** Returns the number of Unicode Grapheme Clusters */
39
41
  export declare function StringGraphemeCount(value: string): number;
@@ -102,6 +102,20 @@ export function IsMultipleOf(dividend, divisor) {
102
102
  return Math.min(Math.abs(mod), Math.abs(mod - divisor)) < tolerance;
103
103
  }
104
104
  // ------------------------------------------------------------------
105
+ // IsClassInstance
106
+ // ------------------------------------------------------------------
107
+ /** Returns true if the value appears to be an instance of a class. */
108
+ export function IsClassInstance(value) {
109
+ if (!IsObject(value))
110
+ return false;
111
+ const proto = globalThis.Object.getPrototypeOf(value);
112
+ if (IsNull(proto))
113
+ return false;
114
+ return IsEqual(typeof proto.constructor, 'function') &&
115
+ !(IsEqual(proto.constructor, globalThis.Object) ||
116
+ IsEqual(proto.constructor.name, 'Object'));
117
+ }
118
+ // ------------------------------------------------------------------
105
119
  // IsValueLike
106
120
  // ------------------------------------------------------------------
107
121
  export function IsValueLike(value) {
@@ -21,7 +21,7 @@ export function ErrorRefine(context, schemaPath, instancePath, schema, value) {
21
21
  return G.EveryAll(schema['~refine'], (refinement, index) => {
22
22
  return refinement.callback(value) || context.AddError({
23
23
  keyword: '~refine',
24
- schemaPath: `${schemaPath}/~refine`,
24
+ schemaPath,
25
25
  instancePath,
26
26
  params: { index, message: refinement.message },
27
27
  });
@@ -20,7 +20,7 @@ export function ErrorStandardSchemaV1(context, schemaPath, instancePath, schema,
20
20
  const result = schema['~standard'].validate(value);
21
21
  return !G.HasPropertyKey(result, 'issues') || context.AddError({
22
22
  keyword: '~standard',
23
- schemaPath: `${schemaPath}/~standard`,
23
+ schemaPath,
24
24
  instancePath,
25
25
  params: { vendor: schema[`~standard`].vendor, issues: result.issues },
26
26
  });
@@ -96,7 +96,7 @@ export function ErrorAdditionalProperties(context, schemaPath, instancePath, sch
96
96
  });
97
97
  return isAdditionalProperties || context.AddError({
98
98
  keyword: 'additionalProperties',
99
- schemaPath: `${schemaPath}/additionalProperties`,
99
+ schemaPath,
100
100
  instancePath: instancePath,
101
101
  params: { additionalProperties },
102
102
  });
@@ -45,7 +45,7 @@ export function ErrorAnyOf(context, schemaPath, instancePath, schema, value) {
45
45
  failedContexts.forEach(failed => failed.GetErrors().forEach(error => context.AddError(error)));
46
46
  return isAnyOf || context.AddError({
47
47
  keyword: 'anyOf',
48
- schemaPath: `${schemaPath}/anyOf`,
48
+ schemaPath,
49
49
  instancePath,
50
50
  params: {}
51
51
  });
@@ -18,7 +18,7 @@ export function CheckBooleanSchema(context, schema, value) {
18
18
  export function ErrorBooleanSchema(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckBooleanSchema(context, schema, value) || context.AddError({
20
20
  keyword: 'boolean',
21
- schemaPath: `${schemaPath}/${schema}`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: {}
24
24
  });
@@ -23,7 +23,7 @@ export function CheckConst(context, schema, value) {
23
23
  export function ErrorConst(context, schemaPath, instancePath, schema, value) {
24
24
  return CheckConst(context, schema, value) || context.AddError({
25
25
  keyword: 'const',
26
- schemaPath: `${schemaPath}/const`,
26
+ schemaPath,
27
27
  instancePath,
28
28
  params: { allowedValue: schema.const },
29
29
  });
@@ -33,7 +33,7 @@ export function CheckContains(context, schema, value) {
33
33
  export function ErrorContains(context, schemaPath, instancePath, schema, value) {
34
34
  return CheckContains(context, schema, value) || context.AddError({
35
35
  keyword: 'contains',
36
- schemaPath: `${schemaPath}/contains`,
36
+ schemaPath,
37
37
  instancePath,
38
38
  params: { minContains: 1 },
39
39
  });
@@ -36,7 +36,7 @@ export function ErrorDependencies(context, schemaPath, instancePath, schema, val
36
36
  return !G.HasPropertyKey(value, key) || (G.IsArray(schema)
37
37
  ? schema.every((dependency) => G.HasPropertyKey(value, dependency) || context.AddError({
38
38
  keyword: 'dependencies',
39
- schemaPath: `${schemaPath}/dependencies`,
39
+ schemaPath,
40
40
  instancePath,
41
41
  params: { property: key, dependencies: schema },
42
42
  })) : ErrorSchema(context, nextSchemaPath, instancePath, schema, value));
@@ -31,7 +31,7 @@ export function ErrorDependentRequired(context, schemaPath, instancePath, schema
31
31
  const isEveryEntry = G.EveryAll(G.Entries(schema.dependentRequired), ([key, keys]) => {
32
32
  return !G.HasPropertyKey(value, key) || G.EveryAll(keys, (dependency) => G.HasPropertyKey(value, dependency) || context.AddError({
33
33
  keyword: 'dependentRequired',
34
- schemaPath: `${schemaPath}/dependentRequired`,
34
+ schemaPath,
35
35
  instancePath,
36
36
  params: { property: key, dependencies: keys },
37
37
  }));
@@ -26,7 +26,7 @@ export function CheckEnum(context, schema, value) {
26
26
  export function ErrorEnum(context, schemaPath, instancePath, schema, value) {
27
27
  return CheckEnum(context, schema, value) || context.AddError({
28
28
  keyword: 'enum',
29
- schemaPath: `${schemaPath}/enum`,
29
+ schemaPath,
30
30
  instancePath,
31
31
  params: { allowedValues: schema.enum }
32
32
  });
@@ -18,7 +18,7 @@ export function CheckExclusiveMaximum(context, schema, value) {
18
18
  export function ErrorExclusiveMaximum(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckExclusiveMaximum(context, schema, value) || context.AddError({
20
20
  keyword: 'exclusiveMaximum',
21
- schemaPath: `${schemaPath}/exclusiveMaximum`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { comparison: '<', limit: schema.exclusiveMaximum }
24
24
  });
@@ -18,7 +18,7 @@ export function CheckExclusiveMinimum(context, schema, value) {
18
18
  export function ErrorExclusiveMinimum(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckExclusiveMinimum(context, schema, value) || context.AddError({
20
20
  keyword: 'exclusiveMinimum',
21
- schemaPath: `${schemaPath}/exclusiveMinimum`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { comparison: '>', limit: schema.exclusiveMinimum }
24
24
  });
@@ -19,7 +19,7 @@ export function CheckFormat(context, schema, value) {
19
19
  export function ErrorFormat(context, schemaPath, instancePath, schema, value) {
20
20
  return CheckFormat(context, schema, value) || context.AddError({
21
21
  keyword: 'format',
22
- schemaPath: `${schemaPath}/format`,
22
+ schemaPath,
23
23
  instancePath,
24
24
  params: { format: schema.format },
25
25
  });
@@ -31,13 +31,13 @@ export function ErrorIf(context, schemaPath, instancePath, schema, value) {
31
31
  const isIf = ErrorSchema(trueContext, `${schemaPath}/if`, instancePath, schema.if, value)
32
32
  ? ErrorSchema(trueContext, `${schemaPath}/then`, instancePath, thenSchema, value) || context.AddError({
33
33
  keyword: 'if',
34
- schemaPath: `${schemaPath}/if`,
34
+ schemaPath,
35
35
  instancePath,
36
36
  params: { failingKeyword: 'then' },
37
37
  })
38
38
  : ErrorSchema(context, `${schemaPath}/else`, instancePath, elseSchema, value) || context.AddError({
39
39
  keyword: 'if',
40
- schemaPath: `${schemaPath}/if`,
40
+ schemaPath,
41
41
  instancePath,
42
42
  params: { failingKeyword: 'else' },
43
43
  });
@@ -33,7 +33,7 @@ export function ErrorMaxContains(context, schemaPath, instancePath, schema, valu
33
33
  const minContains = S.IsMinContains(schema) ? schema.minContains : 1;
34
34
  return CheckMaxContains(context, schema, value) || context.AddError({
35
35
  keyword: 'contains',
36
- schemaPath: `${schemaPath}/contains`,
36
+ schemaPath,
37
37
  instancePath,
38
38
  params: { minContains, maxContains: schema.maxContains },
39
39
  });
@@ -18,7 +18,7 @@ export function CheckMaxItems(context, schema, value) {
18
18
  export function ErrorMaxItems(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMaxItems(context, schema, value) || context.AddError({
20
20
  keyword: 'maxItems',
21
- schemaPath: `${schemaPath}/maxItems`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { limit: schema.maxItems }
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMaxLength(context, schema, value) {
18
18
  export function ErrorMaxLength(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMaxLength(context, schema, value) || context.AddError({
20
20
  keyword: 'maxLength',
21
- schemaPath: `${schemaPath}/maxLength`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { limit: schema.maxLength }
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMaxProperties(context, schema, value) {
18
18
  export function ErrorMaxProperties(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMaxProperties(context, schema, value) || context.AddError({
20
20
  keyword: 'maxProperties',
21
- schemaPath: `${schemaPath}/maxProperties`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { limit: schema.maxProperties },
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMaximum(context, schema, value) {
18
18
  export function ErrorMaximum(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMaximum(context, schema, value) || context.AddError({
20
20
  keyword: 'maximum',
21
- schemaPath: `${schemaPath}/maximum`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { comparison: '<=', limit: schema.maximum }
24
24
  });
@@ -32,7 +32,7 @@ export function CheckMinContains(context, schema, value) {
32
32
  export function ErrorMinContains(context, schemaPath, instancePath, schema, value) {
33
33
  return CheckMinContains(context, schema, value) || context.AddError({
34
34
  keyword: 'contains',
35
- schemaPath: `${schemaPath}/contains`,
35
+ schemaPath,
36
36
  instancePath,
37
37
  params: { minContains: schema.minContains }
38
38
  });
@@ -18,7 +18,7 @@ export function CheckMinItems(context, schema, value) {
18
18
  export function ErrorMinItems(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMinItems(context, schema, value) || context.AddError({
20
20
  keyword: 'minItems',
21
- schemaPath: `${schemaPath}/minItems`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { limit: schema.minItems }
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMinLength(context, schema, value) {
18
18
  export function ErrorMinLength(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMinLength(context, schema, value) || context.AddError({
20
20
  keyword: 'minLength',
21
- schemaPath: `${schemaPath}/minLength`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { limit: schema.minLength }
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMinProperties(context, schema, value) {
18
18
  export function ErrorMinProperties(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMinProperties(context, schema, value) || context.AddError({
20
20
  keyword: 'minProperties',
21
- schemaPath: `${schemaPath}/minProperties`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { limit: schema.minProperties },
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMinimum(context, schema, value) {
18
18
  export function ErrorMinimum(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMinimum(context, schema, value) || context.AddError({
20
20
  keyword: 'minimum',
21
- schemaPath: `${schemaPath}/minimum`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { comparison: '>=', limit: schema.minimum }
24
24
  });
@@ -18,7 +18,7 @@ export function CheckMultipleOf(context, schema, value) {
18
18
  export function ErrorMultipleOf(context, schemaPath, instancePath, schema, value) {
19
19
  return CheckMultipleOf(context, schema, value) || context.AddError({
20
20
  keyword: 'multipleOf',
21
- schemaPath: `${schemaPath}/multipleOf`,
21
+ schemaPath,
22
22
  instancePath,
23
23
  params: { multipleOf: schema.multipleOf }
24
24
  });
@@ -29,7 +29,7 @@ export function CheckNot(context, schema, value) {
29
29
  export function ErrorNot(context, schemaPath, instancePath, schema, value) {
30
30
  return CheckNot(context, schema, value) || context.AddError({
31
31
  keyword: 'not',
32
- schemaPath: `${schemaPath}/not`,
32
+ schemaPath,
33
33
  instancePath,
34
34
  params: {},
35
35
  });
@@ -51,7 +51,7 @@ export function ErrorOneOf(context, schemaPath, instancePath, schema, value) {
51
51
  failedContexts.forEach(failed => failed.GetErrors().forEach(error => context.AddError(error)));
52
52
  return isOneOf || context.AddError({
53
53
  keyword: 'oneOf',
54
- schemaPath: `${schemaPath}/oneOf`,
54
+ schemaPath,
55
55
  instancePath,
56
56
  params: { passingSchemas },
57
57
  });
@@ -21,7 +21,7 @@ export function CheckPattern(context, schema, value) {
21
21
  export function ErrorPattern(context, schemaPath, instancePath, schema, value) {
22
22
  return CheckPattern(context, schema, value) || context.AddError({
23
23
  keyword: 'pattern',
24
- schemaPath: `${schemaPath}/pattern`,
24
+ schemaPath,
25
25
  instancePath,
26
26
  params: { pattern: schema.pattern }
27
27
  });
@@ -30,8 +30,8 @@ export function ErrorPropertyNames(context, schemaPath, instancePath, schema, va
30
30
  });
31
31
  return isPropertyNames || context.AddError({
32
32
  keyword: 'propertyNames',
33
- schemaPath: `${schemaPath}/propertyNames`,
34
- instancePath: instancePath,
33
+ schemaPath,
34
+ instancePath,
35
35
  params: { propertyNames }
36
36
  });
37
37
  }
@@ -25,7 +25,7 @@ export function ErrorRequired(context, schemaPath, instancePath, schema, value)
25
25
  });
26
26
  return isRequired || context.AddError({
27
27
  keyword: 'required',
28
- schemaPath: `${schemaPath}/required`,
28
+ schemaPath,
29
29
  instancePath,
30
30
  params: { requiredProperties }
31
31
  });
@@ -67,7 +67,7 @@ export function ErrorType(context, schemaPath, instancePath, schema, value) {
67
67
  const isType = G.IsArray(schema.type) ? CheckTypeNames(context, schema.type, schema, value) : CheckTypeName(context, schema.type, schema, value);
68
68
  return isType || context.AddError({
69
69
  keyword: 'type',
70
- schemaPath: `${schemaPath}/type`,
70
+ schemaPath,
71
71
  instancePath,
72
72
  params: { type: schema.type }
73
73
  });
@@ -42,7 +42,7 @@ export function ErrorUnevaluatedItems(context, schemaPath, instancePath, schema,
42
42
  });
43
43
  return isUnevaluatedItems || context.AddError({
44
44
  keyword: 'unevaluatedItems',
45
- schemaPath: `${schemaPath}/unevaluatedItems`,
45
+ schemaPath,
46
46
  instancePath,
47
47
  params: { unevaluatedItems }
48
48
  });
@@ -42,7 +42,7 @@ export function ErrorUnevaluatedProperties(context, schemaPath, instancePath, sc
42
42
  });
43
43
  return isUnevaluatedProperties || context.AddError({
44
44
  keyword: 'unevaluatedProperties',
45
- schemaPath: `${schemaPath}/unevaluatedProperties`,
45
+ schemaPath,
46
46
  instancePath,
47
47
  params: { unevaluatedProperties }
48
48
  });
@@ -44,7 +44,7 @@ export function ErrorUniqueItems(context, schemaPath, instancePath, schema, valu
44
44
  const isUniqueItems = G.IsEqual(duplicateItems.length, 0);
45
45
  return isUniqueItems || context.AddError({
46
46
  keyword: 'uniqueItems',
47
- schemaPath: `${schemaPath}/uniqueItems`,
47
+ schemaPath,
48
48
  instancePath,
49
49
  params: { duplicateItems },
50
50
  });
@@ -1,9 +1,22 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Guard, GlobalsGuard } from '../../guard/index.mjs';
3
3
  // ------------------------------------------------------------------
4
- // Object
4
+ // ClassInstance
5
+ //
6
+ // TypeBox does not support cloning arbitrary class instances. It treats
7
+ // class instances as atomic values, similar to number, boolean, and
8
+ // string. In the future, an implementation could detect the presence of
9
+ // a .clone() method, but no formal specification for this behavior
10
+ // exists, so we don't.
11
+ //
5
12
  // ------------------------------------------------------------------
6
- function FromObject(value) {
13
+ function FromClassInstance(value) {
14
+ return value; // atomic
15
+ }
16
+ // ------------------------------------------------------------------
17
+ // ObjectInstance
18
+ // ------------------------------------------------------------------
19
+ function FromObjectInstance(value) {
7
20
  const result = {};
8
21
  for (const key of Object.getOwnPropertyNames(value)) {
9
22
  result[key] = Clone(value[key]);
@@ -14,6 +27,14 @@ function FromObject(value) {
14
27
  return result;
15
28
  }
16
29
  // ------------------------------------------------------------------
30
+ // Object
31
+ // ------------------------------------------------------------------
32
+ function FromObject(value) {
33
+ return (Guard.IsClassInstance(value)
34
+ ? FromClassInstance(value)
35
+ : FromObjectInstance(value));
36
+ }
37
+ // ------------------------------------------------------------------
17
38
  // Array
18
39
  // ------------------------------------------------------------------
19
40
  function FromArray(value) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "A Runtime Type System for JavaScript",
4
- "version": "1.0.4",
4
+ "version": "1.0.6",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
package/readme.md CHANGED
@@ -49,7 +49,7 @@ type T = Static<typeof T> // type T = {
49
49
 
50
50
  ## Overview
51
51
 
52
- [Documentation](https://sinclairzx81.github.io/typebox/)
52
+ [Documentation](https://sinclairzx81.github.io/typebox/) | [1.0 Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
53
53
 
54
54
  TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard Json Schema.
55
55