@orpc/json-schema 2.0.0-beta.5 → 2.0.0-beta.50

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/dist/utils.js ADDED
@@ -0,0 +1,74 @@
1
+ import { JSON_SCHEMA_LOGIC_KEYWORDS, JSON_SCHEMA_PRIMITIVE_TYPES, JSON_SCHEMA_RECORD_KEYWORDS } from "./constants.js";
2
+ import { resolveJsonSchemaLocalPointer } from "./ref-utils.js";
3
+ // src/utils.ts
4
+ /**
5
+ * These utilities assume the schema has only one root-level `$defs` object
6
+ * and exclusively use absolute JSON pointers for `$ref` values.
7
+ */
8
+ /**
9
+ * Returns true when the schema only describes primitive values, by a primitive `type`
10
+ * (or a non-empty list of them, such as `['string', 'null']`), a `const`, or an `enum`.
11
+ * Composition keywords are not inspected, so flatten unions with {@link flattenJsonUnionSchema} first.
12
+ */
13
+ function isJsonPrimitiveSchema(schema) {
14
+ if (typeof schema === "boolean") return false;
15
+ if (typeof schema.type === "string" && JSON_SCHEMA_PRIMITIVE_TYPES.has(schema.type)) return true;
16
+ if (Array.isArray(schema.type) && schema.type.length > 0 && schema.type.every((type) => JSON_SCHEMA_PRIMITIVE_TYPES.has(type))) return true;
17
+ if (schema.const !== void 0) return true;
18
+ if (schema.enum !== void 0) return true;
19
+ return false;
20
+ }
21
+ /**
22
+ * Returns true when the schema is a file-like string schema.
23
+ */
24
+ function isJsonFileSchema(schema) {
25
+ return typeof schema !== "boolean" && schema.type === "string" && (typeof schema.contentMediaType === "string" || schema.format === "binary" || schema.contentEncoding === "binary");
26
+ }
27
+ /**
28
+ * Returns true when a file schema is reachable from the schema, such as through a property, a composition, or a local `$ref`.
29
+ */
30
+ function containsJsonFileSchema(schema) {
31
+ const seen = /* @__PURE__ */ new Set();
32
+ const visit = (value, schemaLevel) => {
33
+ if (!value || typeof value !== "object" || seen.has(value)) return false;
34
+ seen.add(value);
35
+ if (Array.isArray(value)) return value.some((item) => visit(item, schemaLevel));
36
+ const record = value;
37
+ if (schemaLevel && isJsonFileSchema(record)) return true;
38
+ return Object.keys(record).some((key) => {
39
+ const val = record[key];
40
+ if (key === "$ref" && typeof val === "string") return visit(resolveJsonSchemaLocalPointer(schema, val), true);
41
+ if (!schemaLevel) return visit(val, true);
42
+ return key !== "$defs" && (JSON_SCHEMA_LOGIC_KEYWORDS.has(key) || JSON_SCHEMA_RECORD_KEYWORDS.has(key)) && visit(val, !JSON_SCHEMA_RECORD_KEYWORDS.has(key));
43
+ });
44
+ };
45
+ return visit(schema, true);
46
+ }
47
+ /**
48
+ * Returns true when the schema is an object schema.
49
+ */
50
+ function isJsonObjectSchema(schema) {
51
+ return typeof schema !== "boolean" && schema.type === "object";
52
+ }
53
+ /**
54
+ * Returns true when the schema is an array schema.
55
+ */
56
+ function isJsonArraySchema(schema) {
57
+ return typeof schema !== "boolean" && schema.type === "array";
58
+ }
59
+ /**
60
+ * Returns true when the schema does not apply any recognized constraints.
61
+ */
62
+ function isUnconstrainedSchema(schema) {
63
+ if (typeof schema === "boolean") return schema;
64
+ if (Object.keys(schema).every((k) => !JSON_SCHEMA_LOGIC_KEYWORDS.has(k))) return true;
65
+ return false;
66
+ }
67
+ /**
68
+ * Ensures a JSON Schema is represented as an object schema document.
69
+ */
70
+ function ensureJsonSchemaObject(schema) {
71
+ if (typeof schema === "boolean") return schema ? {} : { not: {} };
72
+ return schema;
73
+ }
74
+ export { containsJsonFileSchema, ensureJsonSchemaObject, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isUnconstrainedSchema };
package/package.json CHANGED
@@ -1,42 +1,49 @@
1
1
  {
2
2
  "name": "@orpc/json-schema",
3
- "type": "module",
4
- "version": "2.0.0-beta.5",
5
- "license": "MIT",
3
+ "version": "2.0.0-beta.50",
4
+ "description": "JSON Schema conversion and smart request coercion for oRPC and OpenAPI",
5
+ "keywords": [
6
+ "coercion",
7
+ "json-schema",
8
+ "openapi",
9
+ "orpc",
10
+ "typescript",
11
+ "validation"
12
+ ],
6
13
  "homepage": "https://orpc.dev",
14
+ "license": "MIT",
7
15
  "repository": {
8
16
  "type": "git",
9
17
  "url": "git+https://github.com/middleapi/orpc.git",
10
18
  "directory": "packages/json-schema"
11
19
  },
12
- "keywords": [
13
- "orpc"
20
+ "funding": [
21
+ "https://github.com/sponsors/dinwwwh",
22
+ "https://opencollective.com/middleapi"
14
23
  ],
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "type": "module",
15
28
  "sideEffects": false,
16
29
  "exports": {
17
30
  "./package.json": "./package.json",
18
31
  ".": {
19
- "types": "./dist/index.d.mts",
20
- "import": "./dist/index.mjs",
21
- "default": "./dist/index.mjs"
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "default": "./dist/index.js"
22
35
  }
23
36
  },
24
- "files": [
25
- "dist"
26
- ],
27
37
  "dependencies": {
28
- "@standard-schema/spec": "^1.1.0",
29
- "json-schema-typed": "^8.0.2",
30
- "@orpc/contract": "2.0.0-beta.5",
31
- "@orpc/server": "2.0.0-beta.5",
32
- "@orpc/shared": "2.0.0-beta.5",
33
- "@orpc/client": "2.0.0-beta.5"
38
+ "@openapi-spec/types": "~0.8.0",
39
+ "@orpc/client": "2.0.0-beta.50",
40
+ "@orpc/contract": "2.0.0-beta.50",
41
+ "@orpc/server": "2.0.0-beta.50",
42
+ "@orpc/shared": "2.0.0-beta.50",
43
+ "@standard-schema/spec": "^1.1.0"
34
44
  },
35
45
  "devDependencies": {
36
- "zod": "^4.4.3"
46
+ "zod": "^4.6.7"
37
47
  },
38
- "scripts": {
39
- "build": "unbuild",
40
- "type:check": "tsc -b"
41
- }
48
+ "scripts": {}
42
49
  }
package/dist/index.d.mts DELETED
@@ -1,187 +0,0 @@
1
- import * as Draft2020 from 'json-schema-typed/draft-2020-12';
2
- export { Format as JsonSchemaFormat, TypeName as JsonSchemaType } from 'json-schema-typed/draft-2020-12';
3
- import { AnySchema, RouterContract } from '@orpc/contract';
4
- export { AnySchema, Schema } from '@orpc/contract';
5
- import { Promisable } from '@orpc/shared';
6
- import { Context } from '@orpc/server';
7
- import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
8
- import { ClientContext } from '@orpc/client';
9
- import { StandardLinkPlugin, StandardLinkOptions } from '@orpc/client/standard';
10
- import { StandardJSONSchemaV1 } from '@standard-schema/spec';
11
-
12
- type JsonSchema = Draft2020.JSONSchema;
13
- type JsonSchemaKeywords = typeof Draft2020.keywords[number];
14
- declare enum JsonSchemaXNativeType {
15
- BigInt = "bigint",
16
- RegExp = "regexp",
17
- Date = "date",
18
- Url = "url",
19
- Set = "set",
20
- Map = "map"
21
- }
22
-
23
- declare class JsonSchemaCoercer {
24
- coerce([schema, optional]: [schema: JsonSchema, optional: boolean], value: unknown): unknown;
25
- private coerceInternal;
26
- }
27
-
28
- /**
29
- * These utilities assume the schema has only one root-level `$defs` object
30
- * and exclusively use absolute JSON pointers for `$ref` values.
31
- */
32
-
33
- declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
34
- type JsonFileSchema = JsonSchema & object & {
35
- type: 'string';
36
- contentMediaType?: string;
37
- };
38
- /**
39
- * Returns true when the schema is a file-like string schema.
40
- */
41
- declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
42
- type JsonObjectSchema = JsonSchema & object & {
43
- type: 'object';
44
- };
45
- /**
46
- * Returns true when the schema is an object schema.
47
- */
48
- declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
49
- type JsonArraySchema = JsonSchema & object & {
50
- type: 'array';
51
- };
52
- /**
53
- * Returns true when the schema is an array schema.
54
- */
55
- declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
56
- /**
57
- * Returns true when the schema does not apply any recognized constraints.
58
- */
59
- declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
60
- /**
61
- * Ensures a JSON Schema is represented as an object schema document.
62
- */
63
- declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
64
-
65
- /**
66
- * These utilities assume the schema has only one root-level `$defs` object
67
- * and exclusively use absolute JSON pointers for `$ref` values.
68
- */
69
-
70
- /**
71
- * Combines multiple schemas under the requested composition keyword, promoting branch `$defs` to the root.
72
- */
73
- declare function combineJsonSchemasWithComposition(keyword: 'allOf' | 'anyOf' | 'oneOf', schemas: JsonSchema[]): JsonSchema;
74
- type JsonObjectSchemaEntry = [name: string, schema: JsonSchema, optional: boolean];
75
- /**
76
- * Combines object property entries back into a single object schema.
77
- */
78
- declare function combineJsonObjectSchemaEntries(entries: JsonObjectSchemaEntry[]): JsonObjectSchema;
79
- /**
80
- * Parses an object schema, or a composition of object schemas, into property entries.
81
- */
82
- declare function extractJsonObjectSchemaEntries(schema: JsonSchema): JsonObjectSchemaEntry[] | undefined;
83
- /**
84
- * Flattens `anyOf` and `oneOf` unions when they are the only active constraints.
85
- */
86
- declare function flattenJsonUnionSchema(schema: JsonSchema): JsonSchema[];
87
- /**
88
- * Matches a union made of a single item schema and its array form.
89
- */
90
- declare function matchArrayableJsonSchema(schema: JsonSchema): undefined | [itemSchema: JsonSchema, arraySchema: JsonArraySchema];
91
- declare function deduplicateJsonSchemas(schemas: JsonSchema[]): JsonSchema[];
92
-
93
- type JsonSchemaConverterDirection = 'input' | 'output';
94
- interface JsonSchemaConverter {
95
- /**
96
- * Determines whether this converter can handle the given schema.
97
- */
98
- condition(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promisable<boolean>;
99
- /**
100
- * Converts an ORPC schema to a JSON Schema representation.
101
- */
102
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promisable<[jsonSchema: JsonSchema, optional: boolean]>;
103
- }
104
- declare class DelegatingJsonSchemaConverter implements Pick<JsonSchemaConverter, 'convert'> {
105
- private readonly converters;
106
- constructor(converters?: JsonSchemaConverter[]);
107
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): Promise<[jsonSchema: JsonSchema, optional: boolean]>;
108
- }
109
-
110
- /**
111
- * These utilities assume the schema has only one root-level `$defs` object
112
- * and exclusively use absolute JSON pointers for `$ref` values.
113
- */
114
-
115
- /**
116
- * Encodes a JSON Pointer segment according to RFC 6901.
117
- *
118
- * https://datatracker.ietf.org/doc/html/rfc6901
119
- */
120
- declare function encodeJsonPointerSegment(segment: string): string;
121
- /**
122
- * Decodes a JSON Pointer segment according to RFC 6901.
123
- *
124
- * https://datatracker.ietf.org/doc/html/rfc6901
125
- */
126
- declare function decodeJsonPointerSegment(segment: string): string;
127
- declare function mapJsonSchemaRefs(value: JsonSchema, map: (ref: string, path: Array<string | number>) => string, schemaLevel?: boolean, path?: Array<string | number>): JsonSchema;
128
- /**
129
- * Rewrites recursive root `#` refs by moving the schema body into `$defs`.
130
- */
131
- declare function hoistRecursiveRefToDef(schema: JsonSchema): JsonSchema;
132
- /**
133
- * Resolves a local `$ref` at the **root level** of the given schema, if present.
134
- *
135
- * Only handles refs of the form `#/$defs/<name>` pointing into the provided
136
- * (or schema-embedded) `$defs` map. Nested `$ref`s inside sub-schemas are
137
- * intentionally left untouched.
138
- *
139
- * If the ref cannot be resolved (missing `$defs`, unknown key, etc.) the
140
- * schema is returned as-is.
141
- *
142
- * @param schema - The schema whose root-level `$ref` should be resolved.
143
- * @param $defs - Definition map to resolve against. If omitted, falls back to
144
- * `schema.$defs`. When provided, takes precedence over any `$defs` embedded
145
- * in the schema.
146
- */
147
- declare function resolveJsonSchemaRootLocalRef(schema: JsonSchema, $defs?: Exclude<JsonSchema, boolean>['$defs']): JsonSchema;
148
-
149
- interface SmartCoercionHandlerPluginOptions {
150
- converters?: undefined | JsonSchemaConverter[];
151
- }
152
- declare class SmartCoercionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
153
- name: string;
154
- private readonly converter;
155
- private readonly coercer;
156
- private readonly cache;
157
- constructor(options?: SmartCoercionHandlerPluginOptions);
158
- init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
159
- private coerceValue;
160
- }
161
-
162
- interface SmartCoercionLinkPluginOptions {
163
- converters?: undefined | JsonSchemaConverter[];
164
- }
165
- declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
166
- private readonly contract;
167
- name: string;
168
- /**
169
- * Output and error values should be coerced before validation.
170
- */
171
- after: string[];
172
- private readonly converter;
173
- private readonly coercer;
174
- private readonly cache;
175
- constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
176
- init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
177
- private coerceValue;
178
- }
179
-
180
- declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
181
- condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
182
- convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
183
- convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
184
- }
185
-
186
- export { DelegatingJsonSchemaConverter, JsonSchemaCoercer, JsonSchemaXNativeType, SmartCoercionHandlerPlugin, SmartCoercionLinkPlugin, StandardJsonSchemaConverter, combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, decodeJsonPointerSegment, deduplicateJsonSchemas, encodeJsonPointerSegment, ensureJsonSchemaObject, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, hoistRecursiveRefToDef, isJsonArraySchema, isJsonFileSchema, isJsonObjectSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, mapJsonSchemaRefs, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef };
187
- export type { JsonArraySchema, JsonFileSchema, JsonObjectSchema, JsonObjectSchemaEntry, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection, JsonSchemaKeywords, SmartCoercionHandlerPluginOptions, SmartCoercionLinkPluginOptions };