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

yup-schema-faker

v5.0.0

Published

Yup-schema-faker will generate you a fake data based on your yup schema.

Downloads

9,477

Readme

Yup-schema-faker

Yup-schema-faker will generate you a fake data based on your yup schema.

Showcase & Playground

GitHub Workflow Status (branch) codecov last commit

Key features:

  • Extensible: you can fake for your own schemas, methods or override existing ones.

Version Compatibiility

| yup | @types/yup | yup-schema-faker | | -------------------------------------------------------------------- | --------------------------------------------------- | --------------------------------------------------------------------- | | ~0.28.x | ~0.28.x | ~2.28.x | | ~0.29.x | ~0.29.x | ~2.29.x | | >= 0.32.0, <= 0.32.10 | N/A | ~2.32.x | | >= 0.32.11 | N/A | main |

Getting Started

Install yup-schema-faker and its peer dependencies with your favorite package manager:

pnpm add yup@^0.32.11
pnpm add -D yup-schema-faker@^5.0.0
pnpm add -D @faker-js/faker@^7.4.0
pnpm add -D randexp@^0.5.3

Usage:

import { object, string, number, date } from 'yup'
import { install, fake } from 'yup-schema-faker'
import { faker } from '@faker-js/faker'
// import { faker } from '@faker-js/faker/locale/ja'

// Before using it, you need to install all built-in fakers:
install(faker)

// If you have extended fakers, you need to install them here, too:
//
// addFaker(boolean, BooleanFaker)
//
// fakeDedicatedTest(boolean, 'is-value', schema => {
//   const isValueTest = schema.tests.find(test => test.OPTIONS.name === 'is-value')!
//   return isValueTest.OPTIONS.params?.value === 'true'
// })

// Define schema:
const userSchema = object({
  name: string().required(),
  age: number().required().positive().integer(),
  email: string().email(),
  website: string().url().nullable(),
  createdOn: date().default(() => new Date()),
}).noUnknown()

// Fake data:
const fakeUser = fake(userSchema)
// {
//   "name": " Assumenda eos volup",
//   "age": 73684592,
//   "email": "[email protected]",
//   "website": "https://well-worn-co-producer.org",
//   "createdOn": "Fri Jun 09 2006 19:49:16 GMT+0800 (台北標準時間)",
// }

API

fake

Pass a yup schema and return a fake data.

Function signature:

interface Options {
  // please see https://github.com/jquense/yup#schemastrictenabled-boolean--false-schema
  strict?: boolean

  // External values that used to resolve conditions and references.
  // please see https://github.com/jquense/yup#schemacastvalue-any-options---infertypeschema
  context?: object
}

function fake<Schema etends AnySchema>(schema: Schema, options?: Options): ReturnType<Schema['cast']>;

Example:

const schema1 = yup.number().required()
fake(schema1)
// 763 or "971235"
fake(schema1, { strict: true })
// 9102

const schema2 = object({
  baz: ref('foo.bar'),
  foo: object({
    bar: string(),
  }),
  x: ref('$x'),
})
const context = { x: 5 }
fake(schema2, { context })
// {
//   foo: {
//     bar: 'Sit atque temporibus',
//   },
//   baz: 'Sit atque temporibus',
//   x: 5,
// }

fakeDedicatedTest

Similar to addMethod, you can use fakeDedicatedTest to fake extended methods.

Function signature:

function fakeDedicatedTest<SchemaConstructor extends (...args: any[]) => AnySchema>(
  schemaConstructor: SchemaConstructor,
  name: string,
  fakeFn: (schema: ReturnType<SchemaConstructor>) => ReturnType<ReturnType<SchemaConstructor>['cast']>,
)

Example: string.json example

addFaker

You can create new schemas in yup. Similarly, you can use addFaker to create corresponding fakers for these schemas.

Function signature:

function addFaker<Schema extends AnySchema, Faker>(
  schemaConstructor: (...arg: any[]) => Schema,
  fakerConstructor: Faker,
)

Example: customMixed example

seed

