Skip to content

Commit 9514707

Browse files
author
andy.patterson
committed
feat: add base inline documentation
Annotate every external function with jsdocs. Made sure everything had some example code to go with it.
1 parent e3d9cc8 commit 9514707

File tree

6 files changed

+513
-17
lines changed

6 files changed

+513
-17
lines changed

README.md

+225-5
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,209 @@ A runtime and compile-time type checker library.
88

99

1010
### any
11+
Creates a validator instance that is _always_ true.
12+
This is useful for specifying that an object _must_ have a given parameter, but you don't care what type that parameter is.
13+
###### Example:
14+
```typescript
15+
import * as v from 'validtyped';
16+
17+
const data: any = getData();
18+
19+
const validator = v.object({ a: v.string(), b: v.any() });
20+
21+
if (validator.isValid(data)) doThing(data); // typeof data => `{ a: string, b: any }`
22+
else throw new Error('oops!'); // typeof data => `any`
23+
```
1124

1225
### array
26+
Creates a `Validator` instance that matches on arrays of the given type.
27+
28+
| Param | Description |
29+
| --- | --- |
30+
| v | A `Validator` instance that specifies the shape of the array elements. |
31+
###### Example:
32+
```typescript
33+
import * as v from 'validtyped';
34+
35+
const validator = v.array( v.number() );
36+
const data: any = getData();
37+
38+
if (validator.isValid(data)) doThing(data); // typeof data = `number[]`;
39+
else throw new Error('oops!'); // typeof data = `any`
40+
```
1341

1442
### boolean
43+
Creates a validator instance that is true when the given type is a boolean.
44+
###### Example:
45+
```typescript
46+
import * as v from 'validtyped';
47+
48+
const validator = v.boolean();
49+
const data: any = getData();
50+
51+
if (validator.isValid(data)) doThing(data); // typeof data => `boolean`
52+
else throw new Error('oops!'); // typeof data => `any`
53+
```
1554

1655
### intersect
56+
Creates a `Validator` instance that matches on _both_ of the given possible types.
57+
58+
| Param | Description |
59+
| --- | --- |
60+
| v1 | A `Validator` instance that the specified type must match. |
61+
| v2 | A `Validator` instance that the specified type must match. |
62+
###### Example:
63+
```typescript
64+
import * as v from 'validtyped';
65+
66+
const validator = v.intersect( v.object({ a: v.string() }), v.object({ b: v.number() }) );
67+
const data: any = getData();
68+
69+
if (validator.isValid(data)) doThing(data); // typeof data = `{ a: string, b: number }`
70+
else throw new Error('oops!'); // typeof data = `any`
71+
```
1772

1873
### nominal
74+
Creates a validator instance that passes type checking through to the given validator.
75+
Operates as an identity function for the runtime `Validator` type, but annotates the compile-time encapsulated `Validator<T>`
76+
with a nominal string.
77+
78+
This is useful for marking `id`s or other fields as being a _specific_ type that should not be edited.
79+
80+
| Param | Description |
81+
| --- | --- |
82+
| v | The validator instance that will be passed through for checking runtime correctness. |
83+
| s | The nominal type tag for compile-time types. |
84+
###### Example:
85+
```typescript
86+
import * as v from 'validtyped';
87+
88+
const validator = v.nominal(v.string(), 'id');
89+
const data: any = getData();
90+
91+
if (validator.isValid(data)) doThing(data); // typeof data => `Nominal<string, 'id'>`
92+
else throw new Error('oops!'); // typeof data => `any`
93+
94+
const id = 'user-id' as Nominal<string, 'id'>;
95+
const newId = id + '-new'; // typeof data => `string`. Since we've modified this type, it can no longer be an `id`.
96+
```
1997

2098
### number
99+
Creates a validator instance that is true when the given type is a number.
100+
###### Example:
101+
```typescript
102+
import * as v from 'validtyped';
103+
104+
const validator = v.number();
105+
const data: any = getData();
106+
107+
if (validator.isValid(data)) doThing(data); // typeof data => `number`
108+
else throw new Error('oops!'); // typeof data => `any`
109+
```
21110

