waltronics-types 1.0.1 → 1.0.2
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/Validate.ts +145 -0
- package/package.json +9 -2
package/Validate.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import validator from "validator";
|
|
3
|
+
|
|
4
|
+
export const isName = z
|
|
5
|
+
.string()
|
|
6
|
+
.trim()
|
|
7
|
+
.min(2, "Must enter a name.")
|
|
8
|
+
.max(50, "Must enter a name less than 50 characters.")
|
|
9
|
+
.regex(/^[A-Za-z\s'-]*$/, "Must enter a name without symbols.")
|
|
10
|
+
.transform(s => {
|
|
11
|
+
return s
|
|
12
|
+
.replace(/\s+/g, ' ')
|
|
13
|
+
.split(/\s/)
|
|
14
|
+
.map(w => w[0].toUpperCase() + w.slice(1).toLowerCase())
|
|
15
|
+
.join(" ")
|
|
16
|
+
;
|
|
17
|
+
})
|
|
18
|
+
;
|
|
19
|
+
|
|
20
|
+
export const isEmail = z
|
|
21
|
+
.email("Must be a valid email.")
|
|
22
|
+
.trim()
|
|
23
|
+
.max(320, "Must enter an email less than 320 characters.")
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
;
|
|
26
|
+
|
|
27
|
+
export const isPhone = z
|
|
28
|
+
.string()
|
|
29
|
+
.trim()
|
|
30
|
+
.refine(s => {
|
|
31
|
+
const hasFormat = !!s.match(/[0-9]{3}-[0-9]{3}-[0-9]{4}/);
|
|
32
|
+
const isMobilePhone = validator.isMobilePhone("1" + s.replaceAll("-", ""), "en-US")
|
|
33
|
+
return hasFormat && isMobilePhone;
|
|
34
|
+
}, "Must enter a phone number in the format 123-456-7890.")
|
|
35
|
+
;
|
|
36
|
+
|
|
37
|
+
export const isDate = z.string().trim().regex(/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}(:[0-9]{2}$|:[0-9]{2}.[0-9]{3}$)/);
|
|
38
|
+
export const isMoney = z.coerce.number();
|
|
39
|
+
export const isVIN = z.string().trim().regex(/^[A-HJ-NPR-Z0-9]{17}$/, "Must enter a valid VIN.")
|
|
40
|
+
export const isInteger = z.coerce.number().int();
|
|
41
|
+
export const isIntegerArray = z.array(z.coerce.number().int());
|
|
42
|
+
export const isUUID = z.uuid();
|
|
43
|
+
export const isUUIDArray = z.array(z.uuid());
|
|
44
|
+
export const isBit = z.string().trim().refine(s => s === "0" || s === "1");
|
|
45
|
+
|
|
46
|
+
export const isID = z.union([
|
|
47
|
+
z.literal(""),
|
|
48
|
+
z.coerce.number()
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
export const hasLengthAndClean = (data: {min: number; max: number; name: string}) => {
|
|
52
|
+
return z
|
|
53
|
+
.string()
|
|
54
|
+
.trim()
|
|
55
|
+
.min(data.min, `Must enter a ${data.name}.`)
|
|
56
|
+
.max(data.max, `Must enter a ${data.name} less than 50 characters.`)
|
|
57
|
+
.transform(s => s.replace(/\s+/g, ' '))
|
|
58
|
+
.refine(s => s.length > 0, `Must enter a ${data.name}.`)
|
|
59
|
+
;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const isCode = hasLengthAndClean({
|
|
63
|
+
min: 1,
|
|
64
|
+
max: 100,
|
|
65
|
+
name: "code"
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const isMessage = hasLengthAndClean({
|
|
69
|
+
min: 1,
|
|
70
|
+
max: 500,
|
|
71
|
+
name: "message"
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export const isHead = hasLengthAndClean({
|
|
75
|
+
min: 1,
|
|
76
|
+
max: 100,
|
|
77
|
+
name: "value"
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
export const isBody = hasLengthAndClean({
|
|
81
|
+
min: 1,
|
|
82
|
+
max: 500,
|
|
83
|
+
name: "value"
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const isPartName = hasLengthAndClean({
|
|
87
|
+
min: 1,
|
|
88
|
+
max: 100,
|
|
89
|
+
name: "name"
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export const isPartNumber = hasLengthAndClean({
|
|
93
|
+
min: 1,
|
|
94
|
+
max: 100,
|
|
95
|
+
name: "name"
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
export const isCCN = z
|
|
99
|
+
.string()
|
|
100
|
+
.trim()
|
|
101
|
+
.refine(s => {
|
|
102
|
+
const validLength = s.length >= 13 && s.length <= 16;
|
|
103
|
+
|
|
104
|
+
const providers = ["visa", "amex", "mastercard"] as any;
|
|
105
|
+
let validCreditCardNumber = false;
|
|
106
|
+
|
|
107
|
+
for (const provider of providers) {
|
|
108
|
+
const validCreditCardNumberForProvider = validator.isCreditCard(s, {provider});
|
|
109
|
+
if (validCreditCardNumberForProvider) {
|
|
110
|
+
validCreditCardNumber = true;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return validLength && validCreditCardNumber;
|
|
116
|
+
}, "Must enter a valid credit card number.")
|
|
117
|
+
;
|
|
118
|
+
|
|
119
|
+
export const isEXP = z
|
|
120
|
+
.string()
|
|
121
|
+
.trim()
|
|
122
|
+
.regex(/^(0[1-9]|1[0-2])[0-9]{2}$/, "Must enter a valid expiration date.")
|
|
123
|
+
.refine(s => {
|
|
124
|
+
const month = parseInt(s.substring(0, 2));
|
|
125
|
+
const year = 2000 + parseInt(s.substring(2, 4));
|
|
126
|
+
|
|
127
|
+
const now = new Date();
|
|
128
|
+
const currentYear = now.getFullYear();
|
|
129
|
+
const currentMonth = now.getMonth() + 1;
|
|
130
|
+
|
|
131
|
+
if (year > currentYear)
|
|
132
|
+
return true;
|
|
133
|
+
if (year === currentYear)
|
|
134
|
+
return month >= currentMonth;
|
|
135
|
+
return false;
|
|
136
|
+
}, "Card has expired.");
|
|
137
|
+
;
|
|
138
|
+
|
|
139
|
+
export const isCreditCardType = z.enum(['VISA', 'MASTERCARD', 'AMEX']);
|
|
140
|
+
|
|
141
|
+
export const isRepair = hasLengthAndClean({
|
|
142
|
+
min: 1,
|
|
143
|
+
max: 500,
|
|
144
|
+
name: "repair"
|
|
145
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "waltronics-types",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Contains the types used in the Waltronics application.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -8,5 +8,12 @@
|
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|
|
10
10
|
"author": "",
|
|
11
|
-
"license": "ISC"
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"validator": "^13.15.26",
|
|
14
|
+
"zod": "^4.3.5"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/validator": "^13.15.10"
|
|
18
|
+
}
|
|
12
19
|
}
|