@sinclair/typebox 0.33.4 → 0.33.5
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.
- package/build/cjs/system/policy.d.ts +5 -5
- package/build/cjs/system/policy.js +5 -5
- package/build/cjs/value/convert/convert.js +4 -2
- package/build/cjs/value/transform/decode.js +13 -4
- package/build/cjs/value/transform/encode.js +13 -4
- package/build/esm/system/policy.d.mts +5 -5
- package/build/esm/system/policy.mjs +5 -5
- package/build/esm/value/convert/convert.mjs +5 -3
- package/build/esm/value/transform/decode.mjs +15 -6
- package/build/esm/value/transform/encode.mjs +15 -6
- package/package.json +1 -1
- package/readme.md +1 -0
|
@@ -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
|
-
/**
|
|
19
|
+
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
|
20
20
|
function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
|
|
21
|
-
/**
|
|
21
|
+
/** Checks this value using the AllowArrayObjects policy */
|
|
22
22
|
function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
|
|
23
|
-
/**
|
|
23
|
+
/** Checks this value as a record using the AllowArrayObjects policy */
|
|
24
24
|
function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
|
|
25
|
-
/**
|
|
25
|
+
/** Checks this value using the AllowNaN policy */
|
|
26
26
|
function IsNumberLike(value: unknown): value is number;
|
|
27
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
|
149
|
-
|
|
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
|
}
|
|
@@ -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 (
|
|
96
|
-
|
|
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 (
|
|
99
|
-
|
|
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
|
-
/**
|
|
19
|
+
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
|
20
20
|
function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
|
|
21
|
-
/**
|
|
21
|
+
/** Checks this value using the AllowArrayObjects policy */
|
|
22
22
|
function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
|
|
23
|
-
/**
|
|
23
|
+
/** Checks this value as a record using the AllowArrayObjects policy */
|
|
24
24
|
function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
|
|
25
|
-
/**
|
|
25
|
+
/** Checks this value using the AllowNaN policy */
|
|
26
26
|
function IsNumberLike(value: unknown): value is number;
|
|
27
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
|
145
|
-
|
|
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
|
}
|
|
@@ -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
|
|
89
|
-
|
|
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
|
|
92
|
-
|
|
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
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 |
|