@tsofist/schema-forge 2.9.0 → 2.10.0-next.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/lib/artefacts-policy.d.ts +3 -0
- package/lib/artefacts-policy.d.ts.map +1 -0
- package/lib/artefacts-policy.js +7 -0
- package/lib/dbml-generator/converter.d.ts +5 -0
- package/lib/dbml-generator/converter.d.ts.map +1 -0
- package/lib/dbml-generator/converter.js +15 -0
- package/lib/dbml-generator/generator.d.ts +4 -0
- package/lib/dbml-generator/generator.d.ts.map +1 -0
- package/lib/dbml-generator/generator.js +423 -0
- package/lib/dbml-generator/generator.spec.d.ts +2 -0
- package/lib/dbml-generator/generator.spec.d.ts.map +1 -0
- package/lib/dbml-generator/generator.spec.js +94 -0
- package/lib/dbml-generator/types.d.ts +144 -0
- package/lib/dbml-generator/types.d.ts.map +1 -0
- package/lib/dbml-generator/types.js +53 -0
- package/lib/definition-info/api-signature.d.ts +18 -0
- package/lib/definition-info/api-signature.d.ts.map +1 -0
- package/lib/definition-info/api-signature.js +31 -0
- package/lib/definition-info/parser.d.ts +3 -0
- package/lib/definition-info/parser.d.ts.map +1 -0
- package/lib/definition-info/parser.js +44 -0
- package/lib/definition-info/ref.d.ts +3 -0
- package/lib/definition-info/ref.d.ts.map +1 -0
- package/lib/definition-info/ref.js +6 -0
- package/lib/definition-info/types.d.ts +48 -0
- package/lib/definition-info/types.d.ts.map +1 -0
- package/lib/definition-info/types.js +20 -0
- package/lib/efc.d.ts +27 -0
- package/lib/efc.d.ts.map +1 -0
- package/lib/efc.js +9 -0
- package/lib/fake-generator/generator-host.d.ts +4 -0
- package/lib/fake-generator/generator-host.d.ts.map +1 -0
- package/lib/fake-generator/generator-host.js +57 -0
- package/lib/fake-generator/generator.d.ts +6 -0
- package/lib/fake-generator/generator.d.ts.map +1 -0
- package/lib/fake-generator/generator.js +64 -0
- package/lib/fake-generator/generator.spec.d.ts +2 -0
- package/lib/fake-generator/generator.spec.d.ts.map +1 -0
- package/lib/{fake-generator.spec.js → fake-generator/generator.spec.js} +21 -19
- package/lib/fake-generator/modules.d.ts +3 -0
- package/lib/fake-generator/modules.d.ts.map +1 -0
- package/lib/fake-generator/modules.js +44 -0
- package/lib/fake-generator/types.d.ts +18 -0
- package/lib/fake-generator/types.d.ts.map +1 -0
- package/lib/fake-generator/types.js +2 -0
- package/lib/schema-generator/extended-annotations-reader.d.ts +2 -0
- package/lib/schema-generator/extended-annotations-reader.d.ts.map +1 -0
- package/lib/{util/patch.extended-annotations-reader.js → schema-generator/extended-annotations-reader.js} +2 -2
- package/lib/schema-generator/forge.d.ts +3 -0
- package/lib/schema-generator/forge.d.ts.map +1 -0
- package/lib/{generator.js → schema-generator/forge.js} +13 -33
- package/lib/schema-generator/forge.spec.d.ts +2 -0
- package/lib/schema-generator/forge.spec.d.ts.map +1 -0
- package/lib/{generator.spec.js → schema-generator/forge.spec.js} +157 -103
- package/lib/schema-generator/format-error.d.ts +2 -0
- package/lib/schema-generator/format-error.d.ts.map +1 -0
- package/lib/{util/format.error.js → schema-generator/format-error.js} +3 -2
- package/lib/schema-generator/generate-drafts.d.ts +17 -0
- package/lib/schema-generator/generate-drafts.d.ts.map +1 -0
- package/lib/{generator/types-generator.js → schema-generator/generate-drafts.js} +140 -120
- package/lib/schema-generator/generate-schema.d.ts +15 -0
- package/lib/schema-generator/generate-schema.d.ts.map +1 -0
- package/lib/{generator/schema-generator.js → schema-generator/generate-schema.js} +46 -19
- package/lib/{util/tsc.d.ts → schema-generator/helpers-tsc.d.ts} +2 -1
- package/lib/schema-generator/helpers-tsc.d.ts.map +1 -0
- package/lib/schema-generator/shrink-definition-name.d.ts +7 -0
- package/lib/schema-generator/shrink-definition-name.d.ts.map +1 -0
- package/lib/schema-generator/shrink-definition-name.js +21 -0
- package/lib/schema-generator/sort-properties.d.ts +3 -0
- package/lib/schema-generator/sort-properties.d.ts.map +1 -0
- package/lib/{util → schema-generator}/sort-properties.js +2 -2
- package/lib/schema-generator/types.d.ts +18 -0
- package/lib/schema-generator/types.d.ts.map +1 -0
- package/lib/{generator → schema-generator}/types.js +20 -8
- package/lib/schema-registry/kw-api.d.ts +3 -0
- package/lib/schema-registry/kw-api.d.ts.map +1 -0
- package/lib/schema-registry/kw-api.js +25 -0
- package/lib/schema-registry/kw-common.d.ts +3 -0
- package/lib/schema-registry/kw-common.d.ts.map +1 -0
- package/lib/schema-registry/kw-common.js +47 -0
- package/lib/schema-registry/kw-dbml.d.ts +3 -0
- package/lib/schema-registry/kw-dbml.d.ts.map +1 -0
- package/lib/schema-registry/kw-dbml.js +79 -0
- package/lib/schema-registry/loader.d.ts +10 -0
- package/lib/schema-registry/loader.d.ts.map +1 -0
- package/lib/schema-registry/loader.js +24 -0
- package/lib/schema-registry/registry.d.ts +25 -0
- package/lib/schema-registry/registry.d.ts.map +1 -0
- package/lib/schema-registry/registry.js +230 -0
- package/lib/schema-registry/types.d.ts +78 -0
- package/lib/schema-registry/types.d.ts.map +1 -0
- package/lib/schema-registry/types.js +2 -0
- package/lib/types.d.ts +53 -79
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +0 -15
- package/package.json +9 -6
- package/lib/fake-generator.d.ts +0 -21
- package/lib/fake-generator.js +0 -158
- package/lib/fake-generator.spec.d.ts +0 -1
- package/lib/generator/schema-generator.d.ts +0 -20
- package/lib/generator/types-generator.d.ts +0 -13
- package/lib/generator/types.d.ts +0 -40
- package/lib/generator.d.ts +0 -5
- package/lib/generator.spec.d.ts +0 -1
- package/lib/index.d.ts +0 -7
- package/lib/index.js +0 -64
- package/lib/types/db.types.d.ts +0 -96
- package/lib/types/db.types.js +0 -17
- package/lib/util/format.error.d.ts +0 -1
- package/lib/util/patch.extended-annotations-reader.d.ts +0 -1
- package/lib/util/sort-properties.d.ts +0 -2
- package/lib/validator.d.ts +0 -47
- package/lib/validator.js +0 -396
- /package/lib/{util/tsc.js → schema-generator/helpers-tsc.js} +0 -0
|
@@ -1,20 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.SFG_CONFIG_MANDATORY = exports.SFG_CONFIG_DEFAULTS = exports.TMP_FILES_SUFFIX = void 0;
|
|
4
4
|
exports.TMP_FILES_SUFFIX = '.schema-forge.temporary-generated.tmp';
|
|
5
|
-
exports.
|
|
6
|
-
jsDoc: 'extended',
|
|
7
|
-
topRef: true,
|
|
8
|
-
skipTypeCheck: false,
|
|
9
|
-
};
|
|
10
|
-
exports.SG_CONFIG_DEFAULTS = {
|
|
5
|
+
exports.SFG_CONFIG_DEFAULTS = {
|
|
11
6
|
sortProps: true,
|
|
12
7
|
additionalProperties: false,
|
|
13
8
|
expose: 'export',
|
|
14
9
|
strictTuples: true,
|
|
15
|
-
extraTags: [
|
|
10
|
+
extraTags: [
|
|
11
|
+
//
|
|
12
|
+
'apiMember',
|
|
13
|
+
'apiMethod',
|
|
14
|
+
'apiInterface',
|
|
15
|
+
'apiProperty',
|
|
16
|
+
//
|
|
17
|
+
'dbColumn',
|
|
18
|
+
'dbIndex',
|
|
19
|
+
'dbEntity',
|
|
20
|
+
//
|
|
21
|
+
'faker',
|
|
22
|
+
],
|
|
16
23
|
encodeRefs: false,
|
|
17
24
|
markdownDescription: false,
|
|
18
25
|
discriminatorType: undefined,
|
|
19
26
|
functions: 'hide',
|
|
20
27
|
};
|
|
28
|
+
exports.SFG_CONFIG_MANDATORY = {
|
|
29
|
+
jsDoc: 'extended',
|
|
30
|
+
topRef: true,
|
|
31
|
+
skipTypeCheck: false,
|
|
32
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kw-api.d.ts","sourceRoot":"","sources":["../../src/schema-registry/kw-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,KAAK,CAAC;AAO7C,eAAO,MAAM,wBAAwB,EAAE,SAAS,iBAAiB,EAiBvD,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SFRAPIDefinitionKeywords = void 0;
|
|
4
|
+
const NP_API_INTERFACE = '^[A-Z][a-zA-Z0-9]+$';
|
|
5
|
+
const NP_API_PROP = '^[a-z][a-zA-Z0-9]+$';
|
|
6
|
+
const NP_API_METHOD = NP_API_PROP;
|
|
7
|
+
const NP_API_MEMBER = `${NP_API_INTERFACE.substring(0, NP_API_INTERFACE.length - 1)}#${NP_API_PROP.substring(1)}`;
|
|
8
|
+
exports.SFRAPIDefinitionKeywords = [
|
|
9
|
+
{
|
|
10
|
+
keyword: ['interface', 'apiInterface'],
|
|
11
|
+
metaSchema: { type: 'string', pattern: NP_API_INTERFACE },
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
keyword: ['property', 'apiProperty'],
|
|
15
|
+
metaSchema: { type: 'string', pattern: NP_API_PROP },
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
keyword: ['method', 'apiMethod'],
|
|
19
|
+
metaSchema: { type: 'string', pattern: NP_API_METHOD },
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
keyword: ['member', 'apiMember'],
|
|
23
|
+
metaSchema: { type: 'string', pattern: NP_API_MEMBER },
|
|
24
|
+
},
|
|
25
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kw-common.d.ts","sourceRoot":"","sources":["../../src/schema-registry/kw-common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,KAAK,CAAC;AAI7C,eAAO,MAAM,iBAAiB,EAAE,SAAS,iBAAiB,EA0ChD,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SFRCommonKeywords = void 0;
|
|
4
|
+
const FakerModulePattern = '^[a-zA-Z.]+$';
|
|
5
|
+
exports.SFRCommonKeywords = [
|
|
6
|
+
{
|
|
7
|
+
keyword: 'version',
|
|
8
|
+
dependencies: ['$id', '$schema'],
|
|
9
|
+
metaSchema: { type: 'string' },
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
keyword: 'hash',
|
|
13
|
+
dependencies: ['$id', '$schema'],
|
|
14
|
+
metaSchema: { type: 'string' },
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
keyword: 'faker',
|
|
18
|
+
metaSchema: {
|
|
19
|
+
oneOf: [
|
|
20
|
+
{
|
|
21
|
+
// https://fakerjs.dev/api/person.html
|
|
22
|
+
// https://fakerjs.dev/api/company.html#name
|
|
23
|
+
// https://github.com/json-schema-faker/json-schema-faker/blob/master/docs/USAGE.md
|
|
24
|
+
// example (schema):
|
|
25
|
+
// faker: 'person.fullName'
|
|
26
|
+
// faker: 'person.firstName'
|
|
27
|
+
// faker: 'company.name'
|
|
28
|
+
// example (jsdoc):
|
|
29
|
+
// @faker company.name
|
|
30
|
+
type: 'string',
|
|
31
|
+
pattern: FakerModulePattern,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
// https://fakerjs.dev/api/lorem.html#words
|
|
35
|
+
// example (schema):
|
|
36
|
+
// faker: { 'lorem.words': [{ min: 30, max: 50 }] },
|
|
37
|
+
// example (jsdoc):
|
|
38
|
+
// @faker { 'lorem.words': [{ min: 5, max: 10 }] }
|
|
39
|
+
type: 'object',
|
|
40
|
+
propertyNames: {
|
|
41
|
+
pattern: FakerModulePattern,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kw-dbml.d.ts","sourceRoot":"","sources":["../../src/schema-registry/kw-dbml.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,KAAK,CAAC;AA6C7C,eAAO,MAAM,eAAe,EAAE,SAAS,iBAAiB,EA0C9C,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SFRDBMLKeywords = void 0;
|
|
4
|
+
const types_1 = require("../dbml-generator/types");
|
|
5
|
+
const NP_IX = '^ix_[a-z][a-zA-Z0-9_]+$';
|
|
6
|
+
const NP_ENTITY = '^([a-zA-Z_][a-z0-9_]*\\.)?[a-z_][a-z0-9_]*$';
|
|
7
|
+
const NP_NESTED_PROP = '^[a-z][a-zA-Z0-9.-]+$';
|
|
8
|
+
const DBMLIndexOptionsProperties = {
|
|
9
|
+
name: { type: 'string', pattern: NP_IX },
|
|
10
|
+
unique: { type: 'boolean' },
|
|
11
|
+
pk: { type: 'boolean' },
|
|
12
|
+
type: {
|
|
13
|
+
type: 'string',
|
|
14
|
+
enum: types_1.DBMLIndexTypeList,
|
|
15
|
+
},
|
|
16
|
+
note: { type: 'string' },
|
|
17
|
+
comment: { type: 'string' },
|
|
18
|
+
};
|
|
19
|
+
const DBMLIndexSchema = {
|
|
20
|
+
type: ['string', 'boolean', 'object', 'array'],
|
|
21
|
+
pattern: NP_IX,
|
|
22
|
+
additionalProperties: false,
|
|
23
|
+
properties: DBMLIndexOptionsProperties,
|
|
24
|
+
items: {},
|
|
25
|
+
minItems: 1,
|
|
26
|
+
examples: [
|
|
27
|
+
true,
|
|
28
|
+
false,
|
|
29
|
+
`ix_my_index`,
|
|
30
|
+
`{ unique: true, name: 'ix_my_index', note: 'This is important' }`,
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
DBMLIndexSchema.items = {
|
|
34
|
+
...DBMLIndexSchema,
|
|
35
|
+
type: ['string', 'boolean', 'object'],
|
|
36
|
+
};
|
|
37
|
+
exports.SFRDBMLKeywords = [
|
|
38
|
+
{
|
|
39
|
+
keyword: 'dbIndex',
|
|
40
|
+
metaSchema: DBMLIndexSchema,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
keyword: 'dbColumn',
|
|
44
|
+
metaSchema: {
|
|
45
|
+
type: 'object',
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
properties: {
|
|
48
|
+
pk: { type: 'boolean' },
|
|
49
|
+
type: { enum: types_1.DBMLColumnTypeList },
|
|
50
|
+
// name: { type: 'string' }, todo
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
keyword: 'dbEntity',
|
|
56
|
+
metaSchema: {
|
|
57
|
+
type: ['string', 'object'],
|
|
58
|
+
pattern: NP_ENTITY,
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
properties: {
|
|
61
|
+
name: { type: 'string', pattern: NP_ENTITY },
|
|
62
|
+
indexes: {
|
|
63
|
+
type: 'object',
|
|
64
|
+
additionalProperties: DBMLIndexSchema,
|
|
65
|
+
propertyNames: { pattern: NP_NESTED_PROP },
|
|
66
|
+
},
|
|
67
|
+
note: { type: 'string' },
|
|
68
|
+
comment: { type: 'string' },
|
|
69
|
+
alias: { type: 'string' },
|
|
70
|
+
settings: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
headercolor: { type: 'string' },
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { JSONSchema7Object } from 'json-schema';
|
|
2
|
+
/**
|
|
3
|
+
* Load JSON schema files
|
|
4
|
+
*/
|
|
5
|
+
export declare function loadJSONSchema(files: string[]): Promise<JSONSchema7Object[]>;
|
|
6
|
+
/**
|
|
7
|
+
* Load JSON schema files synchronously
|
|
8
|
+
*/
|
|
9
|
+
export declare function loadJSONSchemaSync(files: string[]): JSONSchema7Object[];
|
|
10
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../src/schema-registry/loader.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD;;GAEG;AACH,wBAAsB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAOlF;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,iBAAiB,EAAE,CAKvE"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadJSONSchema = loadJSONSchema;
|
|
4
|
+
exports.loadJSONSchemaSync = loadJSONSchemaSync;
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const promises_1 = require("node:fs/promises");
|
|
7
|
+
/**
|
|
8
|
+
* Load JSON schema files
|
|
9
|
+
*/
|
|
10
|
+
async function loadJSONSchema(files) {
|
|
11
|
+
return Promise.all(files.map(async (fn) => {
|
|
12
|
+
const raw = await (0, promises_1.readFile)(fn, { encoding: 'utf8' });
|
|
13
|
+
return JSON.parse(raw);
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Load JSON schema files synchronously
|
|
18
|
+
*/
|
|
19
|
+
function loadJSONSchemaSync(files) {
|
|
20
|
+
return files.map((fn) => {
|
|
21
|
+
const raw = (0, node_fs_1.readFileSync)(fn, { encoding: 'utf8' });
|
|
22
|
+
return JSON.parse(raw);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Options as AjvOptions } from 'ajv';
|
|
2
|
+
import { SchemaForgeRegistry } from './types';
|
|
3
|
+
interface SFROptions {
|
|
4
|
+
/**
|
|
5
|
+
* Extra ajv options
|
|
6
|
+
*
|
|
7
|
+
* @see https://ajv.js.org/options.html Options reference
|
|
8
|
+
* @see https://ajv.js.org/json-schema.html Schema reference
|
|
9
|
+
*/
|
|
10
|
+
engine?: AjvOptions;
|
|
11
|
+
/**
|
|
12
|
+
* Use additional formats via ajv-formats
|
|
13
|
+
*
|
|
14
|
+
* @see https://ajv.js.org/packages/ajv-formats.html ajv-formats
|
|
15
|
+
*/
|
|
16
|
+
extendedVocabulary?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Create JSON schema data validation registry
|
|
20
|
+
*
|
|
21
|
+
* @see https://ajv.js.org/json-schema.html ajv
|
|
22
|
+
*/
|
|
23
|
+
export declare function createSchemaForgeRegistry(options?: SFROptions): SchemaForgeRegistry;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/schema-registry/registry.ts"],"names":[],"mappings":"AAOA,OAAY,EAGR,KAAK,OAAO,IAAI,UAAU,EAE7B,MAAM,KAAK,CAAC;AAiBb,OAAO,EAAE,mBAAmB,EAA+C,MAAM,SAAS,CAAC;AAsB3F,UAAU,UAAU;IAChB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,CAAC,EAAE,UAAU,GAAG,mBAAmB,CAkOnF"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSchemaForgeRegistry = createSchemaForgeRegistry;
|
|
4
|
+
const as_array_1 = require("@tsofist/stem/lib/as-array");
|
|
5
|
+
const chunk_1 = require("@tsofist/stem/lib/chunk");
|
|
6
|
+
const error_1 = require("@tsofist/stem/lib/error");
|
|
7
|
+
const entries_1 = require("@tsofist/stem/lib/object/entries");
|
|
8
|
+
const values_1 = require("@tsofist/stem/lib/object/values");
|
|
9
|
+
const delay_1 = require("@tsofist/stem/lib/timers/delay");
|
|
10
|
+
const ajv_1 = require("ajv");
|
|
11
|
+
const ajv_formats_1 = require("ajv-formats");
|
|
12
|
+
const parser_1 = require("../definition-info/parser");
|
|
13
|
+
const ref_1 = require("../definition-info/ref");
|
|
14
|
+
const efc_1 = require("../efc");
|
|
15
|
+
const types_1 = require("../schema-generator/types");
|
|
16
|
+
const kw_api_1 = require("./kw-api");
|
|
17
|
+
const kw_common_1 = require("./kw-common");
|
|
18
|
+
const kw_dbml_1 = require("./kw-dbml");
|
|
19
|
+
const SFR_DEFAULT_OPTIONS = {
|
|
20
|
+
meta: true,
|
|
21
|
+
defaultMeta: 'http://json-schema.org/draft-07/schema',
|
|
22
|
+
allErrors: true,
|
|
23
|
+
strict: true,
|
|
24
|
+
strictSchema: true,
|
|
25
|
+
strictTypes: false,
|
|
26
|
+
strictTuples: false,
|
|
27
|
+
allowUnionTypes: true,
|
|
28
|
+
coerceTypes: false,
|
|
29
|
+
removeAdditional: false,
|
|
30
|
+
unicodeRegExp: true,
|
|
31
|
+
useDefaults: false,
|
|
32
|
+
addUsedSchema: false,
|
|
33
|
+
inlineRefs: true,
|
|
34
|
+
ownProperties: true,
|
|
35
|
+
discriminator: false,
|
|
36
|
+
code: { es5: false, esm: false, optimize: 2 },
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Create JSON schema data validation registry
|
|
40
|
+
*
|
|
41
|
+
* @see https://ajv.js.org/json-schema.html ajv
|
|
42
|
+
*/
|
|
43
|
+
function createSchemaForgeRegistry(options) {
|
|
44
|
+
const engineOptions = { ...SFR_DEFAULT_OPTIONS, ...options?.engine };
|
|
45
|
+
const useAdditionalFormats = options?.extendedVocabulary ?? true;
|
|
46
|
+
const initialSchemas = engineOptions.schemas;
|
|
47
|
+
if (initialSchemas)
|
|
48
|
+
delete engineOptions.schemas;
|
|
49
|
+
let rev = 0;
|
|
50
|
+
let engine = new ajv_1.default(engineOptions);
|
|
51
|
+
engine.removeSchema();
|
|
52
|
+
if (initialSchemas)
|
|
53
|
+
engine.addSchema(initialSchemas);
|
|
54
|
+
addJSDocKeywords(engine);
|
|
55
|
+
if (useAdditionalFormats)
|
|
56
|
+
engine = (0, ajv_formats_1.default)(engine);
|
|
57
|
+
function clone(options, onSchema) {
|
|
58
|
+
const schemas = [];
|
|
59
|
+
for (const env of (0, values_1.nonNullableValues)(engine.schemas)) {
|
|
60
|
+
if (env.meta)
|
|
61
|
+
continue;
|
|
62
|
+
const schema = onSchema
|
|
63
|
+
? onSchema(env.schema)
|
|
64
|
+
: env.schema;
|
|
65
|
+
schemas.push(schema);
|
|
66
|
+
}
|
|
67
|
+
const opts = {
|
|
68
|
+
...options,
|
|
69
|
+
schemas,
|
|
70
|
+
...options,
|
|
71
|
+
};
|
|
72
|
+
return createSchemaForgeRegistry({
|
|
73
|
+
engine: opts,
|
|
74
|
+
extendedVocabulary: useAdditionalFormats,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function getValidator(ref) {
|
|
78
|
+
if (typeof ref === 'string')
|
|
79
|
+
return engine.getSchema(ref);
|
|
80
|
+
const result = engine.compile(ref);
|
|
81
|
+
checkIsSyncValidator(result);
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
function hasSchema(ref) {
|
|
85
|
+
return ref in engine.schemas || ref in engine.refs || engine.getSchema(ref) != null;
|
|
86
|
+
}
|
|
87
|
+
function addSchema(schema) {
|
|
88
|
+
engine.addSchema(schema);
|
|
89
|
+
rev++;
|
|
90
|
+
}
|
|
91
|
+
function removeSchema(ref) {
|
|
92
|
+
for (const item of (0, as_array_1.asArray)(ref))
|
|
93
|
+
engine.removeSchema(item);
|
|
94
|
+
rev++;
|
|
95
|
+
}
|
|
96
|
+
function clear() {
|
|
97
|
+
engine.removeSchema();
|
|
98
|
+
rev++;
|
|
99
|
+
}
|
|
100
|
+
function validationErrorsText(errors, options) {
|
|
101
|
+
return engine.errorsText(errors, options);
|
|
102
|
+
}
|
|
103
|
+
function validateBySchema(ref, data, instancePath = '') {
|
|
104
|
+
const validator = getValidator(ref);
|
|
105
|
+
if (!validator) {
|
|
106
|
+
return efc_1.SchemaForgeErrors.raise(efc_1.SchemaForgeErrors.EC_SF_SCHEMA_NOT_FOUND, {
|
|
107
|
+
schema: ref,
|
|
108
|
+
instancePath,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const valid = validator(data, {
|
|
112
|
+
instancePath,
|
|
113
|
+
parentData: {},
|
|
114
|
+
parentDataProperty: '',
|
|
115
|
+
rootData: {},
|
|
116
|
+
dynamicAnchors: {},
|
|
117
|
+
});
|
|
118
|
+
const errors = validator.errors;
|
|
119
|
+
return {
|
|
120
|
+
valid,
|
|
121
|
+
errors,
|
|
122
|
+
errorsText(options) {
|
|
123
|
+
return validationErrorsText(errors, options);
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function checkBySchema(ref, data, context) {
|
|
128
|
+
const result = validateBySchema(ref, data, context?.instancePath);
|
|
129
|
+
if (!result.valid) {
|
|
130
|
+
efc_1.SchemaForgeErrors.raise(efc_1.SchemaForgeErrors.EC_SF_VALIDATION_FAILED, {
|
|
131
|
+
...context,
|
|
132
|
+
schema: ref,
|
|
133
|
+
errors: result.errors,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return result.valid;
|
|
137
|
+
}
|
|
138
|
+
function mapDefinitions(callback) {
|
|
139
|
+
const result = [];
|
|
140
|
+
for (const [schemaId, env] of (0, entries_1.entries)(engine.schemas)) {
|
|
141
|
+
if (env &&
|
|
142
|
+
typeof env.schema === 'object' &&
|
|
143
|
+
!schemaId.startsWith('http') &&
|
|
144
|
+
env.schema.definitions &&
|
|
145
|
+
typeof env.schema.definitions === 'object') {
|
|
146
|
+
for (const [name, def] of Object.entries(env.schema.definitions)) {
|
|
147
|
+
result.push(callback(name, schemaId, def));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
function readSchemaKeywords(schema) {
|
|
154
|
+
const result = new Set();
|
|
155
|
+
for (const tag of types_1.SFG_CONFIG_DEFAULTS.extraTags) {
|
|
156
|
+
if (tag in schema)
|
|
157
|
+
result.add(tag);
|
|
158
|
+
}
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
function listDefinitions(predicate) {
|
|
162
|
+
const result = [];
|
|
163
|
+
mapDefinitions((name, schemaId, schema) => {
|
|
164
|
+
const info = (0, parser_1.parseSchemaDefinitionInfo)(name, schemaId);
|
|
165
|
+
if (predicate === undefined || predicate(info, readSchemaKeywords(schema))) {
|
|
166
|
+
result.push(info);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
function getSchema(ref) {
|
|
172
|
+
return getValidator(ref)?.schema;
|
|
173
|
+
}
|
|
174
|
+
function getRootSchema(schemaId) {
|
|
175
|
+
return engine.schemas[schemaId]?.schema;
|
|
176
|
+
}
|
|
177
|
+
function warmupCacheSync() {
|
|
178
|
+
mapDefinitions((name, schemaId) => {
|
|
179
|
+
const ref = (0, ref_1.buildSchemaDefinitionRef)(name, schemaId);
|
|
180
|
+
checkIsSyncValidator(engine.getSchema(ref));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async function warmupCache(schemasPerIteration = 5, delayMs = 1) {
|
|
184
|
+
await (0, chunk_1.chunk)(mapDefinitions((name, schemaId) => (0, ref_1.buildSchemaDefinitionRef)(name, schemaId)), schemasPerIteration, async (items) => {
|
|
185
|
+
for (const ref of items)
|
|
186
|
+
engine.getSchema(ref);
|
|
187
|
+
return (0, delay_1.delay)(delayMs);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
get compilationArtifactCount() {
|
|
192
|
+
const names = new Set([
|
|
193
|
+
//
|
|
194
|
+
...Object.keys(engine.refs),
|
|
195
|
+
...Object.keys(engine.schemas),
|
|
196
|
+
]);
|
|
197
|
+
return names.size;
|
|
198
|
+
},
|
|
199
|
+
get rev() {
|
|
200
|
+
return rev;
|
|
201
|
+
},
|
|
202
|
+
clear,
|
|
203
|
+
clone,
|
|
204
|
+
getRootSchema,
|
|
205
|
+
addSchema,
|
|
206
|
+
getSchema,
|
|
207
|
+
hasSchema,
|
|
208
|
+
removeSchema,
|
|
209
|
+
getValidator,
|
|
210
|
+
validationErrorsText,
|
|
211
|
+
validateBySchema,
|
|
212
|
+
checkBySchema,
|
|
213
|
+
listDefinitions,
|
|
214
|
+
warmupCache,
|
|
215
|
+
warmupCacheSync,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function addJSDocKeywords(engine) {
|
|
219
|
+
engine.addVocabulary([
|
|
220
|
+
//
|
|
221
|
+
...kw_common_1.SFRCommonKeywords,
|
|
222
|
+
...kw_api_1.SFRAPIDefinitionKeywords,
|
|
223
|
+
...kw_dbml_1.SFRDBMLKeywords,
|
|
224
|
+
]);
|
|
225
|
+
}
|
|
226
|
+
function checkIsSyncValidator(fn) {
|
|
227
|
+
if (typeof fn === 'function' && '$async' in fn) {
|
|
228
|
+
(0, error_1.raise)('[SchemaForge] Asynchronous validation schemas are not supported');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ArrayMay, Nullable } from '@tsofist/stem';
|
|
2
|
+
import type { NonNegativeInt } from '@tsofist/stem/lib/number/integer/types';
|
|
3
|
+
import type { ErrorsTextOptions, Options } from 'ajv';
|
|
4
|
+
import type { JSONSchema7, JSONSchema7Object } from 'json-schema';
|
|
5
|
+
import type { SchemaDefinitionInfo } from '../definition-info/types';
|
|
6
|
+
import type { SchemaForgeValidationContextBase } from '../efc';
|
|
7
|
+
import type { SchemaForgeDefinitionRef, SchemaForgeValidationFunction, SchemaForgeValidationReport, SchemaForgeValidationResult } from '../types';
|
|
8
|
+
export type SchemaForgeRegistryListDefinitionsPredicate = (info: SchemaDefinitionInfo, keywords: Set<string>) => boolean;
|
|
9
|
+
export interface SchemaForgeRegistry {
|
|
10
|
+
/**
|
|
11
|
+
* Number of compiled schemas in registry
|
|
12
|
+
*/
|
|
13
|
+
readonly compilationArtifactCount: NonNegativeInt;
|
|
14
|
+
/**
|
|
15
|
+
* Revision number of registry.
|
|
16
|
+
* This number is represented as a number of add/remove/clear operations.
|
|
17
|
+
*/
|
|
18
|
+
readonly rev: NonNegativeInt;
|
|
19
|
+
/**
|
|
20
|
+
* Clone schemaRegistry with overridden options
|
|
21
|
+
*/
|
|
22
|
+
clone: (options?: Omit<Options, 'schemas'>, onSchema?: (schema: JSONSchema7Object) => JSONSchema7Object) => SchemaForgeRegistry;
|
|
23
|
+
/**
|
|
24
|
+
* Remove all schemas from registry
|
|
25
|
+
*/
|
|
26
|
+
clear: VoidFunction;
|
|
27
|
+
/**
|
|
28
|
+
* Add root schema's to registry
|
|
29
|
+
*/
|
|
30
|
+
addSchema: (schema: JSONSchema7Object[]) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Check if schema (or definition) is already registered
|
|
33
|
+
*/
|
|
34
|
+
hasSchema: (ref: SchemaForgeDefinitionRef) => boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Remove schema (or definition) from registry
|
|
37
|
+
*/
|
|
38
|
+
removeSchema: (ref: ArrayMay<SchemaForgeDefinitionRef>) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Get schema (or definition) object
|
|
41
|
+
*/
|
|
42
|
+
getSchema: (ref: SchemaForgeDefinitionRef) => JSONSchema7 | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Get schema (or definition) validation function
|
|
45
|
+
*/
|
|
46
|
+
getValidator: <TData = unknown>(ref: SchemaForgeDefinitionRef | JSONSchema7) => SchemaForgeValidationFunction<TData> | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Get root schema object
|
|
49
|
+
*/
|
|
50
|
+
getRootSchema: (schemaId: string) => JSONSchema7 | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Validate data by schema definition reference
|
|
53
|
+
*/
|
|
54
|
+
validateBySchema: (ref: SchemaForgeDefinitionRef, data: unknown, instancePath?: string) => SchemaForgeValidationResult;
|
|
55
|
+
/**
|
|
56
|
+
* Check (throw error if invalid) data by schema definition reference
|
|
57
|
+
*/
|
|
58
|
+
checkBySchema: <T = unknown, Ctx extends SchemaForgeValidationContextBase = SchemaForgeValidationContextBase>(ref: SchemaForgeDefinitionRef, data: T, context?: Ctx) => data is T;
|
|
59
|
+
/**
|
|
60
|
+
* Get validation errors text
|
|
61
|
+
*/
|
|
62
|
+
validationErrorsText: (errors: Nullable<SchemaForgeValidationReport>, options?: ErrorsTextOptions) => string;
|
|
63
|
+
/**
|
|
64
|
+
* List schema definitions
|
|
65
|
+
*/
|
|
66
|
+
listDefinitions: (predicate?: SchemaForgeRegistryListDefinitionsPredicate) => SchemaDefinitionInfo[];
|
|
67
|
+
/**
|
|
68
|
+
* Asynchronously warm up schemaRegistry cache.
|
|
69
|
+
* This action is useful to pre-compile all schemas and their definitions
|
|
70
|
+
*/
|
|
71
|
+
warmupCache: (schemasPerIteration?: number, delayMs?: number) => Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Synchronously warm up schemaRegistry cache.
|
|
74
|
+
* This action is useful to pre-compile all schemas and their definitions
|
|
75
|
+
*/
|
|
76
|
+
warmupCacheSync: VoidFunction;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/schema-registry/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAC7E,OAAO,KAAK,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACtD,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,gCAAgC,EAAE,MAAM,QAAQ,CAAC;AAC/D,OAAO,KAAK,EACR,wBAAwB,EACxB,6BAA6B,EAC7B,2BAA2B,EAC3B,2BAA2B,EAC9B,MAAM,UAAU,CAAC;AAElB,MAAM,MAAM,2CAA2C,GAAG,CACtD,IAAI,EAAE,oBAAoB,EAC1B,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,KACpB,OAAO,CAAC;AAEb,MAAM,WAAW,mBAAmB;IAChC;;OAEG;IACH,QAAQ,CAAC,wBAAwB,EAAE,cAAc,CAAC;IAElD;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,cAAc,CAAC;IAE7B;;OAEG;IACH,KAAK,EAAE,CACH,OAAO,CAAC,EAAE,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,EAClC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,iBAAiB,KAAK,iBAAiB,KAC1D,mBAAmB,CAAC;IAEzB;;OAEG;IACH,KAAK,EAAE,YAAY,CAAC;IAEpB;;OAEG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,iBAAiB,EAAE,KAAK,IAAI,CAAC;IAEjD;;OAEG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,wBAAwB,KAAK,OAAO,CAAC;IAEtD;;OAEG;IACH,YAAY,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC,wBAAwB,CAAC,KAAK,IAAI,CAAC;IAEhE;;OAEG;IACH,SAAS,EAAE,CAAC,GAAG,EAAE,wBAAwB,KAAK,WAAW,GAAG,SAAS,CAAC;IAEtE;;OAEG;IACH,YAAY,EAAE,CAAC,KAAK,GAAG,OAAO,EAC1B,GAAG,EAAE,wBAAwB,GAAG,WAAW,KAC1C,6BAA6B,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAEtD;;OAEG;IACH,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,WAAW,GAAG,SAAS,CAAC;IAE7D;;OAEG;IACH,gBAAgB,EAAE,CACd,GAAG,EAAE,wBAAwB,EAC7B,IAAI,EAAE,OAAO,EACb,YAAY,CAAC,EAAE,MAAM,KACpB,2BAA2B,CAAC;IAEjC;;OAEG;IACH,aAAa,EAAE,CACX,CAAC,GAAG,OAAO,EACX,GAAG,SAAS,gCAAgC,GAAG,gCAAgC,EAE/E,GAAG,EAAE,wBAAwB,EAC7B,IAAI,EAAE,CAAC,EACP,OAAO,CAAC,EAAE,GAAG,KACZ,IAAI,IAAI,CAAC,CAAC;IAEf;;OAEG;IACH,oBAAoB,EAAE,CAClB,MAAM,EAAE,QAAQ,CAAC,2BAA2B,CAAC,EAC7C,OAAO,CAAC,EAAE,iBAAiB,KAC1B,MAAM,CAAC;IAEZ;;OAEG;IACH,eAAe,EAAE,CACb,SAAS,CAAC,EAAE,2CAA2C,KACtD,oBAAoB,EAAE,CAAC;IAE5B;;;OAGG;IACH,WAAW,EAAE,CAAC,mBAAmB,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAE/E;;;OAGG;IACH,eAAe,EAAE,YAAY,CAAC;CACjC"}
|