quival 0.1.0

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/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ insert_final_newline = true
7
+ indent_style = space
8
+ indent_size = 2
9
+ trim_trailing_whitespace = true
10
+
11
+ [*.md]
12
+ trim_trailing_whitespace = false
13
+ indent_size = 4
package/.eslintrc.cjs ADDED
@@ -0,0 +1,13 @@
1
+ module.exports = {
2
+ env: {
3
+ browser: true,
4
+ es2021: true,
5
+ },
6
+ extends: 'eslint:recommended',
7
+ overrides: [],
8
+ parserOptions: {
9
+ ecmaVersion: 'latest',
10
+ sourceType: 'module',
11
+ },
12
+ rules: {},
13
+ };
@@ -0,0 +1,5 @@
1
+ {
2
+ "printWidth": 160,
3
+ "tabWidth": 2,
4
+ "singleQuote": true
5
+ }
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Mohd Hafizuddin M Marzuki <hafizuddin_83@yahoo.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # Quival
2
+
3
+ ![npm](https://img.shields.io/npm/v/quival?style=flat-square)
4
+ ![npm](https://img.shields.io/npm/dt/quival?style=flat-square)
5
+ ![NPM](https://img.shields.io/npm/l/quival?style=flat-square)
6
+
7
+ This library provides the ability to perform data validation easily in JavaScript. It is heavily based on [Laravel Validation](https://laravel.com/docs/10.x/validation).
8
+
9
+ By sharing similar conventions, it is possible to reuse validation rules on both front and back ends. This library can be used for preliminary data validation on the front end, before submitting the data to Laravel-based app, where the data should be validated again.
10
+
11
+ Why is there a need to perform validation on the front end? Early data validation allows an app to show errors to users immediately, before the data is even submitted. Live feedback is beneficial and can improve user experience.
12
+
13
+ This library is intended to be used in the browser environment, and it was not tested in other enviroments such as Node.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install quival
19
+ ```
20
+
21
+ ## Features
22
+
23
+ - Provide similar conventions to Laravel Validation
24
+ - Implement most of the rules listed [here](https://laravel.com/docs/10.x/validation#available-validation-rules)
25
+
26
+ ## Example
27
+
28
+ The code snippet below demonstrates the usage of `Validator` class.
29
+
30
+ ```js
31
+ import { Validator } from 'quival';
32
+ import enLocale from 'quival/src/locales/en.js';
33
+
34
+ // Set localization messages
35
+ Validator.setLocale('en');
36
+ Validator.setMessages('en', enLocale);
37
+
38
+ // Register custom checker
39
+ Validator.addChecker('phone_number', (attribute, value, parameters) => {
40
+ return !/[^0-9\+\-\s]/.test(value);
41
+ }, 'The :attribute field must be a valid phone number.');
42
+
43
+ // Prepare arguments
44
+ const data = {
45
+ username: 'idea💡',
46
+ name: '',
47
+ email: 'test',
48
+ phone_number: 'test',
49
+ letters: ['a', 'b', 'B', 'a'],
50
+ items: {
51
+ x: 'a',
52
+ y: null,
53
+ z: 12,
54
+ },
55
+ payment_type: 'cc',
56
+ card_number: '',
57
+ };
58
+
59
+ const rules = {
60
+ username: ['required', 'ascii', 'min:3'],
61
+ name: ['required', 'min:3'],
62
+ email: ['required', 'email'],
63
+ phone_number: ['required', 'phone_number'],
64
+ 'letters.*': ['distinct:ignore_case'],
65
+ items: ['array', 'size:5'],
66
+ 'items.*': ['required', 'string'],
67
+ payment_type: ['required', 'in:cc,paypal'],
68
+ cc_number: ['required_if:payment_type,cc'],
69
+ };
70
+
71
+ const customMessages = {
72
+ 'items.size': 'The size of the array must be equal to :size items only.',
73
+ };
74
+
75
+ const customAttributes = {
76
+ cc_number: 'Credit Card Number',
77
+ };
78
+
79
+ const customValues = {
80
+ payment_type: {
81
+ cc: 'credit card',
82
+ },
83
+ };
84
+
85
+ // Create Validator instance
86
+ const validator = new Validator(data, rules, customMessages, customAttributes, customValues);
87
+
88
+ // Perform validation
89
+ validator
90
+ .validate()
91
+ .then(() => {
92
+ console.log('Successful!');
93
+ })
94
+ .catch((messages) => {
95
+ if (messages instanceof Error) {
96
+ throw messages;
97
+ }
98
+
99
+ console.log(messages);
100
+ });
101
+ ```
102
+
103
+ The produced error messages for the code snippet above.
104
+
105
+ ```json
106
+ {
107
+ "username": [
108
+ "The username field must only contain single-byte alphanumeric characters and symbols."
109
+ ],
110
+ "name": [
111
+ "The name field is required."
112
+ ],
113
+ "email": [
114
+ "The email field must be a valid email address."
115
+ ],
116
+ "phone_number": [
117
+ "The phone number field must be a valid phone number."
118
+ ],
119
+ "letters.0": [
120
+ "The letters.0 field has a duplicate value."
121
+ ],
122
+ "letters.1": [
123
+ "The letters.1 field has a duplicate value."
124
+ ],
125
+ "letters.2": [
126
+ "The letters.2 field has a duplicate value."
127
+ ],
128
+ "letters.3": [
129
+ "The letters.3 field has a duplicate value."
130
+ ],
131
+ "items": [
132
+ "The size of the array must be equal to 5 items only."
133
+ ],
134
+ "items.y": [
135
+ "The items.y field is required."
136
+ ],
137
+ "items.z": [
138
+ "The items.z field must be a string."
139
+ ],
140
+ "cc_number": [
141
+ "The Credit Card Number field is required when payment type is credit card."
142
+ ]
143
+ }
144
+ ```
145
+
146
+ ## Security Vulnerabilities
147
+
148
+ If you discover any security related issues, please email <hafizuddin_83@yahoo.com> instead of using the issue tracker. Please prefix the subject with `Quival:`.
149
+
150
+ ## Credits
151
+
152
+ - [Mohd Hafizuddin M Marzuki](https://github.com/apih)
153
+ - [All Contributors](../../contributors)
154
+
155
+ ## License
156
+
157
+ The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "quival",
3
+ "version": "0.1.0",
4
+ "description": "Data validation à la Laravel Validation",
5
+ "author": "Mohd Hafizuddin M Marzuki <hafizuddin_83@yahoo.com>",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./src/index.js",
9
+ "keywords": [
10
+ "data validation",
11
+ "form",
12
+ "front end",
13
+ "laravel",
14
+ "quick",
15
+ "validation",
16
+ "validator"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/apih/quival"
21
+ },
22
+ "devDependencies": {
23
+ "eslint": "^8.39.0",
24
+ "prettier": "^2.8.8"
25
+ },
26
+ "scripts": {}
27
+ }