|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import fs from "fs"; |
18 |
| -import Ajv from "ajv"; |
| 17 | +import fs from "node:fs"; |
| 18 | +import Ajv from "ajv/dist/2020"; |
19 | 19 | import addFormats from "ajv-formats";
|
20 |
| -import { join } from "path"; |
| 20 | +import path from "node:path"; |
21 | 21 | import yaml = require("js-yaml");
|
22 | 22 |
|
23 | 23 | export module SWSchemaValidator {
|
24 | 24 | const ajv = new Ajv({ strict: false, allowUnionTypes: true });
|
25 | 25 | addFormats(ajv);
|
26 | 26 |
|
27 | 27 | const workflowSchemaId =
|
28 |
| - "https://serverlessworkflow.io/schemas/1.0.0-alpha1/workflow.json"; |
| 28 | + "https://serverlessworkflow.io/schemas/1.0.0-alpha1/workflow.yaml"; |
29 | 29 | const schemaPath = "../../../schema";
|
30 | 30 | export const defaultEncoding = "utf-8";
|
31 | 31 |
|
32 | 32 | export function prepareSchemas() {
|
33 |
| - fs.readdirSync(join(__dirname, schemaPath), { |
| 33 | + const files = fs.readdirSync(path.join(__dirname, schemaPath), { |
34 | 34 | encoding: defaultEncoding,
|
35 | 35 | recursive: false,
|
36 | 36 | withFileTypes: true,
|
37 |
| - }).forEach((file) => { |
38 |
| - if (file.isFile()) { |
39 |
| - ajv.addSchema(syncReadSchema(file.name)); |
40 |
| - } |
41 | 37 | });
|
| 38 | + |
| 39 | + files |
| 40 | + .filter((file) => file.isFile()) |
| 41 | + .forEach((file) => { |
| 42 | + ajv.addSchema(syncReadSchema(file.name)); |
| 43 | + }); |
42 | 44 | }
|
43 | 45 |
|
44 |
| - function syncReadSchema(filename: string) { |
45 |
| - return toJSON(join(__dirname, `${schemaPath}/${filename}`)); |
| 46 | + function syncReadSchema(filename: string): any { |
| 47 | + return loadAsJSON(path.join(__dirname, `${schemaPath}/${filename}`)); |
46 | 48 | }
|
47 | 49 |
|
48 |
| - export function toJSON(filename: string) { |
49 |
| - const yamlObj = yaml.load(fs.readFileSync(filename, defaultEncoding), { |
| 50 | + export function loadAsJSON(filename: string): any { |
| 51 | + return yamlToJSON(fs.readFileSync(filename, defaultEncoding)); |
| 52 | + } |
| 53 | + |
| 54 | + export function yamlToJSON(yamlStr: string): any { |
| 55 | + const yamlObj = yaml.load(yamlStr, { |
50 | 56 | json: true,
|
51 | 57 | });
|
52 |
| - return JSON.parse(JSON.stringify(yamlObj, null, 2)); |
| 58 | + return structuredClone(yamlObj); |
53 | 59 | }
|
54 | 60 |
|
55 |
| - export function validateSchema(workflow: JSON) { |
| 61 | + export function validateSchema(workflow: Record<string, unknown>) { |
56 | 62 | const validate = ajv.getSchema(workflowSchemaId);
|
57 |
| - if (validate != undefined) { |
58 |
| - const isValid = validate(workflow); |
59 |
| - return { |
60 |
| - valid: isValid, |
61 |
| - errors: validate.errors, |
62 |
| - }; |
| 63 | + |
| 64 | + if (!validate) { |
| 65 | + throw new Error(`Failed to validate schema on workflow`); |
63 | 66 | }
|
64 |
| - throw new Error(`Failed to validate schema on workflow`); |
| 67 | + |
| 68 | + const isValid = validate(workflow); |
| 69 | + return { |
| 70 | + valid: isValid, |
| 71 | + errors: validate.errors, |
| 72 | + }; |
65 | 73 | }
|
66 | 74 | }
|
67 | 75 |
|
|
0 commit comments