Skip to content

Commit 4d9cf46

Browse files
committed
docs: better docs
1 parent 81c93cf commit 4d9cf46

2 files changed

Lines changed: 59 additions & 78 deletions

File tree

README.md

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
# Tiny Schema Validator
22

3-
small, practical, and type-safe Schema validator.
3+
JSON schema validator with great type inference.
44

55
[![GitHub license](https://img.shields.io/github/license/5alidz/tiny-schema-validator)](https://github.com/5alidz/tiny-schema-validator/blob/master/LICENSE) ![Minzipped size](https://img.shields.io/bundlephobia/minzip/tiny-schema-validator.svg)
66

7-
## History
8-
9-
This started as a side-project for me to learn about advanced TypeScript topics and was never intended to be an npm package,
10-
but I liked how it turned up and decided that it might be useful to use in my future projects.
11-
127
## Installation
138

149
```sh
15-
# npm
1610
npm install tiny-schema-validator
17-
18-
#yarn
11+
# or
1912
yarn add tiny-schema-validator
2013
```
2114

@@ -27,13 +20,21 @@ yarn add tiny-schema-validator
2720
import { createSchema, _ } from 'tiny-schema-validator';
2821

2922
export const Person = createSchema({
30-
name: _.string({ maxLength: [100, 'too-long'], minLength: [2, 'too-short'] }),
31-
age: _.number({ max: [150, 'too-old'], min: [13, 'too-young']}),
32-
email: _.string({ pattern: [/^[^@]+@[^@]+\.[^@]+$/, 'invalid-email']});
23+
name: _.string({
24+
maxLength: [100, 'too-long'],
25+
minLength: [2, 'too-short'],
26+
}),
27+
age: _.number({
28+
max: [150, 'too-old'],
29+
min: [13, 'too-young'],
30+
}),
31+
email: _.string({
32+
pattern: [/^[^@]+@[^@]+\.[^@]+$/, 'invalid-email'],
33+
});
3334
});
3435
```
3536

36-
and in TypeScript everything is the same, but to get the data type inferred from the schema you can do this:
37+
and in TypeScript, everything is the same, but to get the data type inferred from the schema, you can do this:
3738

3839
```ts
3940
// PersonType { name: string; age: number; email: string; }
@@ -87,45 +88,45 @@ validators.string(); // creates a string validator
8788

8889
Check out the full validators API below:
8990

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

123124
### Custom validators
124125

125-
To create custom validators that does not break type inference:
126+
To create custom validators that do not break type inference:
126127

127128
- use validators from `_` as building blocks for your custom validator.
128-
- your custom validator should define an `optional` and `required` functions.
129+
- your custom validator should define `optional` and `required` functions.
129130

130131
Example of creating custom validators:
131132

@@ -149,12 +150,12 @@ const Person = createSchema({
149150

150151
## Advanced usage
151152

152-
In addition to validating data, you can also reuse your schema in other areas, like creating forms UI for example,
153+
In addition to validating data, you can also reuse your schema in other areas, like creating forms UI.
153154
`traverse` function come-in handy to help you achieve that.
154155

155156
### Example
156157

157-
In this example, we will transform a schema to create meta-data to be used to create form UI elements.
158+
In this example, we will transform a schema to create meta-data to create form UI elements.
158159

159160
```js
160161
const User = createSchema({
@@ -190,12 +191,11 @@ console.log(form_ui); /*
190191

191192
### How to traverse
192193

193-
The return type of your visitor is important, and there are a few considerations:
194-
195-
Returning `null` from visitor signals to ignore this node from the result, with the exception:
196-
`record | recordof | list | listof`, returning `null` signals to continue down recursively.
194+
there are a few considerations when defining `visitor` object:
197195

198-
So to return something from `record` visitor for example, you will need to visit its children recursively.
196+
- In `string | number | boolean` visitors, returning `null` signals to ignore this node.
197+
- In `record | recordof | list | listof` visitors, returning `null` signals to visit its children.
198+
- To skip over `record | recordof | list | listof` nodes, return `{}` (empty object).
199199

200200
Continuing from the previous `User` Example
201201

@@ -251,4 +251,4 @@ list.validate({ list: ['string', 42, 'string'] }); // { list: { 1: 'invalid-type
251251

252252
## Recursive types
253253

254-
Currently there's no easy way to create recursive types, if you think you could help, PRs are welcome
254+
Currently, there's no easy way to create recursive types. if you think you could help, PRs are welcome

test/index.test.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const Person = createSchema({
5151
// type IPerson = ReturnType<typeof Person['produce']>;
5252

5353
describe('validate', () => {
54-
test('test ignores optional properties when not found', () => {
54+
test('ignores optional properties when not found', () => {
5555
const errors = Person.validate({
5656
is_verified: true,
5757
name: 'abc',
@@ -63,30 +63,17 @@ describe('validate', () => {
6363
},
6464
});
6565

66-
/*
67-
if (errors) {
68-
errors.age;
69-
errors.email;
70-
errors.four_tags;
71-
errors.friends;
72-
errors.is_premium;
73-
errors.is_verified;
74-
errors.meta;
75-
errors.name;
76-
errors.tags;
77-
errors.nested_list;
78-
}
79-
*/
80-
8166
expect(errors).toBe(null);
8267
});
68+
8369
test('validates optional properties when found', () => {
8470
const errors = Person.validate({
8571
is_premium: 'hello world',
8672
is_verified: true,
8773
name: 'abc',
8874
age: 42,
8975
76+
tags: {},
9077
meta: {
9178
id: '123',
9279
created: Date.now(),
@@ -95,6 +82,7 @@ describe('validate', () => {
9582
});
9683
expect(errors).toStrictEqual({
9784
is_premium: 'invalid-type',
85+
tags: 'invalid-type',
9886
meta: {
9987
updated: 'invalid-type',
10088
},
@@ -109,13 +97,6 @@ describe('validate', () => {
10997
true
11098
);
11199
expect(errors).toStrictEqual({ is_premium: 'invalid-type' });
112-
const errors2 = Person.validate(
113-
{
114-
is_premium: 42,
115-
},
116-
true
117-
);
118-
expect(errors2).toStrictEqual({ is_premium: 'invalid-type' });
119100
});
120101
// test('handles eager validation correctly', () => {
121102
// expect(Person.validate({}, true)).toStrictEqual({ name: TYPEERR });

0 commit comments

Comments
 (0)