forked from OAI/OpenAPI-Specification
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Common test script for v3.1 and vNext
- Loading branch information
Showing
49 changed files
with
67 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { readdirSync, readFileSync } from "node:fs"; | ||
import YAML from "yaml"; | ||
import { validate, setMetaSchemaOutputFormat } from "@hyperjump/json-schema/openapi-3-1"; | ||
import { BASIC } from "@hyperjump/json-schema/experimental"; | ||
import { describe, test, expect } from "vitest"; | ||
|
||
import contentTypeParser from "content-type"; | ||
import { addMediaTypePlugin } from "@hyperjump/browser"; | ||
import { buildSchemaDocument } from "@hyperjump/json-schema/experimental"; | ||
|
||
addMediaTypePlugin("application/schema+yaml", { | ||
parse: async (response) => { | ||
const contentType = contentTypeParser.parse(response.headers.get("content-type") ?? ""); | ||
const contextDialectId = contentType.parameters.schema ?? contentType.parameters.profile; | ||
|
||
const foo = YAML.parse(await response.text()); | ||
return buildSchemaDocument(foo, response.url, contextDialectId); | ||
}, | ||
fileMatcher: (path) => path.endsWith(".yaml") | ||
}); | ||
|
||
const parseYamlFromFile = (filePath) => { | ||
const schemaYaml = readFileSync(filePath, "utf8"); | ||
return YAML.parse(schemaYaml, { prettyErrors: true }); | ||
}; | ||
|
||
setMetaSchemaOutputFormat(BASIC); | ||
|
||
const SCHEMAS = [ | ||
{ schema: "./schemas/v3.1/schema.yaml", tests: "./tests/schemas/v3.1" }, | ||
{ schema: "./src/schemas/validation/schema.yaml", tests: "./tests/schemas/vNext" } | ||
]; | ||
|
||
|
||
for (const s of SCHEMAS) { | ||
const validateOpenApi = await validate(s.schema); | ||
const folder = s.tests; | ||
|
||
describe(folder, () => { | ||
describe("Pass", () => { | ||
readdirSync(`${folder}/pass`, { withFileTypes: true }) | ||
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name)) | ||
.forEach((entry) => { | ||
test(entry.name, () => { | ||
const instance = parseYamlFromFile(`${folder}/pass/${entry.name}`); | ||
const output = validateOpenApi(instance, BASIC); | ||
expect(output).to.deep.equal({ valid: true }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Fail", () => { | ||
readdirSync(`${folder}/fail`, { withFileTypes: true }) | ||
.filter((entry) => entry.isFile() && /\.yaml$/.test(entry.name)) | ||
.forEach((entry) => { | ||
test(entry.name, () => { | ||
const instance = parseYamlFromFile(`${folder}/fail/${entry.name}`); | ||
const output = validateOpenApi(instance, BASIC); | ||
expect(output.valid).to.equal(false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.