@orpc/json-schema 2.0.0-beta.47 → 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.
- package/README.md +6 -7
- package/dist/coercer.d.ts +29 -0
- package/dist/coercer.js +498 -0
- package/dist/composition-utils.d.ts +41 -0
- package/dist/composition-utils.js +380 -0
- package/dist/constants.d.ts +8 -0
- package/dist/constants.js +66 -0
- package/dist/convert.d.ts +29 -0
- package/dist/convert.js +14 -0
- package/dist/index.d.ts +10 -297
- package/dist/index.js +9 -0
- package/dist/ref-utils.d.ts +59 -0
- package/dist/ref-utils.js +253 -0
- package/dist/smart-coercion-handler-plugin.d.ts +22 -0
- package/dist/smart-coercion-handler-plugin.js +50 -0
- package/dist/smart-coercion-link-plugin.d.ts +28 -0
- package/dist/smart-coercion-link-plugin.js +75 -0
- package/dist/standard-json-schema-converter.d.ts +16 -0
- package/dist/standard-json-schema-converter.js +45 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.js +40 -0
- package/dist/utils.d.ts +45 -0
- package/dist/utils.js +106 -0
- package/package.json +25 -27
- package/dist/index.d.mts +0 -297
- package/dist/index.mjs +0 -1117
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { toArray } from '@orpc/shared';
|
|
2
|
+
import { JsonSchemaCoercer } from './coercer.js';
|
|
3
|
+
import { DelegatingJsonSchemaConverter } from './convert.js';
|
|
4
|
+
import { StandardJsonSchemaConverter } from './standard-json-schema-converter.js';
|
|
5
|
+
/**
|
|
6
|
+
* Coerces incoming request data to match the expected `.input` schema types before validation,
|
|
7
|
+
* based on JSON schemas produced by the configured converters.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
|
|
10
|
+
*/
|
|
11
|
+
export class SmartCoercionHandlerPlugin {
|
|
12
|
+
name = '~smart-coercion';
|
|
13
|
+
converter;
|
|
14
|
+
coercer;
|
|
15
|
+
cache = new WeakMap();
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.converter = new DelegatingJsonSchemaConverter([
|
|
18
|
+
...toArray(options.converters),
|
|
19
|
+
new StandardJsonSchemaConverter(),
|
|
20
|
+
]);
|
|
21
|
+
this.coercer = new JsonSchemaCoercer(options);
|
|
22
|
+
}
|
|
23
|
+
init(options) {
|
|
24
|
+
return {
|
|
25
|
+
...options,
|
|
26
|
+
clientInterceptors: [
|
|
27
|
+
({ next, input, ...interceptorOptions }) => {
|
|
28
|
+
const inputSchemas = interceptorOptions.procedure['~orpc'].inputSchemas;
|
|
29
|
+
if (!inputSchemas) {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
const coercedInput = this.coerceValue(inputSchemas, input);
|
|
33
|
+
return next({ ...interceptorOptions, input: coercedInput });
|
|
34
|
+
},
|
|
35
|
+
...toArray(options.clientInterceptors),
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
coerceValue(schemas, value) {
|
|
40
|
+
for (const schema of schemas) {
|
|
41
|
+
let converted = this.cache.get(schema);
|
|
42
|
+
if (!converted) {
|
|
43
|
+
converted = this.converter.convert(schema, 'input');
|
|
44
|
+
this.cache.set(schema, converted);
|
|
45
|
+
}
|
|
46
|
+
value = this.coercer.coerce(converted, value);
|
|
47
|
+
}
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ClientContext } from '@orpc/client';
|
|
2
|
+
import type { StandardLinkOptions, StandardLinkPlugin } from '@orpc/client/standard';
|
|
3
|
+
import type { RouterContract } from '@orpc/contract';
|
|
4
|
+
import type { JsonSchemaCoercerOptions } from './coercer.ts';
|
|
5
|
+
import type { JsonSchemaConverter } from './convert.ts';
|
|
6
|
+
export interface SmartCoercionLinkPluginOptions extends JsonSchemaCoercerOptions {
|
|
7
|
+
converters?: JsonSchemaConverter[] | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Coerces server responses to match the expected `.output` or `.errors` schema types before validation,
|
|
11
|
+
* based on JSON schemas produced by the configured converters.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
|
|
14
|
+
*/
|
|
15
|
+
export declare class SmartCoercionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
16
|
+
private readonly contract;
|
|
17
|
+
name: string;
|
|
18
|
+
/**
|
|
19
|
+
* Output and error values should be coerced before validation.
|
|
20
|
+
*/
|
|
21
|
+
after: string[];
|
|
22
|
+
private readonly converter;
|
|
23
|
+
private readonly coercer;
|
|
24
|
+
private readonly cache;
|
|
25
|
+
constructor(contract: RouterContract, options?: SmartCoercionLinkPluginOptions);
|
|
26
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
27
|
+
private coerceValue;
|
|
28
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { cloneORPCError, ORPCError } from '@orpc/client';
|
|
2
|
+
import { getProcedureContractOrThrow } from '@orpc/contract';
|
|
3
|
+
import { getOwn, toArray } from '@orpc/shared';
|
|
4
|
+
import { JsonSchemaCoercer } from './coercer.js';
|
|
5
|
+
import { DelegatingJsonSchemaConverter } from './convert.js';
|
|
6
|
+
import { StandardJsonSchemaConverter } from './standard-json-schema-converter.js';
|
|
7
|
+
/**
|
|
8
|
+
* Coerces server responses to match the expected `.output` or `.errors` schema types before validation,
|
|
9
|
+
* based on JSON schemas produced by the configured converters.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/plugins/smart-coercion | Smart Coercion Plugin}
|
|
12
|
+
*/
|
|
13
|
+
export class SmartCoercionLinkPlugin {
|
|
14
|
+
contract;
|
|
15
|
+
name = '~smart-coercion';
|
|
16
|
+
/**
|
|
17
|
+
* Output and error values should be coerced before validation.
|
|
18
|
+
*/
|
|
19
|
+
after = ['~response-validation'];
|
|
20
|
+
converter;
|
|
21
|
+
coercer;
|
|
22
|
+
cache = new WeakMap();
|
|
23
|
+
constructor(contract, options = {}) {
|
|
24
|
+
this.contract = contract;
|
|
25
|
+
this.converter = new DelegatingJsonSchemaConverter([
|
|
26
|
+
...toArray(options.converters),
|
|
27
|
+
new StandardJsonSchemaConverter(),
|
|
28
|
+
]);
|
|
29
|
+
this.coercer = new JsonSchemaCoercer(options);
|
|
30
|
+
}
|
|
31
|
+
init(options) {
|
|
32
|
+
return {
|
|
33
|
+
...options,
|
|
34
|
+
interceptors: [
|
|
35
|
+
...toArray(options.interceptors),
|
|
36
|
+
async ({ next, path }) => {
|
|
37
|
+
const procedure = getProcedureContractOrThrow(this.contract, path);
|
|
38
|
+
try {
|
|
39
|
+
const output = await next();
|
|
40
|
+
const outputSchemas = procedure['~orpc'].outputSchemas;
|
|
41
|
+
if (!outputSchemas) {
|
|
42
|
+
return output;
|
|
43
|
+
}
|
|
44
|
+
const coercedOutput = this.coerceValue(outputSchemas, output);
|
|
45
|
+
return coercedOutput;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (!(error instanceof ORPCError) || !error.defined) {
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
const errorMap = procedure['~orpc'].errorMap;
|
|
52
|
+
const dataSchema = getOwn(errorMap, error.code)?.data;
|
|
53
|
+
if (!dataSchema) {
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
const cloned = cloneORPCError(error);
|
|
57
|
+
cloned.data = this.coerceValue([dataSchema], cloned.data);
|
|
58
|
+
throw cloned;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
coerceValue(schemas, value) {
|
|
65
|
+
for (const schema of schemas) {
|
|
66
|
+
let converted = this.cache.get(schema);
|
|
67
|
+
if (!converted) {
|
|
68
|
+
converted = this.converter.convert(schema, 'output');
|
|
69
|
+
this.cache.set(schema, converted);
|
|
70
|
+
}
|
|
71
|
+
value = this.coercer.coerce(converted, value);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnySchema } from '@orpc/contract';
|
|
2
|
+
import type { StandardJSONSchemaV1 } from '@standard-schema/spec';
|
|
3
|
+
import type { JsonSchemaConverter, JsonSchemaConverterDirection } from './convert.ts';
|
|
4
|
+
import type { JsonSchema } from './types.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Returns true when the schema accepts `undefined` in the given direction,
|
|
7
|
+
* found by validating `undefined` against it. For `output`, the validated value must also be `undefined`.
|
|
8
|
+
*
|
|
9
|
+
* Returns false when validation throws or is async. An async result is never left as an unhandled rejection.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isStandardSchemaOptional(schema: AnySchema, direction: JsonSchemaConverterDirection): boolean;
|
|
12
|
+
export declare class StandardJsonSchemaConverter implements JsonSchemaConverter {
|
|
13
|
+
condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
|
|
14
|
+
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
15
|
+
convertInternal(schema: StandardJSONSchemaV1 & AnySchema, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
16
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isNonPrimitive } from '@orpc/shared';
|
|
2
|
+
/**
|
|
3
|
+
* Returns true when the schema accepts `undefined` in the given direction,
|
|
4
|
+
* found by validating `undefined` against it. For `output`, the validated value must also be `undefined`.
|
|
5
|
+
*
|
|
6
|
+
* Returns false when validation throws or is async. An async result is never left as an unhandled rejection.
|
|
7
|
+
*/
|
|
8
|
+
export function isStandardSchemaOptional(schema, direction) {
|
|
9
|
+
try {
|
|
10
|
+
const result = schema['~standard'].validate(undefined);
|
|
11
|
+
if (result instanceof Promise) {
|
|
12
|
+
result.catch(() => { });
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return !result.issues && (direction === 'input' || result.value === undefined);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class StandardJsonSchemaConverter {
|
|
22
|
+
condition(schema, _direction) {
|
|
23
|
+
return Boolean(schema &&
|
|
24
|
+
'jsonSchema' in schema['~standard'] &&
|
|
25
|
+
isNonPrimitive(schema['~standard'].jsonSchema) &&
|
|
26
|
+
'input' in schema['~standard'].jsonSchema &&
|
|
27
|
+
typeof schema['~standard'].jsonSchema.input === 'function' &&
|
|
28
|
+
'output' in schema['~standard'].jsonSchema &&
|
|
29
|
+
typeof schema['~standard'].jsonSchema.output === 'function');
|
|
30
|
+
}
|
|
31
|
+
convert(schema, direction) {
|
|
32
|
+
return this.convertInternal(schema, direction);
|
|
33
|
+
}
|
|
34
|
+
convertInternal(schema, direction) {
|
|
35
|
+
try {
|
|
36
|
+
const jsonSchema = direction === 'input'
|
|
37
|
+
? schema['~standard'].jsonSchema.input({ target: 'draft-2020-12' })
|
|
38
|
+
: schema['~standard'].jsonSchema.output({ target: 'draft-2020-12' });
|
|
39
|
+
return [jsonSchema, isStandardSchemaOptional(schema, direction)];
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return [{}, true];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type * as OpenAPIV3_2 from '@openapi-spec/types/v3.2';
|
|
2
|
+
/**
|
|
3
|
+
* A JSON Schema (draft 2020-12) representation used across oRPC's JSON schema tooling.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* It is also the OpenAPI 3.2 Schema Object, so converted schemas embed into OpenAPI documents as-is.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://orpc.dev/docs/integrations/standard-schema | Standard Schema Integration}
|
|
9
|
+
*/
|
|
10
|
+
export type JsonSchema<Value = any> = OpenAPIV3_2.SchemaObject<Value>;
|
|
11
|
+
/**
|
|
12
|
+
* The declared JSON Schema keywords, the index signature is filtered out.
|
|
13
|
+
*/
|
|
14
|
+
export type JsonSchemaKeywords = keyof {
|
|
15
|
+
[K in keyof OpenAPIV3_2.SchemaObjectFields as string extends K ? never : K]: unknown;
|
|
16
|
+
};
|
|
17
|
+
export declare enum JsonSchemaType {
|
|
18
|
+
Array = "array",
|
|
19
|
+
Boolean = "boolean",
|
|
20
|
+
Integer = "integer",
|
|
21
|
+
Null = "null",
|
|
22
|
+
Number = "number",
|
|
23
|
+
Object = "object",
|
|
24
|
+
String = "string"
|
|
25
|
+
}
|
|
26
|
+
export declare enum JsonSchemaFormat {
|
|
27
|
+
Date = "date",
|
|
28
|
+
DateTime = "date-time",
|
|
29
|
+
Duration = "duration",
|
|
30
|
+
Email = "email",
|
|
31
|
+
Hostname = "hostname",
|
|
32
|
+
IDNEmail = "idn-email",
|
|
33
|
+
IDNHostname = "idn-hostname",
|
|
34
|
+
IPv4 = "ipv4",
|
|
35
|
+
IPv6 = "ipv6",
|
|
36
|
+
IRI = "iri",
|
|
37
|
+
IRIReference = "iri-reference",
|
|
38
|
+
JSONPointer = "json-pointer",
|
|
39
|
+
RegEx = "regex",
|
|
40
|
+
RelativeJSONPointer = "relative-json-pointer",
|
|
41
|
+
Time = "time",
|
|
42
|
+
URI = "uri",
|
|
43
|
+
URIReference = "uri-reference",
|
|
44
|
+
URITemplate = "uri-template",
|
|
45
|
+
UUID = "uuid"
|
|
46
|
+
}
|
|
47
|
+
export declare enum JsonSchemaXNativeType {
|
|
48
|
+
BigInt = "bigint",
|
|
49
|
+
Date = "date",
|
|
50
|
+
Url = "url",
|
|
51
|
+
Set = "set",
|
|
52
|
+
Map = "map"
|
|
53
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export var JsonSchemaType;
|
|
2
|
+
(function (JsonSchemaType) {
|
|
3
|
+
JsonSchemaType["Array"] = "array";
|
|
4
|
+
JsonSchemaType["Boolean"] = "boolean";
|
|
5
|
+
JsonSchemaType["Integer"] = "integer";
|
|
6
|
+
JsonSchemaType["Null"] = "null";
|
|
7
|
+
JsonSchemaType["Number"] = "number";
|
|
8
|
+
JsonSchemaType["Object"] = "object";
|
|
9
|
+
JsonSchemaType["String"] = "string";
|
|
10
|
+
})(JsonSchemaType || (JsonSchemaType = {}));
|
|
11
|
+
export var JsonSchemaFormat;
|
|
12
|
+
(function (JsonSchemaFormat) {
|
|
13
|
+
JsonSchemaFormat["Date"] = "date";
|
|
14
|
+
JsonSchemaFormat["DateTime"] = "date-time";
|
|
15
|
+
JsonSchemaFormat["Duration"] = "duration";
|
|
16
|
+
JsonSchemaFormat["Email"] = "email";
|
|
17
|
+
JsonSchemaFormat["Hostname"] = "hostname";
|
|
18
|
+
JsonSchemaFormat["IDNEmail"] = "idn-email";
|
|
19
|
+
JsonSchemaFormat["IDNHostname"] = "idn-hostname";
|
|
20
|
+
JsonSchemaFormat["IPv4"] = "ipv4";
|
|
21
|
+
JsonSchemaFormat["IPv6"] = "ipv6";
|
|
22
|
+
JsonSchemaFormat["IRI"] = "iri";
|
|
23
|
+
JsonSchemaFormat["IRIReference"] = "iri-reference";
|
|
24
|
+
JsonSchemaFormat["JSONPointer"] = "json-pointer";
|
|
25
|
+
JsonSchemaFormat["RegEx"] = "regex";
|
|
26
|
+
JsonSchemaFormat["RelativeJSONPointer"] = "relative-json-pointer";
|
|
27
|
+
JsonSchemaFormat["Time"] = "time";
|
|
28
|
+
JsonSchemaFormat["URI"] = "uri";
|
|
29
|
+
JsonSchemaFormat["URIReference"] = "uri-reference";
|
|
30
|
+
JsonSchemaFormat["URITemplate"] = "uri-template";
|
|
31
|
+
JsonSchemaFormat["UUID"] = "uuid";
|
|
32
|
+
})(JsonSchemaFormat || (JsonSchemaFormat = {}));
|
|
33
|
+
export var JsonSchemaXNativeType;
|
|
34
|
+
(function (JsonSchemaXNativeType) {
|
|
35
|
+
JsonSchemaXNativeType["BigInt"] = "bigint";
|
|
36
|
+
JsonSchemaXNativeType["Date"] = "date";
|
|
37
|
+
JsonSchemaXNativeType["Url"] = "url";
|
|
38
|
+
JsonSchemaXNativeType["Set"] = "set";
|
|
39
|
+
JsonSchemaXNativeType["Map"] = "map";
|
|
40
|
+
})(JsonSchemaXNativeType || (JsonSchemaXNativeType = {}));
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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 type { JsonSchema } from './types.ts';
|
|
6
|
+
/**
|
|
7
|
+
* Returns true when the schema only describes primitive values, by a primitive `type`
|
|
8
|
+
* (or a non-empty list of them, such as `['string', 'null']`), a `const`, or an `enum`.
|
|
9
|
+
* Composition keywords are not inspected, so flatten unions with {@link flattenJsonUnionSchema} first.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isJsonPrimitiveSchema(schema: JsonSchema): schema is JsonSchema & object;
|
|
12
|
+
export type JsonFileSchema = JsonSchema & object & {
|
|
13
|
+
type: 'string';
|
|
14
|
+
contentMediaType?: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Returns true when the schema is a file-like string schema.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isJsonFileSchema(schema: JsonSchema): schema is JsonFileSchema;
|
|
20
|
+
/**
|
|
21
|
+
* Returns true when a file schema is reachable from the schema, such as through a property, a composition, or a local `$ref`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function containsJsonFileSchema(schema: JsonSchema): boolean;
|
|
24
|
+
export type JsonObjectSchema = JsonSchema & object & {
|
|
25
|
+
type: 'object';
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Returns true when the schema is an object schema.
|
|
29
|
+
*/
|
|
30
|
+
export declare function isJsonObjectSchema(schema: JsonSchema): schema is JsonObjectSchema;
|
|
31
|
+
export type JsonArraySchema = JsonSchema & object & {
|
|
32
|
+
type: 'array';
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Returns true when the schema is an array schema.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isJsonArraySchema(schema: JsonSchema): schema is JsonArraySchema;
|
|
38
|
+
/**
|
|
39
|
+
* Returns true when the schema does not apply any recognized constraints.
|
|
40
|
+
*/
|
|
41
|
+
export declare function isUnconstrainedSchema(schema: JsonSchema): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Ensures a JSON Schema is represented as an object schema document.
|
|
44
|
+
*/
|
|
45
|
+
export declare function ensureJsonSchemaObject(schema: JsonSchema): Exclude<JsonSchema, boolean>;
|
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
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.47",
|
|
3
|
+
"version": "2.0.0-beta.49",
|
|
5
4
|
"description": "JSON Schema conversion and smart request coercion for oRPC and OpenAPI",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
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
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
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.
|
|
30
|
-
"import": "./dist/index.
|
|
31
|
-
"default": "./dist/index.
|
|
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.
|
|
39
|
-
"@orpc/client": "2.0.0-beta.
|
|
40
|
-
"@orpc/contract": "2.0.0-beta.
|
|
41
|
-
"@orpc/server": "2.0.0-beta.
|
|
42
|
-
"@orpc/shared": "2.0.0-beta.
|
|
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
|
}
|