Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions scripts/validate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

import { readFile } from "node:fs/promises";
import YAML from "yaml";
import { setMetaSchemaOutputFormat, validate } from "@hyperjump/json-schema/draft-2020-12";
import { BASIC } from "@hyperjump/json-schema/experimental";

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 defaultOutputFormat = BASIC;

if (process.argv.length < 3) {
console.log(`Usage: validate [--schema=schema] [--format=${defaultOutputFormat}] path-to-file.yaml`);
console.log("\t--schema: (schema (default) | schema-base) The name of the schema file to use");
console.log(`\t--format: (Default: ${defaultOutputFormat}) The JSON Schema output format to use. Options: FLAG, BASIC, DETAILED, VERBOSE`);
process.exit(1);
}

const args = process.argv.reduce((acc, arg) => {
if (!arg.startsWith("--")) return acc;

const [argName, argValue] = arg.substring(2).split("=", 2);
return { ...acc, [argName]: argValue };
}, {});

const schemaType = args.schema || "schema";
const outputFormat = args.format || defaultOutputFormat;

// Config
setMetaSchemaOutputFormat(outputFormat);

// Compile / meta-validate
const validateOpenApi = await validate(`./schemas/v1.0/${schemaType}.yaml`);

// Validate instance
const instanceYaml = await readFile(`${process.argv[process.argv.length - 1]}`, "utf8");
const instance = YAML.parse(instanceYaml);
const results = validateOpenApi(instance, outputFormat);
console.log(JSON.stringify(results, null, " "));
9 changes: 9 additions & 0 deletions tests/v1.0/pass/array-modification-example-remove.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
overlay: 1.0.0
info:
title: Add an array element
version: 1.0.0
actions:
- target: $.paths.*.get.parameters
update:
name: newParam
in: query
7 changes: 7 additions & 0 deletions tests/v1.0/pass/array-modification-example-update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
overlay: 1.0.0
info:
title: Remove a array element
version: 1.0.0
actions:
- target: $.paths.*.get.parameters[[email protected] == 'dummy']
remove: true
21 changes: 21 additions & 0 deletions tests/v1.0/pass/structured-overlay-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
overlay: 1.0.0
info:
title: Structured Overlay
version: 1.0.0
actions:
- target: '$' # Root of document
update:
info:
x-overlay-applied: structured-overlay
paths:
'/':
summary: 'The root resource'
get:
summary: 'Retrieve the root resource'
x-rate-limit: 100
'/pets':
get:
summary: 'Retrieve a list of pets'
x-rate-limit: 100
components:
tags:
16 changes: 16 additions & 0 deletions tests/v1.0/pass/targeted-overlay-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
overlay: 1.0.0
info:
title: Targeted Overlay
version: 1.0.0
actions:
- target: $.paths['/foo'].get
update:
description: This is the new description
- target: $.paths['/bar'].get
update:
description: This is the updated description
- target: $.paths['/bar']
update:
post:
description: This is an updated description of a child object
x-safe: false
14 changes: 14 additions & 0 deletions tests/v1.0/pass/traits-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
overlay: 1.0.0
info:
title: Apply Traits
version: 1.0.0
actions:
- target: $.paths.*.get[[email protected]]
update:
parameters:
- name: top
in: query
# ...
- name: skip
in: query
# ...
12 changes: 12 additions & 0 deletions tests/v1.0/pass/wildcard-overlay-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
overlay: 1.0.0
info:
title: Update many objects at once
version: 1.0.0
actions:
- target: $.paths.*.get
update:
x-safe: true
- target: $.paths.*.get.parameters[[email protected]=='filter' && @.in=='query']
update:
schema:
$ref: '/components/schemas/filterSchema'