@jdlien/validator 1.0.3 → 1.0.5
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/.prettierrc +8 -0
- package/README.md +46 -3
- package/demo-src.css +106 -0
- package/demo.html +779 -0
- package/index.ts +5 -0
- package/package.json +1 -10
- package/src/Validator.ts +628 -0
- package/src/types.d.ts +23 -0
- package/src/validator-utils.ts +656 -0
- package/tailwind.config.cjs +12 -0
- package/tests/Validator.test.ts +1922 -0
- package/tests/utils.test.ts +1048 -0
- package/tsconfig.json +19 -0
- package/vite.config.js +22 -0
- package/dist/validator.js +0 -1
package/.prettierrc
ADDED
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Validator
|
|
1
|
+
# Validator - HTML Form Validation Made Easy
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
@@ -162,7 +162,7 @@ You can customize the class(es) that Validator uses to hide the error messages b
|
|
|
162
162
|
|
|
163
163
|
## Color Picker Support
|
|
164
164
|
|
|
165
|
-
If you need to allow a user to pick a color, you can use data-type="color" and the input will be required to be any valid CSS color supported by the browser. This type can also work in conjunction with a native color input. If you do this, you will need to add an input with the name of the data-color input + `-color
|
|
165
|
+
If you need to allow a user to pick a color, you can use data-type="color" and the input will be required to be any valid CSS color supported by the browser. This type can also work in conjunction with a native color input. If you do this, you will need to add an input with `type="color"` and the name of the data-color input + `-color`. This should be inside a linked label which will become the color preview swatch. Such a label should have an ID of the color input's name + `-color-label` so that Validator can change the background to the specified color.
|
|
166
166
|
|
|
167
167
|
A basic example that would work:
|
|
168
168
|
|
|
@@ -241,6 +241,49 @@ const myValidator = new Validator(myForm, {
|
|
|
241
241
|
})
|
|
242
242
|
```
|
|
243
243
|
|
|
244
|
+
## Utility Functions
|
|
245
|
+
|
|
246
|
+
Validator includes several utility functions that may be useful in your own code, so they are exported as part of the module.
|
|
247
|
+
If you wish to use these, you may import the functions directly from the module as an object that contains all the functions:
|
|
248
|
+
|
|
249
|
+
```javascript
|
|
250
|
+
import { validatorUtils } from '@jdlien/validator'
|
|
251
|
+
// you could assign the functions you need to more convenient variables
|
|
252
|
+
const { dateFormat, formatDateTime } = validatorUtils
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Here is a list of the utility functions:
|
|
256
|
+
|
|
257
|
+
- **isFormControl**: Determines if an element is an HTML input, select, or textarea element.
|
|
258
|
+
- **isType**: Checks if an element has a type or data-type attribute matching one of the passed values.
|
|
259
|
+
- **momentToFPFormat**: Converts a moment.js-style format string to the flatpickr format.
|
|
260
|
+
- **monthToNumber**: Converts month string or number to a zero-based month number (January == 0).
|
|
261
|
+
- **yearToFull**: Converts a year string or number to a 4-digit year.
|
|
262
|
+
- **parseDate**: Parses a date string or Date object into a Date object.
|
|
263
|
+
- **parseTime**: Parses a time string into an object with hour, minute, and second properties.
|
|
264
|
+
- **parseTimeToString**: Parses a time string into a formatted string.
|
|
265
|
+
- **formatDateTime**: Formats a date string or Date object into a string with a specified format.
|
|
266
|
+
- **parseDateToString**: Parses a date string or Date object into a formatted string with the specified moment.js-style date format.
|
|
267
|
+
- **isDate**: Determines if a value is a valid date.
|
|
268
|
+
- **isDateInRange**: Determines if a date falls within a specified range (either past or future).
|
|
269
|
+
- **isTime**: Determines if a value is a valid time.
|
|
270
|
+
- **isEmail**: Determines if a value is a valid email address.
|
|
271
|
+
- **parseNANPTel**: Parses a North American phone number string into a standardized format.
|
|
272
|
+
- **isNANPTel**: Determines if a value is a valid North American phone number.
|
|
273
|
+
- **parseInteger**: Parses an integer string into a standardized format.
|
|
274
|
+
- **isNumber**: Determines if a value is a valid number.
|
|
275
|
+
- **parseNumber**: Parses a number string into a standardized format.
|
|
276
|
+
- **isInteger**: Determines if a value is a valid integer.
|
|
277
|
+
- **parseUrl**: Parses a URL string into a standardized format.
|
|
278
|
+
- **isUrl**: Determines if a value is a valid URL.
|
|
279
|
+
- **parseZip**: Parses a zip code string into a standardized format.
|
|
280
|
+
- **isZip**: Determines if a value is a valid zip code.
|
|
281
|
+
- **parsePostalCA**: Parses a Canadian postal code string into a standardized format.
|
|
282
|
+
- **isPostalCA**: Determines if a value is a valid Canadian postal code.
|
|
283
|
+
- **isColor**: Determines if a value is a valid color.
|
|
284
|
+
- **parseColor**: Parses a color string into a standardized format.
|
|
285
|
+
- **normalizeValidationResult**: Normalizes a validation result (like a boolean or string) into an object with a valid property and a messages array of strings.
|
|
286
|
+
|
|
244
287
|
## Contributing
|
|
245
288
|
|
|
246
289
|
Install dev dependencies:
|
|
@@ -249,7 +292,7 @@ Install dev dependencies:
|
|
|
249
292
|
npm install
|
|
250
293
|
```
|
|
251
294
|
|
|
252
|
-
|
|
295
|
+
When running Vite, you may get an error like
|
|
253
296
|
|
|
254
297
|
```
|
|
255
298
|
Module did not self-register: '...\node_modules\canvas\build\Release\canvas.node'
|
package/demo-src.css
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
@tailwind base;
|
|
2
|
+
@tailwind components;
|
|
3
|
+
@tailwind utilities;
|
|
4
|
+
|
|
5
|
+
@layer base {
|
|
6
|
+
[type='text'],
|
|
7
|
+
[type='email'],
|
|
8
|
+
[type='url'],
|
|
9
|
+
[type='password'],
|
|
10
|
+
[type='number'],
|
|
11
|
+
[type='date'],
|
|
12
|
+
[type='datetime-local'],
|
|
13
|
+
[type='month'],
|
|
14
|
+
[type='search'],
|
|
15
|
+
[type='tel'],
|
|
16
|
+
[type='time'],
|
|
17
|
+
[type='week'],
|
|
18
|
+
[type='color'],
|
|
19
|
+
[multiple],
|
|
20
|
+
textarea,
|
|
21
|
+
select {
|
|
22
|
+
@apply border border-zinc-300 rounded bg-zinc-50 px-2 py-1.5 text-zinc-700
|
|
23
|
+
placeholder-zinc-500 placeholder-opacity-50 shadow-inner
|
|
24
|
+
focus:bg-white focus:outline-none focus:ring-2 focus:ring-blue-400/50
|
|
25
|
+
focus:ring-offset-0 focus:ring-offset-transparent;
|
|
26
|
+
|
|
27
|
+
@apply dark:border-zinc-500 dark:bg-zinc-900 dark:text-white dark:focus:bg-zinc-800;
|
|
28
|
+
|
|
29
|
+
@apply disabled:bg-zinc-200 disabled:text-zinc-500 dark:disabled:bg-zinc-800 dark:disabled:text-zinc-300;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* Form input variants (do not apply when disabled) */
|
|
33
|
+
[type='text']:not([disabled]),
|
|
34
|
+
[type='email']:not([disabled]),
|
|
35
|
+
[type='url']:not([disabled]),
|
|
36
|
+
[type='password']:not([disabled]),
|
|
37
|
+
[type='number']:not([disabled]),
|
|
38
|
+
[type='date']:not([disabled]),
|
|
39
|
+
[type='datetime-local']:not([disabled]),
|
|
40
|
+
[type='month']:not([disabled]),
|
|
41
|
+
[type='search']:not([disabled]),
|
|
42
|
+
[type='tel']:not([disabled]),
|
|
43
|
+
[type='time']:not([disabled]),
|
|
44
|
+
[type='week']:not([disabled]),
|
|
45
|
+
[type='color']:not([disabled]),
|
|
46
|
+
[multiple]:not([disabled]),
|
|
47
|
+
textarea:not([disabled]),
|
|
48
|
+
select:not([disabled]) {
|
|
49
|
+
@apply hover:bg-white dark:hover:bg-zinc-800;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
[type='checkbox'],
|
|
53
|
+
[type='radio'] {
|
|
54
|
+
@apply border border-zinc-300 bg-zinc-50 text-blue-600 shadow-sm
|
|
55
|
+
checked:border checked:border-zinc-400 checked:bg-blue-600 focus:outline-none
|
|
56
|
+
focus:ring-2 focus:ring-blue-400/50 focus:ring-offset-0 focus:ring-offset-transparent focus:checked:bg-blue-500;
|
|
57
|
+
|
|
58
|
+
@apply dark:border-zinc-500 dark:bg-zinc-400 dark:checked:bg-blue-600 dark:focus:checked:bg-blue-400;
|
|
59
|
+
|
|
60
|
+
@apply disabled:opacity-60 disabled:checked:hover:bg-blue-600;
|
|
61
|
+
|
|
62
|
+
/* Prevents border change when disabled and hovered */
|
|
63
|
+
@apply disabled:checked:hover:border-zinc-400 disabled:dark:checked:bg-blue-600 dark:disabled:checked:hover:border-zinc-500;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
[type='checkbox']:not([disabled]),
|
|
67
|
+
[type='radio']:not([disabled]) {
|
|
68
|
+
@apply hover:bg-zinc-200 checked:hover:bg-blue-500
|
|
69
|
+
dark:hover:bg-zinc-300 dark:hover:checked:bg-blue-400 dark:hover:focus:checked:bg-blue-400;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
[type='radio'] {
|
|
73
|
+
@apply rounded-full;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
select {
|
|
77
|
+
@apply pr-6;
|
|
78
|
+
/* background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='rgb(120 113 108)' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e") !important; */
|
|
79
|
+
background-image: url('data:image/svg+xml,%3Csvg xmlns=%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22 fill=%22none%22 viewBox=%220 0 24 24%22 stroke-width=%221.5%22 stroke=%22rgb(120 113 108)%22%3E%3Cpath stroke-linecap=%22round%22 stroke-linejoin=%22round%22 d=%22M19.5 8.25l-7.5 7.5-7.5-7.5%22 %2F%3E%3C%2Fsvg%3E') !important;
|
|
80
|
+
background-size: 1.2rem;
|
|
81
|
+
background-position: right 0.1rem center;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@layer components {
|
|
86
|
+
.checked-label {
|
|
87
|
+
@apply ml-1.5 mr-1;
|
|
88
|
+
/* no styles are directly applied unless this is used with a checked input */
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
input:checked + .checked-label {
|
|
92
|
+
@apply text-black dark:text-white;
|
|
93
|
+
/* This is only really visible in dark mode, giving the text a subtle glow */
|
|
94
|
+
text-shadow: 0 0 5px hsl(0 0% 100%/25%);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* Apply checked-border to labels for checkboxes and radio buttons for a label that colors up automatically when checked */
|
|
98
|
+
.checked-border {
|
|
99
|
+
@apply inline-flex items-center border border-zinc-300/30 bg-zinc-50 px-1 text-zinc-700 shadow-inner;
|
|
100
|
+
@apply hover:bg-white dark:border-zinc-500/10 dark:bg-zinc-900 dark:text-zinc-100 dark:hover:bg-zinc-800 dark:focus:bg-zinc-800;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.checked-border:has([type='checkbox']:checked, [type='radio']:checked) {
|
|
104
|
+
@apply border-blue-600 text-black hover:border-blue-500 dark:text-white;
|
|
105
|
+
}
|
|
106
|
+
}
|