Giter Site home page Giter Site logo

json-schema-faker-cli's Introduction

json-schema-faker-cli

MIT License Build Status

NPM status

This is a CLI for json-schema-faker.

install

npm install -g json-schema-faker-cli

usage

params

  • (required) file (path) containing JSON schema
  • (optional) generated file (path) according to the provided schema
  • (optional) number of objects in the array (when you want to generate an array of multiple objects matching the schema)
  • (optional) file (path) containing options (in JS or JSON format)

generating single object from the schema

generate-json schema.json output.json

generating array of multiple objects

generate-json schema.json output.json $ITEMS_LENGTH

generating with options set

generate-json schema.json output.json none options.js

You can use any of the json-schema-faker available options.

an example options file:

module.exports = {
  minLength: 20,
  random: () => 0.2,
};

generating to stdout

generate-json schema.json

skipping a param

You can pass none to skip any of the optional params. It's useful when you need to pass a following param when skipping some of the previous params at the same time.

json-schema-faker-cli's People

Contributors

dependabot[bot] avatar ganqqwerty avatar oprogramador avatar renovate-bot avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

json-schema-faker-cli's Issues

generate-json schema.json output.json not working with faker values

I don't know if I'm just doing this wrong, but I can't seem to get faker providers to work. I've tested this on the web GUI to make sure the schema works as intended, but when running in the CLI it keeps outputting lorem ipsum not what the faker provider should output.

Here is the schema I'm using:

{
  "type": "object",
  "properties": {
    "test": {
      "type": "string",
      "faker": {
        "fake": "{{address.country}}"
      }
    }
  },
  "required": [
    "test"
  ]
}

Option sortProperties: false reverses original order

When I add sortProperties: false to the options.js file, the original order is reversed, see example below. The option sortProperties: true works as intended and sorts all properties.

example json schema:

{ "type": "object", "required": [], "properties": { "first": { "type": "string" }, "second": { "type": "string" }, "third": { "type": "string" } } }

example output

{"third":"sint","second":"aliquip","first":"ad"}

add usage(), user-friendly error messages, and support for the various options offered by the underling json-schema-faker library

I did not see any way to pass command-line options corresponding to the various options offered by the underling json-schema-faker library, so I made some changes for it to do so.

In addition, the CLI does not provide any usage message or user-friendly error messages if, for example, it is run with an insufficient number of arguments.

Lastly, if no output file is specified, it should output to stdout.

Could you please incorporate these changes to address these issues? They are pretty simple, so I didn't bother with a PR. This will allow you to do the following:

generate-json # outputs error message and usage message
generate-json --alwaysFakeOptionals my.schema.json > my-fake.json # saves to my-fake.json
generate-json --notAValidOption my.schema.json # outputs error message and usage message

The changes were to two files, generate.js and generate-json:

// --- app/generate.js ---

const _ = require('lodash');
const jsonfile = require('jsonfile');

function generate(inputPath, outputPath, itemsLength, options) {
  var faker = require('json-schema-faker');
  const inputObject = jsonfile.readFileSync(inputPath);
  if(options)
    faker.option(options);

  const output = itemsLength === undefined
    ? faker(inputObject)
    : _.times(itemsLength, () => faker(inputObject));

  if(!outputPath)
    console.log(JSON.stringify(output));
  else
    jsonfile.writeFileSync(outputPath, output);
}

module.exports = generate;

// --- .bin/generate_json

#!/usr/bin/env node
const generate = require('./app/generate');
const supportedOptions = {
  failOnInvalidTypes: true,
  defaultInvalidTypeProduct: null,
  useDefaultValue: false,
  requiredOnly: false,
  maxItems: null,
  maxLength: null,
  defaultMinItems: 0,
  defaultRandExpMax: 10,
  alwaysFakeOptionals: false
};

const optionUsage = Object.keys(supportedOptions).map(function(optName) {
  return '  --' + optName + '=' + JSON.stringify(supportedOptions[optName]);
});

const usage = [
  'Example:', '  ' + process.argv[1] + ' --alwaysFakeOptionals[=true] schema.json [output.json]',
  'Options and default values:'
].concat(optionUsage).join('\n') + '\n';

var options = {};
var files = [];
for(var i = 2; i < process.argv.length; i++) {
  var arg = process.argv[i];
  if(!arg.startsWith('--'))
    files.push(arg);
  else {
    var parts = arg.slice(2).split('=');
    var optName = parts[0];
    var optValue = parts[1];
    var showUsage = false;
    if(optName == 'help')
      showUsage = true;
    else if(!(optName in supportedOptions)) {
      console.error('Option ' + optName + ' not recognized');
      showUsage = true;
    }

    if(showUsage) {
      console.log(usage);
      return;
    }

    if(!optValue)
      options[optName] = true;
    else {
      try {
        options[optName] = JSON.parse(optValue);
      } catch(e) {
        options[optName] = optValue;
      }
    }
  }
}

if(!files[0])
  console.error('Input schema file not provided\n' + usage);
else
  generate(files[0], files[1], files[2], options);

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.