Generating testing data from JSON Schema with JavaScript

In this tutorial we will learn how to generate testing data from a JSON schema, in JavaScript. We will be using the JSON Schema Faker package.

Introduction

In this tutorial we will learn how to generate testing data from a JSON schema, in JavaScript. We will be using the JSON Schema Faker package.

This package combines the JSON schema specification, which allows to define what is allowed as a JSON object, and fake data generators, which allow to generate testing data according to a schema [1].

This can be useful to generate, for example, testing data or samples to illustrate objects that conform to a given schema.

For documentation, you can check here and here. If you want to test the generator in an easy to use UI, you can check it here.

The package can be easily installed with NPM by sending the following command:

npm install json-schema-faker

I’ll be using version 0.5.0 of the package.

Testing the package

We will start by importing the jsf object exported by the module, which we will use to generate the data.

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

After that, we will define a string containing a JSON schema with the following required properties:

  • name (a string);
  • age (an integer between 10 and 100);
  • married (a boolean).
const schema = `{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 10,
      "maximum": 100
    },
    "married": {
      "type": "boolean"
    }
  },
  "required": [
    "name",
    "age",
    "married"
  ]
}`;

Note that we have defined our schema as a string, but the JSON schema faker library method to generate the data receives an object as input. So, we will need to parse our string to an object. You can check how to parse JSON in JavaScript on this previous tutorial.

const schemaAsObject = JSON.parse(schema);

Now that we have an object representing our JSON schema, we simply need to call the generate method on the imported jsf object. As input, we pass the object we have just parsed and, as output, we will receive an object that conforms with the JSON schema, containing fake data.

const obj = jsf.generate(schemaAsObject);

To finalize, we will print the result to the console.

console.log(obj);

The complete code can be seen here.

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

const schema = `{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 10,
      "maximum": 100
    },
    "married": {
      "type": "boolean"
    }
  },
  "required": [
    "name",
    "age",
    "married"
  ]
}`;

const schemaAsObject = JSON.parse(schema);

const obj = jsf.generate(schemaAsObject);
console.log(obj);

To test the previous code, simply run it in a tool of your choice. I’ll be using Visual Studio Code with the Code Runner extension.

You should get an output similar to figure 1. As can be seen, we got an object that conforms with our schema. Note that, for the name, we obtained a random string that is not a person name. Naturally, since the type of the property in the JSON schema was a string, there was no additional information that allowed to generate more realistic data.

However, additional data generators such as Faker.js or Chance.js can be added, as can be seen here. This allows to generate more realistic data if needed. For example, Faker.js allows to generate random people names, which could have been used in this use case.

Take in consideration that running the previous code multiple times will give different results, since the data is randomly generated.

Output of the program, showing the generated object with fake data.
Figure 1 – Output of the program, showing the generated object with fake data.

Providing custom data examples

As mentioned before, we can use additional data generators to generate more specific data for some fields, if needed. We are not going to cover those more advanced use cases on this tutorial but rather a simpler alternative: the examples keyword of the JSON schema.

The examples keyword allows to specify an array of examples that validate against the schema [2]. Note that these examples are not used for the actual schema validation but may be helpful to document it [2].

In this particular application of generating testing data, the examples array can also be used by the package to retrieve values. By default, this option is set to false. Nonetheless, there is a method called option on the jsf object that we can call to set some configurations.

You can check the full list of options here. The one we are interested on is called useExamplesValue. When set to true, it will return a random value from the examples array, if it exists [3].

You can check below a simple example where we are making use of this option (notice that we are setting it to true at the beginning of the code). For simplicity, we added a schema that only contains a name property, and provided an array of examples.

After that, we just generate the object like we did in the previous section.

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

jsf.option({useExamplesValue: true});

const schemaWithExamples = {
  type: "object",
  properties: {
    name:  {
      type: "string",
      examples: ["John", "Steve", "Matt", "Jake"]
    }
  },
  required: ["name"]
}

const objWithExample = jsf.generate(schemaWithExamples);
console.log(objWithExample);

Upon running the code, you should get a result similar to figure 2. As can be seen, I have obtained a name from the examples array. If you run the code multiple times, you will obtain different values, picked randomly from the array.

Output of the program, with values picked from the examples array.
Figure 2 – Output of the program, with values picked from the examples array.

References

[1] https://github.com/json-schema-faker/json-schema-faker/blob/master/docs/USAGE.md

[2] https://json-schema.org/understanding-json-schema/reference/generic.html

[3] https://github.com/json-schema-faker/json-schema-faker/tree/master/docs#available-options

Leave a Reply

%d bloggers like this: