Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Feb 22, 2021
1 parent f6691cd commit 1f1fb85
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
38 changes: 19 additions & 19 deletions Decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- [Combinators](#combinators)
- [The `literal` constructor](#the-literal-constructor)
- [The `nullable` combinator](#the-nullable-combinator)
- [The `type` combinator](#the-type-combinator)
- [The `struct` combinator](#the-struct-combinator)
- [The `partial` combinator](#the-partial-combinator)
- [The `record` combinator](#the-record-combinator)
- [The `array` combinator](#the-array-combinator)
Expand Down Expand Up @@ -100,12 +100,12 @@ The `nullable` combinator describes a nullable value
export const NullableString: D.Decoder<unknown, null | string> = D.nullable(D.string)
```

## The `type` combinator
## The `struct` combinator

The `type` combinator describes an object with required fields.
The `struct` combinator describes an object with required fields.

```ts
export const Person = D.type({
export const Person = D.struct({
name: D.string,
age: D.number
})
Expand All @@ -114,7 +114,7 @@ console.log(isRight(Person.decode({ name: 'name', age: 42 }))) // => true
console.log(isRight(Person.decode({ name: 'name' }))) // => false
```

The `type` combinator will strip additional fields while decoding
The `struct` combinator will strip additional fields while decoding

```ts
console.log(Person.decode({ name: 'name', age: 42, rememberMe: true }))
Expand Down Expand Up @@ -184,7 +184,7 @@ The `intersect` combinator is useful in order to mix required and optional props

```ts
export const Person = pipe(
D.type({
D.struct({
name: D.string
}),
D.intersect(
Expand Down Expand Up @@ -215,12 +215,12 @@ export const MySum: D.Decoder<
}
// v--- tag name
> = D.sum('type')({
// +----- all union members in the dictionary must own a field named like the chosen tag ("type" in this case)
// |
// v v----- this value must be equal to its corresponding dictionary key ("A" in this case)
A: D.type({ type: D.literal('A'), a: D.string }),
// v----- this value must be equal to its corresponding dictionary key ("B" in this case)
B: D.type({ type: D.literal('B'), b: D.number })
// +----- all union members in the dictionary must own a field named like the chosen tag ("type" in this case)
// |
// v v----- this value must be equal to its corresponding dictionary key ("A" in this case)
A: D.struct({ type: D.literal('A'), a: D.string }),
// v----- this value must be equal to its corresponding dictionary key ("B" in this case)
B: D.struct({ type: D.literal('B'), b: D.number })
})
```

Expand All @@ -240,8 +240,8 @@ export const MySum: D.Decoder<
b: number
}
> = D.sum('type')({
[1]: D.type({ type: D.literal(1), a: D.string }),
[2]: D.type({ type: D.literal(2), b: D.number })
[1]: D.struct({ type: D.literal(1), a: D.string }),
[2]: D.struct({ type: D.literal(2), b: D.number })
})
```

Expand Down Expand Up @@ -270,7 +270,7 @@ interface Category {
}

const Category: D.Decoder<unknown, Category> = D.lazy('Category', () =>
D.type({
D.struct({
title: D.string,
subcategory: D.nullable(Category)
})
Expand All @@ -291,14 +291,14 @@ interface Bar {
}

const Foo: D.Decoder<unknown, Foo> = D.lazy('Foo', () =>
D.type({
D.struct({
foo: D.string,
bar: D.nullable(Bar)
})
)

const Bar: D.Decoder<unknown, Bar> = D.lazy('Bar', () =>
D.type({
D.struct({
bar: D.number,
foo: D.nullable(Foo)
})
Expand Down Expand Up @@ -352,7 +352,7 @@ console.log(isRight(NumberFromString.decode('a'))) // => false
Static types can be extracted from decoders using the `TypeOf` and `InputOf` operators

```ts
export const Person = D.type({
export const Person = D.struct({
name: D.string,
age: D.number
})
Expand Down Expand Up @@ -382,7 +382,7 @@ export interface Person extends D.TypeOf<typeof Person> {}
```ts
import { isLeft } from 'fp-ts/Either'

export const Person = D.type({
export const Person = D.struct({
name: D.string,
age: D.number
})
Expand Down
4 changes: 2 additions & 2 deletions Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import * as S from 'io-ts/Schema'
import * as TD from 'io-ts/TaskDecoder'

export const Person = S.make((S) =>
S.type({
S.struct({
name: S.string,
age: S.number
})
Expand Down Expand Up @@ -111,7 +111,7 @@ Now we can define a schema leveraging the new `Int` capability

```ts
export const Person = make((S) =>
S.type({
S.struct({
name: S.string,
age: S.Int
})
Expand Down

0 comments on commit 1f1fb85

Please sign in to comment.