typebox 1.0.10 → 1.0.11

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.
@@ -3,6 +3,7 @@ import { Unreachable } from '../../system/unreachable/index.mjs';
3
3
  import { Guard } from '../../guard/index.mjs';
4
4
  import { FromType } from './from-type.mjs';
5
5
  import { Callback } from './callback.mjs';
6
+ import { IsOptionalUndefined } from '../shared/index.mjs';
6
7
  // ------------------------------------------------------------------
7
8
  // Decode
8
9
  // ------------------------------------------------------------------
@@ -12,7 +13,8 @@ function Decode(direction, context, type, value) {
12
13
  return Unreachable();
13
14
  // deno-coverage-ignore-stop
14
15
  for (const key of Guard.Keys(type.properties)) {
15
- if (!Guard.HasPropertyKey(value, key))
16
+ // Ignore for non-present or optional-undefined
17
+ if (!Guard.HasPropertyKey(value, key) || IsOptionalUndefined(type.properties[key], key, value))
16
18
  continue;
17
19
  value[key] = FromType(direction, context, type.properties[key], value[key]);
18
20
  }
@@ -26,7 +28,8 @@ function Encode(direction, context, type, value) {
26
28
  if (!Guard.IsObjectNotArray(exterior))
27
29
  return exterior;
28
30
  for (const key of Guard.Keys(type.properties)) {
29
- if (!Guard.HasPropertyKey(exterior, key))
31
+ // Ignore for non-present or optional-undefined
32
+ if (!Guard.HasPropertyKey(exterior, key) || IsOptionalUndefined(type.properties[key], key, exterior))
30
33
  continue;
31
34
  exterior[key] = FromType(direction, context, type.properties[key], exterior[key]);
32
35
  }
@@ -1,20 +1,8 @@
1
1
  // deno-fmt-ignore-file
2
- import { IsOptional } from '../../type/index.mjs';
3
2
  import { Guard } from '../../guard/index.mjs';
4
3
  import { FromType } from './from-type.mjs';
5
4
  import { FromAdditionalProperties } from './from-additional.mjs';
6
- // ------------------------------------------------------------------
7
- // IsOptionalUndefined
8
- //
9
- // Determines whether a property should be excluded from conversion
10
- // because it is both optional and has an undefined value. This case
11
- // cannot be safely converted, since it introduces ambiguity between
12
- // an omitted optional property and a property explicitly set to the
13
- // value undefined.
14
- // ------------------------------------------------------------------
15
- function IsOptionalUndefined(property, key, value) {
16
- return IsOptional(property) && Guard.IsUndefined(value[key]);
17
- }
5
+ import { IsOptionalUndefined } from '../shared/index.mjs';
18
6
  // ------------------------------------------------------------------
19
7
  // FromProperties
20
8
  // ------------------------------------------------------------------
@@ -23,11 +11,10 @@ function FromProperties(context, type, value) {
23
11
  const keys = Guard.Keys(value);
24
12
  for (const [regexp, property] of entries) {
25
13
  for (const key of keys) {
26
- // Only convert the property if the value key matches a schema key,
27
- // and the property is not optional with an undefined value.
28
- if (regexp.test(key) && !IsOptionalUndefined(property, key, value)) {
29
- value[key] = FromType(context, property, value[key]);
30
- }
14
+ // Ignore for non-present or optional-undefined
15
+ if (!regexp.test(key) || IsOptionalUndefined(property, key, value))
16
+ continue;
17
+ value[key] = FromType(context, property, value[key]);
31
18
  }
32
19
  }
33
20
  return (Guard.HasPropertyKey(type, 'additionalProperties') && Guard.IsObject(type.additionalProperties)
@@ -0,0 +1 @@
1
+ export * from './optional-undefined.mjs';
@@ -0,0 +1,2 @@
1
+ // deno-fmt-ignore-file
2
+ export * from './optional-undefined.mjs';
@@ -0,0 +1,2 @@
1
+ import { type TSchema } from '../../type/index.mjs';
2
+ export declare function IsOptionalUndefined(property: TSchema, key: PropertyKey, value: Record<PropertyKey, unknown>): boolean;
@@ -0,0 +1,15 @@
1
+ // deno-fmt-ignore-file
2
+ import { Guard } from '../../guard/index.mjs';
3
+ import { IsOptional } from '../../type/index.mjs';
4
+ // ------------------------------------------------------------------
5
+ // IsOptionalUndefined
6
+ //
7
+ // Indicates whether a key should be excluded from processing when it is
8
+ // defined as optional in the schema and its corresponding value is undefined.
9
+ // This case cannot be reliably distinguished from an omitted key, and therefore
10
+ // introduces ambiguity between a key that is not provided and one that is
11
+ // explicitly assigned an undefined value.
12
+ // ------------------------------------------------------------------
13
+ export function IsOptionalUndefined(property, key, value) {
14
+ return IsOptional(property) && Guard.IsUndefined(value[key]);
15
+ }
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.10",
4
+ "version": "1.0.11",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"