@sinclair/typebox 0.33.4 → 0.33.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.
@@ -16,14 +16,14 @@ export declare namespace TypeSystemPolicy {
16
16
  let AllowNaN: boolean;
17
17
  /** Sets whether `null` should validate for void types. The default is `false` */
18
18
  let AllowNullVoid: boolean;
19
- /** Asserts this value using the ExactOptionalPropertyTypes policy */
19
+ /** Checks this value using the ExactOptionalPropertyTypes policy */
20
20
  function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
21
- /** Asserts this value using the AllowArrayObjects policy */
21
+ /** Checks this value using the AllowArrayObjects policy */
22
22
  function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
23
- /** Asserts this value as a record using the AllowArrayObjects policy */
23
+ /** Checks this value as a record using the AllowArrayObjects policy */
24
24
  function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
25
- /** Asserts this value using the AllowNaN policy */
25
+ /** Checks this value using the AllowNaN policy */
26
26
  function IsNumberLike(value: unknown): value is number;
27
- /** Asserts this value using the AllowVoidNull policy */
27
+ /** Checks this value using the AllowVoidNull policy */
28
28
  function IsVoidLike(value: unknown): value is void;
29
29
  }
@@ -28,28 +28,28 @@ var TypeSystemPolicy;
28
28
  TypeSystemPolicy.AllowNaN = false;
29
29
  /** Sets whether `null` should validate for void types. The default is `false` */
30
30
  TypeSystemPolicy.AllowNullVoid = false;
31
- /** Asserts this value using the ExactOptionalPropertyTypes policy */
31
+ /** Checks this value using the ExactOptionalPropertyTypes policy */
32
32
  function IsExactOptionalProperty(value, key) {
33
33
  return TypeSystemPolicy.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
34
34
  }
35
35
  TypeSystemPolicy.IsExactOptionalProperty = IsExactOptionalProperty;
36
- /** Asserts this value using the AllowArrayObjects policy */
36
+ /** Checks this value using the AllowArrayObjects policy */
37
37
  function IsObjectLike(value) {
38
38
  const isObject = (0, index_1.IsObject)(value);
39
39
  return TypeSystemPolicy.AllowArrayObject ? isObject : isObject && !(0, index_1.IsArray)(value);
40
40
  }
41
41
  TypeSystemPolicy.IsObjectLike = IsObjectLike;
42
- /** Asserts this value as a record using the AllowArrayObjects policy */
42
+ /** Checks this value as a record using the AllowArrayObjects policy */
43
43
  function IsRecordLike(value) {
44
44
  return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
45
45
  }
46
46
  TypeSystemPolicy.IsRecordLike = IsRecordLike;
47
- /** Asserts this value using the AllowNaN policy */
47
+ /** Checks this value using the AllowNaN policy */
48
48
  function IsNumberLike(value) {
49
49
  return TypeSystemPolicy.AllowNaN ? (0, index_1.IsNumber)(value) : Number.isFinite(value);
50
50
  }
51
51
  TypeSystemPolicy.IsNumberLike = IsNumberLike;
