Coder Social home page Coder Social logo

json-schema-faker's Introduction

Fake your schemas!

Build Status NPM version Coverage Status

var jsf = require('json-schema-faker');

var schema = {
  type: 'object',
  properties: {
    user: {
      type: 'object',
      properties: {
        id: {
          $ref: '#/definitions/positiveInt'
        },
        name: {
          type: 'string',
          faker: 'name.findName'
        },
        email: {
          type: 'string',
          format: 'email',
          faker: 'internet.email'
        }
      },
      required: ['id', 'name', 'email']
    }
  },
  required: ['user'],
  definitions: {
    positiveInt: {
      type: 'integer',
      minimum: 0,
      minimumExclusive: true
    }
  }
};

var sample = jsf(schema);

console.log(sample.user.name);
// output: John Doe

Supported keywords

  • $ref — Resolve internal references only, and/or external if provided.
  • required — All required properties are guaranteed, if not can be omitted.
  • pattern — Generate samples based on RegExp values.
  • format — Core formats only: date-time, email, hostname, ipv4, ipv6 and uri.
  • enum — Returns any of these enumerated values.
  • minLength/maxLength — Applies length constraints to string values.
  • minimum/maximum — Applies constraints to numeric values.
  • exclusiveMinimum/exclusiveMaximum — Adds exclusivity for numeric values.
  • multipleOf — Multiply constraints for numeric values.
  • items — Support for subschema and fixed item values.
  • minItems/maxItems — Adds length constraints for array items.
  • uniqueItems — Applies uniqueness constraints for array items.
  • additionalItems — Partially supported (?)
  • allOf/oneOf/anyOf — Subschema combinators.
  • properties — Object properties to be generated.
  • minProperties/maxProperties — Adds length constraints for object properties.
  • patternProperties — RegExp-based object properties.
  • additionalProperties — Partially supported (?)
  • dependencies — Not supported yet (?)
  • not — Not supported yet (?)

Using references

Inline references are fully supported (json-pointers) but external can't be resolved by json-schema-faker.

In order to achieve that you can use refaker and then use the resolved schemas:

var schema = {
  type: 'object',
  properties: {
    someValue: {
      $ref: 'otherSchema'
    }
  }
};

var refs = [
  {
    id: 'otherSchema',
    type: 'string'
  }
];

var sample = jsf(schema, refs);

console.log(sample.someValue);
// output: voluptatem

Faking values

Generate human-friendly samples by using faker property on each subschema:

{
  "type": "string",
  "faker": "internet.email"
}

The above schema will invoke:

require('faker').internet.email();

Not that faker property has higher precedence than format.

Great, Why?

Actually, I've found some projects or services:

But are incomplete or has limited support for some keywords, so I decided to code this library.

Issues

Any contribution is well received.

json-schema-faker's People

Contributors

pateketrueke avatar

Watchers

 avatar

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.