-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbowtie_jlib.js
140 lines (124 loc) · 4.04 KB
/
bowtie_jlib.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* test file for bowtie integration
*
* - Tutorial: https://docs.bowtie.report/en/stable/implementers/
*
* Build Dockerfile:
*
* ```
* docker build -t localhost/jlib .
* ````
*
* Test setup:
*
* ```
bowtie run -i localhost/jlib -V <<EOF
{"description": "test case 1", "schema": {}, "tests": [{"description": "a test", "instance": {}}] }
{"description": "test case 2", "schema": {"const": 37}, "tests": [{"description": "not 37", "instance": {}}, {"description": "is 37", "instance": 37}] }
EOF
*
* Test draft
*
* ```
* bowtie suite -i localhost/jlib draft7 | bowtie summary --show failures
* ```
*
*/
const readline = require("readline/promises");
const process = require("process");
const os = require("os");
const packageJson = require("./package.json");
const jlib = require("./dist/jsonSchemaLibrary.js");
const compileSchema = jlib.compileSchema;
const stdio = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
// const schemaIds: { [id: string]: SchemaDraft } = {
// "https://json-schema.org/draft/2020-12/schema": SchemaDraft.v2020_12,
// "https://json-schema.org/draft/2019-09/schema": SchemaDraft.v2019_09,
// "http://json-schema.org/draft-07/schema#": SchemaDraft.v7,
// "http://json-schema.org/draft-06/schema#": SchemaDraft.v6,
// "http://json-schema.org/draft-04/schema#": SchemaDraft.v4
// };
function send(data) {
console.log(JSON.stringify(data));
}
let started = false;
// let dialect;
const cmds = {
start: async (args) => {
console.assert(args.version === 1, { args });
started = true;
return {
version: 1,
implementation: {
language: "javascript",
name: "json-schema-library",
version: packageJson.version,
homepage: "https://github.com/sagold/json-schema-library",
issues: "https://github.com/sagold/json-schema-library/issues",
source: "https://github.com/sagold/json-schema-library",
dialects: [
"https://json-schema.org/draft/2020-12/schema",
"https://json-schema.org/draft/2019-09/schema",
"http://json-schema.org/draft-07/schema#",
"http://json-schema.org/draft-06/schema#",
"http://json-schema.org/draft-04/schema#"
],
os: os.platform(),
os_version: os.release(),
language_version: process.version
}
};
},
dialect: async (args) => {
console.assert(started, "Not started!");
// dialect = schemaIds[args.dialect];
return { ok: true };
},
run: async (args) => {
console.assert(started, "Not started!");
// for (const id in testCase.registry) {
// ajv.addSchema(testCase.registry[id], id);
// }
const testCase = args.case;
// as {
// registry: Recors<sring, string>;
// schema: Record<string, any>;
// tests: {
// /** testdata */
// instance: unknown;
// }[];
// };
const node = compileSchema(testCase.schema);
const results = testCase.tests.map((test) => {
try {
const errors = node.validate(test.instance);
return { valid: errors.length === 0 };
} catch (error) {
return {
errored: true,
context: {
traceback: error.stack,
message: error.message
}
};
}
});
return { seq: args.seq, results: results };
},
stop: async (_) => {
console.assert(started, "Not started!");
process.exit(0);
}
};
async function main() {
for await (const line of stdio) {
const request = JSON.parse(line);
const response = await cmds[request.cmd](request);
send(response);
}
}
main();