@sinclair/typebox 0.24.39 → 0.24.40
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/package.json +1 -1
- package/value/cast.d.ts +15 -2
- package/value/cast.js +49 -11
package/package.json
CHANGED
package/value/cast.d.ts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
-
export declare class
|
|
2
|
+
export declare class ValueCastReferenceTypeError extends Error {
|
|
3
|
+
readonly schema: Types.TRef | Types.TSelf;
|
|
4
|
+
constructor(schema: Types.TRef | Types.TSelf);
|
|
5
|
+
}
|
|
6
|
+
export declare class ValueCastArrayUniqueItemsTypeError extends Error {
|
|
3
7
|
readonly schema: Types.TSchema;
|
|
4
|
-
|
|
8
|
+
readonly value: unknown;
|
|
9
|
+
constructor(schema: Types.TSchema, value: unknown);
|
|
5
10
|
}
|
|
6
11
|
export declare class ValueCastNeverTypeError extends Error {
|
|
7
12
|
readonly schema: Types.TSchema;
|
|
8
13
|
constructor(schema: Types.TSchema);
|
|
9
14
|
}
|
|
15
|
+
export declare class ValueCastRecursiveTypeError extends Error {
|
|
16
|
+
readonly schema: Types.TSchema;
|
|
17
|
+
constructor(schema: Types.TSchema);
|
|
18
|
+
}
|
|
19
|
+
export declare class ValueCastUnknownTypeError extends Error {
|
|
20
|
+
readonly schema: Types.TSchema;
|
|
21
|
+
constructor(schema: Types.TSchema);
|
|
22
|
+
}
|
|
10
23
|
export declare namespace ValueCast {
|
|
11
24
|
function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
|
|
12
25
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
|
package/value/cast.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueCast = exports.ValueCastNeverTypeError = exports.
|
|
30
|
+
exports.ValueCast = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const create_1 = require("./create");
|
|
33
33
|
const check_1 = require("./check");
|
|
@@ -72,13 +72,24 @@ var UnionValueCast;
|
|
|
72
72
|
}
|
|
73
73
|
UnionValueCast.Create = Create;
|
|
74
74
|
})(UnionValueCast || (UnionValueCast = {}));
|
|
75
|
-
|
|
75
|
+
// -----------------------------------------------------------
|
|
76
|
+
// Errors
|
|
77
|
+
// -----------------------------------------------------------
|
|
78
|
+
class ValueCastReferenceTypeError extends Error {
|
|
76
79
|
constructor(schema) {
|
|
77
|
-
super(
|
|
80
|
+
super(`ValueCast: Cannot locate referenced schema with $id '${schema.$ref}'`);
|
|
78
81
|
this.schema = schema;
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
|
-
exports.
|
|
84
|
+
exports.ValueCastReferenceTypeError = ValueCastReferenceTypeError;
|
|
85
|
+
class ValueCastArrayUniqueItemsTypeError extends Error {
|
|
86
|
+
constructor(schema, value) {
|
|
87
|
+
super('ValueCast: Array cast produced invalid data due to uniqueItems constraint');
|
|
88
|
+
this.schema = schema;
|
|
89
|
+
this.value = value;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.ValueCastArrayUniqueItemsTypeError = ValueCastArrayUniqueItemsTypeError;
|
|
82
93
|
class ValueCastNeverTypeError extends Error {
|
|
83
94
|
constructor(schema) {
|
|
84
95
|
super('ValueCast: Never types cannot be cast');
|
|
@@ -86,11 +97,28 @@ class ValueCastNeverTypeError extends Error {
|
|
|
86
97
|
}
|
|
87
98
|
}
|
|
88
99
|
exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
|
|
100
|
+
class ValueCastRecursiveTypeError extends Error {
|
|
101
|
+
constructor(schema) {
|
|
102
|
+
super('ValueCast.Recursive: Cannot cast recursive schemas');
|
|
103
|
+
this.schema = schema;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.ValueCastRecursiveTypeError = ValueCastRecursiveTypeError;
|
|
107
|
+
class ValueCastUnknownTypeError extends Error {
|
|
108
|
+
constructor(schema) {
|
|
109
|
+
super('ValueCast: Unknown type');
|
|
110
|
+
this.schema = schema;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
89
114
|
var ValueCast;
|
|
90
115
|
(function (ValueCast) {
|
|
91
116
|
// -----------------------------------------------------------
|
|
92
|
-
//
|
|
117
|
+
// Guards
|
|
93
118
|
// -----------------------------------------------------------
|
|
119
|
+
function IsArray(value) {
|
|
120
|
+
return typeof value === 'object' && globalThis.Array.isArray(value);
|
|
121
|
+
}
|
|
94
122
|
function IsString(value) {
|
|
95
123
|
return typeof value === 'string';
|
|
96
124
|
}
|
|
@@ -115,6 +143,9 @@ var ValueCast;
|
|
|
115
143
|
function IsValueFalse(value) {
|
|
116
144
|
return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === 0n) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
|
|
117
145
|
}
|
|
146
|
+
// -----------------------------------------------------------
|
|
147
|
+
// Convert
|
|
148
|
+
// -----------------------------------------------------------
|
|
118
149
|
function TryConvertString(value) {
|
|
119
150
|
return IsValueToString(value) ? value.toString() : value;
|
|
120
151
|
}
|
|
@@ -136,9 +167,16 @@ var ValueCast;
|
|
|
136
167
|
function Array(schema, references, value) {
|
|
137
168
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
138
169
|
return value;
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
170
|
+
const created = IsArray(value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
171
|
+
const minimum = IsNumber(schema.minItems) && created.length < schema.minItems ? [...created, ...globalThis.Array.from({ length: schema.minItems - created.length }, () => null)] : created;
|
|
172
|
+
const maximum = IsNumber(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
|
|
173
|
+
const casted = maximum.map((value) => Visit(schema.items, references, value));
|
|
174
|
+
if (schema.uniqueItems !== true)
|
|
175
|
+
return casted;
|
|
176
|
+
const unique = [...new Set(casted)];
|
|
177
|
+
if (!check_1.ValueCheck.Check(schema, references, unique))
|
|
178
|
+
throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
|
|
179
|
+
return unique;
|
|
142
180
|
}
|
|
143
181
|
function Boolean(schema, references, value) {
|
|
144
182
|
const conversion = TryConvertBoolean(value);
|
|
@@ -210,18 +248,18 @@ var ValueCast;
|
|
|
210
248
|
return result;
|
|
211
249
|
}
|
|
212
250
|
function Recursive(schema, references, value) {
|
|
213
|
-
throw new
|
|
251
|
+
throw new ValueCastRecursiveTypeError(schema);
|
|
214
252
|
}
|
|
215
253
|
function Ref(schema, references, value) {
|
|
216
254
|
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
217
255
|
if (reference === undefined)
|
|
218
|
-
throw new
|
|
256
|
+
throw new ValueCastReferenceTypeError(schema);
|
|
219
257
|
return Visit(reference, references, value);
|
|
220
258
|
}
|
|
221
259
|
function Self(schema, references, value) {
|
|
222
260
|
const reference = references.find((reference) => reference.$id === schema.$ref);
|
|
223
261
|
if (reference === undefined)
|
|
224
|
-
throw new
|
|
262
|
+
throw new ValueCastReferenceTypeError(schema);
|
|
225
263
|
return Visit(reference, references, value);
|
|
226
264
|
}
|
|
227
265
|
function String(schema, references, value) {
|