@orpc/json-schema 2.0.0-beta.48 → 2.0.0-beta.49

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.
Files changed (46) hide show
  1. package/README.md +4 -4
  2. package/dist/coercer.d.ts +29 -0
  3. package/dist/coercer.js +498 -0
  4. package/dist/composition-utils.d.ts +41 -0
  5. package/dist/composition-utils.js +380 -0
  6. package/dist/constants.d.ts +8 -0
  7. package/dist/constants.js +66 -0
  8. package/dist/convert.d.ts +29 -0
  9. package/dist/convert.js +14 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.js +9 -0
  12. package/dist/ref-utils.d.ts +59 -0
  13. package/dist/ref-utils.js +253 -0
  14. package/dist/smart-coercion-handler-plugin.d.ts +22 -0
  15. package/dist/smart-coercion-handler-plugin.js +50 -0
  16. package/dist/smart-coercion-link-plugin.d.ts +28 -0
  17. package/dist/smart-coercion-link-plugin.js +75 -0
  18. package/dist/standard-json-schema-converter.d.ts +16 -0
  19. package/dist/standard-json-schema-converter.js +45 -0
  20. package/dist/types.d.ts +53 -0
  21. package/dist/types.js +40 -0
  22. package/dist/utils.d.ts +45 -0
  23. package/dist/utils.js +106 -0
  24. package/package.json +25 -27
  25. package/dist/coercer.d.mts +0 -29
  26. package/dist/coercer.mjs +0 -465
  27. package/dist/composition-utils.d.mts +0 -41
  28. package/dist/composition-utils.mjs +0 -357
  29. package/dist/constants.d.mts +0 -4
  30. package/dist/constants.mjs +0 -61
  31. package/dist/convert.d.mts +0 -29
  32. package/dist/convert.mjs +0 -14
  33. package/dist/index.d.mts +0 -10
  34. package/dist/index.mjs +0 -9
  35. package/dist/ref-utils.d.mts +0 -44
  36. package/dist/ref-utils.mjs +0 -117
  37. package/dist/smart-coercion-handler-plugin.d.mts +0 -22
  38. package/dist/smart-coercion-handler-plugin.mjs +0 -42
  39. package/dist/smart-coercion-link-plugin.d.mts +0 -28
  40. package/dist/smart-coercion-link-plugin.mjs +0 -61
  41. package/dist/standard-json-schema-converter.d.mts +0 -16
  42. package/dist/standard-json-schema-converter.mjs +0 -30
  43. package/dist/types.d.mts +0 -51
  44. package/dist/types.mjs +0 -40
  45. package/dist/utils.d.mts +0 -40
  46. package/dist/utils.mjs +0 -50
