-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbowtie_ata.js
More file actions
123 lines (107 loc) · 3.38 KB
/
Copy pathbowtie_ata.js
File metadata and controls
123 lines (107 loc) · 3.38 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
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
const readline = require("readline");
const os = require("os");
const process = require("process");
const { Validator } = require("ata-validator");
const ata_version = require("ata-validator/package.json").version;
const stdio = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
function send(data) {
console.log(JSON.stringify(data));
}
var started = false;
const DEFAULT_DIALECT = "https://json-schema.org/draft/2020-12/schema";
var dialect = DEFAULT_DIALECT;
// ata selects its draft from the schema's own $schema keyword, while Bowtie
// announces the dialect out of band. Stamp the announced dialect onto schemas
// that do not carry one, so draft-07 cases are validated as draft-07 rather
// than falling back to 2020-12 semantics.
function withDialect(schema) {
if (typeof schema !== "object" || schema === null || Array.isArray(schema)) {
return schema;
}
return "$schema" in schema ? schema : { ...schema, $schema: dialect };
}
const cmds = {
start: (args) => {
console.assert(args.version === 1, { args });
started = true;
return {
version: 1,
implementation: {
language: "javascript",
name: "ata-validator",
version: ata_version,
homepage: "https://github.com/ata-core/ata-validator",
documentation: "https://github.com/ata-core/ata-validator",
issues: "https://github.com/ata-core/ata-validator/issues",
source: "https://github.com/ata-core/ata-validator",
dialects: [
"https://json-schema.org/draft/2020-12/schema",
"http://json-schema.org/draft-07/schema#",
],
os: os.platform(),
os_version: os.release(),
language_version: process.version,
},
};
},
dialect: (args) => {
console.assert(started, "Not started!");
dialect = args.dialect;
return { ok: true };
},
run: (args) => {
console.assert(started, "Not started!");
const testCase = args.case;
try {
const schemas = [];
for (const id in testCase.registry) {
schemas.push(withDialect({ ...testCase.registry[id], $id: id }));
}
const v = new Validator(withDialect(testCase.schema), {
schemas: schemas.length > 0 ? schemas : undefined,
// ata asserts formats and applies defaults by default, which suits the
// web frameworks it targets. The suite expects the specification
// reading: format is an annotation, and default never affects whether
// an instance is valid.
assertFormat: false,
useDefaults: false,
});
const results = testCase.tests.map((test) => {
try {
return { valid: v.validate(test.instance).valid };
} catch (error) {
return {
errored: true,
context: {
traceback: error.stack,
message: error.message,
},
};
}
});
return { seq: args.seq, results: results };
} catch (error) {
return {
errored: true,
seq: args.seq,
context: {
traceback: error.stack,
message: error.message,
},
};
}
},
stop: (_) => {
console.assert(started, "Not started!");
process.exit(0);
},
};
stdio.on("line", (line) => {
const request = JSON.parse(line);
const response = cmds[request.cmd](request);
send(response);
});