From 12292c429d5386f15dfb32260afc9046806d218e Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 13 Nov 2024 18:03:05 +0100 Subject: [PATCH] many changes Signed-off-by: Matteo Collina --- .github/workflows/nodejs-ci.yml | 5 +- .github/workflows/npm-publish.yml | 20 --- .github/workflows/update-license-year.sh | 14 -- .github/workflows/update-license-year.yml | 13 -- CHANGELOG.md | 190 ---------------------- README.md | 46 +----- UPGRADING.md | 32 ---- bin/bundle-api-cli.js | 82 ---------- bin/validate-api-cli.js | 21 --- index.js | 2 + package.json | 14 +- test/test-bundle-cli.js | 61 ------- test/test-validate-cli.js | 24 --- 13 files changed, 13 insertions(+), 511 deletions(-) delete mode 100644 .github/workflows/npm-publish.yml delete mode 100644 .github/workflows/update-license-year.sh delete mode 100644 .github/workflows/update-license-year.yml delete mode 100644 CHANGELOG.md delete mode 100644 UPGRADING.md delete mode 100755 bin/bundle-api-cli.js delete mode 100755 bin/validate-api-cli.js delete mode 100644 test/test-bundle-cli.js delete mode 100644 test/test-validate-cli.js diff --git a/.github/workflows/nodejs-ci.yml b/.github/workflows/nodejs-ci.yml index 7d789b1..d0d15ce 100644 --- a/.github/workflows/nodejs-ci.yml +++ b/.github/workflows/nodejs-ci.yml @@ -10,10 +10,11 @@ permissions: {} jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: matrix: - node-version: [ 'lts/-1', 'lts/*', 'node'] + node-version: [ '20.x', '22.x' ] + os: ['ubuntu-latest', 'windows-latest'] steps: - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml deleted file mode 100644 index 2707b38..0000000 --- a/.github/workflows/npm-publish.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: Publish Package to npmjs -on: - release: - types: [created] -jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: read - id-token: write - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: 'lts/*' - registry-url: 'https://registry.npmjs.org' - - run: npm ci - - run: npm publish --provenance --access public - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/update-license-year.sh b/.github/workflows/update-license-year.sh deleted file mode 100644 index ac20777..0000000 --- a/.github/workflows/update-license-year.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -license='LICENSE.txt' -prefix='2016-' -current_year=$(date +%Y) -sed -i -e "s/$prefix\([0-9\]\+\)/$prefix$current_year/" $license - -if [ "$(git diff $license)" ]; then - git add $license - git commit -m "Update license year to $current_year" - git push -else - echo "No changes detected in $license" -fi \ No newline at end of file diff --git a/.github/workflows/update-license-year.yml b/.github/workflows/update-license-year.yml deleted file mode 100644 index 65837c1..0000000 --- a/.github/workflows/update-license-year.yml +++ /dev/null @@ -1,13 +0,0 @@ -name: Update License Year - -on: - schedule: - - cron: '5 12 1 1 *' - -jobs: - update_license: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Update license year - run: ./update_license.sh \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index ee9fd26..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,190 +0,0 @@ -# Changelog - -## [Unreleased] -### Changed - -## [v2.2.4] 12-11-2024 -### Changed - - fix: removed dependency on "node:url" to aid in bundling - - updated dependencies - - @biomejs/biome ^1.8.3 → ^1.9.4 - -## [v2.2.3] 31-08-2024 -### Changed - - fix: fixed biome formatting of package.json - -## [v2.2.2] 31-08-2024 -### Changed - - updated dependencies - - @biomejs/biome ^1.5.3 → ^1.8.3 - - ajv ^8.12.0 → ^8.17.1 - - c8 ^10.1.1 → ^10.1.2 - -## [v2.2.1] 17-02-2024 -### Changed - - fix: added validateBundle() to index.d.ts - -## [v2.2.0] 17-02-2024 -### Changed - - feature: added validationBundle functionality including bundle-api cli - - updated dependencies - - @biomejs/biome ^1.5.2 → ^1.5.3 - -## [v2.1.6] 20-01-2024 -### Changed - - Updated dependencies - - @biomejs/biome ^1.3.3 → ^1.5.2 - - c8 ^9.0.0 → ^9.1.0 - -## [v2.1.5] 16-11-2023 -### Changed - - fix: validation after resolveRefs (tansin) - -## [v2.1.4] 13-11-2023 -### Changed - - fix: recursive refs work during validation as well - - fix: update index.d.ts to include single string errors on validate - -## [v2.1.3] 12-11-2023 -### Changed -- fix: ref's are now checked during validation as well -- Updated dependencies - - @biomejs/biome ^1.2.2 → ^1.3.3 - -## [v2.1.2] 27-09-2023 -### Changed - - fix: added addSpecRef() to index.d.ts - - Updated dependencies - - @biomejs/biome ^1.1.2 → ^1.2.2 - -## [v2.1.1] 13-09-2023 -### Changed - - chore: added Biome for formatting and linting - - chore: removed tap - - chore: removed node-fetch - - Updated dependencies - - ajv ^8.11.0 → ^8.12.0 - - c8 ^7.12.0 → ^8.0.1 - -## [v2.1.0] 14-10-2022 -### Changed - - Update OAS v3.1 to the updated 2022-10-07 schema - -## [v2.0.3] 28-09-2022 -### Changed - - Fix ref handing in resolveRefs - - Updated dependencies - - c8 ^7.11.2 → ^7.12.0 - - node-fetch ^3.2.3 → ^3.2.10 - - tap ^16.0.1 → ^16.3.0 - -## [v2.0.2] 12-05-2022 -### Changed - - Fix file handling on Windows - -## [v2.0.1] 26-04-2022 -### Changed - - Fix CLI issue and doc issue (@tbcarver) - -## [v2.0.0] 25-04-2022 -### Changed - - Removed Node 10/12 support from code - - Migrated to ESM, see UPGRADING.md for details - -## [v1.7.1] 22-04-2022 -### Changed - - Updated dependencies - - ajv ^8.6.3 → ^8.11.0 - - node-fetch ^3.0.0 → ^3.2.3 - - tap ^16.0.0 → ^16.0.1 - -## [v1.7.0] 11-03-2022 -### Changed - - Updated OAS v3.0 to the updated 2021-09-28 schema (only descriptions changed) - - Updated OAS v3.1 to 2022-02-27 schema - -## [v1.6.1] 22-2-2022 -### Changed - - Fixed error in addSpecRef example in the README - -## [v1.6.0] 09-10-2021 -### Changed - - New feature, now supports external refs, see addSpecRef in the README. - -## [v1.5.1] 08-10-2021 -### Changed - - Fixed error message on URI ref without a path - -## [v1.5.0] 08-10-2021 -### Changed - - Updated OAS v3.0 to 2021-09-28 schema - - Updated OAS v3.1 to 2021-09-28 schema - -## [v1.4.0] 25-09-2021 -### Changed - - Updated OAS v3.1 schema because of spec update - (https://github.com/OAI/OpenAPI-Specification/issues/2706) - - Added URI decoding to refs - - Updated dependencies - - node-fetch ^2.6.1 → ^3.0.0 - - ajv ^8.6.1 → ^8.6.3 - - ajv-formats ^2.1.0 → ^2.1.1 - - tap ^15.0.9 → ^15.0.10 - -## [v1.3.0] 07-07-2021 -### Changed - - Added validation of formats - - Updated dependencies - - ajv ^8.6.0 → ^8.6.1 - -## [v1.2.0] 27-06-2021 -### Changed - - Cache AJV validators so that multiple validation calls are faster - - Added CLI tool - -## [v1.1.6] 26-06-2021 -### Changed - - Switched to original draft-04 schemas instead of converted draft-07 schemas now that AJV supports draft-04 - -## [v1.1.5] 25-06-2021 -### Changed - - fixed YAML parsing of date strings (e.g: `version: 2021-06-25`) - - Updated dependencies - - ajv ^8.3.0 → ^8.6.0 - -## [v1.1.4] 18-06-2021 -### Changed - - force AJV options strict:false and validateFormats:false even if user supplies empty ajvOptions - -## [v1.1.3] 28-05-2021 -### Changed -- Support schema 3.1 2021-05-20 - -## [v1.1.2] 14-05-2021 -### Changed - - Added Node 10 support - -## [v1.1.1] 14-05-2021 -### Changed - - Added CommonJS example to README.md - -## [v1.1.0] 14-05-2021 -### Changed - - Converted from ESM to CommonJS to improve compatibility - -## [v1.0.3] 10-05-2021 -### Changed -- Fixed bug in v2.0 schema that disallowed multiple parameters - -## [v1.0.2] 10-05-2021 -### Changed -- Updated example in README.md - -## [v1.0.1] 09-05-2021 -### Changed -- Updated dependencies -- Reworked README -- Applied LGTM suggestions - -## [v1.0.0] 09-05-2021 -Initial version diff --git a/README.md b/README.md index 9114aff..618457a 100644 --- a/README.md +++ b/README.md @@ -49,48 +49,6 @@ This module can be used in CommonJS code via: const { Validator } = await (import("@platformatic/openapi-schema-validator")); ``` -See also the [upgrading guide](UPGRADING.md) if you come from a previous major version. - - - -### CLI for API validation - -Run with global install: - -```bash -npm install @platformatic/openapi-schema-validator -g -validate-api -``` - -Run without install: - -```bash -npx -p @platformatic/openapi-schema-validator validate-api -``` - -Where `` refers to a YAML or JSON file containing the specification. - -### CLI for API bundling - -To make it easier to combine multiple YAML or JSON files into a single specification file there is the `bundle-api` command. -(see the [validateBundle](#validateBundle) section below) - -```bash -npm install @platformatic/openapi-schema-validator -g -bundle-api -``` - -Run without install: - -```bash -npx -p @platformatic/openapi-schema-validator bundle-api -``` - -The output will be a validated JSON specification. -Options: - -o --output the filename to save the output to, default is STDOUT. - -t --type [JSON|YAML] the output format, default is JSON. - ## API @@ -275,7 +233,9 @@ This static property returns the OpenApi versions supported by this package as a # Credits -This module was forked at [commit](https://github.com/seriousme/openapi-schema-validator/commit/e4f417ade9e675225381fb872e1b78a9fa392472). +This module was forked from +[https://github.com/seriousme/openapi-schema-validator](https://github.com/seriousme/openapi-schema-validator) +at [commit](https://github.com/seriousme/openapi-schema-validator/commit/e4f417ade9e675225381fb872e1b78a9fa392472). We thank Hans Klunder for his original work on this module. # License diff --git a/UPGRADING.md b/UPGRADING.md deleted file mode 100644 index b19fae1..0000000 --- a/UPGRADING.md +++ /dev/null @@ -1,32 +0,0 @@ -# Upgrade Guide - -This document describes breaking changes and how to upgrade. For a complete list -of changes including minor and patch releases, please refer to the -[changelog](CHANGELOG.md). - - - -## 2.0.0 - -### Changes to initialization - -This version is an ES6 Module and does not do a `default` anymore, it just -exports the `Validator` class - -So where you previously had: - -```javascript -// ESM -import { Validator } from "@seriousme/openapi-schema-validator"; -// CJS -const Validator = require("@seriousme/openapi-schema-validator"); -``` - -You now need to do: - -```javascript -// ESM -import { Validator } from "@seriousme/openapi-schema-validator"; -// CJS -const { Validator } = await (import("@seriousme/openapi-schema-validator")); -``` diff --git a/bin/bundle-api-cli.js b/bin/bundle-api-cli.js deleted file mode 100755 index 482ccd9..0000000 --- a/bin/bundle-api-cli.js +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env node - -import { writeFileSync } from "node:fs"; -import { basename } from "node:path"; -import { argv, exit } from "node:process"; -import { dump } from "js-yaml"; -import argvParser from "minimist"; -import { Validator } from "../index.js"; - -const cmd = basename(argv[1]); -const validator = new Validator(); -const validTypes = new Set(["JSON", "YAML"]); - -function formatOutput(type, data) { - if (type === "YAML") { - return dump(data); - } - return JSON.stringify(data, null, 2); -} - -function writeOutput(filename, data) { - if (filename) { - writeFileSync(filename, data); - return; - } - console.log(data); -} - -function usage() { - console.log(` -Usage: - ${cmd} [-t ] [-o ] - -Options: - -o --output the filename to save the output to, default is STDOUT. - -t --type [JSON|YAML] the output format, default is JSON. - -There can only be one main specification present (starting with swagger/openapi) -but it does not have to be the first one. If the files do not have '$id' attributes, -then the '$id' attribute will be generated from the filename. - -example: - ${cmd} subSpec.yaml mainSpec.yaml -t YAML -o bundle.yaml - -The output will be a validated JSON specification. -`); - exit(1); -} - -const argvOptions = { - string: ["output", "type", "_"], - alias: { - output: "o", - type: "t", - }, - - default: { - output: false, - type: "JSON", - }, -}; - -const args = argvParser(argv.slice(2), argvOptions); -const type = args.type.toUpperCase(); -if (!validTypes.has(type)) { - console.log(`Unknown type: ${args.type}`); - usage(); -} - -if (args._.length === 0) { - usage(); -} - -const result = await validator.validateBundle(args._); - -if (result.valid) { - writeOutput(args.output, formatOutput(type, validator.specification)); - exit(0); -} - -console.log(formatOutput(type, result)); -exit(1); diff --git a/bin/validate-api-cli.js b/bin/validate-api-cli.js deleted file mode 100755 index 310ad9b..0000000 --- a/bin/validate-api-cli.js +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env node - -import { Validator } from "../index.js"; -const validator = new Validator(); -import { argv, exit } from "node:process"; - -const filename = argv[2]; -if (!filename) { - console.log(` - usage: ${argv[1]} - where contains a yaml or json openApi specification. - `); - exit(1); -} - -validator.validate(filename).then((result) => { - console.log(JSON.stringify(result, null, "\t")); - if (!result.valid) { - exit(1); - } -}); diff --git a/index.js b/index.js index c7968ca..7225ef1 100644 --- a/index.js +++ b/index.js @@ -157,6 +157,7 @@ export class Validator { ); } const { version } = getOpenApiVersion(spec); + /* c8 ignore next 5 */ if (!version) { // it is not the main openApi specification, but a subschema this.addSpecRef(spec, spec.$id || fileName); @@ -169,6 +170,7 @@ export class Validator { } specification = spec; } + /* c8 ignore next 1 */ return this.validate(specification); } diff --git a/package.json b/package.json index a203e55..2e6d719 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,9 @@ { - "name": "@seriousme/openapi-schema-validator", + "name": "@platformatic/openapi-schema-validator", "version": "2.2.4", "description": "Validate OpenApi specifications against their JSON schema", "main": "index.js", "type": "module", - "bin": { - "validate-api": "./bin/validate-api-cli.js", - "bundle-api": "./bin/bundle-api-cli.js" - }, "dependencies": { "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", @@ -35,7 +31,7 @@ "realWorldTestFailed": "node test/realworld/realworld.js --failedOnly", "realWorldTestCI": "node test/realworld/realworld.js --ci" }, - "author": "Hans Klunder", + "author": "Platformatic Inc. (https://platformatic.dev)", "license": "MIT", "devDependencies": { "@biomejs/biome": "^1.9.4", @@ -46,7 +42,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/seriousme/openapi-schema-validator.git" + "url": "git+https://github.com/platformatic/openapi-schema-validator.git" }, "keywords": [ "openapi", @@ -55,7 +51,7 @@ "validation" ], "bugs": { - "url": "https://github.com/seriousme/openapi-schema-validator/issues" + "url": "https://github.com/platformatic/openapi-schema-validator/issues" }, - "homepage": "https://github.com/seriousme/openapi-schema-validator#readme" + "homepage": "https://github.com/platformatic/openapi-schema-validator#readme" } diff --git a/test/test-bundle-cli.js b/test/test-bundle-cli.js deleted file mode 100644 index b206641..0000000 --- a/test/test-bundle-cli.js +++ /dev/null @@ -1,61 +0,0 @@ -import { strict as assert } from "node:assert/strict"; -import { execSync } from "node:child_process"; -import { readFileSync, unlinkSync } from "node:fs"; -import { test } from "node:test"; -import { load } from "js-yaml"; - -function localFile(file) { - return new URL(file, import.meta.url).pathname; -} - -function importJSON(file) { - return JSON.parse(readFileSync(localFile(file))); -} - -const cli = localFile("../bin/bundle-api-cli.js"); -const tmpBundle = localFile("./bundle/bundle.tmp.yaml"); - -test("cli does not error", (t) => { - const yamlFileName = localFile("./validation/petstore-openapi.v3.yaml"); - const result = assert.doesNotThrow( - () => JSON.parse(execSync(`node ${cli} ${yamlFileName}`)), - "cli bundle of petstore spec works", - ); -}); - -test("cli fails on empty spec", (t) => { - const yamlFileName = localFile("./validation/empty.json"); - assert.throws(() => execSync(`node ${cli} ${yamlFileName}`)); -}); - -test("cli fails on no spec", (t) => { - assert.throws(() => execSync(`node ${cli}`)); -}); - -test("cli fails on unknown type", (t) => { - assert.throws(() => execSync(`node ${cli} -t yason`)); -}); - -test("cli bundles subspecs as JSON to console", (t) => { - const main = localFile("./bundle/main-spec.v3.yaml"); - const subspec = localFile("./bundle/sub-spec-withID.v3.yaml"); - const subspec2 = localFile("./bundle/sub-spec2.v3.yaml"); - const bundle = importJSON("./bundle/bundle.v3.json"); - const result = JSON.parse( - execSync(`node ${cli} ${main} ${subspec} ${subspec2}`), - ); - assert.deepEqual(result, bundle); -}); - -test("cli bundles subspecs as YAML to file", (t) => { - const main = localFile("./bundle/main-spec.v3.yaml"); - const subspec = localFile("./bundle/sub-spec-withID.v3.yaml"); - const subspec2 = localFile("./bundle/sub-spec2.v3.yaml"); - const bundle = importJSON("./bundle/bundle.v3.json"); - execSync( - `node ${cli} -t yaml -o ${tmpBundle} ${main} ${subspec} ${subspec2}`, - ); - const result = load(readFileSync(tmpBundle)); - unlinkSync(tmpBundle); - assert.deepEqual(result, bundle); -}); diff --git a/test/test-validate-cli.js b/test/test-validate-cli.js deleted file mode 100644 index 5389d87..0000000 --- a/test/test-validate-cli.js +++ /dev/null @@ -1,24 +0,0 @@ -import { strict as assert } from "node:assert/strict"; -import { execSync } from "node:child_process"; -import { test } from "node:test"; - -function localFile(file) { - return new URL(file, import.meta.url).pathname; -} - -const cli = localFile("../bin/validate-api-cli.js"); - -test("cli does not error", (t) => { - const yamlFileName = localFile("./validation/petstore-openapi.v3.yaml"); - const result = JSON.parse(execSync(`node ${cli} ${yamlFileName}`)); - assert.equal(result.valid, true, "cli validation of petstore spec works"); -}); - -test("cli fails on empty spec", (t) => { - const yamlFileName = localFile("./validation/empty.json"); - assert.throws(() => execSync(`node ${cli} ${yamlFileName}`)); -}); - -test("cli fails on no spec", (t) => { - assert.throws(() => execSync(`node ${cli}`)); -});