-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathindex.ts
91 lines (78 loc) · 3.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type { Parser } from '../parser';
import type { AsyncAPISchema, DetailedAsyncAPI, SchemaValidateResult } from '../types';
export interface ValidateSchemaInput<D = unknown, M = unknown> {
readonly asyncapi: DetailedAsyncAPI;
readonly data: D;
readonly meta: M;
readonly path: Array<string | number>;
readonly schemaFormat: string;
readonly defaultSchemaFormat: string;
}
export interface ParseSchemaInput<D = unknown, M = unknown> {
readonly asyncapi: DetailedAsyncAPI;
readonly data: D;
readonly meta: M;
readonly path: Array<string | number>;
readonly schemaFormat: string;
readonly defaultSchemaFormat: string;
}
export interface SchemaParser<D = unknown, M = unknown> {
validate: (input: ValidateSchemaInput<D, M>) => void | SchemaValidateResult[] | Promise<void | SchemaValidateResult[]>;
parse: (input: ParseSchemaInput<D, M>) => AsyncAPISchema | Promise<AsyncAPISchema>;
getMimeTypes: () => Array<string>;
}
export async function validateSchema(parser: Parser, input: ValidateSchemaInput) {
const schemaParser = parser.parserRegistry.get(input.schemaFormat);
if (schemaParser === undefined) {
const { path, schemaFormat } = input;
path.pop(); // remove 'payload' as last element of path
return [
{
message: `Unknown schema format: "${schemaFormat}"`,
path: [...path, 'schemaFormat'],
},
{
message: `Cannot validate and parse given schema due to unknown schema format: "${schemaFormat}"`,
path: [...path, 'payload'],
}
] as SchemaValidateResult[];
}
return schemaParser.validate(input);
}
export async function parseSchema(parser: Parser, input: ParseSchemaInput) {
// First validate that format is a string
if (typeof input.schemaFormat !== 'string') {
throw new Error('Schema format must be a string');
}
const schemaParser = parser.parserRegistry.get(input.schemaFormat);
if (schemaParser === undefined) {
// Simply return the schema as-is for unknown formats
return {
parsed: input.data, // Assuming you meant to return input.data instead of input.schema
format: input.schemaFormat
};
}
return schemaParser.parse(input);
}
export function registerSchemaParser(parser: Parser, schemaParser: SchemaParser) {
if (
typeof schemaParser !== 'object'
|| typeof schemaParser.validate !== 'function'
|| typeof schemaParser.parse !== 'function'
|| typeof schemaParser.getMimeTypes !== 'function'
) {
throw new Error('Custom parser must have "parse()", "validate()" and "getMimeTypes()" functions.');
}
schemaParser.getMimeTypes().forEach(schemaFormat => {
parser.parserRegistry.set(schemaFormat, schemaParser);
});
}
export function getSchemaFormat(schematFormat: string | undefined, asyncapiVersion: string) {
if (typeof schematFormat === 'string') {
return schematFormat;
}
return getDefaultSchemaFormat(asyncapiVersion);
}
export function getDefaultSchemaFormat(asyncapiVersion: string) {
return `application/vnd.aai.asyncapi;version=${asyncapiVersion}`;
}