Skip to content

Commit 7f19e32

Browse files
committed
validate script
1 parent b4aa48e commit 7f19e32

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

schemas/v3.1/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ The [test suite](../../tests/v3.1) is part of this package.
3333
npm install
3434
npm test
3535
```
36+
37+
You can also validate a document individually.
38+
39+
```bash
40+
node scripts/validate.mjs path/to/document/to/validate.yaml
41+
```

scripts/validate.mjs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
#!/usr/bin/env node
22

33
import { readFile } from "node:fs/promises";
4-
import yaml from "yaml";
4+
import YAML from "yaml";
55
import { setMetaSchemaOutputFormat, validate } from "@hyperjump/json-schema/openapi-3-1";
66
import { BASIC } from "@hyperjump/json-schema/experimental";
77

8+
import contentTypeParser from "content-type";
9+
import { addMediaTypePlugin } from "@hyperjump/browser";
10+
import { buildSchemaDocument } from "@hyperjump/json-schema/experimental";
11+
12+
addMediaTypePlugin("application/schema+yaml", {
13+
parse: async (response) => {
14+
const contentType = contentTypeParser.parse(response.headers.get("content-type") ?? "");
15+
const contextDialectId = contentType.parameters.schema ?? contentType.parameters.profile;
16+
17+
const foo = YAML.parse(await response.text());
18+
return buildSchemaDocument(foo, response.url, contextDialectId);
19+
},
20+
fileMatcher: (path) => path.endsWith(".yaml")
21+
});
822

923
const defaultOutputFormat = BASIC;
1024

@@ -29,10 +43,10 @@ const outputFormat = args.format || defaultOutputFormat;
2943
setMetaSchemaOutputFormat(outputFormat);
3044

3145
// Compile / meta-validate
32-
const validateOpenApi = await validate(`./schemas/v3.1/${schemaType}.json`);
46+
const validateOpenApi = await validate(`./schemas/v3.1/${schemaType}.yaml`);
3347

3448
// Validate instance
3549
const instanceYaml = await readFile(`${process.argv[process.argv.length - 1]}`, "utf8");
36-
const instance = yaml.parse(instanceYaml);
50+
const instance = YAML.parse(instanceYaml);
3751
const results = validateOpenApi(instance, outputFormat);
3852
console.log(JSON.stringify(results, null, " "));

0 commit comments

Comments
 (0)