This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.ts
More file actions
75 lines (67 loc) · 1.82 KB
/
metadata.ts
File metadata and controls
75 lines (67 loc) · 1.82 KB
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
// metadata.ts
// Utility functions to generate valid Catalog metadata
import { Generator } from "./generator.js";
import { Parser } from "./parser.js";
import { Validator } from "./validator.js";
import {
type SchemaName,
type SchemaVersion,
supportedVersions,
supportedVersionsTypeMapping,
validateVersion,
} from "./versions.js";
export { validateVersion, supportedVersions, supportedVersionsTypeMapping };
export type JSONLike = Record<string, unknown>;
export function generateMetadata<T extends SchemaName>(
version: string,
data: JSONLike,
): string {
validateVersion(version);
const generator = new Generator<T>(version);
return generator.generateJSON(data);
}
export function parseMetadata<T extends SchemaName>(
version: string,
json: string,
): unknown {
validateVersion(version);
const parser = new Parser<T>(version);
return parser.parse(json);
}
export function validateMetadata<T extends SchemaName>(
version: string,
data: JSONLike,
): boolean {
validateVersion(version);
const validator = new Validator<T>(version);
return validator.validate(data);
}
export function getMetadataErrors<T extends SchemaName>(
version: string,
data: JSONLike,
): string[] | null {
validateVersion(version);
const validator = new Validator<T>(version);
if (validator.validate(data)) {
return null;
}
const errors = validator.getErrors();
return errors
? errors
.map((e) => `${e.instancePath} ${e.message}`)
.filter((message): message is string => message !== undefined)
: null;
}
export function isValidVersion(version: string): boolean {
try {
validateVersion(version);
return true;
} catch {
return false;
}
}
export function getSupportedVersions<T extends SchemaName>(
schemaName: T,
): SchemaVersion<T>[] {
return supportedVersions[schemaName];
}