check-rule-mate 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,11 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
A lightweight and reusable JavaScript "library" for data validation. This library was
|
|
5
|
+
A lightweight and reusable JavaScript "library" for data validation. This library was designed to simplify the process of validating form inputs using flexible rules and error handling. Validating your data by allowing you to define flexible rules, custom error messages, and reusable helper functions—all in a structured format.
|
|
6
6
|
|
|
7
7
|
The core goal is to provide a **reusable and easy-to-extend** for handling various form inputs, including fields like name, email, birthdate, phone number, and more.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
**Github repository:** [check-rule-mate repository](https://github.com/johnrock16/check-rule-mate)
|
|
11
|
+
|
|
12
|
+
**Examples of how implement check-rule-mate:** [check-rule-mate examples](#Example-Usage)
|
|
13
|
+
|
|
14
|
+
Test the core functionalities here: [check-rule-mate demo](https://johnrock16.github.io/check-rule-mate/)
|
|
10
15
|
(Note: Creating or modifying custom validators is not supported in the demo, as it requires JavaScript implementation.)
|
|
11
16
|
|
|
12
17
|
|
|
@@ -23,6 +28,7 @@ Test the core functionalities here: [check-rule-mate Demo](https://johnrock16.gi
|
|
|
23
28
|
- **Modifiers:** Extend rules for specific use cases (e.g., age validation in a date rule).
|
|
24
29
|
- **Dynamic Parameters:** Use $variable to access field data within rules.
|
|
25
30
|
- **Modular Rules and Validators:** Create multiple files for rules and helpers, organizing them by context or form.
|
|
31
|
+
- **Async Validations:** You could create async functions to validate some data. Do You need to use a fetch or wait a promise to be resolved? No problems.
|
|
26
32
|
|
|
27
33
|
## Table of Contents
|
|
28
34
|
|
|
@@ -68,23 +74,29 @@ Here’s an example of validating a set of fields:
|
|
|
68
74
|
const CONTACT_US = require('./dataValidator/rules/data/contactUs.json');
|
|
69
75
|
const MY_VALIDATION_ERROR_MESSAGES = require('./i18n/en_US/errors/myValidatorRules.json');
|
|
70
76
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
77
|
+
async function runDataValidate() {
|
|
78
|
+
const fields = {
|
|
79
|
+
name: "John",
|
|
80
|
+
lastName: "Doe",
|
|
81
|
+
email: "email@email.com",
|
|
82
|
+
emailConfirm: "email@email.com",
|
|
83
|
+
phone: "",
|
|
84
|
+
subject: "I need a coffee",
|
|
85
|
+
message: "Give me coffee"
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// This should return { ok: true }
|
|
89
|
+
const result = await dataValidate(fields, {
|
|
90
|
+
validationHelpers: myValidator,
|
|
91
|
+
rules: MY_RULES,
|
|
92
|
+
dataRule: CONTACT_US,
|
|
93
|
+
dataErrorMessages: MY_VALIDATION_ERROR_MESSAGES,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
console.log(result);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
runDataValidate();
|
|
88
100
|
```
|
|
89
101
|
|
|
90
102
|
#### Parameters for dataValidate:
|
|
@@ -184,7 +196,25 @@ const myValidator = function (value, rule, modifier = null, data = null) {
|
|
|
184
196
|
return value === data[key];
|
|
185
197
|
}
|
|
186
198
|
|
|
187
|
-
|
|
199
|
+
async function isDataFetched() {
|
|
200
|
+
try {
|
|
201
|
+
let result = await new Promise((resolve, reject) => {
|
|
202
|
+
setTimeout(() => {
|
|
203
|
+
const success = !!value;
|
|
204
|
+
if (success) {
|
|
205
|
+
resolve('Data fetched successfully!');
|
|
206
|
+
} else {
|
|
207
|
+
reject('Error fetching data');
|
|
208
|
+
}
|
|
209
|
+
}, 2000);
|
|
210
|
+
});
|
|
211
|
+
return !!result;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return { regex, hasText, equals, isDataFetched };
|
|
188
218
|
};
|
|
189
219
|
```
|
|
190
220
|
|
|
@@ -198,13 +228,44 @@ Define custom error messages in a structured format:
|
|
|
198
228
|
}
|
|
199
229
|
```
|
|
200
230
|
|
|
201
|
-
|
|
231
|
+
## Example Usage
|
|
232
|
+
|
|
233
|
+
Explore examples in the examples folder folder. **Before execute any test change the type in package.json for module instead commonjs**
|
|
202
234
|
|
|
203
|
-
|
|
235
|
+
**Examples folder**: [Github repo examples folder.](https://github.com/johnrock16/check-rule-mate/tree/main/examples)
|
|
204
236
|
|
|
205
|
-
|
|
237
|
+
### Vanilla
|
|
206
238
|
Here you are free to test anything you want about form validation, also We have a lot of tests scenarios in __tests__ which could be a great start.
|
|
207
|
-
|
|
239
|
+
|
|
240
|
+
Command to run Vanilla example:
|
|
241
|
+
```bash
|
|
242
|
+
npm run example:vanilla
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
**Vanilla example**: [Github repo vanilla file.](https://github.com/johnrock16/check-rule-mate/blob/main/examples/vanilla/src/index.js)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
#### __tests__
|
|
249
|
+
|
|
250
|
+
Command to run tests:
|
|
251
|
+
```bash
|
|
252
|
+
npm run test
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**Unit tests examples:** [Github repo unit tests file.](https://github.com/johnrock16/check-rule-mate/blob/main/examples/vanilla/src/__tests__/dataValidator.test.js)
|
|
256
|
+
|
|
257
|
+
### Express:
|
|
208
258
|
See how the check-rule-mate works in back-end using a middleware.
|
|
209
|
-
|
|
259
|
+
|
|
260
|
+
Command to run Express example:
|
|
261
|
+
```bash
|
|
262
|
+
npm run example:express
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Express example:** [Github repo express file.](https://github.com/johnrock16/check-rule-mate/blob/main/examples/express/src/main.js)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
### Frontend:
|
|
210
269
|
Here you can found the DEMO page and it's a type of "playground" to test how RULES works and validations works. (Here you can't create customized javascript so custom validatorHelpers are disabled by default)
|
|
270
|
+
|
|
271
|
+
**Frontend example:** [check-rule-mate demo.](https://johnrock16.github.io/check-rule-mate/)
|