@temporary-name/json-schema 1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.mts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.mjs +369 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Stainless
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as Draft07 from '@temporary-name/interop/json-schema-typed/draft-07';
|
|
2
|
+
import * as Draft2019 from '@temporary-name/interop/json-schema-typed/draft-2019-09';
|
|
3
|
+
import * as Draft2020 from '@temporary-name/interop/json-schema-typed/draft-2020-12';
|
|
4
|
+
import { ConditionalSchemaConverter } from '@temporary-name/openapi';
|
|
5
|
+
import { Context } from '@temporary-name/server';
|
|
6
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@temporary-name/server/standard';
|
|
7
|
+
|
|
8
|
+
type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
|
|
9
|
+
declare enum JsonSchemaXNativeType {
|
|
10
|
+
BigInt = "bigint",
|
|
11
|
+
RegExp = "regexp",
|
|
12
|
+
Date = "date",
|
|
13
|
+
Url = "url",
|
|
14
|
+
Set = "set",
|
|
15
|
+
Map = "map"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface experimental_JsonSchemaCoerceOptions {
|
|
19
|
+
components?: Record<string, JsonSchema>;
|
|
20
|
+
}
|
|
21
|
+
declare class experimental_JsonSchemaCoercer {
|
|
22
|
+
#private;
|
|
23
|
+
coerce(schema: JsonSchema, value: unknown, options?: experimental_JsonSchemaCoerceOptions): unknown;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface experimental_SmartCoercionPluginOptions {
|
|
27
|
+
schemaConverters?: readonly ConditionalSchemaConverter[];
|
|
28
|
+
}
|
|
29
|
+
declare class experimental_SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
30
|
+
#private;
|
|
31
|
+
private readonly converter;
|
|
32
|
+
private readonly coercer;
|
|
33
|
+
private readonly cache;
|
|
34
|
+
constructor(options?: experimental_SmartCoercionPluginOptions);
|
|
35
|
+
init(options: StandardHandlerOptions<T>): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { JsonSchemaXNativeType, experimental_JsonSchemaCoercer, experimental_SmartCoercionPlugin };
|
|
39
|
+
export type { JsonSchema, experimental_JsonSchemaCoerceOptions, experimental_SmartCoercionPluginOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as Draft07 from '@temporary-name/interop/json-schema-typed/draft-07';
|
|
2
|
+
import * as Draft2019 from '@temporary-name/interop/json-schema-typed/draft-2019-09';
|
|
3
|
+
import * as Draft2020 from '@temporary-name/interop/json-schema-typed/draft-2020-12';
|
|
4
|
+
import { ConditionalSchemaConverter } from '@temporary-name/openapi';
|
|
5
|
+
import { Context } from '@temporary-name/server';
|
|
6
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@temporary-name/server/standard';
|
|
7
|
+
|
|
8
|
+
type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
|
|
9
|
+
declare enum JsonSchemaXNativeType {
|
|
10
|
+
BigInt = "bigint",
|
|
11
|
+
RegExp = "regexp",
|
|
12
|
+
Date = "date",
|
|
13
|
+
Url = "url",
|
|
14
|
+
Set = "set",
|
|
15
|
+
Map = "map"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface experimental_JsonSchemaCoerceOptions {
|
|
19
|
+
components?: Record<string, JsonSchema>;
|
|
20
|
+
}
|
|
21
|
+
declare class experimental_JsonSchemaCoercer {
|
|
22
|
+
#private;
|
|
23
|
+
coerce(schema: JsonSchema, value: unknown, options?: experimental_JsonSchemaCoerceOptions): unknown;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface experimental_SmartCoercionPluginOptions {
|
|
27
|
+
schemaConverters?: readonly ConditionalSchemaConverter[];
|
|
28
|
+
}
|
|
29
|
+
declare class experimental_SmartCoercionPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
30
|
+
#private;
|
|
31
|
+
private readonly converter;
|
|
32
|
+
private readonly coercer;
|
|
33
|
+
private readonly cache;
|
|
34
|
+
constructor(options?: experimental_SmartCoercionPluginOptions);
|
|
35
|
+
init(options: StandardHandlerOptions<T>): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { JsonSchemaXNativeType, experimental_JsonSchemaCoercer, experimental_SmartCoercionPlugin };
|
|
39
|
+
export type { JsonSchema, experimental_JsonSchemaCoerceOptions, experimental_SmartCoercionPluginOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { isObject, toArray, guard } from '@temporary-name/shared';
|
|
2
|
+
import { CompositeSchemaConverter } from '@temporary-name/openapi';
|
|
3
|
+
|
|
4
|
+
var JsonSchemaXNativeType = /* @__PURE__ */ ((JsonSchemaXNativeType2) => {
|
|
5
|
+
JsonSchemaXNativeType2["BigInt"] = "bigint";
|
|
6
|
+
JsonSchemaXNativeType2["RegExp"] = "regexp";
|
|
7
|
+
JsonSchemaXNativeType2["Date"] = "date";
|
|
8
|
+
JsonSchemaXNativeType2["Url"] = "url";
|
|
9
|
+
JsonSchemaXNativeType2["Set"] = "set";
|
|
10
|
+
JsonSchemaXNativeType2["Map"] = "map";
|
|
11
|
+
return JsonSchemaXNativeType2;
|
|
12
|
+
})(JsonSchemaXNativeType || {});
|
|
13
|
+
|
|
14
|
+
const FLEXIBLE_DATE_FORMAT_REGEX = /^[^-]+-[^-]+-[^-]+$/;
|
|
15
|
+
class experimental_JsonSchemaCoercer {
|
|
16
|
+
coerce(schema, value, options = {}) {
|
|
17
|
+
const [, coerced] = this.#coerce(schema, value, options);
|
|
18
|
+
return coerced;
|
|
19
|
+
}
|
|
20
|
+
#coerce(schema, originalValue, options) {
|
|
21
|
+
if (typeof schema === "boolean") {
|
|
22
|
+
return [schema, originalValue];
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(schema.type)) {
|
|
25
|
+
return this.#coerce(
|
|
26
|
+
{
|
|
27
|
+
anyOf: schema.type.map((type) => ({ ...schema, type }))
|
|
28
|
+
},
|
|
29
|
+
originalValue,
|
|
30
|
+
options
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
let coerced = originalValue;
|
|
34
|
+
let satisfied = true;
|
|
35
|
+
if (typeof schema.$ref === "string") {
|
|
36
|
+
const refSchema = options?.components?.[schema.$ref];
|
|
37
|
+
if (refSchema !== void 0) {
|
|
38
|
+
const [subSatisfied, subCoerced] = this.#coerce(refSchema, coerced, options);
|
|
39
|
+
coerced = subCoerced;
|
|
40
|
+
satisfied = subSatisfied;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const enumValues = schema.const !== void 0 ? [schema.const] : schema.enum;
|
|
44
|
+
if (enumValues !== void 0 && !enumValues.includes(coerced)) {
|
|
45
|
+
if (typeof coerced === "string") {
|
|
46
|
+
const numberValue = this.#stringToNumber(coerced);
|
|
47
|
+
if (enumValues.includes(numberValue)) {
|
|
48
|
+
coerced = numberValue;
|
|
49
|
+
} else {
|
|
50
|
+
const booleanValue = this.#stringToBoolean(coerced);
|
|
51
|
+
if (enumValues.includes(booleanValue)) {
|
|
52
|
+
coerced = booleanValue;
|
|
53
|
+
} else {
|
|
54
|
+
satisfied = false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
satisfied = false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (typeof schema.type === "string") {
|
|
62
|
+
switch (schema.type) {
|
|
63
|
+
case "null": {
|
|
64
|
+
if (coerced !== null) {
|
|
65
|
+
satisfied = false;
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case "string": {
|
|
70
|
+
if (typeof coerced !== "string") {
|
|
71
|
+
satisfied = false;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "number": {
|
|
76
|
+
if (typeof coerced === "string") {
|
|
77
|
+
coerced = this.#stringToNumber(coerced);
|
|
78
|
+
}
|
|
79
|
+
if (typeof coerced !== "number") {
|
|
80
|
+
satisfied = false;
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case "integer": {
|
|
85
|
+
if (typeof coerced === "string") {
|
|
86
|
+
coerced = this.#stringToInteger(coerced);
|
|
87
|
+
}
|
|
88
|
+
if (typeof coerced !== "number" || !Number.isInteger(coerced)) {
|
|
89
|
+
satisfied = false;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "boolean": {
|
|
94
|
+
if (typeof coerced === "string") {
|
|
95
|
+
coerced = this.#stringToBoolean(coerced);
|
|
96
|
+
}
|
|
97
|
+
if (typeof coerced !== "boolean") {
|
|
98
|
+
satisfied = false;
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
case "array": {
|
|
103
|
+
if (Array.isArray(coerced)) {
|
|
104
|
+
const prefixItemSchemas = "prefixItems" in schema ? toArray(schema.prefixItems) : Array.isArray(schema.items) ? schema.items : [];
|
|
105
|
+
const itemSchema = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
|
|
106
|
+
let shouldUseCoercedItems = false;
|
|
107
|
+
const coercedItems = coerced.map((item, i) => {
|
|
108
|
+
const subSchema = prefixItemSchemas[i] ?? itemSchema;
|
|
109
|
+
if (subSchema === void 0) {
|
|
110
|
+
satisfied = false;
|
|
111
|
+
return item;
|
|
112
|
+
}
|
|
113
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, item, options);
|
|
114
|
+
if (!subSatisfied) {
|
|
115
|
+
satisfied = false;
|
|
116
|
+
}
|
|
117
|
+
if (subCoerced !== item) {
|
|
118
|
+
shouldUseCoercedItems = true;
|
|
119
|
+
}
|
|
120
|
+
return subCoerced;
|
|
121
|
+
});
|
|
122
|
+
if (coercedItems.length < prefixItemSchemas.length) {
|
|
123
|
+
satisfied = false;
|
|
124
|
+
}
|
|
125
|
+
if (shouldUseCoercedItems) {
|
|
126
|
+
coerced = coercedItems;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
satisfied = false;
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case "object": {
|
|
134
|
+
if (Array.isArray(coerced)) {
|
|
135
|
+
coerced = { ...coerced };
|
|
136
|
+
}
|
|
137
|
+
if (isObject(coerced)) {
|
|
138
|
+
let shouldUseCoercedItems = false;
|
|
139
|
+
const coercedItems = {};
|
|
140
|
+
const patternProperties = Object.entries(schema.patternProperties ?? {}).map(
|
|
141
|
+
([key, value]) => [new RegExp(key), value]
|
|
142
|
+
);
|
|
143
|
+
for (const key in coerced) {
|
|
144
|
+
const value = coerced[key];
|
|
145
|
+
const subSchema = schema.properties?.[key] ?? patternProperties.find(([pattern]) => pattern.test(key))?.[1] ?? schema.additionalProperties;
|
|
146
|
+
if (value === void 0 && !schema.required?.includes(key)) {
|
|
147
|
+
coercedItems[key] = value;
|
|
148
|
+
} else if (subSchema === void 0) {
|
|
149
|
+
coercedItems[key] = value;
|
|
150
|
+
satisfied = false;
|
|
151
|
+
} else {
|
|
152
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, value, options);
|
|
153
|
+
coercedItems[key] = subCoerced;
|
|
154
|
+
if (!subSatisfied) {
|
|
155
|
+
satisfied = false;
|
|
156
|
+
}
|
|
157
|
+
if (subCoerced !== value) {
|
|
158
|
+
shouldUseCoercedItems = true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) {
|
|
163
|
+
satisfied = false;
|
|
164
|
+
}
|
|
165
|
+
if (shouldUseCoercedItems) {
|
|
166
|
+
coerced = coercedItems;
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
satisfied = false;
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if ("x-native-type" in schema && typeof schema["x-native-type"] === "string") {
|
|
176
|
+
switch (schema["x-native-type"]) {
|
|
177
|
+
case JsonSchemaXNativeType.Date: {
|
|
178
|
+
if (typeof coerced === "string") {
|
|
179
|
+
coerced = this.#stringToDate(coerced);
|
|
180
|
+
}
|
|
181
|
+
if (!(coerced instanceof Date)) {
|
|
182
|
+
satisfied = false;
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case JsonSchemaXNativeType.BigInt: {
|
|
187
|
+
switch (typeof coerced) {
|
|
188
|
+
case "string":
|
|
189
|
+
coerced = this.#stringToBigInt(coerced);
|
|
190
|
+
break;
|
|
191
|
+
case "number":
|
|
192
|
+
coerced = this.#numberToBigInt(coerced);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
if (typeof coerced !== "bigint") {
|
|
196
|
+
satisfied = false;
|
|
197
|
+
}
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case JsonSchemaXNativeType.RegExp: {
|
|
201
|
+
if (typeof coerced === "string") {
|
|
202
|
+
coerced = this.#stringToRegExp(coerced);
|
|
203
|
+
}
|
|
204
|
+
if (!(coerced instanceof RegExp)) {
|
|
205
|
+
satisfied = false;
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
case JsonSchemaXNativeType.Url: {
|
|
210
|
+
if (typeof coerced === "string") {
|
|
211
|
+
coerced = this.#stringToURL(coerced);
|
|
212
|
+
}
|
|
213
|
+
if (!(coerced instanceof URL)) {
|
|
214
|
+
satisfied = false;
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
case JsonSchemaXNativeType.Set: {
|
|
219
|
+
if (Array.isArray(coerced)) {
|
|
220
|
+
coerced = this.#arrayToSet(coerced);
|
|
221
|
+
}
|
|
222
|
+
if (!(coerced instanceof Set)) {
|
|
223
|
+
satisfied = false;
|
|
224
|
+
}
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case JsonSchemaXNativeType.Map: {
|
|
228
|
+
if (Array.isArray(coerced)) {
|
|
229
|
+
coerced = this.#arrayToMap(coerced);
|
|
230
|
+
}
|
|
231
|
+
if (!(coerced instanceof Map)) {
|
|
232
|
+
satisfied = false;
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (schema.allOf) {
|
|
239
|
+
for (const subSchema of schema.allOf) {
|
|
240
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, coerced, options);
|
|
241
|
+
coerced = subCoerced;
|
|
242
|
+
if (!subSatisfied) {
|
|
243
|
+
satisfied = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
for (const key of ["anyOf", "oneOf"]) {
|
|
248
|
+
if (schema[key]) {
|
|
249
|
+
let bestOptions;
|
|
250
|
+
for (const subSchema of schema[key]) {
|
|
251
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, coerced, options);
|
|
252
|
+
if (subSatisfied) {
|
|
253
|
+
if (!bestOptions || subCoerced === coerced) {
|
|
254
|
+
bestOptions = { coerced: subCoerced, satisfied: subSatisfied };
|
|
255
|
+
}
|
|
256
|
+
if (subCoerced === coerced) {
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
coerced = bestOptions ? bestOptions.coerced : coerced;
|
|
262
|
+
satisfied = bestOptions ? bestOptions.satisfied : false;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (typeof schema.not !== "undefined") {
|
|
266
|
+
const [notSatisfied] = this.#coerce(schema.not, coerced, options);
|
|
267
|
+
if (notSatisfied) {
|
|
268
|
+
satisfied = false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return [satisfied, coerced];
|
|
272
|
+
}
|
|
273
|
+
#stringToNumber(value) {
|
|
274
|
+
const num = Number.parseFloat(value);
|
|
275
|
+
if (Number.isNaN(num) || num !== Number(value)) {
|
|
276
|
+
return value;
|
|
277
|
+
}
|
|
278
|
+
return num;
|
|
279
|
+
}
|
|
280
|
+
#stringToInteger(value) {
|
|
281
|
+
const num = Number.parseInt(value);
|
|
282
|
+
if (Number.isNaN(num) || num !== Number(value)) {
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
285
|
+
return num;
|
|
286
|
+
}
|
|
287
|
+
#stringToBoolean(value) {
|
|
288
|
+
const lower = value.toLowerCase();
|
|
289
|
+
if (lower === "false" || lower === "off") {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
if (lower === "true" || lower === "on") {
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
return value;
|
|
296
|
+
}
|
|
297
|
+
#stringToBigInt(value) {
|
|
298
|
+
return guard(() => BigInt(value)) ?? value;
|
|
299
|
+
}
|
|
300
|
+
#numberToBigInt(value) {
|
|
301
|
+
return guard(() => BigInt(value)) ?? value;
|
|
302
|
+
}
|
|
303
|
+
#stringToDate(value) {
|
|
304
|
+
const date = new Date(value);
|
|
305
|
+
if (Number.isNaN(date.getTime()) || !FLEXIBLE_DATE_FORMAT_REGEX.test(value)) {
|
|
306
|
+
return value;
|
|
307
|
+
}
|
|
308
|
+
return date;
|
|
309
|
+
}
|
|
310
|
+
#stringToRegExp(value) {
|
|
311
|
+
const match = value.match(/^\/(.*)\/([a-z]*)$/);
|
|
312
|
+
if (match) {
|
|
313
|
+
const [, pattern, flags] = match;
|
|
314
|
+
return guard(() => new RegExp(pattern, flags)) ?? value;
|
|
315
|
+
}
|
|
316
|
+
return value;
|
|
317
|
+
}
|
|
318
|
+
#stringToURL(value) {
|
|
319
|
+
return guard(() => new URL(value)) ?? value;
|
|
320
|
+
}
|
|
321
|
+
#arrayToSet(value) {
|
|
322
|
+
const set = new Set(value);
|
|
323
|
+
if (set.size !== value.length) {
|
|
324
|
+
return value;
|
|
325
|
+
}
|
|
326
|
+
return set;
|
|
327
|
+
}
|
|
328
|
+
#arrayToMap(value) {
|
|
329
|
+
if (value.some((item) => !Array.isArray(item) || item.length !== 2)) {
|
|
330
|
+
return value;
|
|
331
|
+
}
|
|
332
|
+
const result = new Map(value);
|
|
333
|
+
if (result.size !== value.length) {
|
|
334
|
+
return value;
|
|
335
|
+
}
|
|
336
|
+
return result;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
class experimental_SmartCoercionPlugin {
|
|
341
|
+
converter;
|
|
342
|
+
coercer;
|
|
343
|
+
cache = /* @__PURE__ */ new WeakMap();
|
|
344
|
+
constructor(options = {}) {
|
|
345
|
+
this.converter = new CompositeSchemaConverter(toArray(options.schemaConverters));
|
|
346
|
+
this.coercer = new experimental_JsonSchemaCoercer();
|
|
347
|
+
}
|
|
348
|
+
init(options) {
|
|
349
|
+
options.clientInterceptors ??= [];
|
|
350
|
+
options.clientInterceptors.unshift(async (options2) => {
|
|
351
|
+
const inputSchema = options2.procedure["~orpc"].inputSchema;
|
|
352
|
+
if (!inputSchema) {
|
|
353
|
+
return options2.next();
|
|
354
|
+
}
|
|
355
|
+
const coercedInput = await this.#coerce(inputSchema, options2.input);
|
|
356
|
+
return options2.next({ ...options2, input: coercedInput });
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
async #coerce(schema, value) {
|
|
360
|
+
let jsonSchema = this.cache.get(schema);
|
|
361
|
+
if (!jsonSchema) {
|
|
362
|
+
jsonSchema = (await this.converter.convert(schema, { strategy: "input" }))[1];
|
|
363
|
+
this.cache.set(schema, jsonSchema);
|
|
364
|
+
}
|
|
365
|
+
return this.coercer.coerce(jsonSchema, value);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export { JsonSchemaXNativeType, experimental_JsonSchemaCoercer, experimental_SmartCoercionPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@temporary-name/json-schema",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://www.stainless.com/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/unnoq/krusty.git",
|
|
10
|
+
"directory": "packages/json-schema"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"krusty"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@temporary-name/contract": "1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8",
|
|
27
|
+
"@temporary-name/interop": "1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8",
|
|
28
|
+
"@temporary-name/openapi": "1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8",
|
|
29
|
+
"@temporary-name/shared": "1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8",
|
|
30
|
+
"@temporary-name/server": "1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"zod": "^4.1.11"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "unbuild",
|
|
37
|
+
"build:watch": "pnpm run build --watch",
|
|
38
|
+
"type:check": "tsc -b"
|
|
39
|
+
}
|
|
40
|
+
}
|