A module to build validators for Swagger(OpenApi) Request parameters and Response objects.
Swagvali
uses is-my-json-valid JSON schema validator as the default validation tool. However, Joi schema validator can be used instead of the json schema validator, by setting the option joischema
.
npm i swagvali
const Swagvali = require('swagvali');
const api = 'http://petstore.swagger.io/v2/swagger.json';
Swagvali(api);
Promise response:
Swagvali(api, {
path: '/pet/findByStatus',
operation: 'get'
}).then(validators => {
//Parameter validator --> validators.parameters.
validators.parameters.forEach(validator => {
//Parameter spec --> validator.spec.
//Validator function --> validator.validate
});
//Response validator --> validators.responses.
Object.keys(validators.responses).forEach(response => {
//Response spec --> validators.responses[response].spec.
//Validator function --> validators.responses[response].validate
});
}).catch(error => {
//Fail on error.
Assert.ifError(error);
});
Callback style:
Swagvali(api, {
path: '/pet/findByStatus',
operation: 'get'
}, (error, validators) => {
//Fail on error.
Assert.ifError(error);
//Parameter validator --> validators.parameters.
validators.parameters.forEach(validator => {
//Parameter spec --> validator.spec.
//Validator function --> validator.validate
});
//Response validator --> validators.responses.
Object.keys(validators.responses).forEach(response => {
//Response spec --> validators.responses[response].spec.
//Validator function --> validators.responses[response].validate
});
});
Swagvali(api, [options], [cb])
-
api
- (Object) or (String) or (Promise) - (required) - api can be one of the following.- A relative or absolute path to the Swagger api document.
- A URL of the Swagger api document.
- The swagger api Object
- A promise (or a
thenable
) that resolves to the swagger api Object
-
options
- (Object) - (optional) - Additional options to create the mock generator.validated
- (Boolean) - (optional) - Set this property totrue
if the api is already validated against swagger schema and already dereferenced all the$ref
. This is really useful to generate validators for parsed api specs. Default value for this isfalse
and the api will be validated using swagger-parser validate.path
- (String) - (optional) - The path for which the validators need to be generated. For example/pet/findByStatus
,/pet
etc. If apath
is not specified, validators will be generated for all the paths defined by the swagger api. If you are setting apath
option, you should set anoperation
as well.operation
- (String) - (optional) - The operation for which the validators need to be generated. For exampleget
,post
etc. Ifoperation
is not specified, validators will be generated for all the operations defined by the swagger api. If you are setting anoperation
option, you should set apath
as well.parameters
- (Boolean) - (optional) - Set tofalse
if you don't need to build validators forparameters
. Default values istrue
.responses
- (Boolean) - (optional) - Set tofalse
if you don't need to build validators forresponses
. Default values istrue
.joischema
- (Boolean) - (optional) - Set totrue
if you want to use Joi schema based Validators. Swagvali uses enjoi - The json to joi schema converter - to build the validator functions, ifjoischema
option is set totrue
.
By default
swagvali
uses is-my-json-valid JSON schema validator (whenjoischema
option is set asfalse
). -
callback
- (Function) - (optional) -function (error, mock)
. If a callback is not provided aPromise
will be returned.
The Swagvali
api generates a validator object for each parameter or response definition. The validator object has,
-
spec
- (Object) - The spec of the parameter or the response. -
validate
- (Function) -function (data)
- The validate function that accepts thedata
to be validated. The validate function uses is-my-json-valid validator by default. If thejoischema
option was set totrue
, the validator function uses Joi schema validator. -
joischema
- (Object) - TheJoi
schema object, if thejoischema
option was set astrue
. Otherwise this will beundefined
.
The validate
function response has,
status
- (Boolean) - The status of the validation.errors
- (Array) - The list of errors for the validation.value
- The validated value with any type coercions/conversions and other modifiers applied. (the input is left unchanged).value
can be incomplete, if validation failed.