Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
yup-schema-faker
Advanced tools
Yup-schema-faker will generate you a fake data based on your yup schema.
Yup-schema-faker will generate you a fake data based on your yup schema.
Key features:
yup | @types/yup | @faker-js/faker | yup-schema-faker |
---|---|---|---|
~0.28.x | ~0.28.x | ^5.1.0 | ~2.28.x |
~0.29.x | ~0.29.x | ^5.1.0 | ~2.29.x |
>= 0.32.0, <= 0.32.10 | N/A | ^5.1.0 | ~2.32.x |
>= 0.32.11, < 1.0.0 | N/A | ^7.4.0 | ^5.x.x |
>= 0.32.11, < 1.0.0 | N/A | ^8.4.1 | ^6.x.x |
^1.4.0 | N/A | ^8.4.1 | ^7.x.x |
Install yup-schema-faker
and its peer dependencies with your favorite package manager:
npm install yup@^1.4.0
npm install --save-dev yup-schema-faker@^7.0.0
npm install --save-dev @faker-js/faker@^8.4.1 # peer dependencies
npm install --save-dev randexp@^0.5.3 # peer dependencies
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": "Frederic.Keebler8@hotmail.com",
// "website": "https://well-worn-co-producer.org",
// "createdOn": "Fri Jun 09 2006 19:49:16 GMT+0800 (台北標準時間)",
// }
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
this feature cannot be detected, use another schema to achieve this behavior
generate trimmed string iff in strict mode
generate lowercase string iff in strict mode
generate uppercase string iff in strict mode
Distributed under the MIT license. See LICENSE for more information.
FAQs
Yup-schema-faker will generate you a fake data based on your yup schema.
The npm package yup-schema-faker receives a total of 1,497 weekly downloads. As such, yup-schema-faker popularity was classified as popular.
We found that yup-schema-faker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.