Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make more of the examples runnable #1165

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/guides/02-validating-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Superstruct is designed to let you validate any data, ensuring that it matches a
The simplest structs are ones that validate "primitive" values, like strings, numbers or booleans. For example:

```ts
import { string } from 'superstruct'
import { assert, string } from 'superstruct'

const Struct = string()

Expand All @@ -22,7 +22,7 @@ In this case, `assert` will throw an error if the input `data` is not a a string
But Superstruct has simple structs like these for more than the primitive types. It has support out of the box for many of the common types you might need to validate—dates, functions, regexps, etc.

```ts
import { date } from 'superstruct'
import { assert, date } from 'superstruct'

const Struct = date()

Expand All @@ -39,6 +39,8 @@ Here we're ensuring that `data` is a valid `Date` object.
In addition to simple, "flat" values, you can also compose structs into more complex shapes. The most common example of this is `object` structs:

```ts
import { assert, number, object, string } from 'superstruct'

const User = object({
id: number(),
email: string(),
Expand Down Expand Up @@ -80,7 +82,7 @@ This `User` struct will ensure that input data is an object with specific shape
You could also define a struct which represents a list of values that all match a specific type, using the `array` factory. For example:

```ts
import { array } from 'superstruct'
import { array, assert, number } from 'superstruct'

const Struct = array(number())

Expand Down Expand Up @@ -114,7 +116,7 @@ const Team = object({
You can also model optional properties. For example, maybe an `email` address isn't strictly required for all your users, you could do:

```ts
import { optional } from 'superstruct'
import { number, object, optional, string } from 'superstruct'

const User = object({
id: number(),
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/03-coercing-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To allow for these use cases, Superstruct has a concept called "coercion", which
Since defaults are such a common case, Superstruct comes with a `defaulted` helper that makes defining default values easy:

```ts
import { defaulted, create } from 'superstruct'
import { create, defaulted, number, object, string } from 'superstruct'

let i = 0

Expand Down
8 changes: 4 additions & 4 deletions docs/guides/04-refining-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ For these situations, you can use "refinements". Refinements allow you to create
Superstruct has several [built-in refinements](../reference/refinements.md) for common use cases. For example, a common one is ensuring that a string matches a specific regular expression pattern:

```ts
import { pattern } from 'superstruct'
import { assert, pattern, string } from 'superstruct'

const Section = pattern(string(), /\d+(\.\d+/)?/)

Expand All @@ -21,7 +21,7 @@ assert('string', Section) // throws!
Or maybe that a string (or array, number, etc.) has a specific size:

```ts
import { size } from 'superstruct'
import { assert, size, string } from 'superstruct'

const Name = size(string(), 1, 100)

Expand All @@ -32,7 +32,7 @@ assert('', Name) // throws!
Another common use case is validating nonnegative integers (like indexes in an array) using the built-in `min` helper:

```ts
import { min, integer } from 'superstruct'
import { assert, min, integer } from 'superstruct'

const Index = min(integer(), 0)

Expand All @@ -49,7 +49,7 @@ These refinements don't change the inferred type of the data, but they do ensure
You can also write your own custom refinements for more domain-specific use cases. For example, for a specific kind of string:

```ts
import { refine } from 'superstruct'
import { refine, string } from 'superstruct'

const MyString = refine(string(), 'MyString', (value) => {
return value.startsWith('The') && value.length > 20
Expand Down
8 changes: 5 additions & 3 deletions docs/guides/06-using-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Superstruct is built with TypeScript, and is designed to integrate seamlessly wi
Whenever you use the `is` or `assert` helpers in Superstruct, TypeScript will infer information about your data and give you type safety. For example:

```ts
import { is, number, object, string } from 'superstruct'

const User = object({
id: number(),
email: email(),
email: string(),
name: string(),
})

Expand Down Expand Up @@ -55,11 +57,11 @@ In this case, the incorrectly defined `id` property will cause TypeScript's comp
You can also do the reverse and infer a TypeScript type using an existing Superstruct struct with the `Infer` utility. For example:

```ts
import { Infer } from 'superstruct'
import { Infer, number, object, string } from 'superstruct'

const User = object({
id: number(),
email: email(),
email: string(),
name: string(),
})

Expand Down
Loading