|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFile } from "node:fs/promises"; |
| 4 | +import yaml from "yaml"; |
| 5 | +import { setMetaSchemaOutputFormat, validate } from "@hyperjump/json-schema/openapi-3-1"; |
| 6 | +import { BASIC } from "@hyperjump/json-schema/experimental"; |
| 7 | + |
| 8 | + |
| 9 | +const defaultOutputFormat = BASIC; |
| 10 | + |
| 11 | +if (process.argv.length < 3) { |
| 12 | + console.log(`Usage: validate [--schema=schema] [--format=${defaultOutputFormat}] path-to-file.yaml`); |
| 13 | + console.log("\t--schema: (schema (default) | schema-base) The name of the schema file to use"); |
| 14 | + console.log(`\t--format: (Default: ${defaultOutputFormat}) The JSON Schema output format to use. Options: FLAG, BASIC, DETAILED, VERBOSE`); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +const args = process.argv.reduce((acc, arg) => { |
| 19 | + if (!arg.startsWith("--")) return acc; |
| 20 | + |
| 21 | + const [argName, argValue] = arg.substring(2).split("=", 2); |
| 22 | + return { ...acc, [argName]: argValue }; |
| 23 | +}, {}); |
| 24 | + |
| 25 | +const schemaType = args.schema || "schema"; |
| 26 | +const outputFormat = args.format || defaultOutputFormat; |
| 27 | + |
| 28 | +// Config |
| 29 | +setMetaSchemaOutputFormat(outputFormat); |
| 30 | + |
| 31 | +// Compile / meta-validate |
| 32 | +const validateOpenApi = await validate(`./schemas/v3.1/${schemaType}.json`); |
| 33 | + |
| 34 | +// Validate instance |
| 35 | +const instanceYaml = await readFile(`${process.argv[process.argv.length - 1]}`, "utf8"); |
| 36 | +const instance = yaml.parse(instanceYaml); |
| 37 | +const results = validateOpenApi(instance, outputFormat); |
| 38 | +console.log(JSON.stringify(results, null, " ")); |
0 commit comments