Skip to content

Commit 827d6ff

Browse files
committed
docs: add errors that users might encounter
1 parent c34d3ca commit 827d6ff

1 file changed

Lines changed: 70 additions & 31 deletions

File tree

README.md

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,38 +88,38 @@ validators.string(); // creates a string validator
8888

8989
Check out the full validators API below:
9090

91-
| validator | signature | props |
92-
| :-------- | ------------------------------- | :------------------------------------------------------------ |
93-
| string | `string(options?)` | options (optional): Object |
94-
| | | - `optional : boolean` defaults to false |
95-
| | | - `maxLength: [length: number, error: string]` |
96-
| | | - `minLength: [length: number, error: string]` |
97-
| | | - `pattern : [pattern: RegExp, error: string]` |
98-
| | | |
99-
| number | `number(options?)` | options(optional): Object |
100-
| | | - `optional: boolean` default to false |
101-
| | | - `min: [number, error: string]` |
102-
| | | - `max: [number, error: string]` |
91+
| validator | signature | props |
92+
| :-------- | ------------------------------- | :------------------------------------------------------------- |
93+
| string | `string(options?)` | options (optional): Object |
94+
| | | - `optional : boolean` defaults to false |
95+
| | | - `maxLength: [length: number, error: string]` |
96+
| | | - `minLength: [length: number, error: string]` |
97+
| | | - `pattern : [pattern: RegExp, error: string]` |
98+
| | | |
99+
| number | `number(options?)` | options(optional): Object |
100+
| | | - `optional: boolean` default to false |
101+
| | | - `min: [number, error: string]` |
102+
| | | - `max: [number, error: string]` |
103103
| | | - `is : ['integer' \| 'float', error: string]` default is both |
104-
| | | |
105-
| boolean | `boolean(options?)` | options(optional): Object |
106-
| | | - `optional: boolean` default to false |
107-
| | | |
108-
| list | `list(validators[], options?)` | validators: Array of validators |
109-
| | | options(optional): Object |
110-
| | | - `optional: boolean` default to false |
111-
| | | |
112-
| listof | `listof(validator, options?)` | validator: Validator |
113-
| | | options(optional): Object |
114-
| | | - `optional: boolean` default to false |
115-
| | | |
116-
| record | `record(shape, options?)` | shape: `Object { [key: string]: Validator }` |
117-
| | | options(optional): Object |
118-
| | | - `optional: boolean` default to false |
119-
| | | |
120-
| recordof | `recordof(validator, options?)` | validator: `Validator` |
121-
| | | options(optional): Object |
122-
| | | - `optional: boolean` default to false |
104+
| | | |
105+
| boolean | `boolean(options?)` | options(optional): Object |
106+
| | | - `optional: boolean` default to false |
107+
| | | |
108+
| list | `list(validators[], options?)` | validators: Array of validators |
109+
| | | options(optional): Object |
110+
| | | - `optional: boolean` default to false |
111+
| | | |
112+
| listof | `listof(validator, options?)` | validator: Validator |
113+
| | | options(optional): Object |
114+
| | | - `optional: boolean` default to false |
115+
| | | |
116+
| record | `record(shape, options?)` | shape: `Object { [key: string]: Validator }` |
117+
| | | options(optional): Object |
118+
| | | - `optional: boolean` default to false |
119+
| | | |
120+
| recordof | `recordof(validator, options?)` | validator: `Validator` |
121+
| | | options(optional): Object |
122+
| | | - `optional: boolean` default to false |
123123

124124
### Custom validators
125125

@@ -252,3 +252,42 @@ list.validate({ list: ['string', 42, 'string'] }); // { list: { 1: 'invalid-type
252252
## Recursive types
253253

254254
Currently, there's no easy way to create recursive types. if you think you could help, PRs are welcome
255+
256+
## Errors while wrapping schemas
257+
258+
if you try to wrap your schema, you will encounter this error (Type instantiation is excessively deep and possibly infinite)
259+
to fix it, you should unwrap your schema and re-create it inside your abstraction.
260+
let's take the following example:
261+
262+
```ts
263+
const User = createSchema({
264+
name: _.string(),
265+
age: _.number(),
266+
});
267+
268+
// your abstraction
269+
function schemaWrapper<T>(schema: T) {
270+
//...
271+
}
272+
273+
const wrappedUser = schemaWrapper(User); // ERROR: Type instantiation is excessively deep and possibly infinite
274+
```
275+
276+
The fix:
277+
278+
```ts
279+
import { Schema, R, RecordOptions } from 'tiny-schema-validator';
280+
/*
281+
optionally, to infer data from the embedded schema, you do DataFrom<T>
282+
283+
import { DataFrom } from 'tiny-schema-validator/dist/type-utils';
284+
*/
285+
286+
// extract schema with Schema.embed()
287+
function schemaWrapper<T extends Schema>(schema: R<RecordOptions<T>>) {
288+
const newSchema = createSchema(schema.shape); // you can add/remove/modify passed schema here
289+
// ...
290+
}
291+
292+
const wrappedUser = schemaWrapper(User.embed()); // all good no errors
293+
```

0 commit comments

Comments
 (0)