npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-validator

v0.0.8

Published

A nodejs lib and command line tool powered by JSON Schema to validate web API output

Downloads

8

Readme

api-validator.js

A nodejs lib and command line tool powered by JSON Schema to validate web API output

npm version Dependency Status Build Status Code Climate License

# [You provide] to api-validator.js , (it do):
[Test Plan] + [API Request Lists] -> (Make Requests) +
                              [API Response Schemas] + -> (Validation!) = (CI for API!)
# In future the whole picture:
[API Spec] -> (Document generation)
           -> (Mock API Server)
           -> (API Request Lists) +
                      (Test Plan) + -> (Make Requests) +
           ->                   (API Response Schemas) + -> (Validation!) = (CI for API!)

Features

  • JSON Schema V4 Draft Validation (powered by jjv)
  • load schema files
  • handle both remote $ref http://... or relative file $ref file://../.
  • API Validation - fetch , save , validate , report
  • describe API request and JSON Schema pairs by yaml file
  • make API requests and save response as json files
  • validate on these response
  • output validation results
  • output different test report formats (powered by mocha reports)
  • Command line tool
  • Validate your schema files TODO
  • List and fetch referenced remote schema files TODO
  • API Validation
  • Customize
  • Authorize requests TODO
  • test on YQL tables
  • test by API blueprint TODO

Command Line Usage

Step 1. Make a plan

# plan.yaml
requestYaml: requests.yaml
schemaDir: mySchema/path
prefix: myResults/output/file_
reportJSON: finalReport/file.json
requestConfig:
  timeout: 500

Step 2. List requests

# request.yaml
- url: http://apihost/endpoint1
  schema: schema_name1
- url: http://apihost/endpoint2
  schema: schema_name2
- yql: select * from sometable
  schema: schema_name3

Step 3. Validate!

api-validator.js plan.yaml > results.json

Will output full context into results.json and exit with 0 when pass.

Step 4. (optional) Run with Mocha

Run with mocha then you can using any mocha reporter to see test results. You need to prepare a test.js like this:

require('api-validator').mocha('results.json');

Then run mocha:

mocha test.js

The output will be:

  API Validator tests reports by test/yaml/example_yql_plan.yaml
    ✓ Task "validatePlan" should be executed
    ✓ Task "loadSchemas" should be executed
    ✓ Task "loadRequestList" should be executed
    ✓ Task "preValidateRequests" should be executed
    ✓ Task "prepareRequest" should be executed
    ✓ Task "validateRequests" should be executed
    ✓ Task "request" should be executed
    ✓ Task "save" should be executed
    ✓ Task "validate" should be executed
    ✓ [Saved in file_0001.json] https://query.yahooapis.com/v1/public/yql should pass schema "https://raw.githubusercontent.com/zordius/api-validator.js/master/schemas/yql.json#/definitions/result"
    ✓ [Saved in file_0002.json] https://query.yahooapis.com/v1/public/yql should pass schema "example://yql.yahoo.com/show_tables"


  11 passing (7ms)

Good JSON Schema practices

  • file name: *.json or *.schema.json
  • title , id and $schema are required
  • Refer to any resources by correct URI. (GOOD: "$ref": "http://real.host/real.json#" , bad: "$ref": "any_name" )
  • Use definitions and $ref to decouple schema into many sub schemas
  • Reuse sub schemas

Check boundled schemas to see examples.