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

swagger-mock-parser

v1.2.0

Published

use chance.js to parse definition schema to mock data

Downloads

347

Readme

Build Status Coverage Status

swagger-mock-parser

A parser for swagger object properties ,mock data returned by chancejs

changeLog

version 1.2.0

break change: object parser use JSON.stringfy to break circular references in schema

version 1.1.14

add useObjectKey option,set this true ,String parser will return the key name;

version 1.1.13

add fixedArray option,set this true ,array parser will return only one child array

version 1.1.12

detect and fix circular references in schema and return null object

version 1.1.8 ~ 1.1.11

add useExample option,set this true parser will return defined example first

version 1.1.7

break change: return string instead of throw error,objectParser canParse add "node.type == object" judge condition

usage

An example for mocking swagger definition schema

// basic usage
var Parser = require('swagger-mock-parser')
var parser = new Parser();
var schema = definition.schema;
return parser.parse(schema);

// useExample option
var Parser = require('swagger-mock-parser')
// set useExample true will return every property's example which has already defined
var parser = new Parser({useExample: true});
var schema = {type: 'string', example: 'will return example first'}
return parser.parse(schema);

// useObjectKey option
var Parser = require('swagger-mock-parser')
// set useObjectKey true will return every Object property's name as the string value,but useExample is prior
var parser = new Parser({useExample: true, useObjectKey: true});
var hasExampleSchema ={
    type: 'object',
    properties: {
        objectKeyName: {type: 'string', example: 'useExample prior'}
    }
}
var noExampleSchema ={
    type: 'object',
    properties: {
        objectKeyName: {type: 'string', example: ''}
    }
}
// this will return { objectKeyName: 'useExample prior' }
return parser.parse(hasExampleSchema);
// this will return { objectKeyName: 'objectKeyName' }
return parser.parse(noExampleSchema);

Specifying custom Chance options

Swagger specifies only a few primitive types; for scenarios where specific chance methods are needed, use the x-chance-type field.

...
definitions:
  NewPet:
    properties:
      name:
        type: string
        x-chance-type: name
      tag:
        type: string
        x-chance-type: guid

Most of the chance methods allow some fine-tuning of the returned data. For example, the integer method allows specification of minimum and maximum output values. These options can be configured in the Swagger YAML file with the x-chance-options block:

...
definitions:
  Pet:
    allOf:
      - $ref: '#/definitions/NewPet'
      - required:
        - id
        properties:
          id:
            type: integer
            format: int64
            x-type-options:
              min: 1
              max: 1000

A note on types:

All of the primitive types defined in the Swagger specification are supported except for file and password. Currently, the format property is ignored; use x-chance-type instead. The server will error on any request with a type other than one of the primitive types if there is no valid x-chance-type also defined.

Returning Fixed Values

Although not a chance method, support has been added for returning fixed values using x-chance-type: fixed. Any value given for the custom tag x-type-value will be returned; below is an example where an object is returned:

    status:
      type: object
      x-chance-type: fixed
      x-type-value:
        type: 'adopted'