22111
### object
112+
Creates a validator instance that validates the given data is an object, and every property of that object matches the given schemas.
113+
By default, all listed properties **are required**.
114+
By default, unlisted properties are also allowed.
115+
116+
| Param | Description |
117+
| --- | --- |
118+
| o | An object whose keys will be required keys of the valid type and whose properties are `Validator` instances matching the valid property's types. |
119+
| opts | [optional] An options object |
120+
| opts.optional | [optional] A list of keys that should be marked as optional in the valid type. |
121+
###### Example:
122+
```typescript
123+
import * as v from 'validtyped';
124+
125+
const validator = v.object({ a: v.string(), b: v.number() });
126+
const data: any = getData();
127+
128+
if (validator.isValid(data)) doThing(data); // typeof data = `{ a: string, b: number }`;
129+
else throw new Error('oops!'); // typeof data = `any`
130+
```
23131

24132
### partial
133+
Creates a `Validator` instance that matches on objects with no required keys, but mandated types for certain keys if they _do_ exist.
134+
135+
| Param | Description |
136+
| --- | --- |
137+
| v | A `Validator` instance that specifies an object type. |
138+
###### Example:
139+
```typescript
140+
import * as v from 'validtyped';
141+
142+
const validator = v.partial( v.object({
143+
a: v.string(),
144+
b: v.number(),
145+
}));
146+
const data: any = getData();
147+
148+
if (validator.isValid(data)) doThing(data); // typeof data = `{ a?: string, b?: number }`;
149+
else throw new Error('oops!'); // typeof data = `any`
150+
```
25151

26152
### record
153+
Creates a `Validator` instance that matches on objects with _any_ keys, and whose properties match the given `Validator`.
154+
155+
This is useful for "dictionary"-like objects, for instance a record of users by userId.
156+
157+
| Param | Description |
158+
| --- | --- |
159+
| types | A `Validator` instance that specifies the right-side types of the record. |
160+
###### Example:
161+
```typescript
162+
import * as v from 'validtyped';
163+
164+
const validator = v.record( v.number() );
165+
const data: any = getData();
166+
167+
if (validator.isValid(data)) doThing(data); // typeof data = `Record<string, number>`;
168+
else throw new Error('oops!'); // typeof data = `any`
169+
```
27170

28171
### string
29172
Creates a validator instance that is true when the given type is a string.
30173

31174
| Param | Description |
32175
| --- | --- |
33176
| union | [optional] an array of possible string literals that the valid data could take. |
177+
###### Example:
178+
```typescript
179+
import * as v from 'validtyped';
180+
181+
const validator = v.string();
182+
const data: any = getData();
183+
184+
if (validator.isValid(data)) doThing(data); // typeof data => `string`
185+
else throw new Error('oops!'); // typeof data => `any`
186+
```
34187

35188
### union
189+
Creates a `Validator` instance that matches on any one of the given list of possible types.
190+
191+
| Param | Description |
192+
| --- | --- |
193+
| v | A list of `Validator` instances that specify the possible types that the valid type could take. |
194+
###### Example:
195+
```typescript
196+
import * as v from 'validtyped';
197+
198+
const validator = v.union([ v.string(), v.number() ]);
199+
const data: any = getData();
200+
201+
if (validator.isValid(data)) doThing(data); // typeof data = `number | string`
202+
else throw new Error('oops!'); // typeof data = `any`
203+
```
36204

37205
### Validator
38-
A `Valdiator<T>` instance is an encapsulated pair of some TS type `T` and a corresponding JSON schema.
206+
A `Validator<T>` instance is an encapsulated pair of some TS type `T` and a corresponding JSON schema.
39207

40208
A `Validator` already knows how to map from `T` to its json schema: using a json schema validator.
41209
Additionally, a `Validator` can perform simple algebraic operations with other `Validator`s resulting in more complex validatable types.
42210

43211
A `Validator` will always maintain a valid json schema representation of the type, `T`, that it encapsulates.
44212
The underlying json schema can always be accessed with `getSchema()`.
45-
#### Example
213+
###### Example:
46214
```typescript
47215
const stringValidator = new Validator<string>({ type: 'string' });
48216
const numberValidator = new Validator<number>({ type: 'number' });
@@ -55,8 +223,8 @@ Creates a new validator that is true whenever the data matches `this` _and_ `v`.
55223

56224
| Param | Description |
57225
| --- | --- |
58-
| v | Another validator instance whose type will form an intersection with `this` encapsulated type. |
59-
#### Example
226+
| other | Another validator instance whose type will form an intersection with `this` encapsulated type. |
227+
###### Example:
60228
```typescript
61229
import * as v from 'validtyped';
62230

