@traversable/valibot-test
v0.0.20
Published
<br> <h1 align="center">แฏ๐๐ฟ๐ฎ๐๐ฒ๐ฟ๐๐ฎ๐ฏ๐น๐ฒ/๐๐ฎ๐น๐ถ๐ฏ๐ผ๐-๐๐ฒ๐๐</h1> <br>
Readme
Requirements
@traversable/valibot-test has 2 peer dependencies:
valibot(v1)fast-check
Usage
$ pnpm add -D @traversable/valibot-test valibot fast-checkHere's an example of importing the library:
import * as v from 'valibot'
import { vxTest } from '@traversable/valibot-test'
// see below for specifc examplesTable of contents
vxTest.seedToSchemavxTest.seedToValidDatavxTest.seedToInvalidDatavxTest.SeedGeneratorvxTest.SeedValidDataGeneratorvxTest.SeedInvalidDataGenerator
vxTest.seedToSchema
Use vxTest.seedToSchema to convert a seed generated by vxTest.SeedGenerator into a
valibot schema that satisfies the configuration options you specified.
Example
import { vxTest } from '@traversable/valibot-test'
import * as fc from 'fast-check'
const builder = vxTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = vxTest.seedToSchema(mySeed)
// ^? const mySchema: v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>vxTest.seedToValidData
Use vxTest.seedToValidData to convert a seed generated by vxTest.SeedGenerator into
data that satisfies the schema that the seed represents.
Example
import type * as v from 'valibot'
import { vxTest } from '@traversable/valibot-test'
import * as fc from 'fast-check'
const builder = vxTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = vxTest.seedToSchema(mySeed)
// ^? const mySchema: v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>
const validData = vxTest.seedToValidData(mySeed)
mySchema.parse(validData) // will never throwvxTest.seedToInvalidData
Use vxTest.seedToInvalidData to convert a seed generated by vxTest.SeedGenerator into
data that does not satisfy the schema that the seed represents.
Example
import { vxTest } from '@traversable/valibot-test'
import * as fc from 'fast-check'
const builder = vxTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = vxTest.seedToSchema(mySeed)
// ^? const mySchema: v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>
const invalidData = vxTest.seedToValidData(mySeed)
mySchema.parse(invalidData) // should always throwvxTest.SeedGenerator
[!NOTE]
vxTest.SeedGeneratoris fairly low-level. All of the other exports of this library have been implemented in terms ofvxTest.SeedGenerator.
Generates a configurable, pseudo-random "seed builder".
- Use
vxTest.seedToSchemato convert a seed into a Valibot schema - Use
vxTest.seedToValidDatato convert a seed into valid data - Use
vxTest.seedToInvalidDatato convert a seed into invalid data
Example
import type * as v from 'valibot'
import * as fc from 'fast-check'
import { vxTest } from '@traversable/valibot-test'
const builder = vxTest.SeedGenerator({
include: ["boolean", "string", "object"],
// ๐ use `include` to only include certain schema types
exclude: ["boolean", "any"],
// ๐ use `exclude` to exclude certain schema types altogether (overrides `include`)
object: { maxKeys: 5 },
// ๐ specific arbitraries are configurable by name
})
// included schemas are present as properties on your generator...
builder.string
builder.object
// ...excluded schemas are not present...
builder.boolean // ๐ซ TypeError
// ...a special wildcard `"*"` property (pronounced "surprise me") is always present:
builder["*"]
/**
* `fast-check` will generate a seed, which is a data structure containing
* integers that represent a kind of AST.
*
* To use a seed, you need to pass it to an interpreter like `vxTest.seedToSchema`,
* `vxTest.seedToValidData` or `vxTest.seedToInvalidData`:
*/
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = vxTest.seedToSchema(mySeed)
// ^? const mySchema: v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>
const validData = vxTest.seedToValidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `validData` should always succeed
const invalidData = vxTest.seedToInvalidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `invalidData` should always failvxTest.SeedValidDataGenerator
Like vxTest.SeedGenerator, except vxTest.SeedValidDataGenerator comes pre-configured to exclude schemas that make it impossible to reliably generate valid data.
[!NOTE]
vxTest.SeedValidDataGeneratordoes not accept any options. If you need more fine-grained control of the schemas being generated, usevxTest.SeedGenerator.
vxTest.SeedInvalidDataGenerator
Like vxTest.SeedGenerator, except vxTest.SeedValidDataGenerator comes pre-configured to exclude schemas that make it impossible to reliably generate invalid data.
[!NOTE]
vxTest.SeedInvalidDataGeneratordoes not accept any options. If you need more fine-grained control of the schemas being generated, usevxTest.SeedGenerator.
