@standard-community/standard-json 0.3.0 → 0.3.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
@@ -33,3 +33,45 @@ const schema = {
33
33
  // Convert it to JSON Schema
34
34
  const jsonSchema = await toJsonSchema(schema);
35
35
  ```
36
+
37
+ ### Sync Usage
38
+
39
+ This is useful for -
40
+
41
+ #### Adding support for Unsupported validation libs
42
+
43
+ ```ts
44
+ import { toJsonSchema, loadVendor } from "@standard-community/standard-json";
45
+ import { convertSchemaToJson } from "your-validation-lib";
46
+
47
+ // The lib should support Standard Schema, like Sury
48
+ // as we use 'schema["~standard"].vendor' to get the vendor name
49
+ // Eg. loadVendor(zod["~standard"].vendor, convertorFunction)
50
+ loadVendor("validation-lib-name", convertSchemaToJson)
51
+
52
+ // Define your validation schema
53
+ const schema = {
54
+ // ...
55
+ };
56
+
57
+ // Convert it to JSON Schema
58
+ const jsonSchema = toJsonSchema(schema);
59
+ ```
60
+
61
+ #### Customize the toJSONFunction of a supported lib
62
+
63
+ ```ts
64
+ import { toJsonSchema, loadVendor } from "@standard-community/standard-json";
65
+ import zodHandler from "@standard-community/standard-json/zod";
66
+
67
+ // Or pass a custom implmentation
68
+ loadVendor("zod", zodHandler())
69
+
70
+ // Define your validation schema
71
+ const schema = {
72
+ // ...
73
+ };
74
+
75
+ // Convert it to JSON Schema
76
+ const jsonSchema = await toJsonSchema(schema);
77
+ ```
package/dist/index.cjs CHANGED
@@ -1,9 +1,59 @@
1
1
  'use strict';
2
2
 
3
- require('quansync');
4
- var index = require('./index-Dxld84sh.cjs');
3
+ var quansync = require('quansync');
4
+ var utils = require('./utils-D7AosoZA.cjs');
5
5
 
6
+ function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); }
6
7
 
8
+ const getToJsonSchemaFn = async (vendor) => {
9
+ const cached = utils.validationMapper.get(vendor);
10
+ if (cached) {
11
+ return cached;
12
+ }
13
+ let vendorFnPromise;
14
+ switch (vendor) {
15
+ case "arktype":
16
+ vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/arktype.cjs')); })).default();
17
+ break;
18
+ case "effect":
19
+ vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/effect.cjs')); })).default();
20
+ break;
21
+ case "valibot":
22
+ vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/valibot.cjs')); })).default();
23
+ break;
24
+ case "zod":
25
+ vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/zod.cjs')); })).default();
26
+ break;
27
+ default:
28
+ throw new Error(
29
+ utils.errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
30
+ );
31
+ }
32
+ const vendorFn = await vendorFnPromise;
33
+ utils.validationMapper.set(vendor, vendorFn);
34
+ return vendorFn;
35
+ };
7
36
 
8
- exports.loadVendor = index.loadVendor;
9
- exports.toJsonSchema = index.toJsonSchema;
37
+ const toJsonSchema = quansync.quansync({
38
+ sync: (schema, options) => {
39
+ const fn = utils.validationMapper.get(schema["~standard"].vendor);
40
+ if (!fn) {
41
+ throw new Error(
42
+ utils.errorMessageWrapper(
43
+ `Unsupported schema vendor "${schema["~standard"].vendor}".`
44
+ )
45
+ );
46
+ }
47
+ return fn(schema, options);
48
+ },
49
+ async: async (schema, options) => {
50
+ const fn = await getToJsonSchemaFn(schema["~standard"].vendor);
51
+ return fn(schema, options);
52
+ }
53
+ });
54
+ function loadVendor(vendor, fn) {
55
+ utils.validationMapper.set(vendor, fn);
56
+ }
57
+
58
+ exports.loadVendor = loadVendor;
59
+ exports.toJsonSchema = toJsonSchema;
package/dist/index.d.cts CHANGED
@@ -1,9 +1,7 @@
1
1
  import * as quansync from 'quansync';
2
2
  import * as json_schema from 'json-schema';
3
- import { JSONSchema7 } from 'json-schema';
4
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
5
-
6
- type ToJsonSchemaFn = (schema: unknown, options?: Record<string, unknown>) => JSONSchema7 | Promise<JSONSchema7>;
4
+ import { T as ToJsonSchemaFn } from './utils-1JaZ8JC_.js';
7
5
 
8
6
  /**
9
7
  * Converts a Standard Schema to a JSON schema.
@@ -11,4 +9,4 @@ type ToJsonSchemaFn = (schema: unknown, options?: Record<string, unknown>) => JS
11
9
  declare const toJsonSchema: quansync.QuansyncFn<json_schema.JSONSchema7 | Promise<json_schema.JSONSchema7>, [schema: StandardSchemaV1<unknown, unknown>, options?: Record<string, unknown> | undefined]>;
12
10
  declare function loadVendor(vendor: string, fn: ToJsonSchemaFn): void;
13
11
 
14
- export { loadVendor, toJsonSchema };
12
+ export { ToJsonSchemaFn, loadVendor, toJsonSchema };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import * as quansync from 'quansync';
2
2
  import * as json_schema from 'json-schema';
3
- import { JSONSchema7 } from 'json-schema';
4
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
5
-
6
- type ToJsonSchemaFn = (schema: unknown, options?: Record<string, unknown>) => JSONSchema7 | Promise<JSONSchema7>;
4
+ import { T as ToJsonSchemaFn } from './utils-1JaZ8JC_.js';
7
5
 
8
6
  /**
9
7
  * Converts a Standard Schema to a JSON schema.
@@ -11,4 +9,4 @@ type ToJsonSchemaFn = (schema: unknown, options?: Record<string, unknown>) => JS
11
9
  declare const toJsonSchema: quansync.QuansyncFn<json_schema.JSONSchema7 | Promise<json_schema.JSONSchema7>, [schema: StandardSchemaV1<unknown, unknown>, options?: Record<string, unknown> | undefined]>;
12
10
  declare function loadVendor(vendor: string, fn: ToJsonSchemaFn): void;
13
11
 
14
- export { loadVendor, toJsonSchema };
12
+ export { ToJsonSchemaFn, loadVendor, toJsonSchema };
package/dist/index.js CHANGED
@@ -1,2 +1,54 @@
1
- import 'quansync';
2
- export { l as loadVendor, t as toJsonSchema } from './index-97sbLB6Z.js';
1
+ import { quansync } from 'quansync';
2
+ import { v as validationMapper, e as errorMessageWrapper } from './utils-BOYKSG8-.js';
3
+
4
+ const getToJsonSchemaFn = async (vendor) => {
5
+ const cached = validationMapper.get(vendor);
6
+ if (cached) {
7
+ return cached;
8
+ }
9
+ let vendorFnPromise;
10
+ switch (vendor) {
11
+ case "arktype":
12
+ vendorFnPromise = (await import('./vendors/arktype.js')).default();
13
+ break;
14
+ case "effect":
15
+ vendorFnPromise = (await import('./vendors/effect.js')).default();
16
+ break;
17
+ case "valibot":
18
+ vendorFnPromise = (await import('./vendors/valibot.js')).default();
19
+ break;
20
+ case "zod":
21
+ vendorFnPromise = (await import('./vendors/zod.js')).default();
22
+ break;
23
+ default:
24
+ throw new Error(
25
+ errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
26
+ );
27
+ }
28
+ const vendorFn = await vendorFnPromise;
29
+ validationMapper.set(vendor, vendorFn);
30
+ return vendorFn;
31
+ };
32
+
33
+ const toJsonSchema = quansync({
34
+ sync: (schema, options) => {
35
+ const fn = validationMapper.get(schema["~standard"].vendor);
36
+ if (!fn) {
37
+ throw new Error(
38
+ errorMessageWrapper(
39
+ `Unsupported schema vendor "${schema["~standard"].vendor}".`
40
+ )
41
+ );
42
+ }
43
+ return fn(schema, options);
44
+ },
45
+ async: async (schema, options) => {
46
+ const fn = await getToJsonSchemaFn(schema["~standard"].vendor);
47
+ return fn(schema, options);
48
+ }
49
+ });
50
+ function loadVendor(vendor, fn) {
51
+ validationMapper.set(vendor, fn);
52
+ }
53
+
54
+ export { loadVendor, toJsonSchema };
@@ -0,0 +1,5 @@
1
+ import { JSONSchema7 } from 'json-schema';
2
+
3
+ type ToJsonSchemaFn = (schema: unknown, options?: Record<string, unknown>) => JSONSchema7 | Promise<JSONSchema7>;
4
+
5
+ export type { ToJsonSchemaFn as T };
@@ -0,0 +1,4 @@
1
+ const errorMessageWrapper = (message) => `standard-json: ${message}`;
2
+ const validationMapper = /* @__PURE__ */ new Map();
3
+
4
+ export { errorMessageWrapper as e, validationMapper as v };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const errorMessageWrapper = (message) => `standard-json: ${message}`;
4
+ const validationMapper = /* @__PURE__ */ new Map();
5
+
6
+ exports.errorMessageWrapper = errorMessageWrapper;
7
+ exports.validationMapper = validationMapper;
@@ -4,4 +4,4 @@ async function getToJsonSchemaFn() {
4
4
  return (schema, options) => schema.toJsonSchema(options);
5
5
  }
6
6
 
7
- exports.default = getToJsonSchemaFn;
7
+ module.exports = getToJsonSchemaFn;
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -1,15 +1,14 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-Dxld84sh.cjs');
4
- require('quansync');
3
+ var utils = require('../utils-D7AosoZA.cjs');
5
4
 
6
5
  async function getToJsonSchemaFn() {
7
6
  try {
8
7
  const { JSONSchema } = await import('effect');
9
8
  return (schema) => JSONSchema.make(schema);
10
9
  } catch {
11
- throw new Error(index.errorMessageWrapper('Missing dependencies "effect".'));
10
+ throw new Error(utils.errorMessageWrapper('Missing dependencies "effect".'));
12
11
  }
13
12
  }
14
13
 
15
- exports.default = getToJsonSchemaFn;
14
+ module.exports = getToJsonSchemaFn;
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -1,5 +1,4 @@
1
- import { e as errorMessageWrapper } from './index-97sbLB6Z.js';
2
- import 'quansync';
1
+ import { e as errorMessageWrapper } from '../utils-BOYKSG8-.js';
3
2
 
4
3
  async function getToJsonSchemaFn() {
5
4
  try {
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-Dxld84sh.cjs');
4
- require('quansync');
3
+ var utils = require('../utils-D7AosoZA.cjs');
5
4
 
6
5
  async function getToJsonSchemaFn() {
7
6
  try {
@@ -9,9 +8,9 @@ async function getToJsonSchemaFn() {
9
8
  return toJsonSchema;
10
9
  } catch {
11
10
  throw new Error(
12
- index.errorMessageWrapper('Missing dependencies "@valibot/to-json-schema".')
11
+ utils.errorMessageWrapper('Missing dependencies "@valibot/to-json-schema".')
13
12
  );
14
13
  }
15
14
  }
16
15
 
17
- exports.default = getToJsonSchemaFn;
16
+ module.exports = getToJsonSchemaFn;
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -1,5 +1,4 @@
1
- import { e as errorMessageWrapper } from './index-97sbLB6Z.js';
2
- import 'quansync';
1
+ import { e as errorMessageWrapper } from '../utils-BOYKSG8-.js';
3
2
 
4
3
  async function getToJsonSchemaFn() {
5
4
  try {
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var index = require('./index-Dxld84sh.cjs');
4
- require('quansync');
3
+ var utils = require('../utils-D7AosoZA.cjs');
5
4
 
6
5
  async function getToJsonSchemaFn() {
7
6
  return async (schema, options) => {
@@ -12,7 +11,7 @@ async function getToJsonSchemaFn() {
12
11
  (mod) => mod.toJSONSchema
13
12
  );
14
13
  } catch {
15
- throw new Error(index.errorMessageWrapper('Missing dependencies "zod v4".'));
14
+ throw new Error(utils.errorMessageWrapper('Missing dependencies "zod v4".'));
16
15
  }
17
16
  } else {
18
17
  try {
@@ -21,7 +20,7 @@ async function getToJsonSchemaFn() {
21
20
  );
22
21
  } catch {
23
22
  throw new Error(
24
- index.errorMessageWrapper('Missing dependencies "zod-to-json-schema".')
23
+ utils.errorMessageWrapper('Missing dependencies "zod-to-json-schema".')
25
24
  );
26
25
  }
27
26
  }
@@ -29,4 +28,4 @@ async function getToJsonSchemaFn() {
29
28
  };
30
29
  }
31
30
 
32
- exports.default = getToJsonSchemaFn;
31
+ module.exports = getToJsonSchemaFn;
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -0,0 +1,6 @@
1
+ import { T as ToJsonSchemaFn } from '../utils-1JaZ8JC_.js';
2
+ import 'json-schema';
3
+
4
+ declare function getToJsonSchemaFn(): Promise<ToJsonSchemaFn>;
5
+
6
+ export { getToJsonSchemaFn as default };
@@ -1,5 +1,4 @@
1
- import { e as errorMessageWrapper } from './index-97sbLB6Z.js';
2
- import 'quansync';
1
+ import { e as errorMessageWrapper } from '../utils-BOYKSG8-.js';
3
2
 
4
3
  async function getToJsonSchemaFn() {
5
4
  return async (schema, options) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@standard-community/standard-json",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.cjs",
@@ -28,13 +28,55 @@
28
28
  "url": "https://github.com/standard-community/standard-json/issues"
29
29
  },
30
30
  "exports": {
31
- "import": {
32
- "types": "./dist/index.d.ts",
33
- "default": "./dist/index.js"
31
+ ".": {
32
+ "import": {
33
+ "types": "./dist/index.d.ts",
34
+ "default": "./dist/index.js"
35
+ },
36
+ "require": {
37
+ "types": "./dist/index.d.cts",
38
+ "default": "./dist/index.cjs"
39
+ }
34
40
  },
35
- "require": {
36
- "types": "./dist/index.d.cts",
37
- "default": "./dist/index.cjs"
41
+ "./arktype": {
42
+ "import": {
43
+ "types": "./dist/vendors/arktype.d.ts",
44
+ "default": "./dist/vendors/arktype.js"
45
+ },
46
+ "require": {
47
+ "types": "./dist/vendors/arktype.d.cts",
48
+ "default": "./dist/vendors/arktype.cjs"
49
+ }
50
+ },
51
+ "./effect": {
52
+ "import": {
53
+ "types": "./dist/vendors/effect.d.ts",
54
+ "default": "./dist/vendors/effect.js"
55
+ },
56
+ "require": {
57
+ "types": "./dist/vendors/effect.d.cts",
58
+ "default": "./dist/vendors/effect.cjs"
59
+ }
60
+ },
61
+ "./valibot": {
62
+ "import": {
63
+ "types": "./dist/vendors/valibot.d.ts",
64
+ "default": "./dist/vendors/valibot.js"
65
+ },
66
+ "require": {
67
+ "types": "./dist/vendors/valibot.d.cts",
68
+ "default": "./dist/vendors/valibot.cjs"
69
+ }
70
+ },
71
+ "./zod": {
72
+ "import": {
73
+ "types": "./dist/vendors/zod.d.ts",
74
+ "default": "./dist/vendors/zod.js"
75
+ },
76
+ "require": {
77
+ "types": "./dist/vendors/zod.d.cts",
78
+ "default": "./dist/vendors/zod.cjs"
79
+ }
38
80
  }
39
81
  },
40
82
  "peerDependencies": {
@@ -1,56 +0,0 @@
1
- import { quansync } from 'quansync';
2
-
3
- const errorMessageWrapper = (message) => `standard-json: ${message}`;
4
- const validationMapper = /* @__PURE__ */ new Map();
5
-
6
- const getToJsonSchemaFn = async (vendor) => {
7
- const cached = validationMapper.get(vendor);
8
- if (cached) {
9
- return cached;
10
- }
11
- let vendorFnPromise;
12
- switch (vendor) {
13
- case "arktype":
14
- vendorFnPromise = (await import('./arktype-DUDu5yUt.js')).default();
15
- break;
16
- case "effect":
17
- vendorFnPromise = (await import('./effect-vnF3G8iM.js')).default();
18
- break;
19
- case "valibot":
20
- vendorFnPromise = (await import('./valibot-BjGSUKDp.js')).default();
21
- break;
22
- case "zod":
23
- vendorFnPromise = (await import('./zod-BciXR7y0.js')).default();
24
- break;
25
- default:
26
- throw new Error(
27
- errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
28
- );
29
- }
30
- const vendorFn = await vendorFnPromise;
31
- validationMapper.set(vendor, vendorFn);
32
- return vendorFn;
33
- };
34
-
35
- const toJsonSchema = quansync({
36
- sync: (schema, options) => {
37
- const fn = validationMapper.get(schema["~standard"].vendor);
38
- if (!fn) {
39
- throw new Error(
40
- errorMessageWrapper(
41
- `Unsupported schema vendor "${schema["~standard"].vendor}".`
42
- )
43
- );
44
- }
45
- return fn(schema, options);
46
- },
47
- async: async (schema, options) => {
48
- const fn = await getToJsonSchemaFn(schema["~standard"].vendor);
49
- return fn(schema, options);
50
- }
51
- });
52
- function loadVendor(vendor, fn) {
53
- validationMapper.set(vendor, fn);
54
- }
55
-
56
- export { errorMessageWrapper as e, loadVendor as l, toJsonSchema as t };
@@ -1,60 +0,0 @@
1
- 'use strict';
2
-
3
- var quansync = require('quansync');
4
-
5
- const errorMessageWrapper = (message) => `standard-json: ${message}`;
6
- const validationMapper = /* @__PURE__ */ new Map();
7
-
8
- const getToJsonSchemaFn = async (vendor) => {
9
- const cached = validationMapper.get(vendor);
10
- if (cached) {
11
- return cached;
12
- }
13
- let vendorFnPromise;
14
- switch (vendor) {
15
- case "arktype":
16
- vendorFnPromise = (await Promise.resolve().then(function () { return require('./arktype-BCitmcgd.cjs'); })).default();
17
- break;
18
- case "effect":
19
- vendorFnPromise = (await Promise.resolve().then(function () { return require('./effect-DiQ4KuZr.cjs'); })).default();
20
- break;
21
- case "valibot":
22
- vendorFnPromise = (await Promise.resolve().then(function () { return require('./valibot-DYwVcDdz.cjs'); })).default();
23
- break;
24
- case "zod":
25
- vendorFnPromise = (await Promise.resolve().then(function () { return require('./zod-BIq-xupo.cjs'); })).default();
26
- break;
27
- default:
28
- throw new Error(
29
- errorMessageWrapper(`Unsupported schema vendor "${vendor}".`)
30
- );
31
- }
32
- const vendorFn = await vendorFnPromise;
33
- validationMapper.set(vendor, vendorFn);
34
- return vendorFn;
35
- };
36
-
37
- const toJsonSchema = quansync.quansync({
38
- sync: (schema, options) => {
39
- const fn = validationMapper.get(schema["~standard"].vendor);
40
- if (!fn) {
41
- throw new Error(
42
- errorMessageWrapper(
43
- `Unsupported schema vendor "${schema["~standard"].vendor}".`
44
- )
45
- );
46
- }
47
- return fn(schema, options);
48
- },
49
- async: async (schema, options) => {
50
- const fn = await getToJsonSchemaFn(schema["~standard"].vendor);
51
- return fn(schema, options);
52
- }
53
- });
54
- function loadVendor(vendor, fn) {
55
- validationMapper.set(vendor, fn);
56
- }
57
-
58
- exports.errorMessageWrapper = errorMessageWrapper;
59
- exports.loadVendor = loadVendor;
60
- exports.toJsonSchema = toJsonSchema;