@takeshape/json-schema 11.144.1 → 11.154.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/package.json +2 -2
- package/dist/converters/index.d.ts +0 -1
- package/dist/converters/index.js +0 -1
- package/dist/converters/schema-converter.d.ts +0 -64
- package/dist/converters/schema-converter.js +0 -558
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/schema-validator.d.ts +0 -36
- package/dist/schema-validator.js +0 -207
- package/dist/utils/constants.d.ts +0 -3
- package/dist/utils/constants.js +0 -56
- package/dist/utils/index.d.ts +0 -4
- package/dist/utils/index.js +0 -4
- package/dist/utils/keys.d.ts +0 -5
- package/dist/utils/keys.js +0 -8
- package/dist/utils/references.d.ts +0 -145
- package/dist/utils/references.js +0 -113
- package/dist/utils/type-utils.d.ts +0 -18
- package/dist/utils/type-utils.js +0 -55
- package/dist/utils/types.d.ts +0 -76
- package/dist/utils/types.js +0 -1
package/dist/schema-validator.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import { visit } from '@takeshape/util';
|
|
2
|
-
import { Ajv } from 'ajv';
|
|
3
|
-
import addFormats from 'ajv-formats';
|
|
4
|
-
import get from 'lodash/get.js';
|
|
5
|
-
import isString from 'lodash/isString.js';
|
|
6
|
-
import unset from 'lodash/unset.js';
|
|
7
|
-
export function parseDataPath(instancePath) {
|
|
8
|
-
return instancePath.substr(1).split('/');
|
|
9
|
-
}
|
|
10
|
-
function getData(error, data) {
|
|
11
|
-
return get(data, parseDataPath(error.instancePath));
|
|
12
|
-
}
|
|
13
|
-
function ignoreMissing(error) {
|
|
14
|
-
// ignore top level missing errors
|
|
15
|
-
return !(error.instancePath === '' && error.keyword === 'required');
|
|
16
|
-
}
|
|
17
|
-
function ignoreNull(error, data, topLevelSchema) {
|
|
18
|
-
return !((error.keyword === 'type' || error.keyword === 'oneOf') &&
|
|
19
|
-
getData(error, data) === null &&
|
|
20
|
-
!isInvalidPropertyRequired(topLevelSchema, error));
|
|
21
|
-
}
|
|
22
|
-
function getErrorFilter(params, data, schema) {
|
|
23
|
-
return (error) => (!params.ignoreMissing || ignoreMissing(error)) && (!params.ignoreNulls || ignoreNull(error, data, schema));
|
|
24
|
-
}
|
|
25
|
-
export function isInvalidPropertyRequired(topLevelSchema, error) {
|
|
26
|
-
const instancePath = parseDataPath(error.instancePath);
|
|
27
|
-
const parentDataPath = instancePath.slice(0, instancePath.length - 1);
|
|
28
|
-
const parentSchema = followSchemaPath(topLevelSchema, parentDataPath);
|
|
29
|
-
const name = getName(error.instancePath);
|
|
30
|
-
if (!parentSchema) {
|
|
31
|
-
throw new Error('Unexpected error cannot find parent schema');
|
|
32
|
-
}
|
|
33
|
-
return isRequired(topLevelSchema, parentSchema, name);
|
|
34
|
-
}
|
|
35
|
-
function isRequired(topLevelSchema, schema, name) {
|
|
36
|
-
const schemas = schema.allOf || schema.anyOf || schema.oneOf || [schema];
|
|
37
|
-
return schemas.some((childSchema) => followRef(topLevelSchema, childSchema)?.required?.includes(name));
|
|
38
|
-
}
|
|
39
|
-
function getName(path) {
|
|
40
|
-
const parts = parseDataPath(path);
|
|
41
|
-
return parts[parts.length - 1];
|
|
42
|
-
}
|
|
43
|
-
export function refToPath(ref) {
|
|
44
|
-
return ref.substring(2).split('/');
|
|
45
|
-
}
|
|
46
|
-
function followRef(topLevelSchema, schema) {
|
|
47
|
-
if (schema.$ref) {
|
|
48
|
-
const referencedSchema = get(topLevelSchema, refToPath(schema.$ref));
|
|
49
|
-
if (!referencedSchema) {
|
|
50
|
-
throw new Error(`Could not resolve ${schema.$ref}`);
|
|
51
|
-
}
|
|
52
|
-
return referencedSchema;
|
|
53
|
-
}
|
|
54
|
-
return schema;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Given a schema object traverse it using a "instancePath" and return the schema at that path
|
|
58
|
-
*/
|
|
59
|
-
export function followSchemaPath(topLevelSchema, instancePath) {
|
|
60
|
-
const followPath = (schemaObject, path) => {
|
|
61
|
-
const schema = followRef(topLevelSchema, schemaObject);
|
|
62
|
-
if (path.length === 0) {
|
|
63
|
-
return schema;
|
|
64
|
-
}
|
|
65
|
-
const combinedSchemas = schema.allOf || schema.anyOf || schema.oneOf;
|
|
66
|
-
if (combinedSchemas) {
|
|
67
|
-
for (const childSchema of combinedSchemas) {
|
|
68
|
-
const result = followPath(childSchema, path);
|
|
69
|
-
if (result) {
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const [first, ...rest] = path;
|
|
75
|
-
if (schema.items && /^\d+$/.exec(first)) {
|
|
76
|
-
return followPath(schema.items, rest);
|
|
77
|
-
}
|
|
78
|
-
const prop = schema?.properties?.[first];
|
|
79
|
-
if (prop) {
|
|
80
|
-
return rest.length ? followPath(prop, rest) : prop;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
return followPath(topLevelSchema, instancePath);
|
|
84
|
-
}
|
|
85
|
-
function isSchemaObject(schema) {
|
|
86
|
-
return typeof schema === 'object';
|
|
87
|
-
}
|
|
88
|
-
function getSchemaWithDefinitions(ajv, id) {
|
|
89
|
-
if (id.startsWith('#')) {
|
|
90
|
-
const rootSchema = ajv.getSchema('#')?.schema;
|
|
91
|
-
if (isSchemaObject(rootSchema)) {
|
|
92
|
-
const path = refToPath(id);
|
|
93
|
-
const { definitions } = rootSchema;
|
|
94
|
-
return {
|
|
95
|
-
...get(rootSchema, path),
|
|
96
|
-
definitions
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return ajv.getSchema(id)?.schema;
|
|
101
|
-
}
|
|
102
|
-
export function validate(ajv, id, data, options) {
|
|
103
|
-
const params = {
|
|
104
|
-
ignoreMissing: false,
|
|
105
|
-
ignoreNulls: true,
|
|
106
|
-
errorsText: false,
|
|
107
|
-
...options
|
|
108
|
-
};
|
|
109
|
-
let valid = ajv.validate(id, data);
|
|
110
|
-
let errors;
|
|
111
|
-
if (valid) {
|
|
112
|
-
errors = [];
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
errors = ajv.errors;
|
|
116
|
-
if (params.ignoreNulls || params.ignoreMissing) {
|
|
117
|
-
const schema = getSchemaWithDefinitions(ajv, id);
|
|
118
|
-
if (isSchemaObject(schema)) {
|
|
119
|
-
errors = errors.filter(getErrorFilter(params, data, schema));
|
|
120
|
-
valid = errors.length === 0;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
const errorsText = params.errorsText && !valid ? ajv.errorsText(errors) : '';
|
|
125
|
-
return { valid, errors, errorsText };
|
|
126
|
-
}
|
|
127
|
-
export { Ajv };
|
|
128
|
-
export function createAjv(options) {
|
|
129
|
-
const ajv = new Ajv({
|
|
130
|
-
discriminator: true,
|
|
131
|
-
allErrors: true,
|
|
132
|
-
unevaluated: true,
|
|
133
|
-
strict: false,
|
|
134
|
-
...options
|
|
135
|
-
});
|
|
136
|
-
addFormats.default(ajv);
|
|
137
|
-
/**
|
|
138
|
-
* Formats ingested by Stripe OpenAPI and possibly other — trigger warnings
|
|
139
|
-
* in the client when Stripe types are loaded.
|
|
140
|
-
*/
|
|
141
|
-
ajv.addFormat('unix-time', /^[0-9]{10}$/);
|
|
142
|
-
ajv.addFormat('decimal', /^[0-9]+\.[0-9]{1,12}$/);
|
|
143
|
-
return ajv;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Determine whether a string is a valid regular expression
|
|
147
|
-
*/
|
|
148
|
-
function isValidRegex(str) {
|
|
149
|
-
try {
|
|
150
|
-
new RegExp(str, 'u');
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
catch {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Apply various fixes to the schema to work around AJV issues and bugs.
|
|
159
|
-
* See inline comments for more.
|
|
160
|
-
*/
|
|
161
|
-
export function fixSchema(schema) {
|
|
162
|
-
visit(schema, ['pattern', 'oneOf'], (value, path) => {
|
|
163
|
-
const key = path.slice(-1)[0];
|
|
164
|
-
const parent = get(schema, path.slice(0, -1));
|
|
165
|
-
/**
|
|
166
|
-
* Fix schema by removing any broken regexes that will cause ajv to throw a compile error.
|
|
167
|
-
* It is better to remove the invalid regex rather than forgo validation altogether
|
|
168
|
-
* This should be fixed in ajv see the comment here
|
|
169
|
-
* https://github.com/ajv-validator/ajv/blob/9f1c3eaa4b91ca17b72b122cdac9b108d1ac30cb/lib/vocabularies/validation/pattern.ts#L21
|
|
170
|
-
*/
|
|
171
|
-
if (key === 'pattern') {
|
|
172
|
-
if (parent.type === 'string' && (!isString(value) || !isValidRegex(value))) {
|
|
173
|
-
unset(schema, path);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
return schema;
|
|
178
|
-
}
|
|
179
|
-
export function createSchemaValidator(schema, metaSchemas = [], options = {}) {
|
|
180
|
-
const schemas = Array.isArray(schema) ? schema : [schema];
|
|
181
|
-
const ajv = createAjv({
|
|
182
|
-
removeAdditional: true,
|
|
183
|
-
...options
|
|
184
|
-
});
|
|
185
|
-
if (metaSchemas) {
|
|
186
|
-
for (const metaSchema of metaSchemas) {
|
|
187
|
-
ajv.addMetaSchema(metaSchema);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
for (const schema of schemas) {
|
|
191
|
-
ajv.addSchema(fixSchema(schema));
|
|
192
|
-
}
|
|
193
|
-
const defaultRef = schemas[0].$id;
|
|
194
|
-
if (!defaultRef) {
|
|
195
|
-
throw Error('Failed to create schema validator: schema is missing $id');
|
|
196
|
-
}
|
|
197
|
-
return (data, options) => {
|
|
198
|
-
return validate(ajv, defaultRef, data, options);
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Returns a compiled validator from a schema which can act as a type guard.
|
|
203
|
-
*/
|
|
204
|
-
export function createTypedValidator(schema, options = {}) {
|
|
205
|
-
const ajv = createAjv(options);
|
|
206
|
-
return ajv.compile(schema);
|
|
207
|
-
}
|
package/dist/utils/constants.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
export const jsonSchema7Keys = [
|
|
2
|
-
'$id',
|
|
3
|
-
'$schema',
|
|
4
|
-
'$ref',
|
|
5
|
-
'$comment',
|
|
6
|
-
'title',
|
|
7
|
-
'description',
|
|
8
|
-
'default',
|
|
9
|
-
'readOnly',
|
|
10
|
-
'writeOnly',
|
|
11
|
-
'examples',
|
|
12
|
-
'multipleOf',
|
|
13
|
-
'maximum',
|
|
14
|
-
'exclusiveMaximum',
|
|
15
|
-
'minimum',
|
|
16
|
-
'exclusiveMinimum',
|
|
17
|
-
'maxLength',
|
|
18
|
-
'minLength',
|
|
19
|
-
'pattern',
|
|
20
|
-
'additionalItems',
|
|
21
|
-
'items',
|
|
22
|
-
'maxItems',
|
|
23
|
-
'minItems',
|
|
24
|
-
'uniqueItems',
|
|
25
|
-
'contains',
|
|
26
|
-
'maxProperties',
|
|
27
|
-
'minProperties',
|
|
28
|
-
'required',
|
|
29
|
-
'properties',
|
|
30
|
-
'patternProperties',
|
|
31
|
-
'additionalProperties',
|
|
32
|
-
'dependencies',
|
|
33
|
-
'propertyNames',
|
|
34
|
-
'const',
|
|
35
|
-
'enum',
|
|
36
|
-
'type',
|
|
37
|
-
'format',
|
|
38
|
-
'contentMediaType',
|
|
39
|
-
'contentEncoding',
|
|
40
|
-
'if',
|
|
41
|
-
'then',
|
|
42
|
-
'else',
|
|
43
|
-
'allOf',
|
|
44
|
-
'anyOf',
|
|
45
|
-
'oneOf',
|
|
46
|
-
'not',
|
|
47
|
-
'definitions',
|
|
48
|
-
'$defs'
|
|
49
|
-
];
|
|
50
|
-
export const jsonSchema7PrimitiveTypes = [
|
|
51
|
-
'string',
|
|
52
|
-
'number',
|
|
53
|
-
'boolean',
|
|
54
|
-
'integer',
|
|
55
|
-
'null'
|
|
56
|
-
];
|
package/dist/utils/index.d.ts
DELETED
package/dist/utils/index.js
DELETED
package/dist/utils/keys.d.ts
DELETED
package/dist/utils/keys.js
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema7, JSONSchema7Definition } from 'json-schema';
|
|
2
|
-
export declare function getDefinitionKey(ref: string): string;
|
|
3
|
-
/**
|
|
4
|
-
* Get all the definitions that are referenced by other definitions.
|
|
5
|
-
*/
|
|
6
|
-
export declare function getReferenceMap(definitions: Record<string, JSONSchema7Definition>): Map<string, Set<string>>;
|
|
7
|
-
/**
|
|
8
|
-
* Prunes the schema definitions to only include those that are referenced either directly
|
|
9
|
-
* or indirectly by the specified root reference.
|
|
10
|
-
*
|
|
11
|
-
* @param {JSONSchema7} schema - The JSON schema object containing the definitions to prune.
|
|
12
|
-
* @param {string} ref - The reference identifier that serves as the root for pruning definitions.
|
|
13
|
-
* @return {JSONSchema7} - A new JSON schema object with pruned definitions.
|
|
14
|
-
*/
|
|
15
|
-
export declare function pruneSchemaDefinitions(schema: JSONSchema7, ref: string): {
|
|
16
|
-
definitions: Record<string, JSONSchema7Definition>;
|
|
17
|
-
$id?: string | undefined;
|
|
18
|
-
$ref?: string | undefined;
|
|
19
|
-
$schema?: import("json-schema").JSONSchema7Version | undefined;
|
|
20
|
-
$comment?: string | undefined;
|
|
21
|
-
$defs?: {
|
|
22
|
-
[key: string]: JSONSchema7Definition;
|
|
23
|
-
} | undefined;
|
|
24
|
-
type?: import("json-schema").JSONSchema7TypeName | import("json-schema").JSONSchema7TypeName[] | undefined;
|
|
25
|
-
enum?: import("json-schema").JSONSchema7Type[] | undefined;
|
|
26
|
-
const?: import("json-schema").JSONSchema7Type | undefined;
|
|
27
|
-
multipleOf?: number | undefined;
|
|
28
|
-
maximum?: number | undefined;
|
|
29
|
-
exclusiveMaximum?: number | undefined;
|
|
30
|
-
minimum?: number | undefined;
|
|
31
|
-
exclusiveMinimum?: number | undefined;
|
|
32
|
-
maxLength?: number | undefined;
|
|
33
|
-
minLength?: number | undefined;
|
|
34
|
-
pattern?: string | undefined;
|
|
35
|
-
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
36
|
-
additionalItems?: JSONSchema7Definition | undefined;
|
|
37
|
-
maxItems?: number | undefined;
|
|
38
|
-
minItems?: number | undefined;
|
|
39
|
-
uniqueItems?: boolean | undefined;
|
|
40
|
-
contains?: JSONSchema7Definition | undefined;
|
|
41
|
-
maxProperties?: number | undefined;
|
|
42
|
-
minProperties?: number | undefined;
|
|
43
|
-
required?: string[] | undefined;
|
|
44
|
-
properties?: {
|
|
45
|
-
[key: string]: JSONSchema7Definition;
|
|
46
|
-
} | undefined;
|
|
47
|
-
patternProperties?: {
|
|
48
|
-
[key: string]: JSONSchema7Definition;
|
|
49
|
-
} | undefined;
|
|
50
|
-
additionalProperties?: JSONSchema7Definition | undefined;
|
|
51
|
-
dependencies?: {
|
|
52
|
-
[key: string]: JSONSchema7Definition | string[];
|
|
53
|
-
} | undefined;
|
|
54
|
-
propertyNames?: JSONSchema7Definition | undefined;
|
|
55
|
-
if?: JSONSchema7Definition | undefined;
|
|
56
|
-
then?: JSONSchema7Definition | undefined;
|
|
57
|
-
else?: JSONSchema7Definition | undefined;
|
|
58
|
-
allOf?: JSONSchema7Definition[] | undefined;
|
|
59
|
-
anyOf?: JSONSchema7Definition[] | undefined;
|
|
60
|
-
oneOf?: JSONSchema7Definition[] | undefined;
|
|
61
|
-
not?: JSONSchema7Definition | undefined;
|
|
62
|
-
format?: string | undefined;
|
|
63
|
-
contentMediaType?: string | undefined;
|
|
64
|
-
contentEncoding?: string | undefined;
|
|
65
|
-
title?: string | undefined;
|
|
66
|
-
description?: string | undefined;
|
|
67
|
-
default?: import("json-schema").JSONSchema7Type | undefined;
|
|
68
|
-
readOnly?: boolean | undefined;
|
|
69
|
-
writeOnly?: boolean | undefined;
|
|
70
|
-
examples?: import("json-schema").JSONSchema7Type | undefined;
|
|
71
|
-
discriminator?: {
|
|
72
|
-
propertyName: string;
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* Promotes a specific object schema definition within a JSON Schema, potentially simplifying the schema
|
|
77
|
-
* by focusing on the desired object and pruning unused definitions.
|
|
78
|
-
*
|
|
79
|
-
* Note this does not support promoting schemas that are self-referential
|
|
80
|
-
*
|
|
81
|
-
* @param {JSONSchema7} schema - The full JSON Schema to process.
|
|
82
|
-
* @param {string} ref - The reference string identifying the target object schema within the definitions.
|
|
83
|
-
* @return {ObjectSchemaWithProperties} The promoted object schema along with pruned definitions.
|
|
84
|
-
* @throws {Error} If the specified reference does not correspond to a valid object definition within the schema.
|
|
85
|
-
*/
|
|
86
|
-
export declare function promoteObjectSchema(schema: JSONSchema7, ref: string): {
|
|
87
|
-
definitions: {
|
|
88
|
-
[x: string]: JSONSchema7Definition;
|
|
89
|
-
};
|
|
90
|
-
minimum?: number | undefined | undefined;
|
|
91
|
-
maximum?: number | undefined | undefined;
|
|
92
|
-
exclusiveMinimum?: number | undefined | undefined;
|
|
93
|
-
exclusiveMaximum?: number | undefined | undefined;
|
|
94
|
-
multipleOf?: number | undefined | undefined;
|
|
95
|
-
format?: string | undefined | undefined;
|
|
96
|
-
allOf?: JSONSchema7Definition[] | undefined | undefined;
|
|
97
|
-
anyOf?: JSONSchema7Definition[] | undefined | undefined;
|
|
98
|
-
oneOf?: JSONSchema7Definition[] | undefined | undefined;
|
|
99
|
-
if?: JSONSchema7Definition | undefined;
|
|
100
|
-
then?: JSONSchema7Definition | undefined;
|
|
101
|
-
else?: JSONSchema7Definition | undefined;
|
|
102
|
-
not?: JSONSchema7Definition | undefined;
|
|
103
|
-
$id?: string | undefined | undefined;
|
|
104
|
-
$ref?: string | undefined | undefined;
|
|
105
|
-
$defs?: {
|
|
106
|
-
[key: string]: JSONSchema7Definition;
|
|
107
|
-
} | undefined | undefined;
|
|
108
|
-
$schema?: string | undefined;
|
|
109
|
-
$comment?: string | undefined | undefined;
|
|
110
|
-
type: "object";
|
|
111
|
-
enum?: import("json-schema").JSONSchema7Type[] | undefined | undefined;
|
|
112
|
-
const?: import("json-schema").JSONSchema7Type | undefined;
|
|
113
|
-
maxLength?: number | undefined | undefined;
|
|
114
|
-
minLength?: number | undefined | undefined;
|
|
115
|
-
pattern?: string | undefined | undefined;
|
|
116
|
-
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
117
|
-
additionalItems?: JSONSchema7Definition | undefined;
|
|
118
|
-
maxItems?: number | undefined | undefined;
|
|
119
|
-
minItems?: number | undefined | undefined;
|
|
120
|
-
uniqueItems?: boolean | undefined | undefined;
|
|
121
|
-
contains?: JSONSchema7Definition | undefined;
|
|
122
|
-
maxProperties?: number | undefined | undefined;
|
|
123
|
-
minProperties?: number | undefined | undefined;
|
|
124
|
-
required?: string[] | undefined | undefined;
|
|
125
|
-
patternProperties?: {
|
|
126
|
-
[key: string]: JSONSchema7Definition;
|
|
127
|
-
} | undefined | undefined;
|
|
128
|
-
additionalProperties?: JSONSchema7Definition | undefined;
|
|
129
|
-
dependencies?: {
|
|
130
|
-
[key: string]: JSONSchema7Definition | string[];
|
|
131
|
-
} | undefined | undefined;
|
|
132
|
-
propertyNames?: JSONSchema7Definition | undefined;
|
|
133
|
-
contentMediaType?: string | undefined | undefined;
|
|
134
|
-
contentEncoding?: string | undefined | undefined;
|
|
135
|
-
title?: string | undefined | undefined;
|
|
136
|
-
description?: string | undefined | undefined;
|
|
137
|
-
default?: import("json-schema").JSONSchema7Type | undefined;
|
|
138
|
-
readOnly?: boolean | undefined | undefined;
|
|
139
|
-
writeOnly?: boolean | undefined | undefined;
|
|
140
|
-
examples?: import("json-schema").JSONSchema7Type | undefined;
|
|
141
|
-
discriminator?: {
|
|
142
|
-
propertyName: string;
|
|
143
|
-
} | undefined;
|
|
144
|
-
properties: Exclude<JSONSchema7["properties"], undefined>;
|
|
145
|
-
};
|
package/dist/utils/references.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { visit } from '@takeshape/util';
|
|
2
|
-
import { isObjectSchemaWithProperties, isSchema } from "./type-utils.js";
|
|
3
|
-
function collectRefs(def, key, collect) {
|
|
4
|
-
if (!isSchema(def)) {
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
7
|
-
visit(def, ['$ref'], ($ref) => {
|
|
8
|
-
if ($ref && typeof $ref === 'string') {
|
|
9
|
-
collect($ref, key);
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
export function getDefinitionKey(ref) {
|
|
14
|
-
const [, prefix, name] = ref.split('/');
|
|
15
|
-
if (!name || (prefix !== 'definitions' && prefix !== '$defs')) {
|
|
16
|
-
throw new Error(`Invalid reference: ${ref}`);
|
|
17
|
-
}
|
|
18
|
-
return name;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Get all the definitions that are referenced by other definitions.
|
|
22
|
-
*/
|
|
23
|
-
export function getReferenceMap(definitions) {
|
|
24
|
-
const references = new Map();
|
|
25
|
-
const visited = new Set();
|
|
26
|
-
const addToReferenceMap = (ref, key) => {
|
|
27
|
-
const refKey = getDefinitionKey(ref);
|
|
28
|
-
// Circular reference
|
|
29
|
-
if (key === refKey) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const visitKey = `${key}->${refKey}`;
|
|
33
|
-
if (visited.has(visitKey))
|
|
34
|
-
return;
|
|
35
|
-
visited.add(visitKey);
|
|
36
|
-
const refSet = references.get(key) ?? new Set();
|
|
37
|
-
refSet.add(refKey);
|
|
38
|
-
references.set(key, refSet);
|
|
39
|
-
const refDef = definitions[refKey];
|
|
40
|
-
if (refDef && isSchema(refDef)) {
|
|
41
|
-
collectRefs(refDef, key, addToReferenceMap);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
for (const [key, def] of Object.entries(definitions)) {
|
|
45
|
-
if (isSchema(def)) {
|
|
46
|
-
collectRefs(def, key, addToReferenceMap);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return references;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Prunes the schema definitions to only include those that are referenced either directly
|
|
53
|
-
* or indirectly by the specified root reference.
|
|
54
|
-
*
|
|
55
|
-
* @param {JSONSchema7} schema - The JSON schema object containing the definitions to prune.
|
|
56
|
-
* @param {string} ref - The reference identifier that serves as the root for pruning definitions.
|
|
57
|
-
* @return {JSONSchema7} - A new JSON schema object with pruned definitions.
|
|
58
|
-
*/
|
|
59
|
-
export function pruneSchemaDefinitions(schema, ref) {
|
|
60
|
-
if (!schema.definitions) {
|
|
61
|
-
throw new Error('Schema does not have definitions');
|
|
62
|
-
}
|
|
63
|
-
const references = new Set();
|
|
64
|
-
const visited = new Set();
|
|
65
|
-
const addToReferences = (ref) => {
|
|
66
|
-
const definitionKey = getDefinitionKey(ref);
|
|
67
|
-
if (visited.has(definitionKey)) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
visited.add(definitionKey);
|
|
71
|
-
references.add(definitionKey);
|
|
72
|
-
const refDef = schema.definitions?.[definitionKey];
|
|
73
|
-
if (refDef && isSchema(refDef)) {
|
|
74
|
-
collectRefs(refDef, definitionKey, addToReferences);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
addToReferences(ref);
|
|
78
|
-
const newDefinitions = {};
|
|
79
|
-
for (const ref of references) {
|
|
80
|
-
const def = schema.definitions?.[ref];
|
|
81
|
-
if (def) {
|
|
82
|
-
newDefinitions[ref] = def;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return {
|
|
86
|
-
...schema,
|
|
87
|
-
definitions: newDefinitions
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Promotes a specific object schema definition within a JSON Schema, potentially simplifying the schema
|
|
92
|
-
* by focusing on the desired object and pruning unused definitions.
|
|
93
|
-
*
|
|
94
|
-
* Note this does not support promoting schemas that are self-referential
|
|
95
|
-
*
|
|
96
|
-
* @param {JSONSchema7} schema - The full JSON Schema to process.
|
|
97
|
-
* @param {string} ref - The reference string identifying the target object schema within the definitions.
|
|
98
|
-
* @return {ObjectSchemaWithProperties} The promoted object schema along with pruned definitions.
|
|
99
|
-
* @throws {Error} If the specified reference does not correspond to a valid object definition within the schema.
|
|
100
|
-
*/
|
|
101
|
-
export function promoteObjectSchema(schema, ref) {
|
|
102
|
-
const key = getDefinitionKey(ref);
|
|
103
|
-
const definition = schema.definitions?.[key];
|
|
104
|
-
if (!definition || !isObjectSchemaWithProperties(definition)) {
|
|
105
|
-
throw new Error(`Could not find object definition for ${ref}`);
|
|
106
|
-
}
|
|
107
|
-
const prunedSchema = pruneSchemaDefinitions(schema, ref);
|
|
108
|
-
const { [key]: _, ...rest } = prunedSchema.definitions;
|
|
109
|
-
return {
|
|
110
|
-
...definition,
|
|
111
|
-
definitions: rest
|
|
112
|
-
};
|
|
113
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema7Definition } from 'json-schema';
|
|
2
|
-
import type { AllOfSchema, AnyOfSchema, ArraySchema, ConstSchema, EnumSchema, ListSchema, ObjectSchema, ObjectSchemaWithProperties, OneOfSchema, PrimitiveSchema, RefSchema, TupleSchema, TypeUnionSchema } from './types.ts';
|
|
3
|
-
export declare function isBooleanProperty(schema: JSONSchema7Definition): schema is boolean;
|
|
4
|
-
export declare function isSchema(schema: JSONSchema7Definition): schema is Exclude<JSONSchema7Definition, boolean>;
|
|
5
|
-
export declare function isOneOfSchema(schema: JSONSchema7Definition): schema is OneOfSchema;
|
|
6
|
-
export declare function isAnyOfSchema(schema: JSONSchema7Definition): schema is AnyOfSchema;
|
|
7
|
-
export declare function isAllOfSchema(schema: JSONSchema7Definition): schema is AllOfSchema;
|
|
8
|
-
export declare function isUnionSchema(schema: JSONSchema7Definition): schema is OneOfSchema | AnyOfSchema | AllOfSchema;
|
|
9
|
-
export declare function isObjectSchema(schema: JSONSchema7Definition): schema is ObjectSchema;
|
|
10
|
-
export declare function isObjectSchemaWithProperties(schema: JSONSchema7Definition): schema is ObjectSchemaWithProperties;
|
|
11
|
-
export declare function isArraySchema(schema: JSONSchema7Definition): schema is ArraySchema;
|
|
12
|
-
export declare function isListSchema(schema: JSONSchema7Definition): schema is ListSchema;
|
|
13
|
-
export declare function isTupleSchema(schema: JSONSchema7Definition): schema is TupleSchema;
|
|
14
|
-
export declare function isEnumSchema(schema: JSONSchema7Definition): schema is EnumSchema;
|
|
15
|
-
export declare function isConstSchema(schema: JSONSchema7Definition): schema is ConstSchema;
|
|
16
|
-
export declare function isRefSchema(schema: JSONSchema7Definition): schema is RefSchema;
|
|
17
|
-
export declare function isTypeUnionSchema(schema: JSONSchema7Definition): schema is TypeUnionSchema;
|
|
18
|
-
export declare function isPrimitiveSchema(schema: JSONSchema7Definition): schema is PrimitiveSchema;
|
package/dist/utils/type-utils.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { jsonSchema7PrimitiveTypes } from "./constants.js";
|
|
2
|
-
export function isBooleanProperty(schema) {
|
|
3
|
-
return typeof schema === 'boolean';
|
|
4
|
-
}
|
|
5
|
-
export function isSchema(schema) {
|
|
6
|
-
return !isBooleanProperty(schema);
|
|
7
|
-
}
|
|
8
|
-
export function isOneOfSchema(schema) {
|
|
9
|
-
return isSchema(schema) && Array.isArray(schema.oneOf);
|
|
10
|
-
}
|
|
11
|
-
export function isAnyOfSchema(schema) {
|
|
12
|
-
return isSchema(schema) && Array.isArray(schema.anyOf);
|
|
13
|
-
}
|
|
14
|
-
export function isAllOfSchema(schema) {
|
|
15
|
-
return isSchema(schema) && Array.isArray(schema.allOf);
|
|
16
|
-
}
|
|
17
|
-
export function isUnionSchema(schema) {
|
|
18
|
-
return isOneOfSchema(schema) || isAnyOfSchema(schema) || isAllOfSchema(schema);
|
|
19
|
-
}
|
|
20
|
-
export function isObjectSchema(schema) {
|
|
21
|
-
return isSchema(schema) && schema.type === 'object';
|
|
22
|
-
}
|
|
23
|
-
export function isObjectSchemaWithProperties(schema) {
|
|
24
|
-
return isObjectSchema(schema) && typeof schema.properties === 'object';
|
|
25
|
-
}
|
|
26
|
-
export function isArraySchema(schema) {
|
|
27
|
-
return isSchema(schema) && schema.type === 'array';
|
|
28
|
-
}
|
|
29
|
-
export function isListSchema(schema) {
|
|
30
|
-
return isArraySchema(schema) && typeof schema.items === 'object' && !Array.isArray(schema.items);
|
|
31
|
-
}
|
|
32
|
-
export function isTupleSchema(schema) {
|
|
33
|
-
return isArraySchema(schema) && Array.isArray(schema.items);
|
|
34
|
-
}
|
|
35
|
-
export function isEnumSchema(schema) {
|
|
36
|
-
return Boolean(isSchema(schema) && schema.enum);
|
|
37
|
-
}
|
|
38
|
-
export function isConstSchema(schema) {
|
|
39
|
-
return Boolean(isSchema(schema) && schema.const);
|
|
40
|
-
}
|
|
41
|
-
export function isRefSchema(schema) {
|
|
42
|
-
return Boolean(isSchema(schema) && schema.$ref);
|
|
43
|
-
}
|
|
44
|
-
export function isTypeUnionSchema(schema) {
|
|
45
|
-
return isSchema(schema) && Array.isArray(schema.type);
|
|
46
|
-
}
|
|
47
|
-
export function isPrimitiveSchema(schema) {
|
|
48
|
-
if (typeof schema !== 'object') {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
if (isTypeUnionSchema(schema)) {
|
|
52
|
-
return schema.type.every((t) => jsonSchema7PrimitiveTypes.includes(t));
|
|
53
|
-
}
|
|
54
|
-
return jsonSchema7PrimitiveTypes.includes(schema.type);
|
|
55
|
-
}
|