Gathering detailed insights and metrics for @hyperjump/oas-schema-validator
Gathering detailed insights and metrics for @hyperjump/oas-schema-validator
Gathering detailed insights and metrics for @hyperjump/oas-schema-validator
Gathering detailed insights and metrics for @hyperjump/oas-schema-validator
A JSON Schema validator for Open API vocabularies
npm install @hyperjump/oas-schema-validator
Typescript
Module System
Node Version
NPM Version
JavaScript (96.38%)
TypeScript (3.62%)
Total Downloads
3,749
Last Day
1
Last Week
7
Last Month
58
Last Year
572
MIT License
1 Stars
29 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Oct 10, 2023
Latest Version
0.9.0
Package Id
@hyperjump/oas-schema-validator@0.9.0
Unpacked Size
10.18 MB
Size
1.53 MB
File Count
42
NPM Version
8.3.1
Node Version
16.14.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
-46.2%
7
Compared to previous week
Last Month
48.7%
58
Compared to previous month
Last Year
-22.9%
572
Compared to previous year
1
17
OAS Schema Validator is built on JSON Schema Core.
JSV includes support for node.js JavaScript (CommonJS and ES Modules), TypeScript, and browsers.
1npm install @hyperjump/oas-schema-validator
When in a browser context, this package is designed to use the browser's fetch
implementation instead of a node.js fetch clone. The Webpack bundler does this
properly without any extra configuration, but if you are using the Rollup
bundler you will need to include the browser: true
option in your Rollup
configuration.
1 plugins: [ 2 resolve({ 3 browser: true 4 }), 5 commonjs() 6 ]
This project is in beta and there may be breaking changes at any time. When it's stable enough, I'll publish v1.0.0 and follow semantic versioning from there on out.
1const OasSchema = require("@hyperjump/oas-schema-validator"); 2 3 4// Example: Inline schema 5const schemaJson = { 6 "$schema": "https://spec.openapis.org/oas/3.1/dialect/base", 7 "$id": "http://example.com/schemas/string", 8 "type": "string" 9} 10OasSchema.add(schemaJson); 11const schema = await OasSchema.get("http://example.com/schemas/string"); 12 13// Example: Fetch from the web 14const schema = await OasSchema.get("http://example.com/schemas/string"); 15 16// Example: Fetch from file 17const schema = await OasSchema.get(`file://${__dirname}/schemas/string.schema.json`); 18 19// Example: Validate instance 20const output = await OasSchema.validate(schema, "foo"); 21if (output.valid) { 22 console.log("Instance is valid :-)"); 23} else { 24 console.log("Instance is invalid :-("); 25} 26 27// Example: Precompile validator 28const isString = await OasSchema.validate(schema); 29const output = isString("foo"); 30 31// Example: Validate OpenAPI document with no schema validation 32const openApiSchema = await OasSchema.get("https://spec.openapis.org/oas/3.1/schema"); 33const validateOpenApi = await OasSchema.validate(openApiSchema); 34 35const result = validateOpenApi(openApiDoc); 36console.log("Is Valid:", result.valid); 37 38// Example: Validate OpenAPI document with default dialect 39const openApiSchema = await OasSchema.get("https://spec.openapis.org/oas/3.1/schema-base"); 40const validateOpenApi = await OasSchema.validate(openApiSchema); 41 42const result = validateOpenApi(openApiDoc); 43console.log("Is Valid:", result.valid); 44 45// Example: Specify output format 46const output = await OasSchema.validate(schema, "foo", OasSchema.VERBOSE); 47 48// Example: Specify meta-validation output format 49OasSchema.setMetaOutputFormat(OasSchema.FLAG); 50 51// Example: Disable meta-validation 52OasSchema.setShouldMetaValidate(false);
Although the package is written in JavaScript, type definitions are included for TypeScript support. The following example shows the types you might want to know.
1import OasSchema, { InvalidSchemaError } from "@hyperjump/oas-schema-validator"; 2import type { SchemaDocument, Validator, Result, Oas31Schema } from "@hyperjump/json-schema"; 3 4 5const schemaJson: Oas31Schema = { 6 "$id": "https://json-schema.hyperjump.io/schema", 7 "$schema": "https://spec.openapis.org/oas/3.1/dialect/base", 8 9 "type": "string" 10}; 11OasSchema.add(schemaJson); 12 13const schema: SchemaDocument = await OasSchema.get("https://json-schema.hyperjump.io/schema"); 14try { 15 const isString: Validator = await OasSchema.validate(schema); 16 const result: Result = isString("foo"); 17 console.log("isString:", result.valid); 18} catch (error: unknown) { 19 if (error instanceof InvalidSchemaError) { 20 console.log(error.output); 21 } else { 22 console.log(error); 23 } 24}
add: (schema: object, url?: URI, dialectId?: string) => SDoc
Load a schema. See JSC - $id and JSC - $schema for more information.
get: (url: URI, contextDoc?: SDoc, recursive: boolean = false) => Promise
Fetch a schema. Schemas can come from an HTTP request, a file, or a schema
that was added with add
.
validate: (schema: SDoc, instance: any, outputFormat: OutputFormat = FLAG) => Promise
Validate an instance against a schema. The function is curried to allow compiling the schema once and applying it to multiple instances.
compile: (schema: SDoc) => Promise
Compile a schema to be interpreted later. A compiled schema is a JSON serializable structure that can be serialized an restored for later use.
interpret: (schema: CompiledSchema, instance: any, outputFormat: OutputFormat = FLAG) => OutputUnit
A curried function for validating an instance against a compiled schema.
setMetaOutputFormat: (outputFormat: OutputFormat = DETAILED) => undefined
Set the output format for meta-validation. Meta-validation output is only returned if meta-validation results in an error.
setShouldMetaValidate: (isEnabled: boolean) => undefined
Enable or disable meta-validation.
OutputFormat: [FLAG | BASIC | DETAILED | VERBOSE]
See JSC - Output for more information on output formats.
This implementation supports all required features of OAS 3.1 Schema Object. The following optional features are not supported yet.
Run the tests
1npm test
Run the tests with a continuous test runner
1npm test -- --watch
No vulnerabilities found.