Skip to content

Commit

Permalink
make more of the examples runnable (#1165)
Browse files Browse the repository at this point in the history
* make more of the examples runnable

* Reformat

---------

Co-authored-by: Artur Müller <[email protected]>
  • Loading branch information
rauno56 and arturmuller authored Dec 18, 2023
1 parent 4511928 commit c88b111
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
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

0 comments on commit c88b111

Please sign in to comment.