package/dist/utils.js ADDED
@@ -0,0 +1,106 @@
1
+ /**
2
+ * These utilities assume the schema has only one root-level `$defs` object
3
+ * and exclusively use absolute JSON pointers for `$ref` values.
4
+ */
5
+ import { JSON_SCHEMA_LOGIC_KEYWORDS, JSON_SCHEMA_PRIMITIVE_TYPES, JSON_SCHEMA_RECORD_KEYWORDS, } from './constants.js';
6
+ import { resolveJsonSchemaLocalPointer } from './ref-utils.js';
7
+ /**
8
+ * Returns true when the schema only describes primitive values, by a primitive `type`
9
+ * (or a non-empty list of them, such as `['string', 'null']`), a `const`, or an `enum`.
10
+ * Composition keywords are not inspected, so flatten unions with {@link flattenJsonUnionSchema} first.
11
+ */
12
+ export function isJsonPrimitiveSchema(schema) {
13
+ if (typeof schema === 'boolean') {
14
+ return false;
15
+ }
16
+ if (typeof schema.type === 'string' && JSON_SCHEMA_PRIMITIVE_TYPES.has(schema.type)) {
17
+ return true;
18
+ }
19
+ if (Array.isArray(schema.type) &&
20
+ schema.type.length > 0 &&
21
+ schema.type.every((type) => JSON_SCHEMA_PRIMITIVE_TYPES.has(type))) {
22
+ return true;
23
+ }
24
+ if (schema.const !== undefined) {
25
+ return true;
26
+ }
27
+ if (schema.enum !== undefined) {
28
+ return true;
29
+ }
30
+ return false;
31
+ }
32
+ /**
33
+ * Returns true when the schema is a file-like string schema.
34
+ */
35
+ export function isJsonFileSchema(schema) {
36
+ return (typeof schema !== 'boolean' &&
37
+ schema.type === 'string' &&
38
+ (typeof schema.contentMediaType === 'string' ||
39
+ schema.format === 'binary' ||
40
+ schema.contentEncoding === 'binary'));
41
+ }
42
+ /**
43
+ * Returns true when a file schema is reachable from the schema, such as through a property, a composition, or a local `$ref`.
44
+ */
45
+ export function containsJsonFileSchema(schema) {
46
+ const seen = new Set();
47
+ const visit = (value, schemaLevel) => {
48
+ if (!value || typeof value !== 'object' || seen.has(value)) {
49
+ return false;
50
+ }
51
+ seen.add(value);
52
+ if (Array.isArray(value)) {
53
+ return value.some((item) => visit(item, schemaLevel));
54
+ }
55
+ const record = value;
56
+ if (schemaLevel && isJsonFileSchema(record)) {
57
+ return true;
58
+ }
59
+ return Object.keys(record).some((key) => {
60
+ const val = record[key];
61
+ if (key === '$ref' && typeof val === 'string') {
62
+ return visit(resolveJsonSchemaLocalPointer(schema, val), true);
63
+ }
64
+ if (!schemaLevel) {
65
+ return visit(val, true);
66
+ }
67
+ return (key !== '$defs' &&
68
+ (JSON_SCHEMA_LOGIC_KEYWORDS.has(key) || JSON_SCHEMA_RECORD_KEYWORDS.has(key)) &&
69
+ visit(val, !JSON_SCHEMA_RECORD_KEYWORDS.has(key)));
70
+ });
71
+ };
72
+ return visit(schema, true);
73
+ }
74
+ /**
75
+ * Returns true when the schema is an object schema.
76
+ */
77
+ export function isJsonObjectSchema(schema) {
78
+ return typeof schema !== 'boolean' && schema.type === 'object';
79
+ }
80
+ /**
81
+ * Returns true when the schema is an array schema.
82
+ */
83
+ export function isJsonArraySchema(schema) {
84
+ return typeof schema !== 'boolean' && schema.type === 'array';
85
+ }
86
+ /**
87
+ * Returns true when the schema does not apply any recognized constraints.
88
+ */
89
+ export function isUnconstrainedSchema(schema) {
90
+ if (typeof schema === 'boolean') {
91
+ return schema;
92
+ }
93
+ if (Object.keys(schema).every((k) => !JSON_SCHEMA_LOGIC_KEYWORDS.has(k))) {
94
+ return true;
95
+ }
96
+ return false;
97
+ }
98
+ /**
99
+ * Ensures a JSON Schema is represented as an object schema document.
100
+ */
101
+ export function ensureJsonSchemaObject(schema) {
102
+ if (typeof schema === 'boolean') {
103
+ return schema ? {} : { not: {} };
104
+ }
105
+ return schema;
106
+ }
package/package.json CHANGED
@@ -1,51 +1,49 @@
1
1
  {
2
2
  "name": "@orpc/json-schema",
3
- "type": "module",
4
- "version": "2.0.0-beta.48",
3
+ "version": "2.0.0-beta.49",
5
4
  "description": "JSON Schema conversion and smart request coercion for oRPC and OpenAPI",
6
- "license": "MIT",
7
- "funding": [
8
- "https://github.com/sponsors/dinwwwh",
9
- "https://opencollective.com/middleapi"
5
+ "keywords": [
6
+ "coercion",
7
+ "json-schema",
8
+ "openapi",
9
+ "orpc",
10
+ "typescript",
11
+ "validation"
10
12
  ],
11
13
  "homepage": "https://orpc.dev",
14
+ "license": "MIT",
12
15
  "repository": {
13
16
  "type": "git",
14
17
  "url": "git+https://github.com/middleapi/orpc.git",
15
18
  "directory": "packages/json-schema"
16
19
  },
17
- "keywords": [
18
- "orpc",
19
- "json-schema",
20
- "openapi",
21
- "coercion",
22
- "validation",
23
- "typescript"
20
+ "funding": [
21
+ "https://github.com/sponsors/dinwwwh",
22
+ "https://opencollective.com/middleapi"
24
23
  ],
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "type": "module",
25
28
  "sideEffects": false,
26
29
  "exports": {
27
30
  "./package.json": "./package.json",
28
31
  ".": {
29
- "types": "./dist/index.d.mts",
30
- "import": "./dist/index.mjs",
31
- "default": "./dist/index.mjs"
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "default": "./dist/index.js"
32
35
  }
33
36
  },
34
- "files": [
35
- "dist"
36
- ],
37
37
  "dependencies": {
38
- "@openapi-spec/types": "~0.6.0",
39
- "@orpc/client": "2.0.0-beta.48",
40
- "@orpc/contract": "2.0.0-beta.48",
41
- "@orpc/server": "2.0.0-beta.48",
42
- "@orpc/shared": "2.0.0-beta.48",
38
+ "@openapi-spec/types": "~0.7.1",
39
+ "@orpc/client": "2.0.0-beta.49",
40
+ "@orpc/contract": "2.0.0-beta.49",
41
+ "@orpc/server": "2.0.0-beta.49",
42
+ "@orpc/shared": "2.0.0-beta.49",
43
43
  "@standard-schema/spec": "^1.1.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "zod": "^4.6.7"
47
47
  },
48
- "scripts": {
49
- "type:check": "tsc -b"
50
- }
48
+ "scripts": {}
51
49
  }
@@ -1,29 +0,0 @@
1
- import type { JsonSchema } from "./types.mjs";
2
- export interface JsonSchemaCoercerOptions {
3
- /**
4
- * Maximum digits, excluding a leading `-`, of a string coerced to `bigint`. Longer strings are left unchanged.
5
- * `Number.POSITIVE_INFINITY` disables it.
6
- *
7
- * @default 4300
8
- */
9
- maxBigIntDigits?: number | undefined;
10
- /**
11
- * Maximum coercion steps per input value, where a step applies one schema to one value.
12
- * Inputs with fewer than 100 values are budgeted as 100. Past the budget the whole input is left uncoerced,
13
- * so validation still runs and reports any mismatch.
14
- *
15
- * @default 1000
16
- */
17
- maxStepsPerValue?: number | undefined;
18
- }
19
- export declare class JsonSchemaCoercer {
20
- private readonly nativeCoerceOptions;
21
- private readonly maxStepsPerValue;
22
- /** Reused between calls, since building a context is slow until V8 warms up. */
23
- private idleContext;
24
- constructor(options?: JsonSchemaCoercerOptions);
25
- coerce([schema, optional]: [schema: JsonSchema, optional: boolean], value: unknown): unknown;
26
- private coerceInternal;
27
- private coerceRef;
28
- private extendBudget;
29
- }
package/dist/coercer.mjs DELETED
@@ -1,465 +0,0 @@
1
- import { DEFAULT_MAX_BIGINT_DIGITS, get, getOwn, isPlainObject, isTypescriptObject, parseBigIntString, toArray, tryOrUndefined } from "@orpc/shared";
2
- import { decodeJsonPointerSegment } from "./ref-utils.mjs";
3
- import { JsonSchemaXNativeType } from "./types.mjs";
4
-
5
- const UNSATISFIED = 0;
6
-
7
- const LOOSELY_SATISFIED = 1;
8
-
9
- const SATISFIED = 2;
10
-
11
- const NATIVE_TYPES = /* @__PURE__ */ (() => new Map([
12
- [JsonSchemaXNativeType.Date, {
13
- types: ["string"],
14
- is: (value) => value instanceof Date,
15
- coerce: (value) => typeof value === "string" ? stringToDate(value) : value
16
- }],
17
- [JsonSchemaXNativeType.BigInt, {
18
- types: [
19
- "string",
20
- "integer",
21
- "number"
22
- ],
23
- is: (value) => typeof value === "bigint",
24
- coerce: (value, { maxBigIntDigits }) => typeof value === "string" ? stringToBigInt(value, maxBigIntDigits) : typeof value === "number" ? numberToBigInt(value) : value
25
- }],
26
- [JsonSchemaXNativeType.Url, {
27
- types: ["string"],
28
- is: (value) => value instanceof URL,
29
- coerce: (value) => typeof value === "string" ? stringToURL(value) : value
30
- }],
31
- [JsonSchemaXNativeType.Set, {
32
- types: ["array"],
33
- is: (value) => value instanceof Set,
34
- coerce: (value) => Array.isArray(value) ? arrayToSet(value) : value,
35
- afterTypeCheck: true
36
- }],
37
- [JsonSchemaXNativeType.Map, {
38
- types: ["array"],
39
- is: (value) => value instanceof Map,
40
- coerce: (value) => Array.isArray(value) ? arrayToMap(value) : value,
41
- afterTypeCheck: true
42
- }]
43
- ]))();
44
- function minSatisfaction(a, b) {
45
- return a < b ? a : b;
46
- }
47
- const MIN_BUDGETED_VALUES = 100;
48
- function createContext() {
49
- return {
50
- rootSchema: true,
51
- memo: undefined,
52
- memoize: false,
53
- steps: 0,
54
- maxSteps: 0,
55
- input: undefined
56
- };
57
- }
58
- class CoercionBudgetExceededError extends Error {}
59
- export class JsonSchemaCoercer {
60
- nativeCoerceOptions;
61
- maxStepsPerValue;
62
-
63
- idleContext = createContext();
64
- constructor(options = {}) {
65
- this.nativeCoerceOptions = { maxBigIntDigits: options.maxBigIntDigits ?? DEFAULT_MAX_BIGINT_DIGITS };
66
- this.maxStepsPerValue = options.maxStepsPerValue ?? 1e3;
67
- }
68
- coerce([schema, optional], value) {
69
- if (optional && value === undefined) {
70
- return value;
71
- }
72
- const context = this.idleContext ?? createContext();
73
- this.idleContext = undefined;
74
- context.rootSchema = schema;
75
- context.memoize = false;
76
- context.steps = 0;
77
- context.maxSteps = this.maxStepsPerValue * MIN_BUDGETED_VALUES;
78
- context.input = value;
79
- try {
80
- const [, coerced] = this.coerceInternal(context, schema, value);
81
- return coerced;
82
- } catch (error) {
83
-
84
- if (error instanceof CoercionBudgetExceededError) {
85
- return value;
86
- }
87
- throw error;
88
- } finally {
89
-
90
- context.memo = undefined;
91
- context.input = undefined;
92
- this.idleContext = context;
93
- }
94
- }
95
- coerceInternal(context, schema, value, appliedRefs) {
96
- if (++context.steps > context.maxSteps) {
97
- this.extendBudget(context);
98
- }
99
- if (typeof schema === "boolean") {
100
- return [schema ? SATISFIED : UNSATISFIED, value];
101
- }
102
- if (Array.isArray(schema.type)) {
103
- return this.coerceInternal(context, { anyOf: schema.type.map((type) => ({
104
- ...schema,
105
- type
106
- })) }, value, appliedRefs);
107
- }
108
- let coerced = value;
109
- let satisfied = SATISFIED;
110
- if (typeof schema.$ref === "string") {
111
- const resolved = schema.$ref.startsWith("#/") ? get(context.rootSchema, schema.$ref.slice("#/".length).split("/").map(decodeJsonPointerSegment)) : schema.$ref === "#" ? context.rootSchema : undefined;
112
- if (resolved !== undefined && !appliedRefs?.has(resolved)) {
113
- if (schema.type === "object" || schema.type === "array") {
114
- context.memoize = true;
115
- }
116
- const [subSatisfied, subCoerced] = this.coerceRef(context, resolved, coerced, appliedRefs);
117
- coerced = subCoerced;
118
- satisfied = minSatisfaction(satisfied, subSatisfied);
119
- }
120
- }
121
- const enumValues = schema.const !== undefined ? [schema.const] : schema.enum;
122
- if (enumValues !== undefined && !enumValues.includes(coerced)) {
123
- if (typeof coerced === "string") {
124
- const numberValue = stringToNumber(coerced);
125
- if (enumValues.includes(numberValue)) {
126
- coerced = numberValue;
127
- } else {
128
- const booleanValue = stringToBoolean(coerced);
129
- if (enumValues.includes(booleanValue)) {
130
- coerced = booleanValue;
131
- } else {
132
- satisfied = UNSATISFIED;
133
- }
134
- }
135
- } else {
136
- satisfied = UNSATISFIED;
137
- }
138
- }
139
- const declaredNativeType = NATIVE_TYPES.get(schema["x-native-type"]);
140
- const nativeType = schema.type === undefined || declaredNativeType?.types.includes(schema.type) ? declaredNativeType : undefined;
141
- if (nativeType && !nativeType.afterTypeCheck) {
142
- coerced = nativeType.coerce(coerced, this.nativeCoerceOptions);
143
- }
144
- if (schema.type && !nativeType?.is(coerced)) {
145
- switch (schema.type) {
146
- case "null": {
147
- if (coerced !== null) {
148
- satisfied = UNSATISFIED;
149
- }
150
- break;
151
- }
152
- case "string": {
153
- if (typeof coerced !== "string") {
154
- satisfied = UNSATISFIED;
155
- }
156
- break;
157
- }
158
- case "number": {
159
- if (typeof coerced === "string") {
160
- coerced = stringToNumber(coerced);
161
- }
162
- if (typeof coerced !== "number") {
163
- satisfied = UNSATISFIED;
164
- }
165
- break;
166
- }
167
- case "integer": {
168
- if (typeof coerced === "string") {
169
- coerced = stringToInteger(coerced);
170
- }
171
- if (typeof coerced !== "number" || !Number.isInteger(coerced)) {
172
- satisfied = UNSATISFIED;
173
- }
174
- break;
175
- }
176
- case "boolean": {
177
- if (typeof coerced === "string") {
178
- coerced = stringToBoolean(coerced);
179
- }
180
- if (typeof coerced !== "boolean") {
181
- satisfied = UNSATISFIED;
182
- }
183
- break;
184
- }
185
- case "array": {
186
- if (Array.isArray(coerced)) {
187
- const prefixItemSchemas = "prefixItems" in schema ? toArray(schema.prefixItems) : Array.isArray(schema.items) ? schema.items : [];
188
- const itemSchema = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
189
- let shouldUseCoercedItems = false;
190
- const coercedItems = coerced.map((item, i) => {
191
- const subSchema = prefixItemSchemas[i] ?? itemSchema;
192
- if (subSchema === undefined) {
193
- satisfied = minSatisfaction(satisfied, LOOSELY_SATISFIED);
194
- return item;
195
- }
196
- const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, item);
197
- satisfied = minSatisfaction(satisfied, subSatisfied);
198
- if (subCoerced !== item) {
199
- shouldUseCoercedItems = true;
200
- }
201
- return subCoerced;
202
- });
203
- if (typeof schema.minItems === "number" && coercedItems.length < schema.minItems) {
204
- satisfied = UNSATISFIED;
205
- }
206
- if (shouldUseCoercedItems) {
207
- coerced = coercedItems;
208
- }
209
- } else {
210
- satisfied = UNSATISFIED;
211
- }
212
- break;
213
- }
214
- case "object": {
215
- if (Array.isArray(coerced)) {
216
- coerced = { ...coerced };
217
- }
218
- if (isPlainObject(coerced)) {
219
- let shouldUseCoercedItems = false;
220
-
221
- const coercedItems = { ...coerced };
222
- const patternProperties = Object.entries(schema.patternProperties ?? {}).flatMap(([key, value]) => {
223
-
224
- const pattern = tryOrUndefined(() => new RegExp(key));
225
- return pattern ? [[pattern, value]] : [];
226
- });
227
- const propertySchemas = schema.properties ?? {};
228
- for (const key of Object.keys(coerced)) {
229
- const value = coerced[key];
230
- const subSchema = getOwn(propertySchemas, key) ?? patternProperties.find(([pattern]) => pattern.test(key))?.[1] ?? schema.additionalProperties;
231
- if (value !== undefined || schema.required?.includes(key)) {
232
- if (subSchema === undefined) {
233
- satisfied = minSatisfaction(satisfied, LOOSELY_SATISFIED);
234
- } else {
235
- const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, value);
236
- coercedItems[key] = subCoerced;
237
- satisfied = minSatisfaction(satisfied, subSatisfied);
238
- if (subCoerced !== value) {
239
- shouldUseCoercedItems = true;
240
- }
241
- }
242
- }
243
- }
244
- if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) {
245
- satisfied = UNSATISFIED;
246
- }
247
- if (shouldUseCoercedItems) {
248
- coerced = coercedItems;
249
- }
250
- } else {
251
- satisfied = UNSATISFIED;
252
- }
253
- break;
254
- }
255
- }
256
- }
257
- if (nativeType) {
258
- if (nativeType.afterTypeCheck) {
259
- coerced = nativeType.coerce(coerced, this.nativeCoerceOptions);
260
- }
261
- if (!nativeType.is(coerced)) {
262
- satisfied = UNSATISFIED;
263
- }
264
- }
265
- if (schema.allOf) {
266
- context.memoize = true;
267
- for (const subSchema of schema.allOf) {
268
- const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, coerced, appliedRefs);
269
- coerced = subCoerced;
270
- satisfied = minSatisfaction(satisfied, subSatisfied);
271
- }
272
- }
273
- for (const key of ["anyOf", "oneOf"]) {
274
- if (schema[key]) {
275
- context.memoize = true;
276
-
277
- let best;
278
- for (const subSchema of schema[key]) {
279
- const [subSatisfied, subCoerced] = this.coerceInternal(context, subSchema, coerced, appliedRefs);
280
- if (subSatisfied === UNSATISFIED) {
281
- continue;
282
- }
283
- if (subCoerced === coerced) {
284
- best = {
285
- satisfied: subSatisfied,
286
- value: subCoerced
287
- };
288
- break;
289
- }
290
- if (!best || subSatisfied > best.satisfied) {
291
- best = {
292
- satisfied: subSatisfied,
293
- value: subCoerced
294
- };
295
- }
296
- }
297
-
298
- if (best) {
299
- coerced = best.value;
300
- satisfied = minSatisfaction(satisfied, best.satisfied);
301
- } else {
302
- satisfied = UNSATISFIED;
303
- }
304
- }
305
- }
306
- if (typeof schema.not !== "undefined") {
307
- context.memoize = true;
308
- const [notSatisfied] = this.coerceInternal(context, schema.not, coerced, appliedRefs);
309
- if (notSatisfied !== UNSATISFIED) {
310
- satisfied = UNSATISFIED;
311
- }
312
- }
313
- return [satisfied, coerced];
314
- }
315
- coerceRef(context, resolved, value, appliedRefs) {
316
- let memo;
317
-
318
- if (context.memoize && !appliedRefs?.size && isTypescriptObject(value)) {
319
- context.memo ??= new Map();
320
- memo = context.memo.get(resolved);
321
- if (!memo) {
322
- memo = new Map();
323
- context.memo.set(resolved, memo);
324
- }
325
- const cached = memo.get(value);
326
- if (cached) {
327
- return cached;
328
- }
329
- }
330
- appliedRefs ??= new Set();
331
- appliedRefs.add(resolved);
332
- const result = this.coerceInternal(context, resolved, value, appliedRefs);
333
- appliedRefs.delete(resolved);
334
- memo?.set(value, result);
335
- return result;
336
- }
337
- extendBudget(context) {
338
- const limit = 2 * context.steps / this.maxStepsPerValue;
339
- context.maxSteps = this.maxStepsPerValue * Math.max(countValues(context.input, limit), MIN_BUDGETED_VALUES);
340
- if (context.steps > context.maxSteps) {
341
- throw new CoercionBudgetExceededError();
342
- }
343
- }
344
- }
345
- function countValues(input, limit) {
346
- const walked = new Set();
347
- const stack = [input];
348
- let count = 1;
349
- while (stack.length) {
350
- const value = stack.pop();
351
- if ((Array.isArray(value) || isPlainObject(value)) && !walked.has(value)) {
352
- walked.add(value);
353
- const items = Array.isArray(value) ? value : Object.values(value);
354
- for (let i = 0; i < items.length; i++) {
355
- if (++count >= limit) {
356
- return count;
357
- }
358
- const item = items[i];
359
- if (isTypescriptObject(item)) {
360
- stack.push(item);
361
- }
362
- }
363
- }
364
- }
365
- return count;
366
- }
367
- const NUMBER_PATTERN = /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/;
368
- function stringToNumber(value) {
369
- if (!NUMBER_PATTERN.test(value)) {
370
- return value;
371
- }
372
- const num = Number(value);
373
-
374
- if (num < Number.MIN_SAFE_INTEGER || num > Number.MAX_SAFE_INTEGER) {
375
- return value;
376
- }
377
- return num;
378
- }
379
- const INTEGER_PATTERN = /^-?(?:0|[1-9]\d*)$/;
380
- function stringToInteger(value) {
381
- if (!INTEGER_PATTERN.test(value)) {
382
- return value;
383
- }
384
- const num = Number(value);
385
- if (!Number.isSafeInteger(num)) {
386
- return value;
387
- }
388
- return num;
389
- }
390
- function stringToBigInt(value, maxDigits) {
391
- return parseBigIntString(value, maxDigits) ?? value;
392
- }
393
- function numberToBigInt(value) {
394
- if (!Number.isInteger(value)) {
395
- return value;
396
- }
397
- return BigInt(value);
398
- }
399
- function stringToBoolean(value) {
400
- const lower = value.toLowerCase();
401
- if (lower === "false" || lower === "off") {
402
- return false;
403
- }
404
- if (lower === "true" || lower === "on") {
405
- return true;
406
- }
407
- return value;
408
- }
409
- const DATE_TIME_PATTERN = /^(?!-0{6})(?:[+-]\d{2})?\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
410
- const DAYS_IN_MONTH = [
411
- 31,
412
- 29,
413
- 31,
414
- 30,
415
- 31,
416
- 30,
417
- 31,
418
- 31,
419
- 30,
420
- 31,
421
- 30,
422
- 31
423
- ];
424
- function stringToDate(value) {
425
- if (!DATE_TIME_PATTERN.test(value)) {
426
- return value;
427
- }
428
- const yearEnd = value.indexOf("-", 1);
429
- const month = readTwoDigits(value, yearEnd + 1);
430
- const day = readTwoDigits(value, yearEnd + 4);
431
- if (day < 1 || day > (DAYS_IN_MONTH[month - 1] ?? 0) || month === 2 && day === 29 && !isLeapYear(Number(value.slice(0, yearEnd)))) {
432
- return value;
433
- }
434
- const date = new Date(value);
435
- if (Number.isNaN(date.getTime())) {
436
- return value;
437
- }
438
- return date;
439
- }
440
- function readTwoDigits(value, index) {
441
- return (value.charCodeAt(index) - 48) * 10 + value.charCodeAt(index + 1) - 48;
442
- }
443
- function isLeapYear(year) {
444
- return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
445
- }
446
- function stringToURL(value) {
447
- return tryOrUndefined(() => new URL(value)) ?? value;
448
- }
449
- function arrayToSet(value) {
450
- const set = new Set(value);
451
- if (set.size !== value.length) {
452
- return value;
453
- }
454
- return set;
455
- }
456
- function arrayToMap(value) {
457
- if (value.some((item) => !Array.isArray(item) || item.length !== 2)) {
458
- return value;
459
- }
460
- const result = new Map(value);
461
- if (result.size !== value.length) {
462
- return value;
463
- }
464
- return result;
465
- }