swagvali v1.0.1
swagvali
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.
Install
npm i swagvaliUsage
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
});
});API
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 totrueif 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 isfalseand 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,/petetc. If apathis not specified, validators will be generated for all the paths defined by the swagger api. If you are setting apathoption, you should set anoperationas well.operation- (String) - (optional) - The operation for which the validators need to be generated. For exampleget,postetc. Ifoperationis not specified, validators will be generated for all the operations defined by the swagger api. If you are setting anoperationoption, you should set apathas well.parameters- (Boolean) - (optional) - Set tofalseif you don't need to build validators forparameters. Default values istrue.responses- (Boolean) - (optional) - Set tofalseif you don't need to build validators forresponses. Default values istrue.joischema- (Boolean) - (optional) - Set totrueif you want to use Joi schema based Validators. Swagvali uses enjoi - The json to joi schema converter - to build the validator functions, ifjoischemaoption is set totrue.By default
swagvaliuses is-my-json-valid JSON schema validator (whenjoischemaoption is set asfalse).
callback- (Function) - (optional) -function (error, mock). If a callback is not provided aPromisewill be returned.
Validators
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 thedatato be validated. The validate function uses is-my-json-valid validator by default. If thejoischemaoption was set totrue, the validator function uses Joi schema validator.joischema- (Object) - TheJoischema object, if thejoischemaoption was set astrue. Otherwise this will beundefined.
validate response
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).valuecan be incomplete, if validation failed.