Skip to content

Commit ca7bcf0

Browse files
authored
docs: Update documentation (#378)
1 parent dca5b3b commit ca7bcf0

File tree

6 files changed

+171
-78
lines changed

6 files changed

+171
-78
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ packages/
3131
│ ├── src/ # Vest's prebuilt source code.
3232
│ │ └── core/ # Modules required for vest's functionality.
3333
│ │ │ └── Context/ # Vest's shared runtime. Used across the whole library.
34-
│ │ │ └── createSuite/ # Initializes the suite and creates a context and state.
3534
│ │ │ └── produce/ # Generates the out put object and callbaks.
3635
│ │ │ └── state/ # Vest's persistent state. Used across the whole library.
37-
│ │ │ └── suiteResult/ # The output object is generated here.
3836
│ │ │ └── test/ # Contains the test function and its lifecycle.
39-
│ │ │ └── validate/ # Creates and runs a stateless suite.
37+
│ │ │ └── suite/ # Contains all the suite modules and methods
38+
│ │ │ └── create/ # Initializes the suite and creates a context and state.
39+
│ │ │ └── validate/ # Creates and runs a stateless suite.
4040
│ │ └── hooks/ # Functions that extend vest's functionality. They all use Context.
4141
│ │ │ └── draft/ # Allows access to the intermediate test result.
4242
│ │ │ └── exclusive/ # Allows including or excluding fields in runtime.
@@ -45,6 +45,12 @@ packages/
4545
│ │ │ └── group/ # Adds another nesting level within suites.
4646
│ │ └── lib/ # Shared helper functions.
4747
│ │ └── testUtils/ # Test helper functions.
48+
│ │ └── typings/ # Contains typescript declaration files for the exported modules.
49+
│ │ └── utilities/ # Single file exported modules.
50+
│ │ └── any/
51+
│ │ └── enforceExtended/
52+
│ │ └── classNames/
53+
│ │ └── promisify/
4854
├── eslint-plugin-vest/ # Eslint plugin with vest specific rules
4955
│ ├── lib/ # Contains all rules
5056
├── n4s/ # Assertion library used by vest

packages/n4s/docs/custom.md

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,54 @@
11
# Custom enforce rules
22

3-
To make it easier to reuse validations across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".
3+
To make it easier to reuse logic across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".
44

5-
Your custom rules are essentially a single javascript object containing your rules.
5+
Rules are called with the argument passed to enforce(x) followed by the arguments passed to `.yourRule(y, z)`.
66

77
```js
8-
const myCustomRules = {
9-
isValidEmail: value => value.indexOf('@') > -1,
10-
hasKey: (value, { key }) => value.hasOwnProperty(key),
11-
passwordsMatch: (passConfirm, options) =>
12-
passConfirm === options.passConfirm && options.passIsValid,
13-
};
8+
enforce.extend({
9+
yourRule(x, y, z) {
10+
return {
11+
pass: true,
12+
message: () => '',
13+
};
14+
},
15+
});
1416
```
1517

16-
Just like the predefined rules, your custom rules can accepts two parameters:
17-
18-
- `value` The actual value you are testing against.
19-
- `args` (optional) the arguments which you pass on when running your tests.
20-
21-
You can extend enforce with your custom rules by creating a new instance of `Enforce` and adding the rules object as the argument.
22-
2318
```js
24-
import enforce from 'n4s';
19+
import { enforce } from 'n4s';
2520

26-
const myCustomRules = {
21+
enforce.extend({
2722
isValidEmail: value => value.indexOf('@') > -1,
2823
hasKey: (value, key) => value.hasOwnProperty(key),
2924
passwordsMatch: (passConfirm, options) =>
3025
passConfirm === options.passConfirm && options.passIsValid,
31-
};
32-
33-
enforce.extend(myCustomRules);
26+
});
3427

3528
enforce(user.email).isValidEmail();
3629
```
30+
31+
## Custom rules return value
32+
33+
Rules can either return boolean indicating success or failure, or an object with two keys. `pass` indicates whether the validation is successful or not, and message provides a function with no arguments that return an error message in case of failure. Thus, when pass is false, message should return the error message for when enforce(x).yourRule() fails.
34+
35+
```js
36+
enforce.extend({
37+
isWithinRange(received, floor, ceiling) {
38+
const pass = received >= floor && received <= ceiling;
39+
if (pass) {
40+
return {
41+
message: () =>
42+
`expected ${received} not to be within range ${floor} - ${ceiling}`,
43+
pass: true,
44+
};
45+
} else {
46+
return {
47+
message: () =>
48+
`expected ${received} to be within range ${floor} - ${ceiling}`,
49+
pass: false,
50+
};
51+
}
52+
},
53+
});
54+
```

packages/vest/docs/enforce.md

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# Enforce
2+
23
For assertions, Vest is bundled with [Enforce](https://npmjs.com/package/n4s). Enforce is a validation assertion library. It allows you to run your data against rules and conditions and test whether it passes your validations. It is intended for validation logic that gets repeated over and over again and should not be written manually. It comes with a wide variety of pre-built rules, but it can also be extended to support your own repeated custom logic.
34

45
The way Enforce operates is similar to most common assertion libraries. You pass it a value, and one or more rules to test your value against - if the validation fails, it throws an Error, otherwise - it will move on to the next rule in the chain.
56

67
```js
7-
import { enforce } from 'vest'
8+
import { enforce } from 'vest';
89

9-
enforce(4)
10-
.isNumber();
10+
enforce(4).isNumber();
1111
// passes
1212

13-
enforce(4)
14-
.isNumber()
15-
.greaterThan(2);
13+
enforce(4).isNumber().greaterThan(2);
1614
// passes
1715

1816
enforce(4)
19-
.lessThan(2) // throws an error, will not carry on to the next rule
20-
.greaterThan(3);
17+
.lessThan(2) // throws an error, will not carry on to the next rule
18+
.greaterThan(3);
2119
```
2220

2321
## Content
22+
2423
- [List of Enforce rules](#list-of-enforce-rules) - All default enforce rules
2524
- [Custom Enforce Rules](#custom-enforce-rules) - How to extend enforce with your rules
2625
- [Business Related Rules](#business-related-rules) - Using more rules such as isEmail and isCreditCard
@@ -996,38 +995,61 @@ enforce([0]).isEven();
996995
// throws
997996
```
998997

999-
1000998
# Custom enforce rules
999+
10011000
To make it easier to reuse logic across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".
10021001

1003-
Your custom rules are essentially a single javascript object containing your rules.
1002+
Rules are called with the argument passed to enforce(x) followed by the arguments passed to `.yourRule(y, z)`.
1003+
10041004
```js
1005-
const myCustomRules = {
1006-
isValidEmail: (value) => value.indexOf('@') > -1,
1007-
hasKey: (value, {key}) => value.hasOwnProperty(key),
1008-
passwordsMatch: (passConfirm, options) => passConfirm === options.passConfirm && options.passIsValid
1009-
}
1005+
enforce.extend({
1006+
yourRule(x, y, z) {
1007+
return {
1008+
pass: true,
1009+
message: () => '',
1010+
};
1011+
},
1012+
});
10101013
```
1011-
Just like the predefined rules, your custom rules can accepts two parameters:
1012-
* `value` The actual value you are testing against.
1013-
* `args` (optional) the arguments which you pass on when running your tests.
1014-
1015-
You can extend enforce with your custom rules by creating a new instance of `Enforce` and adding the rules object as the argument.
10161014

10171015
```js
10181016
import { enforce } from 'vest';
10191017

1020-
const myCustomRules = {
1021-
isValidEmail: (value) => value.indexOf('@') > -1,
1022-
hasKey: (value, key) => value.hasOwnProperty(key),
1023-
passwordsMatch: (passConfirm, options) => passConfirm === options.passConfirm && options.passIsValid
1024-
}
1025-
1026-
enforce.extend(myCustomRules);
1018+
enforce.extend({
1019+
isValidEmail: value => value.indexOf('@') > -1,
1020+
hasKey: (value, key) => value.hasOwnProperty(key),
1021+
passwordsMatch: (passConfirm, options) =>
1022+
passConfirm === options.passConfirm && options.passIsValid,
1023+
});
10271024

10281025
enforce(user.email).isValidEmail();
10291026
```
10301027

1028+
## Custom rules return value
1029+
1030+
Rules can either return boolean indicating success or failure, or an object with two keys. `pass` indicates whether the validation is successful or not, and message provides a function with no arguments that return an error message in case of failure. Thus, when pass is false, message should return the error message for when enforce(x).yourRule() fails.
1031+
1032+
```js
1033+
enforce.extend({
1034+
isWithinRange(received, floor, ceiling) {
1035+
const pass = received >= floor && received <= ceiling;
1036+
if (pass) {
1037+
return {
1038+
message: () =>
1039+
`expected ${received} not to be within range ${floor} - ${ceiling}`,
1040+
pass: true,
1041+
};
1042+
} else {
1043+
return {
1044+
message: () =>
1045+
`expected ${received} to be within range ${floor} - ${ceiling}`,
1046+
pass: false,
1047+
};
1048+
}
1049+
},
1050+
});
1051+
```
1052+
10311053
# Business Related Rules
10321054

10331055
Along with the existing rules, you might need different business related rules, for email, phone number, credit card validations, and more.
@@ -1073,6 +1095,7 @@ import enforce, {
10731095
To read the full documentation on these rules and the options they take, please visit [validator.js](https://github.com/validatorjs/validator.js).
10741096

10751097
### validator.js license:
1098+
10761099
```
10771100
Copyright (c) 2018 Chris O'Hara <[email protected]>
10781101

packages/vest/docs/enforce.md.bak

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# Enforce
2+
23
For assertions, Vest is bundled with [Enforce](https://npmjs.com/package/n4s). Enforce is a validation assertion library. It allows you to run your data against rules and conditions and test whether it passes your validations. It is intended for validation logic that gets repeated over and over again and should not be written manually. It comes with a wide variety of pre-built rules, but it can also be extended to support your own repeated custom logic.
34

45
The way Enforce operates is similar to most common assertion libraries. You pass it a value, and one or more rules to test your value against - if the validation fails, it throws an Error, otherwise - it will move on to the next rule in the chain.
56

67
```js
7-
import { enforce } from 'vest'
8+
import { enforce } from 'vest';
89

9-
enforce(4)
10-
.isNumber();
10+
enforce(4).isNumber();
1111
// passes
1212

13-
enforce(4)
14-
.isNumber()
15-
.greaterThan(2);
13+
enforce(4).isNumber().greaterThan(2);
1614
// passes
1715

1816
enforce(4)
19-
.lessThan(2) // throws an error, will not carry on to the next rule
20-
.greaterThan(3);
17+
.lessThan(2) // throws an error, will not carry on to the next rule
18+
.greaterThan(3);
2119
```
2220

2321
## Content
22+
2423
- [List of Enforce rules](#list-of-enforce-rules) - All default enforce rules
2524
- [Custom Enforce Rules](#custom-enforce-rules) - How to extend enforce with your rules
2625
- [Business Related Rules](#business-related-rules) - Using more rules such as isEmail and isCreditCard
@@ -30,36 +29,60 @@ Enforce exposes all predefined and custom rules. You may use chaining to make mu
3029
{{LIST_OF_ENFORCE_RULES}}
3130

3231
# Custom enforce rules
32+
3333
To make it easier to reuse logic across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".
3434

35-
Your custom rules are essentially a single javascript object containing your rules.
35+
Rules are called with the argument passed to enforce(x) followed by the arguments passed to `.yourRule(y, z)`.
36+
3637
```js
37-
const myCustomRules = {
38-
isValidEmail: (value) => value.indexOf('@') > -1,
39-
hasKey: (value, {key}) => value.hasOwnProperty(key),
40-
passwordsMatch: (passConfirm, options) => passConfirm === options.passConfirm && options.passIsValid
41-
}
38+
enforce.extend({
39+
yourRule(x, y, z) {
40+
return {
41+
pass: true,
42+
message: () => '',
43+
};
44+
},
45+
});
4246
```
43-
Just like the predefined rules, your custom rules can accepts two parameters:
44-
* `value` The actual value you are testing against.
45-
* `args` (optional) the arguments which you pass on when running your tests.
46-
47-
You can extend enforce with your custom rules by creating a new instance of `Enforce` and adding the rules object as the argument.
4847

4948
```js
5049
import { enforce } from 'vest';
5150

52-
const myCustomRules = {
53-
isValidEmail: (value) => value.indexOf('@') > -1,
54-
hasKey: (value, key) => value.hasOwnProperty(key),
55-
passwordsMatch: (passConfirm, options) => passConfirm === options.passConfirm && options.passIsValid
56-
}
57-
58-
enforce.extend(myCustomRules);
51+
enforce.extend({
52+
isValidEmail: value => value.indexOf('@') > -1,
53+
hasKey: (value, key) => value.hasOwnProperty(key),
54+
passwordsMatch: (passConfirm, options) =>
55+
passConfirm === options.passConfirm && options.passIsValid,
56+
});
5957

6058
enforce(user.email).isValidEmail();
6159
```
6260

61+
## Custom rules return value
62+
63+
Rules can either return boolean indicating success or failure, or an object with two keys. `pass` indicates whether the validation is successful or not, and message provides a function with no arguments that return an error message in case of failure. Thus, when pass is false, message should return the error message for when enforce(x).yourRule() fails.
64+
65+
```js
66+
enforce.extend({
67+
isWithinRange(received, floor, ceiling) {
68+
const pass = received >= floor && received <= ceiling;
69+
if (pass) {
70+
return {
71+
message: () =>
72+
`expected ${received} not to be within range ${floor} - ${ceiling}`,
73+
pass: true,
74+
};
75+
} else {
76+
return {
77+
message: () =>
78+
`expected ${received} to be within range ${floor} - ${ceiling}`,
79+
pass: false,
80+
};
81+
}
82+
},
83+
});
84+
```
85+
6386
# Business Related Rules
6487

6588
Along with the existing rules, you might need different business related rules, for email, phone number, credit card validations, and more.
@@ -105,6 +128,7 @@ import enforce, {
105128
To read the full documentation on these rules and the options they take, please visit [validator.js](https://github.com/validatorjs/validator.js).
106129

107130
### validator.js license:
131+
108132
```
109133
Copyright (c) 2018 Chris O'Hara <[email protected]>
110134

packages/vest/docs/result.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ A result object would look somewhat like this:
2727
}
2828
```
2929

30-
## Accessing the last result object with `vest.get`
30+
## Accessing the last result object with `.get`
3131

32-
Alternatively, if you need to access your validation results out of context - for example, from a different UI component or function, you can use `vest.get`.
32+
If you need to access your validation results out of context - for example, from a different UI component or function, you can use `vest.get`
3333

3434
Vest exposes the `vest.get` function that is able to retrieve the most recent validation result of [**stateful**](./state) suites (suites created using vest.create()).
3535

@@ -45,6 +45,16 @@ const res = vest.get('suite_name');
4545
res.hasErrors('fieldName');
4646
```
4747

48+
Alternatively, you can access your suite's state from the `get` property on your suite function:
49+
50+
```js
51+
const v = vest.create('my_form', () => {
52+
/*...*/
53+
});
54+
55+
v.get(); // -> returns the same value as vest.get('my_form');
56+
```
57+
4858
# Result Object Methods:
4959

5060
Along with these values, the result object exposes the following methods:

packages/vest/docs/state.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,15 @@ validate(); // validation result is added to the state.
4848

4949
vest.reset('suite_name'); // validation result is removed
5050
```
51+
52+
Alternatively, if you have access to your suite, you can call `suite.reset` directly without providing the suite name:
53+
54+
```js
55+
import vest from 'vest';
56+
57+
const v = vest.create('suite_name', () => {
58+
// Your tests go here
59+
});
60+
61+
v.reset(); // validation result is removed from Vest's state.
62+
```

0 commit comments

Comments
 (0)