52
- /** Asserts this value using the AllowVoidNull policy */
52
+ /** Checks this value using the AllowVoidNull policy */
53
53
  function IsVoidLike(value) {
54
54
  const isUndefined = (0, index_1.IsUndefined)(value);
55
55
  return TypeSystemPolicy.AllowNullVoid ? isUndefined || value === null : isUndefined;
@@ -145,8 +145,10 @@ function FromNumber(schema, references, value) {
145
145
  function FromObject(schema, references, value) {
146
146
  if (!(0, index_5.IsObject)(value))
147
147
  return value;
148
- for (const key of Object.getOwnPropertyNames(schema.properties)) {
149
- value[key] = Visit(schema.properties[key], references, value[key]);
148
+ for (const propertyKey of Object.getOwnPropertyNames(schema.properties)) {
149
+ if (!(0, index_5.HasPropertyKey)(value, propertyKey))
150
+ continue;
151
+ value[propertyKey] = Visit(schema.properties[propertyKey], references, value[propertyKey]);
150
152
  }
151
153
  return value;
152
154
  }
@@ -49,22 +49,25 @@ function FromObject(schema, references, value) {
49
49
  const defaulted = ValueOrDefault(schema, value);
50
50
  if (!(0, index_5.IsObject)(defaulted))
51
51
  return defaulted;
52
- const additionalPropertiesSchema = schema.additionalProperties;
53
52
  const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
54
53
  // properties
55
54
  for (const key of knownPropertyKeys) {
56
- if (!HasDefaultProperty(schema.properties[key]))
55
+ // note: we need to traverse into the object and test if the return value
56
+ // yielded a non undefined result. Here we interpret an undefined result as
57
+ // a non assignable property and continue.
58
+ const propertyValue = Visit(schema.properties[key], references, defaulted[key]);
59
+ if ((0, index_5.IsUndefined)(propertyValue))
57
60
  continue;
58
- defaulted[key] = Visit(schema.properties[key], references, defaulted[key]);
61
+ defaulted[key] = propertyValue;
59
62
  }
60
63
  // return if not additional properties
61
- if (!HasDefaultProperty(additionalPropertiesSchema))
64
+ if (!HasDefaultProperty(schema.additionalProperties))
62
65
  return defaulted;
63
66
  // additional properties
64
67
  for (const key of Object.getOwnPropertyNames(defaulted)) {
65
68
  if (knownPropertyKeys.includes(key))
66
69
  continue;
67
- defaulted[key] = Visit(additionalPropertiesSchema, references, defaulted[key]);
70
+ defaulted[key] = Visit(schema.additionalProperties, references, defaulted[key]);
68
71
  }
69
72
  return defaulted;
70
73
  }
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.TransformDecodeError = exports.TransformDecodeCheckError = void 0;
5
5
  exports.TransformDecode = TransformDecode;
6
+ const policy_1 = require("../../system/policy");
6
7
  const index_1 = require("../../type/symbols/index");
7
8
  const index_2 = require("../../type/error/index");
8
9
  const index_3 = require("../../type/keyof/index");
@@ -91,10 +92,18 @@ function FromObject(schema, references, path, value) {
91
92
  return Default(schema, path, value);
92
93
  const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
93
94
  const knownProperties = { ...value };
94
- for (const key of knownKeys)
95
- if (key in knownProperties) {
96
- knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
97
- }
95
+ for (const key of knownKeys) {
96
+ if (!(0, index_6.HasPropertyKey)(knownProperties, key))
97
+ continue;
98
+ // if the property value is undefined, but the target is not, nor does it satisfy exact optional
99
+ // property policy, then we need to continue. This is a special case for optional property handling
100
+ // where a transforms wrapped in a optional modifiers should not run.
101
+ if ((0, index_6.IsUndefined)(knownProperties[key]) && (!(0, type_1.IsUndefined)(schema.properties[key]) ||
102
+ policy_1.TypeSystemPolicy.IsExactOptionalProperty(knownProperties, key)))
103
+ continue;
104
+ // decode property
105
+ knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
106
+ }
98
107
  if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
99
108
  return Default(schema, path, knownProperties);
100
109
  }
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.TransformEncodeError = exports.TransformEncodeCheckError = void 0;
5
5
  exports.TransformEncode = TransformEncode;
6
+ const policy_1 = require("../../system/policy");
6
7
  const index_1 = require("../../type/symbols/index");
7
8
  const index_2 = require("../../type/error/index");
8
9
  const index_3 = require("../../type/keyof/index");
@@ -94,10 +95,18 @@ function FromObject(schema, references, path, value) {
94
95
  return defaulted;
95
96
  const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
96
97
  const knownProperties = { ...defaulted };
97
- for (const key of knownKeys)
98
- if (key in knownProperties) {
99
- knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
100
- }
98
+ for (const key of knownKeys) {
99
+ if (!(0, index_6.HasPropertyKey)(knownProperties, key))
100
+ continue;
101
+ // if the property value is undefined, but the target is not, nor does it satisfy exact optional
102
+ // property policy, then we need to continue. This is a special case for optional property handling
103
+ // where a transforms wrapped in a optional modifiers should not run.
104
+ if ((0, index_6.IsUndefined)(knownProperties[key]) && (!(0, type_1.IsUndefined)(schema.properties[key]) ||
105
+ policy_1.TypeSystemPolicy.IsExactOptionalProperty(knownProperties, key)))
106
+ continue;
107
+ // encode property
108
+ knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
109
+ }
101
110
  if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
102
111
  return knownProperties;
103
112
  }
@@ -16,14 +16,14 @@ export declare namespace TypeSystemPolicy {
16
16
  let AllowNaN: boolean;
17
17
  /** Sets whether `null` should validate for void types. The default is `false` */
18
18
  let AllowNullVoid: boolean;
19
- /** Asserts this value using the ExactOptionalPropertyTypes policy */
19
+ /** Checks this value using the ExactOptionalPropertyTypes policy */
20
20
  function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
21
- /** Asserts this value using the AllowArrayObjects policy */
21
+ /** Checks this value using the AllowArrayObjects policy */
22
22
  function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
23
- /** Asserts this value as a record using the AllowArrayObjects policy */
23
+ /** Checks this value as a record using the AllowArrayObjects policy */
24
24
  function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
25
- /** Asserts this value using the AllowNaN policy */
25
+ /** Checks this value using the AllowNaN policy */
26
26
  function IsNumberLike(value: unknown): value is number;
27
- /** Asserts this value using the AllowVoidNull policy */
27
+ /** Checks this value using the AllowVoidNull policy */
28
28
  function IsVoidLike(value: unknown): value is void;
29
29
  }
@@ -24,28 +24,28 @@ export var TypeSystemPolicy;
24
24
  TypeSystemPolicy.AllowNaN = false;
25
25
  /** Sets whether `null` should validate for void types. The default is `false` */
26
26
  TypeSystemPolicy.AllowNullVoid = false;
27
- /** Asserts this value using the ExactOptionalPropertyTypes policy */
27
+ /** Checks this value using the ExactOptionalPropertyTypes policy */
28
28
  function IsExactOptionalProperty(value, key) {
29
29
  return TypeSystemPolicy.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
30
30
  }
31
31
  TypeSystemPolicy.IsExactOptionalProperty = IsExactOptionalProperty;
32
- /** Asserts this value using the AllowArrayObjects policy */
32
+ /** Checks this value using the AllowArrayObjects policy */
33
33
  function IsObjectLike(value) {
34
34
  const isObject = IsObject(value);
35
35
  return TypeSystemPolicy.AllowArrayObject ? isObject : isObject && !IsArray(value);
36
36
  }
37
37
  TypeSystemPolicy.IsObjectLike = IsObjectLike;
38
- /** Asserts this value as a record using the AllowArrayObjects policy */
38
+ /** Checks this value as a record using the AllowArrayObjects policy */
39
39
  function IsRecordLike(value) {
40
40
  return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
41
41
  }
42
42
  TypeSystemPolicy.IsRecordLike = IsRecordLike;
43
- /** Asserts this value using the AllowNaN policy */
43
+ /** Checks this value using the AllowNaN policy */
44
44
  function IsNumberLike(value) {
45
45
  return TypeSystemPolicy.AllowNaN ? IsNumber(value) : Number.isFinite(value);
46
46
  }
47
47
  TypeSystemPolicy.IsNumberLike = IsNumberLike;
48
- /** Asserts this value using the AllowVoidNull policy */
48
+ /** Checks this value using the AllowVoidNull policy */
49
49
  function IsVoidLike(value) {
50
50
  const isUndefined = IsUndefined(value);
51
51
  return TypeSystemPolicy.AllowNullVoid ? isUndefined || value === null : isUndefined;
@@ -5,7 +5,7 @@ import { Kind } from '../../type/symbols/index.mjs';
5
5
  // ------------------------------------------------------------------
6
6
  // ValueGuard
7
7
  // ------------------------------------------------------------------
8
- import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from '../guard/index.mjs';
8
+ import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol, HasPropertyKey } from '../guard/index.mjs';
9
9
  // ------------------------------------------------------------------
10
10
  // Conversions
11
11
  // ------------------------------------------------------------------
@@ -141,8 +141,10 @@ function FromNumber(schema, references, value) {
141
141
  function FromObject(schema, references, value) {
142
142
  if (!IsObject(value))
143
143
  return value;
144
- for (const key of Object.getOwnPropertyNames(schema.properties)) {
145
- value[key] = Visit(schema.properties[key], references, value[key]);
144
+ for (const propertyKey of Object.getOwnPropertyNames(schema.properties)) {
145
+ if (!HasPropertyKey(value, propertyKey))
146
+ continue;
147
+ value[propertyKey] = Visit(schema.properties[propertyKey], references, value[propertyKey]);
146
148
  }
147
149
  return value;
148
150
  }
@@ -45,22 +45,25 @@ function FromObject(schema, references, value) {
45
45
  const defaulted = ValueOrDefault(schema, value);
46
46
  if (!IsObject(defaulted))
47
47
  return defaulted;
48
- const additionalPropertiesSchema = schema.additionalProperties;
49
48
  const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
50
49
  // properties
51
50
  for (const key of knownPropertyKeys) {
52
- if (!HasDefaultProperty(schema.properties[key]))
51
+ // note: we need to traverse into the object and test if the return value
52
+ // yielded a non undefined result. Here we interpret an undefined result as
53
+ // a non assignable property and continue.
54
+ const propertyValue = Visit(schema.properties[key], references, defaulted[key]);
55
+ if (IsUndefined(propertyValue))
53
56
  continue;
54
- defaulted[key] = Visit(schema.properties[key], references, defaulted[key]);
57
+ defaulted[key] = propertyValue;
55
58
  }
56
59
  // return if not additional properties
57
- if (!HasDefaultProperty(additionalPropertiesSchema))
60
+ if (!HasDefaultProperty(schema.additionalProperties))
58
61
  return defaulted;
59
62
  // additional properties
60
63
  for (const key of Object.getOwnPropertyNames(defaulted)) {
61
64
  if (knownPropertyKeys.includes(key))
62
65
  continue;
63
- defaulted[key] = Visit(additionalPropertiesSchema, references, defaulted[key]);
66
+ defaulted[key] = Visit(schema.additionalProperties, references, defaulted[key]);
64
67
  }
65
68
  return defaulted;
66
69
  }
@@ -1,3 +1,4 @@
1
+ import { TypeSystemPolicy } from '../../system/policy.mjs';
1
2
  import { Kind, TransformKind } from '../../type/symbols/index.mjs';
2
3
  import { TypeBoxError } from '../../type/error/index.mjs';
3
4
  import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index.mjs';
@@ -6,11 +7,11 @@ import { Check } from '../check/index.mjs';
6
7
  // ------------------------------------------------------------------
7
8
  // ValueGuard
8
9
  // ------------------------------------------------------------------
9
- import { IsObject, IsArray, IsValueType } from '../guard/index.mjs';
10
+ import { HasPropertyKey, IsObject, IsArray, IsValueType, IsUndefined as IsUndefinedValue } from '../guard/index.mjs';
10
11
  // ------------------------------------------------------------------
11
12
  // TypeGuard
12
13
  // ------------------------------------------------------------------
13
- import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
14
+ import { IsTransform, IsSchema, IsUndefined } from '../../type/guard/type.mjs';
14
15
  // ------------------------------------------------------------------
15
16
  // Errors
16
17
  // ------------------------------------------------------------------
@@ -84,10 +85,18 @@ function FromObject(schema, references, path, value) {
84
85
  return Default(schema, path, value);
85
86
  const knownKeys = KeyOfPropertyKeys(schema);
86
87
  const knownProperties = { ...value };
87
- for (const key of knownKeys)
88
- if (key in knownProperties) {
89
- knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
90
- }
88
+ for (const key of knownKeys) {
89
+ if (!HasPropertyKey(knownProperties, key))
90
+ continue;
91
+ // if the property value is undefined, but the target is not, nor does it satisfy exact optional
92
+ // property policy, then we need to continue. This is a special case for optional property handling
93
+ // where a transforms wrapped in a optional modifiers should not run.
94
+ if (IsUndefinedValue(knownProperties[key]) && (!IsUndefined(schema.properties[key]) ||
95
+ TypeSystemPolicy.IsExactOptionalProperty(knownProperties, key)))
96
+ continue;
97
+ // decode property
98
+ knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
99
+ }
91
100
  if (!IsSchema(schema.additionalProperties)) {
92
101
  return Default(schema, path, knownProperties);
93
102
  }
@@ -1,3 +1,4 @@
1
+ import { TypeSystemPolicy } from '../../system/policy.mjs';
1
2
  import { Kind, TransformKind } from '../../type/symbols/index.mjs';
2
3
  import { TypeBoxError } from '../../type/error/index.mjs';
3
4
  import { KeyOfPropertyKeys, KeyOfPropertyEntries } from '../../type/keyof/index.mjs';
@@ -6,11 +7,11 @@ import { Check } from '../check/index.mjs';
6
7
  // ------------------------------------------------------------------
7
8
  // ValueGuard
8
9
  // ------------------------------------------------------------------
9
- import { IsObject, IsArray, IsValueType } from '../guard/index.mjs';
10
+ import { HasPropertyKey, IsObject, IsArray, IsValueType, IsUndefined as IsUndefinedValue } from '../guard/index.mjs';
10
11
  // ------------------------------------------------------------------
11
12
  // TypeGuard
12
13
  // ------------------------------------------------------------------
13
- import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
14
+ import { IsTransform, IsSchema, IsUndefined } from '../../type/guard/type.mjs';
14
15
  // ------------------------------------------------------------------
15
16
  // Errors
16
17
  // ------------------------------------------------------------------
@@ -87,10 +88,18 @@ function FromObject(schema, references, path, value) {
87
88
  return defaulted;
88
89
  const knownKeys = KeyOfPropertyKeys(schema);
89
90
  const knownProperties = { ...defaulted };
90
- for (const key of knownKeys)
91
- if (key in knownProperties) {
92
- knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
93
- }
91
+ for (const key of knownKeys) {
92
+ if (!HasPropertyKey(knownProperties, key))
93
+ continue;
94
+ // if the property value is undefined, but the target is not, nor does it satisfy exact optional
95
+ // property policy, then we need to continue. This is a special case for optional property handling
96
+ // where a transforms wrapped in a optional modifiers should not run.
97
+ if (IsUndefinedValue(knownProperties[key]) && (!IsUndefined(schema.properties[key]) ||
98
+ TypeSystemPolicy.IsExactOptionalProperty(knownProperties, key)))
99
+ continue;
100
+ // encode property
101
+ knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
102
+ }
94
103
  if (!IsSchema(schema.additionalProperties)) {
95
104
  return knownProperties;
96
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.33.4",
3
+ "version": "0.33.6",
4
4
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -1676,6 +1676,7 @@ The following is a list of community packages that offer general tooling, extend
1676
1676
  | [fetch-typebox](https://github.com/erfanium/fetch-typebox) | Drop-in replacement for fetch that brings easy integration with TypeBox |
1677
1677
  | [h3-typebox](https://github.com/kevinmarrec/h3-typebox) | Schema validation utilities for h3 using TypeBox & Ajv |
1678
1678
  | [http-wizard](https://github.com/flodlc/http-wizard) | Type safe http client library for Fastify |
1679
+ | [nominal-typebox](https://github.com/Coder-Spirit/nominal/tree/main/%40coderspirit/nominal-typebox) | Allows devs to integrate nominal types into TypeBox schemas |
1679
1680
  | [openapi-box](https://github.com/geut/openapi-box) | Generate TypeBox types from OpenApi IDL + Http client library |
1680
1681
  | [prismabox](https://github.com/m1212e/prismabox) | Converts a prisma.schema to typebox schema matching the database models |
1681
1682
  | [schema2typebox](https://github.com/xddq/schema2typebox) | Creating TypeBox code from Json Schemas |