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

json-schema-api-test-data-generator

v0.1.2

Published

Generate sample test data based on JSON Hyper-Schema

Downloads

5

Readme

json-schema-api-test-data-generator

Not overly sophisticated utility that generates sample test data based on links in JSON Hyper-Schema.

Installation

npm install json-schema-api-test-data-generator

Usage

Module exports a single function that takes a JSON Hyper-Schema definition with links objects and outputs an array of objects based on the links definitions covering various (but not all) combinations to check against. The output objects are in format:

{
  link:     // the link definition
  data:     // array|undefined: an array of test data for that link definition
}

If the link definition has a schema defined we generate test data for that links. Each data element is an object in form:

{
  valid:    // boolean|undefined: whether the test data is valid against the schema or not
  data:     // object|undefined: the actual data
  message:  // string: a descriptive message for the test
  property: // string|undefined: if available, the key / property at test
}

Sample usage:

var generate = require('json-schema-api-test-data-generator');

// hyper-schema that defines our REST API
var schema = {
  "type": "object",
  "definitions": {
    "user": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "minLength": 5
        },
        "active": {
          "type": "boolean"
        },
        "email": {
          "type": "string",
          "format": "email"
        },
        "accountNumber": {
          "type": "number"
        }
      },
      "required": ["name", "email"]
    }
  },
  "properties": {
    "user": {
      "$ref": "#/definitions/user"
    }
  },
  "links": [{
    "description": "Create a new user.",
    "href": "/users",
    "method": "POST",
    "rel": "create",
    "title": "Create",
    "schema": {
      "$ref": "#/definitions/user"
    },
    "targetSchema": {
      "$ref": "#/definitions/user"
    }
  }, {
    "description": "List users.",
    "href": "/users",
    "method": "GET",
    "rel": "instances",
    "title": "List",
    "targetSchema": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/user"
      }
    }
  }, {
    "description": "Update an existing user.",
    "href": "/users/{email}",
    "method": "PUT",
    "rel": "update",
    "title": "Update",
    "schema": {
      "$ref": "#/definitions/user"
    },
    "targetSchema": {
      "$ref": "#/definitions/user"
    }
  }, {
    "description": "Delete an existing user.",
    "href": "/users/{email}",
    "method": "DELETE",
    "rel": "destroy",
    "title": "Update",
    "targetSchema": {
      "$ref": "#/definitions/user"
    }
  }]
};

generate(schema, function(err, res) {
  console.dir(res, {depth: null});
});

Output:

