File: examples.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 (39 lines) | stat: -rw-r--r-- 720 bytes parent folder | download | duplicates (3)
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
---
id: examples
title: Examples
---

#### Validate that an unknown value is a port protocol:

```ts
const isPort = t.applyCascade(t.isNumber(), [
    t.isInteger(),
    t.isInInclusiveRange(1, 65535),
]);

isPort(42000);
```

#### Validate that an unknown value is a record with specific fields, regardless of the others:

```ts
const isDiv = t.isObject({
    tagName: t.isLiteral(`DIV`),
}, {
    extra: t.isUnknown(),
});

isDiv({tagName: `div`, appendChild: () => {}});
```

#### Validate that a specific field is a specific value, and that others are all numbers:

```ts
const isModel = t.isObject({
    uid: t.isString(),
}, {
    extra: t.isDict(t.isNumber()),
});

isModel({uid: `foo`, valA: 12, valB: 24});
```