vovk 3.0.0-draft.233 → 3.0.0-draft.235
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/cjs/openapi/vovkSchemaToOpenAPI.d.ts +3 -2
- package/cjs/openapi/vovkSchemaToOpenAPI.js +5 -5
- package/cjs/utils/createCodeExamples.d.ts +2 -1
- package/cjs/utils/createCodeExamples.js +7 -7
- package/cjs/utils/jsonSchemaSampler.d.ts +2 -0
- package/cjs/utils/jsonSchemaSampler.js +141 -0
- package/mjs/openapi/vovkSchemaToOpenAPI.d.ts +3 -2
- package/mjs/openapi/vovkSchemaToOpenAPI.js +5 -5
- package/mjs/utils/createCodeExamples.d.ts +2 -1
- package/mjs/utils/createCodeExamples.js +7 -7
- package/mjs/utils/jsonSchemaSampler.d.ts +2 -0
- package/mjs/utils/jsonSchemaSampler.js +141 -0
- package/package.json +1 -2
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { OpenAPIObject } from 'openapi3-ts/oas31';
|
|
2
2
|
import { type CodeSamplePackageJson } from '../utils/createCodeExamples';
|
|
3
|
-
import { type VovkSchema } from '../types';
|
|
4
|
-
export declare function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject, package: packageJson, }: {
|
|
3
|
+
import { type VovkSchema, KnownAny } from '../types';
|
|
4
|
+
export declare function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject, package: packageJson, sampler, }: {
|
|
5
5
|
rootEntry: string;
|
|
6
6
|
schema: VovkSchema;
|
|
7
7
|
openAPIObject?: Partial<OpenAPIObject>;
|
|
8
8
|
package?: CodeSamplePackageJson;
|
|
9
|
+
sampler?: (schema: KnownAny) => KnownAny;
|
|
9
10
|
}): OpenAPIObject;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.vovkSchemaToOpenAPI = vovkSchemaToOpenAPI;
|
|
4
|
-
const json_schema_sampler_1 = require("@stoplight/json-schema-sampler");
|
|
5
4
|
const createCodeExamples_1 = require("../utils/createCodeExamples");
|
|
5
|
+
const jsonSchemaSampler_1 = require("../utils/jsonSchemaSampler");
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
function extractComponents(schema) {
|
|
8
8
|
if (!schema)
|
|
@@ -43,7 +43,7 @@ function extractComponents(schema) {
|
|
|
43
43
|
const processedSchema = process(schema);
|
|
44
44
|
return [processedSchema, components];
|
|
45
45
|
}
|
|
46
|
-
function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject = {}, package: packageJson = { name: 'vovk-client' }, }) {
|
|
46
|
+
function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject = {}, package: packageJson = { name: 'vovk-client' }, sampler = jsonSchemaSampler_1.jsonSchemaSampler, }) {
|
|
47
47
|
const paths = {};
|
|
48
48
|
const components = {};
|
|
49
49
|
for (const [segmentName, segmentSchema] of Object.entries(fullSchema.segments ?? {})) {
|
|
@@ -145,9 +145,9 @@ function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject = {}
|
|
|
145
145
|
...iterationValidation,
|
|
146
146
|
examples: iterationValidation.examples ?? [
|
|
147
147
|
[
|
|
148
|
-
JSON.stringify((
|
|
149
|
-
JSON.stringify((
|
|
150
|
-
JSON.stringify((
|
|
148
|
+
JSON.stringify(sampler(iterationValidation)),
|
|
149
|
+
JSON.stringify(sampler(iterationValidation)),
|
|
150
|
+
JSON.stringify(sampler(iterationValidation)),
|
|
151
151
|
].join('\n'),
|
|
152
152
|
],
|
|
153
153
|
},
|
|
@@ -7,11 +7,12 @@ export type CodeSamplePackageJson = {
|
|
|
7
7
|
py_name?: string;
|
|
8
8
|
[key: string]: KnownAny;
|
|
9
9
|
};
|
|
10
|
-
export declare function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, }: {
|
|
10
|
+
export declare function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, sampler, }: {
|
|
11
11
|
handlerName: string;
|
|
12
12
|
handlerSchema: VovkHandlerSchema;
|
|
13
13
|
controllerSchema: VovkControllerSchema;
|
|
14
14
|
package?: CodeSamplePackageJson;
|
|
15
|
+
sampler?: (schema: KnownAny) => KnownAny;
|
|
15
16
|
}): {
|
|
16
17
|
ts: string;
|
|
17
18
|
py: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createCodeExamples = createCodeExamples;
|
|
4
|
-
const
|
|
4
|
+
const jsonSchemaSampler_1 = require("./jsonSchemaSampler");
|
|
5
5
|
const stringifyTsSample = (data, pad = 4) => JSON.stringify(data, null, 2)
|
|
6
6
|
.replace(/"([A-Za-z_$][0-9A-Za-z_$]*)":/g, '$1:')
|
|
7
7
|
.split('\n')
|
|
@@ -17,17 +17,17 @@ const toSnakeCase = (str) => str
|
|
|
17
17
|
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1_$2') // Add underscore between uppercase letters if the second one is followed by a lowercase
|
|
18
18
|
.toLowerCase()
|
|
19
19
|
.replace(/^_/, ''); // Remove leading underscore
|
|
20
|
-
function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, }) {
|
|
20
|
+
function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, sampler = jsonSchemaSampler_1.jsonSchemaSampler, }) {
|
|
21
21
|
const queryValidation = handlerSchema?.validation?.query;
|
|
22
22
|
const bodyValidation = handlerSchema?.validation?.body;
|
|
23
23
|
const paramsValidation = handlerSchema?.validation?.params;
|
|
24
24
|
const outputValidation = handlerSchema?.validation?.output;
|
|
25
25
|
const iterationValidation = handlerSchema?.validation?.iteration;
|
|
26
|
-
const queryFake = queryValidation && (
|
|
27
|
-
const bodyFake = bodyValidation && (
|
|
28
|
-
const paramsFake = paramsValidation && (
|
|
29
|
-
const outputFake = outputValidation && (
|
|
30
|
-
const iterationFake = iterationValidation && (
|
|
26
|
+
const queryFake = queryValidation && sampler(queryValidation);
|
|
27
|
+
const bodyFake = bodyValidation && sampler(bodyValidation);
|
|
28
|
+
const paramsFake = paramsValidation && sampler(paramsValidation);
|
|
29
|
+
const outputFake = outputValidation && sampler(outputValidation);
|
|
30
|
+
const iterationFake = iterationValidation && sampler(iterationValidation);
|
|
31
31
|
const hasArg = !!queryFake || !!bodyFake || !!paramsFake;
|
|
32
32
|
const rpcName = controllerSchema.rpcModuleName;
|
|
33
33
|
const handlerNameSnake = toSnakeCase(handlerName);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonSchemaSampler = jsonSchemaSampler;
|
|
4
|
+
function jsonSchemaSampler(schema, rootSchema) {
|
|
5
|
+
// Use the input schema as the root if not provided
|
|
6
|
+
rootSchema = rootSchema || schema;
|
|
7
|
+
// If there's an example, use it
|
|
8
|
+
if (schema.example !== undefined) {
|
|
9
|
+
return schema.example;
|
|
10
|
+
}
|
|
11
|
+
// If there are examples, use one of them
|
|
12
|
+
if (schema.examples && schema.examples.length > 0) {
|
|
13
|
+
return schema.examples[0];
|
|
14
|
+
}
|
|
15
|
+
// Handle $ref if present
|
|
16
|
+
if (schema.$ref) {
|
|
17
|
+
return handleRef(schema.$ref, rootSchema);
|
|
18
|
+
}
|
|
19
|
+
// Handle enum if present
|
|
20
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
21
|
+
return schema.enum[0];
|
|
22
|
+
}
|
|
23
|
+
// Handle oneOf, anyOf, allOf
|
|
24
|
+
if (schema.oneOf && schema.oneOf.length > 0) {
|
|
25
|
+
return jsonSchemaSampler(schema.oneOf[0], rootSchema);
|
|
26
|
+
}
|
|
27
|
+
if (schema.anyOf && schema.anyOf.length > 0) {
|
|
28
|
+
return jsonSchemaSampler(schema.anyOf[0], rootSchema);
|
|
29
|
+
}
|
|
30
|
+
if (schema.allOf && schema.allOf.length > 0) {
|
|
31
|
+
// Merge all schemas in allOf
|
|
32
|
+
const mergedSchema = schema.allOf.reduce((acc, s) => ({ ...acc, ...s }), {});
|
|
33
|
+
return jsonSchemaSampler(mergedSchema, rootSchema);
|
|
34
|
+
}
|
|
35
|
+
// Handle different types
|
|
36
|
+
if (schema.type) {
|
|
37
|
+
switch (schema.type) {
|
|
38
|
+
case 'string':
|
|
39
|
+
return handleString(schema);
|
|
40
|
+
case 'number':
|
|
41
|
+
case 'integer':
|
|
42
|
+
return handleNumber(schema);
|
|
43
|
+
case 'boolean':
|
|
44
|
+
return handleBoolean();
|
|
45
|
+
case 'object':
|
|
46
|
+
return handleObject(schema, rootSchema);
|
|
47
|
+
case 'array':
|
|
48
|
+
return handleArray(schema, rootSchema);
|
|
49
|
+
case 'null':
|
|
50
|
+
return null;
|
|
51
|
+
default:
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// If type is not specified but properties are, treat it as an object
|
|
56
|
+
if (schema.properties) {
|
|
57
|
+
return handleObject(schema, rootSchema);
|
|
58
|
+
}
|
|
59
|
+
// Default fallback
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function handleRef(ref, rootSchema) {
|
|
63
|
+
// Parse the reference path
|
|
64
|
+
const path = ref.split('/').slice(1); // Remove the initial '#'
|
|
65
|
+
// Navigate through the schema to find the referenced definition
|
|
66
|
+
let current = rootSchema;
|
|
67
|
+
for (const segment of path) {
|
|
68
|
+
current = current[segment];
|
|
69
|
+
if (current === undefined) {
|
|
70
|
+
return null; // Reference not found
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Process the referenced schema
|
|
74
|
+
return jsonSchemaSampler(current, rootSchema);
|
|
75
|
+
}
|
|
76
|
+
function handleString(schema) {
|
|
77
|
+
if (schema.format) {
|
|
78
|
+
switch (schema.format) {
|
|
79
|
+
case 'email':
|
|
80
|
+
return 'user@example.com';
|
|
81
|
+
case 'uri':
|
|
82
|
+
case 'url':
|
|
83
|
+
return 'https://example.com';
|
|
84
|
+
case 'date':
|
|
85
|
+
return '2023-01-01';
|
|
86
|
+
case 'date-time':
|
|
87
|
+
return '2023-01-01T00:00:00Z';
|
|
88
|
+
case 'uuid':
|
|
89
|
+
return '00000000-0000-0000-0000-000000000000';
|
|
90
|
+
default:
|
|
91
|
+
return 'string';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (schema.pattern) {
|
|
95
|
+
// For simplicity, return a basic string for patterns
|
|
96
|
+
return 'pattern-string';
|
|
97
|
+
}
|
|
98
|
+
return 'string';
|
|
99
|
+
}
|
|
100
|
+
function handleNumber(schema) {
|
|
101
|
+
if (schema.minimum !== undefined && schema.maximum !== undefined) {
|
|
102
|
+
return schema.minimum;
|
|
103
|
+
}
|
|
104
|
+
else if (schema.minimum !== undefined) {
|
|
105
|
+
return schema.minimum;
|
|
106
|
+
}
|
|
107
|
+
else if (schema.maximum !== undefined) {
|
|
108
|
+
return schema.maximum;
|
|
109
|
+
}
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
function handleBoolean() {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
function handleObject(schema, rootSchema) {
|
|
116
|
+
const result = {};
|
|
117
|
+
if (schema.properties) {
|
|
118
|
+
const required = schema.required || [];
|
|
119
|
+
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
120
|
+
// Only include required properties or as a basic example
|
|
121
|
+
if (required.includes(key) || required.length === 0) {
|
|
122
|
+
result[key] = jsonSchemaSampler(propSchema, rootSchema);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// Handle additionalProperties
|
|
127
|
+
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
|
|
128
|
+
result['additionalProp'] = jsonSchemaSampler(schema.additionalProperties, rootSchema);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function handleArray(schema, rootSchema) {
|
|
133
|
+
if (schema.items) {
|
|
134
|
+
const itemSchema = schema.items;
|
|
135
|
+
const minItems = schema.minItems || 1;
|
|
136
|
+
// Create minimum number of items (capped at a reasonable max for examples)
|
|
137
|
+
const numItems = Math.min(minItems, 3);
|
|
138
|
+
return Array.from({ length: numItems }, () => jsonSchemaSampler(itemSchema, rootSchema));
|
|
139
|
+
}
|
|
140
|
+
return [];
|
|
141
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { OpenAPIObject } from 'openapi3-ts/oas31';
|
|
2
2
|
import { type CodeSamplePackageJson } from '../utils/createCodeExamples';
|
|
3
|
-
import { type VovkSchema } from '../types';
|
|
4
|
-
export declare function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject, package: packageJson, }: {
|
|
3
|
+
import { type VovkSchema, KnownAny } from '../types';
|
|
4
|
+
export declare function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject, package: packageJson, sampler, }: {
|
|
5
5
|
rootEntry: string;
|
|
6
6
|
schema: VovkSchema;
|
|
7
7
|
openAPIObject?: Partial<OpenAPIObject>;
|
|
8
8
|
package?: CodeSamplePackageJson;
|
|
9
|
+
sampler?: (schema: KnownAny) => KnownAny;
|
|
9
10
|
}): OpenAPIObject;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.vovkSchemaToOpenAPI = vovkSchemaToOpenAPI;
|
|
4
|
-
const json_schema_sampler_1 = require("@stoplight/json-schema-sampler");
|
|
5
4
|
const createCodeExamples_1 = require("../utils/createCodeExamples");
|
|
5
|
+
const jsonSchemaSampler_1 = require("../utils/jsonSchemaSampler");
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
function extractComponents(schema) {
|
|
8
8
|
if (!schema)
|
|
@@ -43,7 +43,7 @@ function extractComponents(schema) {
|
|
|
43
43
|
const processedSchema = process(schema);
|
|
44
44
|
return [processedSchema, components];
|
|
45
45
|
}
|
|
46
|
-
function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject = {}, package: packageJson = { name: 'vovk-client' }, }) {
|
|
46
|
+
function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject = {}, package: packageJson = { name: 'vovk-client' }, sampler = jsonSchemaSampler_1.jsonSchemaSampler, }) {
|
|
47
47
|
const paths = {};
|
|
48
48
|
const components = {};
|
|
49
49
|
for (const [segmentName, segmentSchema] of Object.entries(fullSchema.segments ?? {})) {
|
|
@@ -145,9 +145,9 @@ function vovkSchemaToOpenAPI({ rootEntry, schema: fullSchema, openAPIObject = {}
|
|
|
145
145
|
...iterationValidation,
|
|
146
146
|
examples: iterationValidation.examples ?? [
|
|
147
147
|
[
|
|
148
|
-
JSON.stringify((
|
|
149
|
-
JSON.stringify((
|
|
150
|
-
JSON.stringify((
|
|
148
|
+
JSON.stringify(sampler(iterationValidation)),
|
|
149
|
+
JSON.stringify(sampler(iterationValidation)),
|
|
150
|
+
JSON.stringify(sampler(iterationValidation)),
|
|
151
151
|
].join('\n'),
|
|
152
152
|
],
|
|
153
153
|
},
|
|
@@ -7,11 +7,12 @@ export type CodeSamplePackageJson = {
|
|
|
7
7
|
py_name?: string;
|
|
8
8
|
[key: string]: KnownAny;
|
|
9
9
|
};
|
|
10
|
-
export declare function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, }: {
|
|
10
|
+
export declare function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, sampler, }: {
|
|
11
11
|
handlerName: string;
|
|
12
12
|
handlerSchema: VovkHandlerSchema;
|
|
13
13
|
controllerSchema: VovkControllerSchema;
|
|
14
14
|
package?: CodeSamplePackageJson;
|
|
15
|
+
sampler?: (schema: KnownAny) => KnownAny;
|
|
15
16
|
}): {
|
|
16
17
|
ts: string;
|
|
17
18
|
py: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createCodeExamples = createCodeExamples;
|
|
4
|
-
const
|
|
4
|
+
const jsonSchemaSampler_1 = require("./jsonSchemaSampler");
|
|
5
5
|
const stringifyTsSample = (data, pad = 4) => JSON.stringify(data, null, 2)
|
|
6
6
|
.replace(/"([A-Za-z_$][0-9A-Za-z_$]*)":/g, '$1:')
|
|
7
7
|
.split('\n')
|
|
@@ -17,17 +17,17 @@ const toSnakeCase = (str) => str
|
|
|
17
17
|
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1_$2') // Add underscore between uppercase letters if the second one is followed by a lowercase
|
|
18
18
|
.toLowerCase()
|
|
19
19
|
.replace(/^_/, ''); // Remove leading underscore
|
|
20
|
-
function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, }) {
|
|
20
|
+
function createCodeExamples({ handlerName, handlerSchema, controllerSchema, package: packageJson, sampler = jsonSchemaSampler_1.jsonSchemaSampler, }) {
|
|
21
21
|
const queryValidation = handlerSchema?.validation?.query;
|
|
22
22
|
const bodyValidation = handlerSchema?.validation?.body;
|
|
23
23
|
const paramsValidation = handlerSchema?.validation?.params;
|
|
24
24
|
const outputValidation = handlerSchema?.validation?.output;
|
|
25
25
|
const iterationValidation = handlerSchema?.validation?.iteration;
|
|
26
|
-
const queryFake = queryValidation && (
|
|
27
|
-
const bodyFake = bodyValidation && (
|
|
28
|
-
const paramsFake = paramsValidation && (
|
|
29
|
-
const outputFake = outputValidation && (
|
|
30
|
-
const iterationFake = iterationValidation && (
|
|
26
|
+
const queryFake = queryValidation && sampler(queryValidation);
|
|
27
|
+
const bodyFake = bodyValidation && sampler(bodyValidation);
|
|
28
|
+
const paramsFake = paramsValidation && sampler(paramsValidation);
|
|
29
|
+
const outputFake = outputValidation && sampler(outputValidation);
|
|
30
|
+
const iterationFake = iterationValidation && sampler(iterationValidation);
|
|
31
31
|
const hasArg = !!queryFake || !!bodyFake || !!paramsFake;
|
|
32
32
|
const rpcName = controllerSchema.rpcModuleName;
|
|
33
33
|
const handlerNameSnake = toSnakeCase(handlerName);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonSchemaSampler = jsonSchemaSampler;
|
|
4
|
+
function jsonSchemaSampler(schema, rootSchema) {
|
|
5
|
+
// Use the input schema as the root if not provided
|
|
6
|
+
rootSchema = rootSchema || schema;
|
|
7
|
+
// If there's an example, use it
|
|
8
|
+
if (schema.example !== undefined) {
|
|
9
|
+
return schema.example;
|
|
10
|
+
}
|
|
11
|
+
// If there are examples, use one of them
|
|
12
|
+
if (schema.examples && schema.examples.length > 0) {
|
|
13
|
+
return schema.examples[0];
|
|
14
|
+
}
|
|
15
|
+
// Handle $ref if present
|
|
16
|
+
if (schema.$ref) {
|
|
17
|
+
return handleRef(schema.$ref, rootSchema);
|
|
18
|
+
}
|
|
19
|
+
// Handle enum if present
|
|
20
|
+
if (schema.enum && schema.enum.length > 0) {
|
|
21
|
+
return schema.enum[0];
|
|
22
|
+
}
|
|
23
|
+
// Handle oneOf, anyOf, allOf
|
|
24
|
+
if (schema.oneOf && schema.oneOf.length > 0) {
|
|
25
|
+
return jsonSchemaSampler(schema.oneOf[0], rootSchema);
|
|
26
|
+
}
|
|
27
|
+
if (schema.anyOf && schema.anyOf.length > 0) {
|
|
28
|
+
return jsonSchemaSampler(schema.anyOf[0], rootSchema);
|
|
29
|
+
}
|
|
30
|
+
if (schema.allOf && schema.allOf.length > 0) {
|
|
31
|
+
// Merge all schemas in allOf
|
|
32
|
+
const mergedSchema = schema.allOf.reduce((acc, s) => ({ ...acc, ...s }), {});
|
|
33
|
+
return jsonSchemaSampler(mergedSchema, rootSchema);
|
|
34
|
+
}
|
|
35
|
+
// Handle different types
|
|
36
|
+
if (schema.type) {
|
|
37
|
+
switch (schema.type) {
|
|
38
|
+
case 'string':
|
|
39
|
+
return handleString(schema);
|
|
40
|
+
case 'number':
|
|
41
|
+
case 'integer':
|
|
42
|
+
return handleNumber(schema);
|
|
43
|
+
case 'boolean':
|
|
44
|
+
return handleBoolean();
|
|
45
|
+
case 'object':
|
|
46
|
+
return handleObject(schema, rootSchema);
|
|
47
|
+
case 'array':
|
|
48
|
+
return handleArray(schema, rootSchema);
|
|
49
|
+
case 'null':
|
|
50
|
+
return null;
|
|
51
|
+
default:
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// If type is not specified but properties are, treat it as an object
|
|
56
|
+
if (schema.properties) {
|
|
57
|
+
return handleObject(schema, rootSchema);
|
|
58
|
+
}
|
|
59
|
+
// Default fallback
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function handleRef(ref, rootSchema) {
|
|
63
|
+
// Parse the reference path
|
|
64
|
+
const path = ref.split('/').slice(1); // Remove the initial '#'
|
|
65
|
+
// Navigate through the schema to find the referenced definition
|
|
66
|
+
let current = rootSchema;
|
|
67
|
+
for (const segment of path) {
|
|
68
|
+
current = current[segment];
|
|
69
|
+
if (current === undefined) {
|
|
70
|
+
return null; // Reference not found
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Process the referenced schema
|
|
74
|
+
return jsonSchemaSampler(current, rootSchema);
|
|
75
|
+
}
|
|
76
|
+
function handleString(schema) {
|
|
77
|
+
if (schema.format) {
|
|
78
|
+
switch (schema.format) {
|
|
79
|
+
case 'email':
|
|
80
|
+
return 'user@example.com';
|
|
81
|
+
case 'uri':
|
|
82
|
+
case 'url':
|
|
83
|
+
return 'https://example.com';
|
|
84
|
+
case 'date':
|
|
85
|
+
return '2023-01-01';
|
|
86
|
+
case 'date-time':
|
|
87
|
+
return '2023-01-01T00:00:00Z';
|
|
88
|
+
case 'uuid':
|
|
89
|
+
return '00000000-0000-0000-0000-000000000000';
|
|
90
|
+
default:
|
|
91
|
+
return 'string';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (schema.pattern) {
|
|
95
|
+
// For simplicity, return a basic string for patterns
|
|
96
|
+
return 'pattern-string';
|
|
97
|
+
}
|
|
98
|
+
return 'string';
|
|
99
|
+
}
|
|
100
|
+
function handleNumber(schema) {
|
|
101
|
+
if (schema.minimum !== undefined && schema.maximum !== undefined) {
|
|
102
|
+
return schema.minimum;
|
|
103
|
+
}
|
|
104
|
+
else if (schema.minimum !== undefined) {
|
|
105
|
+
return schema.minimum;
|
|
106
|
+
}
|
|
107
|
+
else if (schema.maximum !== undefined) {
|
|
108
|
+
return schema.maximum;
|
|
109
|
+
}
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
function handleBoolean() {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
function handleObject(schema, rootSchema) {
|
|
116
|
+
const result = {};
|
|
117
|
+
if (schema.properties) {
|
|
118
|
+
const required = schema.required || [];
|
|
119
|
+
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
120
|
+
// Only include required properties or as a basic example
|
|
121
|
+
if (required.includes(key) || required.length === 0) {
|
|
122
|
+
result[key] = jsonSchemaSampler(propSchema, rootSchema);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// Handle additionalProperties
|
|
127
|
+
if (schema.additionalProperties && typeof schema.additionalProperties === 'object') {
|
|
128
|
+
result['additionalProp'] = jsonSchemaSampler(schema.additionalProperties, rootSchema);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function handleArray(schema, rootSchema) {
|
|
133
|
+
if (schema.items) {
|
|
134
|
+
const itemSchema = schema.items;
|
|
135
|
+
const minItems = schema.minItems || 1;
|
|
136
|
+
// Create minimum number of items (capped at a reasonable max for examples)
|
|
137
|
+
const numItems = Math.min(minItems, 3);
|
|
138
|
+
return Array.from({ length: numItems }, () => jsonSchemaSampler(itemSchema, rootSchema));
|
|
139
|
+
}
|
|
140
|
+
return [];
|
|
141
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vovk",
|
|
3
|
-
"version": "3.0.0-draft.
|
|
3
|
+
"version": "3.0.0-draft.235",
|
|
4
4
|
"main": "./cjs/index.js",
|
|
5
5
|
"module": "./mjs/index.js",
|
|
6
6
|
"types": "./mjs/index.d.ts",
|
|
@@ -48,7 +48,6 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@standard-schema/spec": "^1.0.0",
|
|
51
|
-
"@stoplight/json-schema-sampler": "^0.3.0",
|
|
52
51
|
"openapi3-ts": "^4.4.0"
|
|
53
52
|
}
|
|
54
53
|
}
|