@scalar/openapi-validator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/dist/detect-version.d.ts +10 -0
- package/dist/detect-version.d.ts.map +1 -0
- package/dist/detect-version.js +28 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +10 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/schemas/v2.0/schema.d.ts +1407 -0
- package/dist/schemas/v2.0/schema.d.ts.map +1 -0
- package/dist/schemas/v2.0/schema.js +1466 -0
- package/dist/schemas/v3.0/schema.d.ts +1269 -0
- package/dist/schemas/v3.0/schema.d.ts.map +1 -0
- package/dist/schemas/v3.0/schema.js +1499 -0
- package/dist/schemas/v3.1/schema.d.ts +1225 -0
- package/dist/schemas/v3.1/schema.d.ts.map +1 -0
- package/dist/schemas/v3.1/schema.js +1292 -0
- package/dist/schemas/v3.2/schema.d.ts +1409 -0
- package/dist/schemas/v3.2/schema.d.ts.map +1 -0
- package/dist/schemas/v3.2/schema.js +1509 -0
- package/dist/specifications.d.ts +5310 -0
- package/dist/specifications.d.ts.map +1 -0
- package/dist/specifications.js +14 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/validate-path-parameters.d.ts +7 -0
- package/dist/validate-path-parameters.d.ts.map +1 -0
- package/dist/validate-path-parameters.js +90 -0
- package/dist/validate.d.ts +36 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +35 -0
- package/package.json +63 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @scalar/openapi-validator
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#9967](https://github.com/scalar/scalar/pull/9967): Add a new `@scalar/openapi-validator` package that validates OpenAPI documents on its own. `@scalar/openapi-parser` now uses it under the hood.
|
|
8
|
+
|
|
9
|
+
Two type-level changes in `@scalar/openapi-parser` are worth noting:
|
|
10
|
+
- `ErrorObject.path` is now `string | string[]` instead of `string[]`. Schema errors carry a JSON Pointer string, semantic errors carry path segments — both shapes were already produced at runtime, the type just says so now. Narrow with `Array.isArray` before treating it as a list.
|
|
11
|
+
- The unused `ValidationOutcome` type and the internal `OpenApiDocument` alias are no longer exported.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Scalar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Scalar OpenAPI Validator
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@scalar/openapi-validator)
|
|
4
|
+
[](https://www.npmjs.com/package/@scalar/openapi-validator)
|
|
5
|
+
[](https://www.npmjs.com/package/@scalar/openapi-validator)
|
|
6
|
+
[](https://discord.gg/scalar)
|
|
7
|
+
|
|
8
|
+
Validate OpenAPI documents against the OpenAPI Specification. Supports OpenAPI 3.2, 3.1, 3.0 and Swagger 2.0.
|
|
9
|
+
|
|
10
|
+
This package does the schema validation part of [`@scalar/openapi-parser`](https://www.npmjs.com/package/@scalar/openapi-parser) on its own. Use it when all you need is validation and you do not want the rest of the parser.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Scalar is an open-source API platform for teams who want beautiful developer interfaces without vendor lock-in.
|
|
15
|
+
|
|
16
|
+
- **[API References](https://scalar.com/products/api-references/getting-started)** — Interactive API documentation from OpenAPI and AsyncAPI specs.
|
|
17
|
+
- **[Developer Docs](https://scalar.com/products/docs/getting-started)** — Write in Markdown/MDX, generate API references, sync with two-way Git.
|
|
18
|
+
- **[SDK Generator](https://scalar.com/products/sdk-generator/getting-started)** — Type-safe SDKs and CLIs in TypeScript, Python, Go, PHP, Java, and Ruby.
|
|
19
|
+
- **[API Client](https://scalar.com/products/api-client/getting-started)** — Open-source, offline-first Postman alternative built on OpenAPI.
|
|
20
|
+
|
|
21
|
+
20M+ monthly npm installs · 15,500+ GitHub stars · MIT licensed · [scalar.com](https://scalar.com)
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm add @scalar/openapi-validator
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
Pass a JSON string, a YAML string, or an object:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { validate } from '@scalar/openapi-validator'
|
|
37
|
+
|
|
38
|
+
const result = validate({
|
|
39
|
+
openapi: '3.1.0',
|
|
40
|
+
info: {
|
|
41
|
+
title: 'Hello World',
|
|
42
|
+
version: '1.0.0',
|
|
43
|
+
},
|
|
44
|
+
paths: {},
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
console.log(result.valid)
|
|
48
|
+
|
|
49
|
+
if (!result.valid) {
|
|
50
|
+
console.log(result.errors)
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### References
|
|
55
|
+
|
|
56
|
+
This validator does not resolve references. It validates a single, self-contained document. If your document references other files or URLs, bundle or dereference it first (for example with [`@scalar/json-magic`](https://www.npmjs.com/package/@scalar/json-magic) or [`@scalar/openapi-parser`](https://www.npmjs.com/package/@scalar/openapi-parser)) and then validate the result.
|
|
57
|
+
|
|
58
|
+
### Throw on error
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { validate } from '@scalar/openapi-validator'
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
validate(document, { throwOnError: true })
|
|
65
|
+
} catch (error) {
|
|
66
|
+
// Handle the first validation error
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Community
|
|
71
|
+
|
|
72
|
+
We are API nerds. You too? Let's chat on Discord: <https://discord.gg/scalar>
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
The source code in this repository is licensed under [MIT](https://github.com/scalar/scalar/blob/main/LICENSE).
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type OpenApiVersion } from './specifications.js';
|
|
2
|
+
/**
|
|
3
|
+
* Detects the OpenAPI/Swagger version of a document by looking at its top-level
|
|
4
|
+
* `openapi` (3.x) or `swagger` (2.0) field.
|
|
5
|
+
*
|
|
6
|
+
* This is an intentionally small, internal copy of the parser's version
|
|
7
|
+
* detection. It is kept private so the validator does not depend on the parser.
|
|
8
|
+
*/
|
|
9
|
+
export declare function detectVersion(document: unknown): OpenApiVersion | undefined;
|
|
10
|
+
//# sourceMappingURL=detect-version.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detect-version.d.ts","sourceRoot":"","sources":["../src/detect-version.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,cAAc,EAAmB,MAAM,kBAAkB,CAAA;AAEvE;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,OAAO,GAAG,cAAc,GAAG,SAAS,CAuB3E"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isObject } from '@scalar/helpers/object/is-object';
|
|
2
|
+
import { OpenApiVersions } from './specifications.js';
|
|
3
|
+
/**
|
|
4
|
+
* Detects the OpenAPI/Swagger version of a document by looking at its top-level
|
|
5
|
+
* `openapi` (3.x) or `swagger` (2.0) field.
|
|
6
|
+
*
|
|
7
|
+
* This is an intentionally small, internal copy of the parser's version
|
|
8
|
+
* detection. It is kept private so the validator does not depend on the parser.
|
|
9
|
+
*/
|
|
10
|
+
export function detectVersion(document) {
|
|
11
|
+
if (!isObject(document)) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
for (const version of OpenApiVersions) {
|
|
15
|
+
const field = version === '2.0' ? 'swagger' : 'openapi';
|
|
16
|
+
const value = document[field];
|
|
17
|
+
if (typeof value !== 'string') {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
// Match on major.minor exactly. A `startsWith` check would mistake a future
|
|
21
|
+
// "3.10.0" for "3.1" and validate it against the wrong schema.
|
|
22
|
+
const [major, minor] = value.split('.');
|
|
23
|
+
if (`${major}.${minor}` === version) {
|
|
24
|
+
return version;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error messages used by the validator.
|
|
3
|
+
*
|
|
4
|
+
* Kept local to this package on purpose: error messages should live where they
|
|
5
|
+
* are used and be copied rather than imported across packages.
|
|
6
|
+
*/
|
|
7
|
+
export declare const ERRORS: {
|
|
8
|
+
readonly EMPTY_OR_INVALID: "Can't find JSON, YAML or filename in data.";
|
|
9
|
+
readonly OPENAPI_VERSION_NOT_SUPPORTED: "Can't find supported Swagger/OpenAPI version in the provided document, version must be a string.";
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,MAAM;;;CAIT,CAAA"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error messages used by the validator.
|
|
3
|
+
*
|
|
4
|
+
* Kept local to this package on purpose: error messages should live where they
|
|
5
|
+
* are used and be copied rather than imported across packages.
|
|
6
|
+
*/
|
|
7
|
+
export const ERRORS = {
|
|
8
|
+
EMPTY_OR_INVALID: "Can't find JSON, YAML or filename in data.",
|
|
9
|
+
OPENAPI_VERSION_NOT_SUPPORTED: "Can't find supported Swagger/OpenAPI version in the provided document, version must be a string.",
|
|
10
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { OpenApiVersions as supportedVersions } from './specifications.js';
|
|
2
|
+
export type { ErrorObject, OpenApiVersion, ThrowOnErrorOption, ValidationOutcome } from './types.js';
|
|
3
|
+
export { type ValidateOptions, validate } from './validate.js';
|
|
4
|
+
export { validatePathParameters } from './validate-path-parameters.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACvE,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AACjG,OAAO,EAAE,KAAK,eAAe,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA"}
|
package/dist/index.js
ADDED