If you want to produce consistent results, you can set your own seed with integer:

import { seed, fake } from 'yup-schema-faker'
import { string } from 'yup'

seed(123)
const first = fake(string())

seed(123)
const second = fake(string())

console.log(first === second) // true

Supported yup API

  • yup
    • ✅ yup.ref(path: string, options: { contextPrefix: string }): Ref
    • ✅ yup.lazy((value: any) => Schema): Lazy
  • mixed
    • ✅ mixed.strict(isStrict: boolean = false): Schema
    • ✅ mixed.default(value: any): Schema
    • ✅ mixed.nullable(isNullable: boolean = true): Schema
    • ✅ mixed.required(message?: string | function): Schema
    • ✅ mixed.notRequired(): Schema Alias: optional()
    • ✅ mixed.defined(): Schema
    • ✅ mixed.oneOf(arrayOfValues: Array, message?: string | function): Schema Alias: equals
    • ✅ mixed.notOneOf(arrayOfValues: Array, message?: string | function)
    • ✅ mixed.when(keys: string | Array, builder: object | (value, schema)=> Schema): Schema
  • string
    • ✅ string.required(message?: string | function): Schema
    • ✅ string.length(limit: number | Ref, message?: string | function): Schema
    • ✅ string.min(limit: number | Ref, message?: string | function): Schema
    • ✅ string.max(limit: number | Ref, message?: string | function): Schema
    • ✅ string.matches(regex: Regex, message?: string | function): Schema
    • ❌ string.matches(regex: Regex, options: { message: string, excludeEmptyString: bool }): Schema

      this feature cannot be detected, use another schema to achieve this behavior

    • ✅ string.email(message?: string | function): Schema
    • ✅ string.url(message?: string | function): Schema
    • ✅ string.uuid(message?: string | function): Schema
    • ✅ string.trim(message?: string | function): Schema

      generate trimmed string iff in strict mode

    • ✅ string.lowercase(message?: string | function): Schema

      generate lowercase string iff in strict mode

    • ✅ string.uppercase(message?: string | function): Schema

      generate uppercase string iff in strict mode

  • number
    • ✅ number.min(limit: number | Ref, message?: string | function): Schema
    • ✅ number.max(limit: number | Ref, message?: string | function): Schema
    • ✅ number.lessThan(max: number | Ref, message?: string | function): Schema
    • ✅ number.moreThan(min: number | Ref, message?: string | function): Schema
    • ✅ number.positive(message?: string | function): Schema
    • ✅ number.negative(message?: string | function): Schema
    • ✅ number.integer(message?: string | function): Schema
  • boolean
    • ✅ boolean.isTrue(message?: string | function): Schema
    • ✅ boolean.isFalse(message?: string | function): Schema
  • date
    • ✅ date.min(limit: Date | string | Ref, message?: string | function): Schema
    • ✅ date.max(limit: Date | string | Ref, message?: string | function): Schema
  • array
    • ✅ array.of(type: Schema): Schema
    • ✅ array.length(length: number | Ref, message?: string | function): Schema
    • ✅ array.min(limit: number | Ref, message?: string | function): Schema
    • ✅ array.max(limit: number | Ref, message?: string | function): Schema
  • object
    • ✅ object.shape(fields: object, noSortEdges?: Array<[string, string]>): Schema
    • ✅ object.noUnknown(onlyKnownKeys: boolean = true, message?: string | function): Schema

Contributing

We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:

  • Reporting a bug
  • Discussing the current state of the code
  • Submitting a fix
  • Proposing new features
  • Becoming a maintainer

People love thorough bug reports. I'm not even kidding.

Report bugs using Github's issues

We use GitHub issues to track public bugs. Report a bug by opening a new issue; it's that easy!

Write bug reports with detail, background, and sample code

Great Bug Reports tend to have:

  • A quick summary and/or background
  • Steps to reproduce
    • Be specific!
    • Give sample code if you can.
  • What you expected would happen
  • What actually happens
  • Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

Support Me

paypal buymeacoffee