@seriousme/openapi-schema-validator
Advanced tools
Comparing version 2.1.6 to 2.2.0
{ | ||
"linter": { | ||
"enabled": true, | ||
"files": { | ||
"ignore": [ | ||
@@ -10,3 +9,6 @@ ".nyc_output", | ||
"dummy.json" | ||
], | ||
] | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
@@ -16,12 +18,3 @@ "recommended": true, | ||
} | ||
}, | ||
"formatter": { | ||
"ignore": [ | ||
".nyc_output", | ||
"coverage", | ||
"schemas", | ||
"schemas.orig", | ||
"dummy.json" | ||
] | ||
} | ||
} |
@@ -6,2 +6,8 @@ # Changelog | ||
## [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 | ||
@@ -8,0 +14,0 @@ ### Changed |
36
index.js
@@ -140,2 +140,38 @@ import { readFileSync } from "fs"; | ||
async validateBundle(data) { | ||
let specification = undefined; | ||
if (!Array.isArray(data)) { | ||
return { | ||
valid: false, | ||
errors: "Parameter data must be an array", | ||
}; | ||
} | ||
for (const item of data) { | ||
const spec = await getSpecFromData(item); | ||
let fileName = undefined; | ||
if (typeof item === "string" && !item.match(/\n/)) { | ||
// item is a filename | ||
fileName = item; | ||
} | ||
if (spec === undefined) { | ||
throw new Error( | ||
`Cannot find JSON, YAML or filename in ${fileName || "data"}`, | ||
); | ||
} | ||
const { version } = getOpenApiVersion(spec); | ||
if (!version) { | ||
// it is not the main openApi specification, but a subschema | ||
this.addSpecRef(spec, spec.$id || fileName); | ||
continue; | ||
} | ||
if (specification) { | ||
throw new Error( | ||
"Only one openApi specification can be validated at a time", | ||
); | ||
} | ||
specification = spec; | ||
} | ||
return this.validate(specification); | ||
} | ||
getAjvValidator(version) { | ||
@@ -142,0 +178,0 @@ if (!this.ajvValidators[version]) { |
MIT License | ||
Copyright (c) 2016-2023 Hans Klunder | ||
Copyright (c) 2016-2024 Hans Klunder | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "@seriousme/openapi-schema-validator", | ||
"version": "2.1.6", | ||
"version": "2.2.0", | ||
"description": "Validate OpenApi specifications against their JSON schema", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"bin": { | ||
"validate-api": "./bin/validate-api-cli.js" | ||
"validate-api": "./bin/validate-api-cli.js", | ||
"bundle-api": "./bin/bundle-api-cli.js" | ||
}, | ||
@@ -15,3 +16,4 @@ "dependencies": { | ||
"ajv-formats": "^2.1.1", | ||
"js-yaml": "^4.1.0" | ||
"js-yaml": "^4.1.0", | ||
"minimist": "^1.2.8" | ||
}, | ||
@@ -41,3 +43,3 @@ "scripts": { | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.5.2", | ||
"@biomejs/biome": "^1.5.3", | ||
"c8": "^9.1.0" | ||
@@ -44,0 +46,0 @@ }, |
@@ -65,3 +65,3 @@ # OpenAPI schema validator | ||
### CLI | ||
### CLI for API validation | ||
@@ -83,2 +83,23 @@ Run with global install: | ||
### 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 @seriousme/openapi-schema-validator -g | ||
bundle-api <specFiles> | ||
``` | ||
Run without install: | ||
```bash | ||
npx -p @seriousme/openapi-schema-validator bundle-api <spec files> | ||
``` | ||
The output will be a validated JSON specification. | ||
Options: | ||
-o --output <filename> the filename to save the output to, default is STDOUT. | ||
-t --type [JSON|YAML] the output format, default is JSON. | ||
<a name="API"></a> | ||
@@ -167,3 +188,5 @@ | ||
- a YAML string | ||
- a filename `uri` must be a string (e.g. `http://www.example.com/subspec`), but | ||
- a filename | ||
`uri` must be a string (e.g. `http://www.example.com/subspec`), but | ||
is not required if the subSpecification holds a `$id` attribute at top level. | ||
@@ -175,4 +198,4 @@ If you specify a value for `uri` it will overwrite the definition in the `$id` | ||
of the specification. The specification refers to these sub specifications using | ||
`external references`. Since references are based on URI's (so Identifier and | ||
Location as in URL's!) there needs to be a way to tell the validator how to | ||
`external references`. Since references are based on URI's (so `Identifier` and not | ||
`Location` as in URL's!) there needs to be a way to tell the validator how to | ||
resolve those references. This is where this function comes in: | ||
@@ -220,2 +243,3 @@ | ||
```javascript | ||
import { Validator } from "@seriousme/openapi-schema-validator"; | ||
const validator = new Validator(); | ||
@@ -230,2 +254,28 @@ await validator.addSpecRef("./sub-spec.yaml", "http://www.example.com/subspec"); | ||
<a name="validateBundle"></a> | ||
### `<instance>.validateBundle([specification,subspecification, ...])` | ||
This offers an alternative to the combination of `addSpecRef` and `validate`. | ||
You can pass an array of (sub)specifications where each can be one of: | ||
- a JSON object | ||
- a JSON object encoded as string | ||
- a YAML string | ||
- a filename | ||
There can only be one main specification present (starting with swagger/openapi) but it does not have to be the first one. If you provide filenames and the files do not have `$id` attributes, then the `$id` attribute will be generated from the filename. | ||
If we take the YAML specifications from the previous example then validation can be performed as follows: | ||
```javascript | ||
import { Validator } from "@seriousme/openapi-schema-validator"; | ||
const validator = new Validator(); | ||
const res = await validator.validateBundle([ "./sub-spec.yaml", "./main-spec.yaml"]); | ||
// res now contains the results of the validation across main-spec and sub-spec | ||
const specification = validator.specification; | ||
// specification now contains a Javascript object containing the specification | ||
// with the subspec inlined | ||
``` | ||
<a name="supportedVersions"></a> | ||
@@ -232,0 +282,0 @@ |
@@ -165,3 +165,3 @@ import { readFileSync } from "fs"; | ||
test("Invalid yaml specification as string gives an error", async (t) => { | ||
test("invalid yaml specification as string gives an error", async (t) => { | ||
const yamlSpec = ` | ||
@@ -233,3 +233,3 @@ yaml : : : | ||
test("Invalid filename returns an error", async (t) => { | ||
test("invalid filename returns an error", async (t) => { | ||
const validator = new Validator(); | ||
@@ -337,1 +337,34 @@ | ||
}); | ||
test("validateBundle: no spec returns an error", async (t) => { | ||
const validator = new Validator(); | ||
const res = await validator.validateBundle(); | ||
assert.equal(res.valid, false, "validation fails as expected"); | ||
assert.equal( | ||
res.errors, | ||
"Parameter data must be an array", | ||
"error message matches expection", | ||
); | ||
}); | ||
test("validateBundle: unreadable spec returns an error", async (t) => { | ||
const yamlSpec = ` | ||
yaml : : : | ||
yaml : : : | ||
`; | ||
const validator = new Validator(); | ||
assert.rejects( | ||
validator.validateBundle([yamlSpec]), | ||
new Error("Cannot find JSON, YAML or filename in data"), | ||
"error message matches expectation", | ||
); | ||
}); | ||
test("validateBundle: double spec returns an error", async (t) => { | ||
const validator = new Validator(); | ||
assert.rejects( | ||
validator.validateBundle([yamlFileName, yamlFileName]), | ||
new Error("Only one openApi specification can be validated at a time"), | ||
"error message matches expectation", | ||
); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
347096
59
9993
287
5
14
4
+ Addedminimist@^1.2.8
+ Addedminimist@1.2.8(transitive)