File: types.md

package info (click to toggle)
node-typanion 3.14.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 392 kB
  • sloc: javascript: 51; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 4,301 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
---
id: types
title: Type predicates
---

## `isArray`

```ts
const validate = t.isArray(values, {delimiter?}):
```

Ensure that the values are arrays whose values all match the specified schema. The `delimiter` option only matters when coercion is enabled, and will allow string inputs where each item is separated by the given delimiter.

## `isBoolean`

```ts
const validate = t.isBoolean();
```

Ensure that the values are all booleans. Prefer `isLiteral` if you wish to specifically check for one of `true` or `false`. This predicate supports coercion.

## `isDate`

```ts
const validate = t.isDate();
```

Ensure that the values are proper `Date` instances. This predicate supports coercion via either ISO8601, or raw numbers (in which case they're interpreted as the number of *seconds* since epoch, not milliseconds).

## `isDict`

```ts
const validate = t.isDict(values, {keys?});
```

Ensure that the values are all a standard JavaScript objects containing an arbitrary number of fields whose values all match the given schema. The `keys` option can be used to apply a schema on the keys as well (this will always have to be strings, so you'll likely want to use `cascade(isString(), [...])` to define the pattern).

## `isEnum`

```ts
const validate = t.isEnum(values);
```

Ensure that the values are all amongst the allowed set of values. This predicate accepts either an array of literals as parameter, or a record. Since the classic TS enums also happen to be records, you can use them as well:

```ts
enum MyEnum { /* ... */ };

const validate = t.isEnum(MyEnum);
```

## `isLiteral`

```ts
const validate = t.isLiteral(value);
```

Ensure that the values are strictly equal to the specified expected value. It's an handy tool that you can combine with `isOneOf` and `isObject` to parse structures similar to Redux actions, etc.

## `isMap`

```ts
const validate = t.isMap(keySpec, valueSpec);
```

Ensure that the values are a `Map` instance. This predicate supports coercion under the form of arrays of tuples (like what standard maps spread into).

## `isNumber`

```ts
const validate = t.isNumber();
```

Ensure that the values are all numbers. This predicate supports coercion.

## `isObject`

```ts
const validate = t.isObject(props, {extra?});
```

Ensure that the values are plain old objects whose properties match the given shape. Extraneous properties will be aggregated and validated against the optional `extra` schema. If you need to validate against an object that may have any number of extraneous properties, either use `isPartial` instead or set `extra` to `isDict(isUnknown())`.

## `isPartial`

```ts
const validate = t.isPartial(props);
```

Same as `isObject`, but allows any number of extraneous properties.

## `isPayload`

```ts
const validate = t.isPayload(spec);
```

Ensure that the values are JSON string whose parsed representation match the given validator. Unlike [`isJSON`](cascading.md#isJSON), `isPayload` will coerce the original value to match the nested spec. The drawback, however, is that it can only be used if the coercion is enabled, as it will otherwise always fail (that's because if it were to pass, then the resulting type refinement would be incorrect).

## `isSet`

```ts
const validate = t.isSet(values, {delimiter?});
```

Ensure that the values are a `Set` instance. This predicate supports coercion as either regular arrays or, if configured, delimiter-separated strings.

## `isString`

```ts
const validate = t.isString();
```

Ensure that the values are all regular strings.

## `isTuple`

```ts
const validate = t.isTuple(values, {delimiter?});
```

Ensure that the values are tuples whose items match the specified schemas. The `delimiter` option only matters when coercion is enabled, and will allow string inputs where each item is separated by the given delimiter.

## `isUnknown`

```ts
const validate = t.isUnknown();
```

Accept whatever is the input without validating it, but without refining the type inference either. Note that by default `isUnknown` will forbid `undefined` and `null`, but this can be switched off by explicitly allowing them via `isOptional` and `isNullable`.

## `isInstanceOf`

```ts
const validate = t.isInstanceOf(constructor);
```

Ensure that the values are instances of a given constructor.