@standard-community/standard-openapi 0.2.0-rc.1 → 0.2.1

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 CHANGED
@@ -18,7 +18,7 @@ For some specific vendor, install the respective package also -
18
18
 
19
19
  | Vendor | Package |
20
20
  | ------- | ------- |
21
- | Zod v3 | `zod-openapi` |
21
+ | Zod v3 | `zod-openapi@4` |
22
22
  | Valibot | `@valibot/to-json-schema` |
23
23
 
24
24
  ## Usage
@@ -39,16 +39,27 @@ const schema = v.pipe(
39
39
  const openapiSchema = await toOpenAPISchema(schema);
40
40
  ```
41
41
 
42
- ## Compatibility
42
+ ### Sync Usage
43
43
 
44
- List of supported validators -
44
+ This is useful for -
45
45
 
46
- | Vendor | Supported |
47
- | ------- | ------- |
48
- | Zod | ✅ |
49
- | Valibot | ✅ |
50
- | ArkType | |
51
- | Typebox | (Using [TypeMap](https://github.com/sinclairzx81/typemap) |
52
- | Effect Schema | 🛠️ |
46
+ 1. Adding support for Unsupported validation libs, like Sury
47
+ 2. Customize the toOpenAPISchema of a supported lib
48
+
49
+ ```ts
50
+ import { toOpenAPISchema, loadVendor } from "@standard-community/standard-openapi";
51
+ import { convertSchemaToJson } from "your-validation-lib";
52
+
53
+ // The lib should support Standard Schema
54
+ // as we use 'schema["~standard"].vendor' to get the vendor name
55
+ // Eg. loadVendor(zod["~standard"].vendor, convertorFunction)
56
+ loadVendor("validation-lib-name", convertSchemaToJson)
53
57
 
54
- You can check the compatibility versions at [standardschema.dev](https://standardschema.dev/)
58
+ // Define your validation schema
59
+ const schema = {
60
+ // ...
61
+ };
62
+
63
+ // Convert it to OpenAPI Schema
64
+ const openapiSchema = toOpenAPISchema(schema);
65
+ ```
@@ -0,0 +1,44 @@
1
+ const errorMessageWrapper = (message) => `standard-openapi: ${message}`;
2
+ const openapiVendorMap = /* @__PURE__ */ new Map();
3
+
4
+ const getToOpenAPISchemaFn = async (vendor) => {
5
+ const cached = openapiVendorMap.get(vendor);
6
+ if (cached) {
7
+ return cached;
8
+ }
9
+ let vendorFnPromise;
10
+ switch (vendor) {
11
+ case "valibot":
12
+ vendorFnPromise = (await import('./valibot-pYvQeYpK.js')).default();
13
+ break;
14
+ case "zod":
15
+ vendorFnPromise = (await import('./zod-FmKJCmIM.js')).default();
16
+ break;
17
+ case "arktype":
18
+ case "effect":
19
+ vendorFnPromise = (await import('./default-B3j_H5mf.js')).default();
20
+ break;
21
+ default:
22
+ throw new Error(
23
+ errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
24
+ );
25
+ }
26
+ const vendorFn = await vendorFnPromise;
27
+ openapiVendorMap.set(vendor, vendorFn);
28
+ return vendorFn;
29
+ };
30
+
31
+ const toOpenAPISchema = async (schema, context = {}) => {
32
+ const fn = await getToOpenAPISchemaFn(schema["~standard"].vendor);
33
+ const { components = {}, options } = context;
34
+ const _schema = await fn(schema, { components, options });
35
+ return {
36
+ schema: _schema,
37
+ components: Object.keys(components).length > 0 ? components : void 0
38
+ };
39
+ };
40
+ function loadVendor(vendor, fn) {
41
+ openapiVendorMap.set(vendor, fn);
42
+ }
43
+
44
+ export { errorMessageWrapper as e, loadVendor as l, toOpenAPISchema as t };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ const errorMessageWrapper = (message) => `standard-openapi: ${message}`;
4
+ const openapiVendorMap = /* @__PURE__ */ new Map();
5
+
6
+ const getToOpenAPISchemaFn = async (vendor) => {
7
+ const cached = openapiVendorMap.get(vendor);
8
+ if (cached) {
9
+ return cached;
10
+ }
11
+ let vendorFnPromise;
12
+ switch (vendor) {
13
+ case "valibot":
14
+ vendorFnPromise = (await Promise.resolve().then(function () { return require('./valibot-CmK54MCp.cjs'); })).default();
15
+ break;
16
+ case "zod":
17
+ vendorFnPromise = (await Promise.resolve().then(function () { return require('./zod-Dcc_GKUA.cjs'); })).default();
18
+ break;
19
+ case "arktype":
20
+ case "effect":
21
+ vendorFnPromise = (await Promise.resolve().then(function () { return require('./default-CLvGm-jP.cjs'); })).default();
22
+ break;
23
+ default:
24
+ throw new Error(
25
+ errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
26
+ );
27
+ }
28
+ const vendorFn = await vendorFnPromise;
29
+ openapiVendorMap.set(vendor, vendorFn);
30
+ return vendorFn;
31
+ };
32
+
33
+ const toOpenAPISchema = async (schema, context = {}) => {
34
+ const fn = await getToOpenAPISchemaFn(schema["~standard"].vendor);
35
+ const { components = {}, options } = context;
36
+ const _schema = await fn(schema, { components, options });
37
+ return {
38
+ schema: _schema,
39
+ components: Object.keys(components).length > 0 ? components : void 0
40
+ };
41
+ };
42
+ function loadVendor(vendor, fn) {
43
+ openapiVendorMap.set(vendor, fn);
44
+ }
45
+
46
+ exports.errorMessageWrapper = errorMessageWrapper;
47
+ exports.loadVendor = loadVendor;
48
+ exports.toOpenAPISchema = toOpenAPISchema;
package/dist/index.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-6sg7Y400.cjs');
3
+ var index = require('./index-DbUgUmtU.cjs');
4
4
 
5
5
 
6
6
 
7
+ exports.loadVendor = index.loadVendor;
7
8
  exports.toOpenAPISchema = index.toOpenAPISchema;
package/dist/index.d.cts CHANGED
@@ -1,12 +1,20 @@
1
- import { StandardSchemaV1 } from '@standard-schema/spec';
1
+ import * as openapi_types from 'openapi-types';
2
2
  import { OpenAPIV3_1 } from 'openapi-types';
3
+ import { StandardSchemaV1 } from '@standard-schema/spec';
4
+
5
+ type ToOpenAPISchemaContext = {
6
+ components: OpenAPIV3_1.ComponentsObject;
7
+ options?: Record<string, unknown>;
8
+ };
9
+ type ToOpenAPISchemaFn = (schema: StandardSchemaV1, context: ToOpenAPISchemaContext) => OpenAPIV3_1.SchemaObject | Promise<OpenAPIV3_1.SchemaObject>;
3
10
 
4
11
  /**
5
12
  * Converts a Standard Schema to a OpenAPI schema.
6
13
  */
7
- declare const toOpenAPISchema: (schema: StandardSchemaV1, options?: Record<string, unknown>) => Promise<{
8
- schema: OpenAPIV3_1.SchemaObject;
9
- components: OpenAPIV3_1.ComponentsObject | undefined;
14
+ declare const toOpenAPISchema: (schema: StandardSchemaV1, context?: Partial<ToOpenAPISchemaContext>) => Promise<{
15
+ schema: openapi_types.OpenAPIV3_1.SchemaObject;
16
+ components: openapi_types.OpenAPIV3_1.ComponentsObject | undefined;
10
17
  }>;
18
+ declare function loadVendor(vendor: string, fn: ToOpenAPISchemaFn): void;
11
19
 
12
- export { toOpenAPISchema };
20
+ export { loadVendor, toOpenAPISchema };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,20 @@
1
- import { StandardSchemaV1 } from '@standard-schema/spec';
1
+ import * as openapi_types from 'openapi-types';
2
2
  import { OpenAPIV3_1 } from 'openapi-types';
3
+ import { StandardSchemaV1 } from '@standard-schema/spec';
4
+
5
+ type ToOpenAPISchemaContext = {
6
+ components: OpenAPIV3_1.ComponentsObject;
7
+ options?: Record<string, unknown>;
8
+ };
9
+ type ToOpenAPISchemaFn = (schema: StandardSchemaV1, context: ToOpenAPISchemaContext) => OpenAPIV3_1.SchemaObject | Promise<OpenAPIV3_1.SchemaObject>;
3
10
 
4
11
  /**
5
12
  * Converts a Standard Schema to a OpenAPI schema.
6
13
  */
7
- declare const toOpenAPISchema: (schema: StandardSchemaV1, options?: Record<string, unknown>) => Promise<{
8
- schema: OpenAPIV3_1.SchemaObject;
9
- components: OpenAPIV3_1.ComponentsObject | undefined;
14
+ declare const toOpenAPISchema: (schema: StandardSchemaV1, context?: Partial<ToOpenAPISchemaContext>) => Promise<{
15
+ schema: openapi_types.OpenAPIV3_1.SchemaObject;
16
+ components: openapi_types.OpenAPIV3_1.ComponentsObject | undefined;
10
17
  }>;
18
+ declare function loadVendor(vendor: string, fn: ToOpenAPISchemaFn): void;
11
19
 
12
- export { toOpenAPISchema };
20
+ export { loadVendor, toOpenAPISchema };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { t as toOpenAPISchema } from './index-BBkvvbCv.js';
1
+ export { l as loadVendor, t as toOpenAPISchema } from './index-2CxeAJPp.js';
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var standardJson = require('@standard-community/standard-json');
4
+ var convert = require('./convert--bmLap0k.cjs');
5
+
6
+ function getToOpenAPISchemaFn() {
7
+ return (schema, context) => standardJson.toJsonSchema(schema, {
8
+ // @ts-expect-error
9
+ overrideAction: ({ valibotAction, jsonSchema }) => {
10
+ const _jsonSchema = convert.convertToOpenAPISchema(jsonSchema, context);
11
+ if (valibotAction.kind === "metadata" && valibotAction.type === "metadata" && !("$ref" in _jsonSchema)) {
12
+ const metadata = valibotAction.metadata;
13
+ if (metadata.example !== void 0) {
14
+ _jsonSchema.example = metadata.example;
15
+ }
16
+ if (metadata.examples && metadata.examples.length > 0) {
17
+ _jsonSchema.examples = metadata.examples;
18
+ }
19
+ if (metadata.ref) {
20
+ context.components.schemas = {
21
+ ...context.components.schemas,
22
+ [metadata.ref]: _jsonSchema
23
+ };
24
+ return {
25
+ $ref: `#/components/schemas/${metadata.ref}`
26
+ };
27
+ }
28
+ }
29
+ return _jsonSchema;
30
+ },
31
+ ...context.options
32
+ });
33
+ }
34
+
35
+ exports.default = getToOpenAPISchemaFn;
@@ -0,0 +1,33 @@
1
+ import { toJsonSchema } from '@standard-community/standard-json';
2
+ import { c as convertToOpenAPISchema } from './convert-BrW5dcj8.js';
3
+
4
+ function getToOpenAPISchemaFn() {
5
+ return (schema, context) => toJsonSchema(schema, {
6
+ // @ts-expect-error
7
+ overrideAction: ({ valibotAction, jsonSchema }) => {
8
+ const _jsonSchema = convertToOpenAPISchema(jsonSchema, context);
9
+ if (valibotAction.kind === "metadata" && valibotAction.type === "metadata" && !("$ref" in _jsonSchema)) {
10
+ const metadata = valibotAction.metadata;
11
+ if (metadata.example !== void 0) {
12
+ _jsonSchema.example = metadata.example;
13
+ }
14
+ if (metadata.examples && metadata.examples.length > 0) {
15
+ _jsonSchema.examples = metadata.examples;
16
+ }
17
+ if (metadata.ref) {
18
+ context.components.schemas = {
19
+ ...context.components.schemas,
20
+ [metadata.ref]: _jsonSchema
21
+ };
22
+ return {
23
+ $ref: `#/components/schemas/${metadata.ref}`
24
+ };
25
+ }
26
+ }
27
+ return _jsonSchema;
28
+ },
29
+ ...context.options
30
+ });
31
+ }
32
+
33
+ export { getToOpenAPISchemaFn as default };
@@ -2,7 +2,7 @@
2
2
 
