npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

faker-schema

v2.0.0

Published

Create deterministic schemas with Faker

Downloads

1,483

Readme

faker-schema

Create deterministic schemas with Faker

Coverage Status npm version node node Code of Conduct

Introduction

This library, faker-schema, provides a neat API around @faker-js/faker to deterministically create one or more pseudo-random records from schemas with advanced features like derived values and probability.

Installation

npm i faker-schema

Usage

Basic Usage

The Schema class is used to create schemas from a blueprint. A blueprint is a function that returns an object literal.

Instatiate a new Schema with a blueprint:

const { faker } = require('@faker-js/faker')
const { Schema } = require('faker-schema')

const personSchema = new Schema(() => ({
  firstName: faker.name.firstName(),
  lastName: faker.name.lastName(),
  address: {
    streetAddress: faker.address.streetAddress(),
    city: faker.address.city(),
    state: faker.address.state(),
    country: faker.address.country(),
    postalCode: faker.address.zipCode(),
  }
}))

Make a random person from the schema:

const person = personSchema.makeOne()

Example output from console.log(person):

{
  "firstName": "Amira",
  "lastName": "Hintz",
  "address": {
    "streetAddress": "64724 Reinhold Plaza",
    "city": "Jastport",
    "state": "Georgia",
    "country": "Rwanda",
    "postalCode": "16974-6122"
  }
} 

Make an array of five random people from the schema:

const people = personSchema.make(5)

You can seed the schema which will result in a deterministic sequence:

personSchema.setSeed(123)

But an even better approach is to pass the optional seed argument to the makeOne and make methods:

const seed = 123
const person = personSchema.makeOne(seed)
const people = personSchema.make(5, seed)

The above techinique is deterministic. The same random record will be returned if the methods are called again with the same seed.

If you seed the make method, the nth item in the array will have a seed of seed + n - 1. This means if you seed make with 2, such as make(5, 2), then makeOne(2) will return the first item from the result of make and makeOne(3) will return the second item from the result of make, and so on.

Tip: Because of the behavior described above, seeding by id is a great way to use this library to implement a deterministic mock API with pseudo-random data.

Advanced Usage

Blueprints for schemas can be nested any-level deep, have derived properties that are deterministic, and using the withProbability helper, can return either a value or null, also deterministically.

The following code snippet displays how to combine these advanced techniques:

const { faker } = require('@faker-js/faker')
const { 
  Schema, 
  withProbability 
} = require('faker-schema')

const personSchema = new Schema(() => ({
  firstName: faker.name.firstName(),
  lastName: withProbability(
    faker.name.lastName(), 0.5
  ),
  fullName: ({ firstName, lastName }) =>
    firstName && lastName
      ? `${firstName} ${lastName}`
      : firstName || lastName,
  address: {
    streetAddress: withProbabilit(
      faker.address.streetAddress(), 0.5
    ),
    city: faker.address.city(),
    state: faker.address.state(),
    country: withProbability(
      faker.address.country(), 0.5
    ),
    postalCode: withProbability(
      faker.address.zipCode(), 0.25
    ),
  },
  fullAddress: ({ address }) =>
    Object
      .values(address)
      .filter(v => v !== null)
      .join(', ')
      .trim()
}))

const seed = 123
const person = personSchema.makeOne(seed)
const people = personSchema.make(5, seed)

Example output from console.log(person):

{
  "firstName": "Nannie",
  "lastName": null,
  "fullName": "Nannie",
  "address": {
    "streetAddress": "82890 Andreane Pass",
    "city": "Pabloshire",
    "state": "Massachusetts",
    "country": "Qatar",
    "postalCode": null 
  },
  "fullAddress": "82890 Andreane Pass, Pabloshire, Massachusetts, Qatar"
}

More Information

As you may have noticed, much of the heavy-lifting here comes from Faker. Consult the Faker README for more information.

License

Copyright (c) 2019-2022 Nickolas Kenyeres [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.