|
1 |
| -import { getTypeName, toString } from '@stacksjs/utils' |
2 |
| -import validator from 'validator' |
| 1 | +import { currency, getTypeName, toString } from '@stacksjs/utils' |
3 | 2 | import type { HashAlgorithm } from '@stacksjs/types'
|
| 3 | +import { validator } from '@stacksjs/validation' |
| 4 | +import v from 'validator' |
| 5 | +import { USD } from '@dinero.js/currencies' |
| 6 | +import type { FieldContext } from '@vinejs/vine/build/src/types' |
| 7 | + |
| 8 | +export const isMoney = validator.createRule((value: unknown, _, field: FieldContext) => { |
| 9 | + /** |
| 10 | + * Convert string representation of a number to a JavaScript |
| 11 | + * Number data type. |
| 12 | + */ |
| 13 | + const numericValue = validator.helpers.asNumber(value) |
| 14 | + |
| 15 | + /** |
| 16 | + * Report error, if the value is NaN post-conversion |
| 17 | + */ |
| 18 | + if (Number.isNaN(numericValue)) { |
| 19 | + field.report( |
| 20 | + 'The {{ field }} field value must be a number', |
| 21 | + 'money', |
| 22 | + field, |
| 23 | + ) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Create amount type |
| 29 | + */ |
| 30 | + const amount = currency({ amount: numericValue, currency: USD }) |
| 31 | + |
| 32 | + /** |
| 33 | + * Mutate the field's value |
| 34 | + */ |
| 35 | + field.mutate(amount, field) |
| 36 | +}) |
4 | 37 |
|
5 | 38 | export function isEmail(email: string) {
|
6 |
| - return validator.isEmail(email) |
| 39 | + return validator.helpers.isEmail(email) |
7 | 40 | }
|
8 | 41 |
|
9 | 42 | export function isStrongPassword(password: string) {
|
10 |
| - return validator.isStrongPassword(password) |
| 43 | + return v.isStrongPassword(password) |
11 | 44 | }
|
12 | 45 |
|
13 | 46 | export function isAlphanumeric(username: string) {
|
14 |
| - return validator.isAlphanumeric(username) |
| 47 | + return v.isAlphanumeric(username) |
15 | 48 | }
|
16 | 49 |
|
17 | 50 | export function validateUsername(username: string) {
|
18 | 51 | return isAlphanumeric(username)
|
19 | 52 | }
|
20 | 53 |
|
21 | 54 | export function isURL(url: string) {
|
22 |
| - return validator.isURL(url) |
| 55 | + return validator.helpers.isURL(url) |
23 | 56 | }
|
24 | 57 |
|
25 | 58 | export function isMobilePhone(phoneNumber: string) {
|
26 |
| - return validator.isMobilePhone(phoneNumber) |
| 59 | + return validator.helpers.isMobilePhone(phoneNumber) |
27 | 60 | }
|
28 | 61 |
|
29 | 62 | export function isAlpha(name: string) {
|
30 |
| - return validator.isAlpha(name) |
| 63 | + return validator.helpers.isAlpha(name) |
31 | 64 | }
|
32 | 65 |
|
33 | 66 | export function isPostalCode(zipCode: string) {
|
34 |
| - return validator.isPostalCode(zipCode, 'any') |
| 67 | + return validator.helpers.isPostalCode(zipCode, 'any') |
35 | 68 | }
|
36 | 69 |
|
37 | 70 | export function isNumeric(number: string) {
|
38 |
| - return validator.isNumeric(number) |
| 71 | + return validator.helpers.isNumeric(number) |
39 | 72 | }
|
40 | 73 |
|
41 | 74 | export function isHexColor(color: string) {
|
42 |
| - return validator.isHexColor(color) |
| 75 | + return validator.helpers.isHexColor(color) |
43 | 76 | }
|
44 | 77 |
|
45 | 78 | export function isHexadecimal(hex: string) {
|
46 |
| - return validator.isHexadecimal(hex) |
| 79 | + return v.isHexadecimal(hex) |
47 | 80 | }
|
48 | 81 |
|
49 | 82 | export function isBase64(base64: string) {
|
50 |
| - return validator.isBase64(base64) |
| 83 | + return v.isBase64(base64) |
51 | 84 | }
|
52 | 85 |
|
53 | 86 | export function isUUID(uuid: string) {
|
54 |
| - return validator.isUUID(uuid) |
| 87 | + return validator.helpers.isUUID(uuid) |
55 | 88 | }
|
56 | 89 |
|
57 | 90 | export function isJSON(json: string) {
|
58 |
| - return validator.isJSON(json) |
| 91 | + return v.isJSON(json) |
59 | 92 | }
|
60 | 93 |
|
61 | 94 | export function isCreditCard(creditCard: string) {
|
62 |
| - return validator.isCreditCard(creditCard) |
| 95 | + return validator.helpers.isCreditCard(creditCard) |
63 | 96 | }
|
64 | 97 |
|
65 | 98 | export function isISBN(isbn: string) {
|
66 |
| - return validator.isISBN(isbn) |
| 99 | + return v.isISBN(isbn) |
67 | 100 | }
|
68 | 101 |
|
69 | 102 | export function isIP(ip: string) {
|
70 |
| - return validator.isIP(ip) |
| 103 | + return validator.helpers.isIP(ip) |
71 | 104 | }
|
72 | 105 |
|
73 | 106 | export function isIPRange(ip: string) {
|
74 |
| - return validator.isIPRange(ip) |
| 107 | + return v.isIPRange(ip) |
75 | 108 | }
|
76 | 109 |
|
77 | 110 | export function isMACAddress(macAddress: string) {
|
78 |
| - return validator.isMACAddress(macAddress) |
| 111 | + return v.isMACAddress(macAddress) |
79 | 112 | }
|
80 | 113 |
|
81 | 114 | export function isLatLong(latitude: string) {
|
82 |
| - return validator.isLatLong(latitude) |
| 115 | + return validator.helpers.isLatLong(latitude) |
83 | 116 | }
|
84 | 117 |
|
85 | 118 | export function isLatitude(longitude: string) {
|
86 |
| - return validator.isLatLong(longitude) |
| 119 | + return validator.helpers.isLatLong(longitude) |
87 | 120 | }
|
88 | 121 |
|
89 | 122 | export function isLongitude(longitude: string) {
|
90 |
| - return validator.isLatLong(longitude) |
| 123 | + return validator.helpers.isLatLong(longitude) |
91 | 124 | }
|
92 | 125 |
|
93 | 126 | export function isCurrency(currency: string) {
|
94 |
| - return validator.isCurrency(currency) |
| 127 | + return v.isCurrency(currency) |
95 | 128 | }
|
96 | 129 |
|
97 | 130 | export function isDataURI(dataURI: string) {
|
98 |
| - return validator.isDataURI(dataURI) |
| 131 | + return v.isDataURI(dataURI) |
99 | 132 | }
|
100 | 133 |
|
101 | 134 | export function isMimeType(mimeType: string) {
|
102 |
| - return validator.isMimeType(mimeType) |
| 135 | + return v.isMimeType(mimeType) |
103 | 136 | }
|
104 | 137 |
|
105 | 138 | export function isJWT(jwt: string) {
|
106 |
| - return validator.isJWT(jwt) |
107 |
| -} |
108 |
| - |
109 |
| -export function isMongoId(mongoId: string) { |
110 |
| - return validator.isMongoId(mongoId) |
| 139 | + return validator.helpers.isJWT(jwt) |
111 | 140 | }
|
112 | 141 |
|
113 | 142 | export function isAscii(ascii: string) {
|
114 |
| - return validator.isAscii(ascii) |
| 143 | + return validator.helpers.isAscii(ascii) |
115 | 144 | }
|
116 | 145 |
|
117 | 146 | export function isBase32(base32: string) {
|
118 |
| - return validator.isBase32(base32) |
| 147 | + return v.isBase32(base32) |
119 | 148 | }
|
120 | 149 |
|
121 | 150 | export function isByteLength(byteLength: string) {
|
122 |
| - return validator.isByteLength(byteLength) |
| 151 | + return v.isByteLength(byteLength) |
123 | 152 | }
|
124 | 153 |
|
125 | 154 | export function isFQDN(fqdn: string) {
|
126 |
| - return validator.isFQDN(fqdn) |
| 155 | + return v.isFQDN(fqdn) |
127 | 156 | }
|
128 | 157 |
|
129 | 158 | export function isFullWidth(fullWidth: string) {
|
130 |
| - return validator.isFullWidth(fullWidth) |
| 159 | + return v.isFullWidth(fullWidth) |
131 | 160 | }
|
132 | 161 |
|
133 | 162 | export function isHalfWidth(halfWidth: string) {
|
134 |
| - return validator.isHalfWidth(halfWidth) |
| 163 | + return v.isHalfWidth(halfWidth) |
135 | 164 | }
|
136 | 165 |
|
137 | 166 | export function isHash(hash: string, algorithm: HashAlgorithm) {
|
138 |
| - return validator.isHash(hash, algorithm) |
| 167 | + return v.isHash(hash, algorithm) |
139 | 168 | }
|
140 | 169 |
|
141 | 170 | export function isHSL(hsl: string) {
|
142 |
| - return validator.isHSL(hsl) |
| 171 | + return v.isHSL(hsl) |
143 | 172 | }
|
144 | 173 |
|
145 | 174 | export function isIBAN(iban: string) {
|
146 |
| - return validator.isIBAN(iban) |
| 175 | + return v.isIBAN(iban) |
147 | 176 | }
|
148 | 177 |
|
149 | 178 | export function isIdentityCard(identityCard: string) {
|
150 |
| - return validator.isIdentityCard(identityCard) |
| 179 | + return v.isIdentityCard(identityCard) |
151 | 180 | }
|
152 | 181 |
|
153 | 182 | export function isISIN(isin: string) {
|
154 |
| - return validator.isISIN(isin) |
| 183 | + return v.isISIN(isin) |
155 | 184 | }
|
156 | 185 |
|
157 | 186 | export function isISO8601(iso8601: string) {
|
158 |
| - return validator.isISO8601(iso8601) |
| 187 | + return v.isISO8601(iso8601) |
159 | 188 | }
|
160 | 189 |
|
161 | 190 | export function isISRC(isrc: string) {
|
162 |
| - return validator.isISRC(isrc) |
| 191 | + return v.isISRC(isrc) |
163 | 192 | }
|
164 | 193 |
|
165 | 194 | export function isISSN(issn: string) {
|
166 |
| - return validator.isISSN(issn) |
| 195 | + return v.isISSN(issn) |
167 | 196 | }
|
168 | 197 |
|
169 | 198 | export function isISO31661Alpha2(iso31661Alpha2: string) {
|
170 |
| - return validator.isISO31661Alpha2(iso31661Alpha2) |
| 199 | + return v.isISO31661Alpha2(iso31661Alpha2) |
171 | 200 | }
|
172 | 201 |
|
173 | 202 | export function isISO31661Alpha3(iso31661Alpha3: string) {
|
174 |
| - return validator.isISO31661Alpha3(iso31661Alpha3) |
| 203 | + return v.isISO31661Alpha3(iso31661Alpha3) |
175 | 204 | }
|
176 | 205 |
|
177 | 206 | export function isDef<T = any>(val?: T): val is T {
|
|
0 commit comments