Skip to content

anttiviljami/mock-json-schema

Repository files navigation

mock-json-schema

CI npm version npm downloads License Buy me a coffee

Simple utility to mock example objects based on JSON schema definitions

Features

  • Minimal & deterministic. Predictable single example with no randomisation involved
  • Thoroughly tested feature set
  • Supports example, default
  • Supports anyOf, allOf, oneOf
  • Built-in examples for following string formats:
    • email
    • hostname
    • ipv4
    • ipv6
    • uri
    • uri-reference
    • uri-template
    • json-pointer
    • date-time
    • uuid
  • TypeScript types included
  • Supports $ref pointers

Usage

const { mock } = require('mock-json-schema');
const assert = require('assert');

const schema = {
  type: 'array',
  items: {
    type: 'object',
    properties: {
      id: {
        type: 'integer',
        minimum: 1,
      },
      name: {
        type: 'string',
        example: 'John Doe',
      },
      email: {
        type: 'string',
        format: 'email',
      },
    },
  },
};

assert.deepEqual(mock(schema), [{ id: 1, name: 'John Doe', email: '[email protected]' }]);

View more examples

Contributing

mock-json-schema is Free and Open Source Software. Issues and pull requests are more than welcome!