3
3
  var standardJson = require('@standard-community/standard-json');
4
4
  var convert = require('./convert--bmLap0k.cjs');
5
- var index = require('./index-6sg7Y400.cjs');
5
+ var index = require('./index-DbUgUmtU.cjs');
6
6
 
7
7
  async function getToOpenAPISchemaFn() {
8
8
  return async (schema, context) => {
@@ -1,6 +1,6 @@
1
1
  import { toJsonSchema } from '@standard-community/standard-json';
2
2
  import { c as convertToOpenAPISchema } from './convert-BrW5dcj8.js';
3
- import { e as errorMessageWrapper } from './index-BBkvvbCv.js';
3
+ import { e as errorMessageWrapper } from './index-2CxeAJPp.js';
4
4
 
5
5
  async function getToOpenAPISchemaFn() {
6
6
  return async (schema, context) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@standard-community/standard-openapi",
3
- "version": "0.2.0-rc.1",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
@@ -23,8 +23,7 @@
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",
26
- "url": "git+https://github.com/standard-community/standard-openapi.git",
27
- "directory": "packages/core"
26
+ "url": "git+https://github.com/standard-community/standard-openapi.git"
28
27
  },
29
28
  "bugs": {
30
29
  "url": "https://github.com/standard-community/standard-openapi/issues"
@@ -40,7 +39,7 @@
40
39
  }
41
40
  },
42
41
  "peerDependencies": {
43
- "@standard-community/standard-json": "^0.3.0-rc.1",
42
+ "@standard-community/standard-json": "^0.3.0",
44
43
  "@standard-schema/spec": "^1.0.0",
45
44
  "arktype": "^2.1.20",
46
45
  "openapi-types": "^12.1.3",
@@ -65,6 +64,7 @@
65
64
  "devDependencies": {
66
65
  "@biomejs/biome": "^2.0.4",
67
66
  "@types/json-schema": "^7.0.15",
67
+ "@types/node": "^24.3.1",
68
68
  "@valibot/to-json-schema": "^1.3.0",
69
69
  "pkgroll": "^2.13.1",
70
70
  "typescript": "^5.8.3",
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- const errorMessageWrapper = (message) => `standard-openapi: ${message}`;
4
-
5
- const getToOpenAPISchemaFn = async (vendor) => {
6
- switch (vendor) {
7
- case "valibot":
8
- return (await Promise.resolve().then(function () { return require('./valibot-Bhs--VcX.cjs'); })).default();
9
- case "zod":
10
- return (await Promise.resolve().then(function () { return require('./zod-DBFeduLX.cjs'); })).default();
11
- case "arktype":
12
- case "effect":
13
- return (await Promise.resolve().then(function () { return require('./default-CLvGm-jP.cjs'); })).default();
14
- default:
15
- throw new Error(
16
- errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
17
- );
18
- }
19
- };
20
-
21
- const toOpenAPISchema = async (schema, options) => {
22
- const components = {};
23
- const _schema = await getToOpenAPISchemaFn(schema["~standard"].vendor).then(
24
- (toOpenAPISchemaFn) => toOpenAPISchemaFn(schema, { components, options })
25
- );
26
- return {
27
- schema: _schema,
28
- components: Object.keys(components).length > 0 ? components : void 0
29
- };
30
- };
31
-
32
- exports.errorMessageWrapper = errorMessageWrapper;
33
- exports.toOpenAPISchema = toOpenAPISchema;
@@ -1,30 +0,0 @@
1
- const errorMessageWrapper = (message) => `standard-openapi: ${message}`;
2
-
3
- const getToOpenAPISchemaFn = async (vendor) => {
4
- switch (vendor) {
5
- case "valibot":
6
- return (await import('./valibot-68LL3XLE.js')).default();
7
- case "zod":
8
- return (await import('./zod-DMhHBlXu.js')).default();
9
- case "arktype":
10
- case "effect":
11
- return (await import('./default-B3j_H5mf.js')).default();
12
- default:
13
- throw new Error(
14
- errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
15
- );
16
- }
17
- };
18
-
19
- const toOpenAPISchema = async (schema, options) => {
20
- const components = {};
21
- const _schema = await getToOpenAPISchemaFn(schema["~standard"].vendor).then(
22
- (toOpenAPISchemaFn) => toOpenAPISchemaFn(schema, { components, options })
23
- );
24
- return {
25
- schema: _schema,
26
- components: Object.keys(components).length > 0 ? components : void 0
27
- };
28
- };
29
-
30
- export { errorMessageWrapper as e, toOpenAPISchema as t };
@@ -1,36 +0,0 @@
1
- import { toJsonSchema } from '@standard-community/standard-json';
2
- import { c as convertToOpenAPISchema } from './convert-BrW5dcj8.js';
3
-
4
- async function getToOpenAPISchemaFn() {
5
- return async (schema, context) => toJsonSchema(
6
- schema,
7
- {
8
- // @ts-expect-error
9
- overrideAction: ({ valibotAction, jsonSchema }) => {
10
- const _jsonSchema = convertToOpenAPISchema(jsonSchema, context);
11
- if (valibotAction.kind === "metadata" && valibotAction.type === "metadata" && !("$ref" in _jsonSchema)) {
12
- const metadata = valibotAction.metadata;
13
- if (metadata.example !== void 0) {
14
- _jsonSchema.example = metadata.example;
15
- }
16
- if (metadata.examples && metadata.examples.length > 0) {
17
- _jsonSchema.examples = metadata.examples;
18
- }
19
- if (metadata.ref) {
20
- context.components.schemas = {
21
- ...context.components.schemas,
22
- [metadata.ref]: _jsonSchema
23
- };
24
- return {
25
- $ref: `#/components/schemas/${metadata.ref}`
26
- };
27
- }
28
- }
29
- return _jsonSchema;
30
- },
31
- ...context.options
32
- }
33
- );
34
- }
35
-
36
- export { getToOpenAPISchemaFn as default };
@@ -1,38 +0,0 @@
1
- 'use strict';
2
-
3
- var standardJson = require('@standard-community/standard-json');
4
- var convert = require('./convert--bmLap0k.cjs');
5
-
6
- async function getToOpenAPISchemaFn() {
7
- return async (schema, context) => standardJson.toJsonSchema(
8
- schema,
9
- {
10
- // @ts-expect-error
11
- overrideAction: ({ valibotAction, jsonSchema }) => {
12
- const _jsonSchema = convert.convertToOpenAPISchema(jsonSchema, context);
13
- if (valibotAction.kind === "metadata" && valibotAction.type === "metadata" && !("$ref" in _jsonSchema)) {
14
- const metadata = valibotAction.metadata;
15
- if (metadata.example !== void 0) {
16
- _jsonSchema.example = metadata.example;
17
- }
18
- if (metadata.examples && metadata.examples.length > 0) {
19
- _jsonSchema.examples = metadata.examples;
20
- }
21
- if (metadata.ref) {
22
- context.components.schemas = {
23
- ...context.components.schemas,
24
- [metadata.ref]: _jsonSchema
25
- };
26
- return {
27
- $ref: `#/components/schemas/${metadata.ref}`
28
- };
29
- }
30
- }
31
- return _jsonSchema;
32
- },
33
- ...context.options
34
- }
35
- );
36
- }
37
-
38
- exports.default = getToOpenAPISchemaFn;