@@ -69,6 +237,13 @@ const and = v1.and(v2).and(v3); // { a: string, b: number, c: boolean }
69237

70238
### Validator.getSchema
71239
Returns the underlying JSON schema.
240+
###### Example:
241+
```typescript
242+
import * as v from 'validtyped';
243+
244+
const schema = v.string().getSchema();
245+
console.log(schema); // { type: 'string' }
246+
```
72247

73248
### Validator.isValid
74249
Predicate returning `true` if the given data matches the JSON schema.
@@ -77,13 +252,32 @@ Acts as a type guard for the encapsulated typescript type.
77252
| Param | Description |
78253
| --- | --- |
79254
| thing | Any data of unknown type which will be validated. |
255+
###### Example:
256+
```typescript
257+
import * as v from 'validtyped';
258+
259+
const userIdModel = v.nominal(v.string(), 'userId');
260+
const userModel = v.object({
261+
name: v.string(),
262+
id: userIdModel,
263+
});
264+
265+
const x: any = getUserData();
266+
if (userModel.isValid(x)) doThing(x);
267+
```
80268

81269
### Validator.or
82270
Creates a new validator that is true whenever the data matches `this` _or_ `v`.
83271

84272
| Param | Description |
85273
| --- | --- |
86-
| v | Another validator instance whose type will form a union with `this` encapsulated type. |
274+
| other | Another validator instance whose type will form a union with `this` encapsulated type. |
275+
###### Example:
276+
```typescript
277+
import * as v from 'validtyped';
278+
279+
const or = v.string().or(v.number()).or(v.boolean()); // string | number | boolean
280+
```
87281

88282
### Validator.setSchemaMetaData
89283
Add meta-data to the underlying JSON schema.
@@ -93,6 +287,13 @@ but supplies the `getSchema` method to have a complete JSON schema with meta-dat
93287
| Param | Description |
94288
| --- | --- |
95289
| meta | JSON schema meta-data (name, description, etc.) |
290+
###### Example:
291+
```typescript
292+
import * as v from 'validtyped';
293+
294+
const validator = v.string();
295+
validator.setSchemaMetaData({ name: 'string validator' });
296+
```
96297

97298
### Validator.validate
98299
Takes data of unknown type and returns a discriminated union with either the data as the valid type,
@@ -101,6 +302,25 @@ or an error object describing what part of the data did not match.
101302
| Param | Description |
102303
| --- | --- |
103304
| data | Any data of unknown type which will be validated. |
305+
###### Example:
306+
```typescript
307+
import * as v from 'validtyped';
308+
309+
const stringModel = v.string();
310+
311+
const result = stringModel.validate(22);
312+
if (result.valid) doThing(result.data);
313+
else logger.error(...result.errors);
314+
```
104315

105316
### ValidType
317+
Returns the encapsulated type of a `Validator` type.
318+
###### Example:
319+
```typescript
320+
import * as v from 'validtyped';
321+
322+
const validator = v.string();
323+
324+
type x = v.ValidType<typeof validator>; // `string`
325+
```
106326

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"types": "dist/src/index.d.ts",
77
"scripts": {
88
"doc": "ts-node scripts/generateDocumentation.ts > README.md",
9+
"commitDocs": "sh scripts/commitDocsIfChanged.sh",
910
"commitmsg": "commitlint -e $GIT_PARAMS",
1011
"lint": "tslint --config tslint.json --project . --format stylish",
1112
"test": "jest",
1213
"tsc": "tsc",
13-
"prepush": "npm run -s lint && npm test",
14+
"prepush": "npm run -s lint && npm test && npm run -s commitDocs",
1415
"release": "rm -rf dist && npm run tsc && npx semantic-release"
1516
},
1617
"jest": {

scripts/commitDocsIfChanged.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
npm run doc
2+
CHANGED=$(git status 'README.md' --porcelain)
3+
if [ -n "${CHANGED}" ]; then
4+
git add README.md
5+
git commit -m "docs: generate documentation"
6+
fi

scripts/generateDocumentation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const generateMarkdown = (fileName: string) => {
120120
: '';
121121

122122
const example = typeInfo.example
123-
? `#### Example\n ${typeInfo.example}`
123+
? `###### Example:\n ${typeInfo.example}`
124124
: '';
125125

126126
const parts = [

0 commit comments

Comments
 (0)