[ { link:
     { description: 'Create a new user.',
       href: '/users',
       method: 'POST',
       rel: 'create',
       title: 'Create',
       schema:
        { type: 'object',
          properties:
           { name: { type: 'string', minLength: 5 },
             active: { type: 'boolean' },
             email: { type: 'string', format: 'email' },
             accountNumber: { type: 'number' } },
          required: [ 'name', 'email' ] },
       targetSchema:
        { type: 'object',
          properties:
           { name: { type: 'string', minLength: 5 },
             active: { type: 'boolean' },
             email: { type: 'string', format: 'email' },
             accountNumber: { type: 'number' } },
          required: [ 'name', 'email' ] } },
    data:
     [ { valid: true,
         data:
          { name: 'reprehenderit',
            email: '[email protected]',
            active: true },
         message: 'should work with all required properties' },
       { valid: true,
         data:
          { name: 'est aliquip irure',
            email: '[email protected]' },
         message: 'should work without optional property: active',
         property: 'active' },
       { valid: true,
         data: { name: 'sint ipsum amet ad', email: '[email protected]' },
         message: 'should work without optional property: accountNumber',
         property: 'accountNumber' },
       { valid: false,
         data: { email: '[email protected]' },
         message: 'should not work without required property: name',
         property: 'name' },
       { valid: false,
         data: { name: 'nulla' },
         message: 'should not work without required property: email',
         property: 'email' },
       { valid: false,
         data:
          { name: false,
            email: '[email protected]',
            accountNumber: 15629399.986937642 },
         message: 'should not work with \'name\' of type \'boolean\'',
         property: 'name' },
       { valid: false,
         data: { name: '[mlP', email: '[email protected]' },
         message: 'should not pass validation for minLength of property: name',
         property: 'name' },
       { valid: false,
         data:
          { name: 'et quis',
            email: '[email protected]',
            accountNumber: -18683489.50520158,
            active: 7785059067101184 },
         message: 'should not work with \'active\' of type \'integer\'',
         property: 'active' },
       { valid: false,
         data:
          { name: 'id sint nostrud',
            email: 6920340101922816,
            accountNumber: 15984299.592673779 },
         message: 'should not work with \'email\' of type \'integer\'',
         property: 'email' },
       { valid: false,
         data: { name: 'proident', email: 'QU9A))%$' },
         message: 'should not pass validation for format of property: email',
         property: 'email' },
       { valid: false,
         data:
          { name: 'velit fugiat ullamco',
            email: '[email protected]',
            accountNumber: null },
         message: 'should not work with \'accountNumber\' of type \'null\'',
         property: 'accountNumber' } ] },
  { link:
     { description: 'List users.',
       href: '/users',
       method: 'GET',
       rel: 'instances',
       title: 'List',
       targetSchema:
        { type: 'array',
          items:
           { type: 'object',
             properties:
              { name: { type: 'string', minLength: 5 },
                active: { type: 'boolean' },
                email: { type: 'string', format: 'email' },
                accountNumber: { type: 'number' } },
             required: [ 'name', 'email' ] } } },
    data:
     [ { message: 'should get empty list of instances if none exist' },
       { message: 'should get list of instances' } ] },
  { link:
     { description: 'Update an existing user.',
       href: '/users/{email}',
       method: 'PUT',
       rel: 'update',
       title: 'Update',
       schema:
        { type: 'object',
          properties:
           { name: { type: 'string', minLength: 5 },
             active: { type: 'boolean' },
             email: { type: 'string', format: 'email' },
             accountNumber: { type: 'number' } },
          required: [ 'name', 'email' ] },
       targetSchema:
        { type: 'object',
          properties:
           { name: { type: 'string', minLength: 5 },
             active: { type: 'boolean' },
             email: { type: 'string', format: 'email' },
             accountNumber: { type: 'number' } },
          required: [ 'name', 'email' ] } },
    data:
     [ { valid: true,
         data:
          { name: 'occaecat est do',
            email: '[email protected]',
            active: true },
         message: 'should work with all required properties' },
       { valid: true,
         data: { name: 'id ex dolore sint', email: '[email protected]' },
         message: 'should work without optional property: active',
         property: 'active' },
       { valid: true,
         data:
          { name: 'magna id nisi sit',
            email: '[email protected]',
            active: false },
         message: 'should work without optional property: accountNumber',
         property: 'accountNumber' },
       { valid: false,
         data:
          { email: '[email protected]',
            active: true },
         message: 'should not work without required property: name',
         property: 'name' },
       { valid: false,
         data: { name: 'dolore ipsum', accountNumber: -17545116.59964919 },
         message: 'should not work without required property: email',
         property: 'email' },
       { valid: false,
         data: { name: 4448630052225024, email: '[email protected]' },
         message: 'should not work with \'name\' of type \'integer\'',
         property: 'name' },
       { valid: false,
         data:
          { name: 'i@@]',
            email: '[email protected]',
            active: true },
         message: 'should not pass validation for minLength of property: name',
         property: 'name' },
       { valid: false,
         data:
          { name: 'minim dolor',
            email: '[email protected]',
            active: 61.03 },
         message: 'should not work with \'active\' of type \'number\'',
         property: 'active' },
       { valid: false,
         data: { name: 'estdeserunt', email: 9500069199872, active: true },
         message: 'should not work with \'email\' of type \'integer\'',
         property: 'email' },
       { valid: false,
         data:
          { name: 'laborum nulla reprehenderit dolore',
            email: 'GnfOGqpY)azctsLQsA' },
         message: 'should not pass validation for format of property: email',
         property: 'email' },
       { valid: false,
         data:
          { name: 'tempor',
            email: '[email protected]',
            accountNumber: -6349397491187712 },
         message: 'should not work with \'accountNumber\' of type \'integer\'',
         property: 'accountNumber' } ] },
  { link:
     { description: 'Delete an existing user.',
       href: '/users/{email}',
       method: 'DELETE',
       rel: 'destroy',
       title: 'Update',
       targetSchema:
        { type: 'object',
          properties:
           { name: { type: 'string', minLength: 5 },
             active: { type: 'boolean' },
             email: { type: 'string', format: 'email' },
             accountNumber: { type: 'number' } },
          required: [ 'name', 'email' ] } },
    data:
     [ { message: 'should delete instance' },
       { message: 'should get 404 on unknown instance' } ] } ]

API Reference

generate(schema, options, fn)

Generate sample test data based on links in JSON Hyper-Schema definition

Kind: global function

| Param | Type | Description | | --- | --- | --- | | schema | Object | JSON Hyper-Schema definition | | options | Object | The options. For additional schema deref options See json-schema-deref | | options.skipDeref | Boolean | If you are sure that there are no $ref's in your schema, use this to skip schema deref, and it will make things faster. Default: false. | | fn | function | Callback in standard form (err, result) |

CLI

jsapitdgen schema.json > testdata.json