Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

api-test-data-gen-lib

Package Overview
Dependencies
2
Maintainers
1
Versions
60
Issues
File Explorer

Advanced tools

api-test-data-gen-lib

- [Overview](#overview) - [Key features](#key-features) - [Example JSON](#example-json) - [Example usage (Typescript)](#example-usage-typescript) - [Generators](#generators) - [Numbers](#numbers) - [Strings](#strings) - [randomRecord(options, va

    0.0.60latest

Version published
Maintainers
1
Yearly downloads
3,399

Weekly downloads

Readme

Source

Overview

Nodejs lib to generate JSON test data from JSON

:construction::construction::construction::construction::construction::construction:

This is very much a work in progress. Examples below are rather nonsensical and documention is lacking. Having said that the basic stuff works :smile:

:construction::construction::construction::construction::construction::construction:

Key features

  • Built in generators for random numbers, dates, places, strings, bank account etc.
  • Generate fields from user defined arrays
  • Generate multiple JSON records from a single source
  • Generate multiple nested arrays
  • Flexible
  • Localisation support

Example JSON

{ "vars": { "firstName":"Frank", "firstNames": ["Fred", "John", "Zara", "Sam", "Tom", "Claire", "Louise"], "version":1, "iterators": { "APIDATAGEN_root": "${randomInt(min:1,max:10)}", "addresses": "${randomInt(min:0,max:2)}", "creditCards": 6 } }, "seed": { "randomInt": "${randomInt({min:1,max:120})}", "timestamp": "{{$timestamp}}", "timestamp2": "${timestamp()}", "version": "${getField(source: version)}", "testDate":"${randomDate(start:1975/02/01, end:2022-02-04T16:00:00.000Z, format:DD-MMM-YYYY)}", "today": "${dateNow(format:MM-DD-YYYY)}", "yesterday":"${dateTPlus(format:MM-DD-YYYY, offset: -1)}", "tomorrow":"${dateTPlus(format:MM-DD-YYYY, offset: 1)}", "firstName":"${randomFirstname()}", "middleNme":"${randomMiddlename()}", "lastName":"${randomLastname()}", "name": "${randomString(source: firstNames)} ${randomInt()} / ${randomDecimal( min:200, max:230, style:currency,currency:EUR)}", "age": "${randomInt(min:1,max:120):number}/${randomInt(min:200,max:230)}", "addresses": [ { "seq": "${seq(name:addressId,start : 10, step: 20)}", "address_line_1": "${randomInt(min:1,max:30,style:currency,currency:EUR,minimumFractionDigits:2,maximumFractionDigits:2)} abc street", "address_line_2": "abc", "country":"${randomCountry()}", "country2":"${randomCountry(field:alpha2)}", "country3":"${randomCountry(field:alpha3)}", "country4":"${randomCountry(field:regionCode)}", "currency":"${randomCurrency()}", "currency2":"${randomCurrency(field:name)}" } ], "creditCards": [ { "cardNumber": "${randomInt(min:10000,max:2000000)}", "bank": "abc", "amount":"${randomDecimal(locale:de-DE, maximumFractionDigits: 2, min:0, max:1000000)}", "accountNumber":"${randomIntFixedLength(length:9)}", "accountNumber2":"${randomBankAccount()}", "sortCode":"${randomSortCode()}", "bankCode": "${randomBank(field:code)}", "bankName": "${randomBank(field:name)}" } ] } }

Example usage (Typescript)

import { parse } from "api-test-data-gen-lib"; const parseRequest = (req, res) => { const vars = req.body.vars; const seed = req.body.seed; const parsedData: any = parse(seed, vars); if (parsedData.length === 0) { return []; } const response = JSON.parse(parsedData); return response; }; { "randomProduct": { "code": "001", "desc": "Sponge" }, "randomProductDesc": "Sponge", "randomProductCode": "001", "randomInt": 18, "timestamp": "1650736291", "timestamp2": "1650736291", "version": 1, "testDate": "12-Feb-2010", "today": "04-23-2022", "yesterday": "04-22-2022", "tomorrow": "04-24-2022", "firstName": "Theo", "middleNme": "Massimo", "lastName": "Loni", "name": "Claire ${randomInt()} / €204.27", "age": "${randomInt(min:1,max:120):number}/${randomInt(min:200,max:230)}", "addresses": [ { "seq": 10, "address_line_1": "€8.00 abc street", "address_line_2": "abc", "countryName": { "name": "Norfolk Island", "alpha2": "NF", "alpha3": "NFK", "countryCode": "574", "iso3166_2": "ISO 3166-2:NF", "region": "Oceania", "subRegion": "Australia and New Zealand", "intermediateRegion": "", "regionCode": "009", "subRegionCode": "053", "intermediateregionCode": "" }, "countryRegion": "Oceania", "country2": "CA", "country3": "SOM", "country4": "002", "currency": "TOP", "currency2": "Mauritian Rupee" } ], "creditCards": [ { "cardNumber": 1166040, "amount": "26.703,15", "accountNumber": "764934816", "accountNumber2": "439998711", "sortCode": "65-57-72", "bankCode": "MPCX:", "bankName": "Sankheda Nagarik Sahakari Bank", "bankCode2": "LATX:", "bankName2": "Latur Urban Co-operative Bank Latur", "bankCode3": "LATX:" } ] }

Generators

Numbers

nameparamsDescriptionexample
randomIntmin - lowest value
max - highest value
Generate a random int${randomInt(min:1,max:120)}
randomDecimalmin - lowest value
max - highest value
Generate a random decimal${randomDecimal(min:1,max:120)}
randomIntFixedLengthmin - lowest value
max - highest value
length - max length of resulting int in chars
Generate a random int with a fixed length${randomIntFixedLength(min:1,max:120,length:2)}
seqname - name of the sequence
start - start int of sequence
step - the amount that the sequence is incremented
Generate a random int with a fixed length${seq(name:addressId,start:10,step: 20)}

Strings


randomRecord(options, vars) ⇒ string

Select a random record from an array of records

Kind: global function
Returns: string -

a random string

ParamTypeDescription
optionsObject
options.sourcestring

property name in vars of the source array

[options.field]string

field name within the source to select

varsObject

object containing the source array

INPUT { "vars": { "products": [{"code":"001","desc":"Sponge"},{"code":"002","desc":"Hat"},{"code":"003","desc":"shell Suit"}], "iterators": { "APIDATAGEN_root": 2 } }, "seed": { "randomProduct":"${randomRecord(source:products)}" } } OUPUT [ { "randomProduct": { "code": "003", "desc": "shell Suit" } }, { "randomProduct": { "code": "002", "desc": "Hat" } } ]

Dates

Finance

Names

Places

FAQs

What is api-test-data-gen-lib?

- [Overview](#overview) - [Key features](#key-features) - [Example JSON](#example-json) - [Example usage (Typescript)](#example-usage-typescript) - [Generators](#generators) - [Numbers](#numbers) - [Strings](#strings) - [randomRecord(options, va

Is api-test-data-gen-lib popular?

The npm package api-test-data-gen-lib receives a total of 60 weekly downloads. As such, api-test-data-gen-lib popularity was classified as not popular.

Is api-test-data-gen-lib well maintained?

We found that api-test-data-gen-lib demonstrated a healthy version release cadence and project activity. It has 1 open source maintainer collaborating on the project.

Last updated on 23 Apr 2022
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc