@traversable/json-schema-test
v0.0.25
Published
<br> <h1 align="center">แฏ๐๐ฟ๐ฎ๐๐ฒ๐ฟ๐๐ฎ๐ฏ๐น๐ฒ/๐ท๐๐ผ๐ป-๐๐ฐ๐ต๐ฒ๐บ๐ฎ-๐๐ฒ๐๐</h1> <br>
Readme
Requirements
@traversable/json-schema-test has 1 peer dependency:
Usage
$ pnpm add -D @traversable/json-schema-test fast-checkHere's an example of importing the library:
import { JsonSchemaTest } from '@traversable/json-schema-test'
// see below for specifc examplesTable of contents
JsonSchemaTest.seedToSchemaJsonSchemaTest.seedToValidDataJsonSchemaTest.seedToInvalidDataJsonSchemaTest.SeedGeneratorJsonSchemaTest.SeedValidDataGeneratorJsonSchemaTest.SeedInvalidDataGenerator
JsonSchemaTest.seedToSchema
Use JsonSchemaTest.seedToSchema to convert a seed generated by JsonSchemaTest.SeedGenerator into a
JSON Schema schema that satisfies the configuration options you specified.
Example
import { JsonSchemaTest } from '@traversable/json-schema-test'
import * as fc from 'fast-check'
const builder = JsonSchemaTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = JsonSchemaTest.seedToSchema(mySeed)
// ^? const mySchema: JsonSchemaJsonSchemaTest.seedToValidData
Use JsonSchemaTest.seedToValidData to convert a seed generated by JsonSchemaTest.SeedGenerator into
data that satisfies the schema that the seed represents.
Example
import { JsonSchemaTest } from '@traversable/json-schema-test'
import * as fc from 'fast-check'
const builder = JsonSchemaTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = JsonSchemaTest.seedToSchema(mySeed)
// ^? const mySchema: JsonSchema
const validData = JsonSchemaTest.seedToValidData(mySeed)
// ^? validated against `mySchema`, will always succeedJsonSchemaTest.seedToInvalidData
Use JsonSchemaTest.seedToInvalidData to convert a seed generated by JsonSchemaTest.SeedGenerator into
data that does not satisfy the schema that the seed represents.
Example
import { JsonSchemaTest } from '@traversable/json-schema-test'
import * as fc from 'fast-check'
const builder = JsonSchemaTest.SeedGenerator()['*']
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = JsonSchemaTest.seedToSchema(mySeed)
// ^? const mySchema: JsonSchema.TSchema
const invalidData = JsonSchemaTest.seedToValidData(mySeed)
// ^? validated against `mySchema`, will always failJsonSchemaTest.SeedGenerator
[!NOTE]
JsonSchemaTest.SeedGeneratoris fairly low-level. All of the other exports of this library have been implemented in terms ofJsonSchemaTest.SeedGenerator.
Generates a configurable, pseudo-random "seed builder".
- Use
JsonSchemaTest.seedToSchemato convert a seed into a JSON Schema - Use
JsonSchemaTest.seedToValidDatato convert a seed into valid data - Use
JsonSchemaTest.seedToInvalidDatato convert a seed into invalid data
Example
import { JsonSchemaTest, type JsonSchema } from '@traversable/json-schema-test'
import * as fc from 'fast-check'
const builder = JsonSchemaTest.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 `JsonSchemaTest.seedToSchema`,
* `JsonSchemaTest.seedToValidData` or `JsonSchemaTest.seedToInvalidData`:
*/
const [mySeed] = fc.sample(builder.object, 1)
const mySchema = JsonSchemaTest.seedToSchema(mySeed)
// ^? const mySchema: JsonSchema
const validData = JsonSchemaTest.seedToValidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `validData` should always succeed
const invalidData = JsonSchemaTest.seedToInvalidData(mySeed)
// ^? since the `mySeed` was also used to generate `mySchema`,
// parsing `invalidData` should always failJsonSchemaTest.SeedValidDataGenerator
Like JsonSchemaTest.SeedGenerator, except JsonSchemaTest.SeedValidDataGenerator comes pre-configured to exclude schemas that make it impossible to reliably generate valid data.
[!NOTE]
JsonSchemaTest.SeedValidDataGeneratordoes not accept any options. If you need more fine-grained control of the schemas being generated, useJsonSchemaTest.SeedGenerator.
JsonSchemaTest.SeedInvalidDataGenerator
Like JsonSchemaTest.SeedGenerator, except JsonSchemaTest.SeedValidDataGenerator comes pre-configured to exclude schemas that make it impossible to reliably generate invalid data.
[!NOTE]
JsonSchemaTest.SeedInvalidDataGeneratordoes not accept any options. If you need more fine-grained control of the schemas being generated, useJsonSchemaTest.SeedGenerator.
