Skip to content

Commit

Permalink
Add nullable method to docs (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesunny authored Jun 20, 2020
1 parent 5ae0a62 commit 9870956
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 17 additions & 3 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [`number`](#number)
- [`object`](#object)
- [`optional`](#optional)
- [`nullable`](#nullable)
- [`partial`](#partial)
- [`record`](#record)
- [`set`](#set)
Expand Down Expand Up @@ -260,6 +261,19 @@ undefined

`optional` structs validate that a value matches a specific struct, or that it is `undefined`.

### `nullable`

```ts
nullable(string())
```

```ts
'a string of text'
nullable
```

`nullable` structs validate that a value matches a specific struct, or that it is `null`.

### `partial`

```ts
Expand Down Expand Up @@ -373,7 +387,7 @@ import isEmail from 'is-email'
import isUuid from 'is-uuid'

const Email = struct('Email', isEmail)
const Uuid = struct('Uuid', value => isUuid.v4(value))
const Uuid = struct('Uuid', (value) => isUuid.v4(value))

const User = object({
id: Uuid,
Expand Down Expand Up @@ -448,7 +462,7 @@ You can also define your own custom refinments that are specific to your applica
```ts
import { refinement } from 'superstruct'

const PositiveInteger = refinement(number(), 'PositiveInteger', value => {
const PositiveInteger = refinement(number(), 'PositiveInteger', (value) => {
return Number.isInteger(value) && value >= 0
})
```
Expand Down Expand Up @@ -502,7 +516,7 @@ You can also define your own custom coercions that are specific to your applicat
```ts
import { coercion } from 'superstruct'

const PositiveInteger = coercion(string(), value => {
const PositiveInteger = coercion(string(), (value) => {
return typeof value === 'string' ? value.trim() : value
})
```
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"test": "yarn build:types && yarn test:types && yarn build:cjs && yarn test:mocha",
"test:mocha": "mocha --require ./test/register.cjs --require source-map-support/register ./test/index.ts",
"test:types": "tsc --noEmit && tsc --project ./test/tsconfig.json --noEmit",
"watch": "yarn build:cjs --watch"
"watch": "yarn build:cjs --watch",
"watch:types": "yarn build:types --watch"
},
"keywords": [
"api",
Expand Down

0 comments on commit 9870956

Please sign in to comment.