package
Statically compile an OpenAPI spec into a JS validation function
npm install openapi-static-validatorGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
openapi-static-validatorCLI to compile an OpenAPI specification as JavaScript validation file, optimized for performances.
npm install openapi-static-validator -gopenapi-static-validator spec.json > validate.jsThen the validation can be imported in your code:
import { validateRequest, RequestError } from './validate';
const result = validateRequest({
path: 'say/hello',
method: 'post',
headers: {},
query: {},
body: {
message: 'Hello world',
},
});
if (result instanceof RequestError) {
// Do something with the error
} else {
console.log(result);
}When using format for data of type: string, you need to define validators for them:
import { validateRequest, ValidationError } from './validate';
const result = validateRequest(
{
path: 'say/hello',
method: 'post',
headers: {},
query: {},
body: {
message: 'Hello world',
},
},
{
stringFormats: {
uri: (value, path) =>
value.startsWith('https://') ? null : new ValidationError(path, 'Invalid url'),
},
},
);in: header and in: cookie parameterstype: integer, not just as numbervalidateResponse functionTo publish the package:
package.jsonnpm run build and npm publishEstimated unpacked size across 10 packages.
1.6 MB
Includes dependencies.
samypesse
samypesse@gmail.com
^2.1.0
2.1.0
^3.0.0
3.0.0
^6.2.1
9.0.0