Skip to content

Commit c88b111

Browse files
rauno56arturmuller
andauthored
make more of the examples runnable (#1165)
* make more of the examples runnable * Reformat --------- Co-authored-by: Artur Müller <[email protected]>
1 parent 4511928 commit c88b111

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

docs/guides/02-validating-data.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Superstruct is designed to let you validate any data, ensuring that it matches a
77
The simplest structs are ones that validate "primitive" values, like strings, numbers or booleans. For example:
88

99
```ts
10-
import { string } from 'superstruct'
10+
import { assert, string } from 'superstruct'
1111

1212
const Struct = string()
1313

@@ -22,7 +22,7 @@ In this case, `assert` will throw an error if the input `data` is not a a string
2222
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.
2323

2424
```ts
25-
import { date } from 'superstruct'
25+
import { assert, date } from 'superstruct'
2626

2727
const Struct = date()
2828

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

4141
```ts
42+
import { assert, number, object, string } from 'superstruct'
43+
4244
const User = object({
4345
id: number(),
4446
email: string(),
@@ -80,7 +82,7 @@ This `User` struct will ensure that input data is an object with specific shape
8082
You could also define a struct which represents a list of values that all match a specific type, using the `array` factory. For example:
8183

8284
```ts
83-
import { array } from 'superstruct'
85+
import { array, assert, number } from 'superstruct'
8486

8587
const Struct = array(number())
8688

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

116118
```ts
117-
import { optional } from 'superstruct'
119+
import { number, object, optional, string } from 'superstruct'
118120

119121
const User = object({
120122
id: number(),

docs/guides/03-coercing-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ To allow for these use cases, Superstruct has a concept called "coercion", which
99
Since defaults are such a common case, Superstruct comes with a `defaulted` helper that makes defining default values easy:
1010

1111
```ts
12-
import { defaulted, create } from 'superstruct'
12+
import { create, defaulted, number, object, string } from 'superstruct'
1313

1414
let i = 0
1515

docs/guides/04-refining-validation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ For these situations, you can use "refinements". Refinements allow you to create
99
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:
1010

1111
```ts
12-
import { pattern } from 'superstruct'
12+
import { assert, pattern, string } from 'superstruct'
1313

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

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

2323
```ts
24-
import { size } from 'superstruct'
24+
import { assert, size, string } from 'superstruct'
2525

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

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

3434
```ts
35-
import { min, integer } from 'superstruct'
35+
import { assert, min, integer } from 'superstruct'
3636

3737
const Index = min(integer(), 0)
3838

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

5151
```ts
52-
import { refine } from 'superstruct'
52+
import { refine, string } from 'superstruct'
5353

5454
const MyString = refine(string(), 'MyString', (value) => {
5555
return value.startsWith('The') && value.length > 20

docs/guides/06-using-typescript.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ Superstruct is built with TypeScript, and is designed to integrate seamlessly wi
99
Whenever you use the `is` or `assert` helpers in Superstruct, TypeScript will infer information about your data and give you type safety. For example:
1010

1111
```ts
12+
import { is, number, object, string } from 'superstruct'
13+
1214
const User = object({
1315
id: number(),
14-
email: email(),
16+
email: string(),
1517
name: string(),
1618
})
1719

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

5759
```ts
58-
import { Infer } from 'superstruct'
60+
import { Infer, number, object, string } from 'superstruct'
5961

6062
const User = object({
6163
id: number(),
62-
email: email(),
64+
email: string(),
6365
name: string(),
6466
})
6567

0 commit comments

Comments
 (0)