Skip to content

dmi3y/json-api-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsonSchema API validator

Slightly opinionated view on JsonSchema API validation.

Install

json-api-validator

Proposed usecase

But not limited.

Let's say there is folder structure of json schemas.

api +
     \schemas +
               \request +
               |         \pathA
               |            get.json
               |            post.json
               |            put.json
               |                     +
               |                      \subpathA
               |                            get.json
               |          .... etc ....
               \response +
                          \pathA
                            get.json
                            post.json
                            put.json
                                    +
                                     \subpathA
                                            get.json
                          .... etc ....

This will be possible setup for validator.

api/validator.js

var validator = require('api-validator')({
  // from cwd
  base: './schemas'
})

// ... and later in the code
var result = validator(jsonToValidate, 'response/pashA/get.json')
// ... do something based on result

See tests for better examples.

API

This module is a wrapper over the tv4 JsonSchema validator. The result returned from the call is direct pipeline from tv4.validateMultiple method.

There is options you can pass for initiation:

  • base root folder from which schemas will be searched up, default to cwd.
  • globPattern the pattern used to search schemas, default to /**/*.
  • formats hash of the custom formats for tv4.addFormat multiple form.

Notes

As of now the all schemas are loaded in the memory, no matter will they be used or not. The lazy loading are considerably on the road map.

Releases

No releases published

Packages

No packages published