Skip to content

Commit c6d981e

Browse files
committed
chore: wip
1 parent 107c092 commit c6d981e

File tree

6 files changed

+255
-232
lines changed

6 files changed

+255
-232
lines changed

.stacks/core/error-handling/src/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fs } from '@stacksjs/storage'
2-
import { italic, log } from '@stacksjs/cli'
2+
import { log } from '@stacksjs/cli'
33
import { logsPath } from '@stacksjs/path'
44

55
export {
@@ -29,17 +29,13 @@ class ErrorHandler {
2929
}
3030

3131
static writeErrorToFile(err: Error, options?: any) {
32-
try {
33-
const formattedError = `[${new Date().toISOString()}] ${err.name}: ${err.message}\n`
32+
const formattedError = `[${new Date().toISOString()}] ${err.name}: ${err.message}\n`
3433

35-
fs.appendFile(this.logFile, formattedError, (err: any) => {
36-
if (err)
37-
log.error(`Failed to write error to log file: ${italic(err.message)}`, err)
34+
fs.appendFile(this.logFile, formattedError, 'utf8')
35+
.then(() => {})
36+
.catch((err) => {
37+
log.error(`Failed to write error to ./storage/logs/errors.log: ${italic(err.message)}`, err)
3838
})
39-
}
40-
catch (err: any) {
41-
log.error(`Failed to write error to log file: ${italic(err.message)}`, err)
42-
}
4339
}
4440

4541
static writeErrorToConsole(err: Error, options?: any) {

.stacks/core/utils/src/currency.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export {
2323
trimScale,
2424
haveSameCurrency,
2525
haveSameAmount,
26+
dinero as currency,
2627
} from 'dinero.js'

.stacks/core/validation/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@
4646
},
4747
"peerDependencies": {
4848
"@stacksjs/utils": "workspace:*",
49-
"zod": "^3.21.4",
50-
"zod-error": "^1.5.0"
49+
"@vinejs/vine": "^1.4.1"
5150
},
5251
"dependencies": {
53-
"zod-error": "^1.5.0"
52+
"@vinejs/vine": "^1.4.1"
5453
},
5554
"devDependencies": {
5655
"@stacksjs/development": "workspace:*",

.stacks/core/validation/src/is.ts

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,206 @@
1-
import { getTypeName, toString } from '@stacksjs/utils'
2-
import validator from 'validator'
1+
import { currency, getTypeName, toString } from '@stacksjs/utils'
32
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+
})
437

538
export function isEmail(email: string) {
6-
return validator.isEmail(email)
39+
return validator.helpers.isEmail(email)
740
}
841

942
export function isStrongPassword(password: string) {
10-
return validator.isStrongPassword(password)
43+
return v.isStrongPassword(password)
1144
}
1245

1346
export function isAlphanumeric(username: string) {
14-
return validator.isAlphanumeric(username)
47+
return v.isAlphanumeric(username)
1548
}
1649

1750
export function validateUsername(username: string) {
1851
return isAlphanumeric(username)
1952
}
2053

2154
export function isURL(url: string) {
22-
return validator.isURL(url)
55+
return validator.helpers.isURL(url)
2356
}
2457

2558
export function isMobilePhone(phoneNumber: string) {
26-
return validator.isMobilePhone(phoneNumber)
59+
return validator.helpers.isMobilePhone(phoneNumber)
2760
}
2861

2962
export function isAlpha(name: string) {
30-
return validator.isAlpha(name)
63+
return validator.helpers.isAlpha(name)
3164
}
3265

3366
export function isPostalCode(zipCode: string) {
34-
return validator.isPostalCode(zipCode, 'any')
67+
return validator.helpers.isPostalCode(zipCode, 'any')
3568
}
3669

3770
export function isNumeric(number: string) {
38-
return validator.isNumeric(number)
71+
return validator.helpers.isNumeric(number)
3972
}
4073

4174
export function isHexColor(color: string) {
42-
return validator.isHexColor(color)
75+
return validator.helpers.isHexColor(color)
4376
}
4477

4578
export function isHexadecimal(hex: string) {
46-
return validator.isHexadecimal(hex)
79+
return v.isHexadecimal(hex)
4780
}
4881

4982
export function isBase64(base64: string) {
50-
return validator.isBase64(base64)
83+
return v.isBase64(base64)
5184
}
5285

5386
export function isUUID(uuid: string) {
54-
return validator.isUUID(uuid)
87+
return validator.helpers.isUUID(uuid)
5588
}
5689

5790
export function isJSON(json: string) {
58-
return validator.isJSON(json)
91+
return v.isJSON(json)
5992
}
6093

6194
export function isCreditCard(creditCard: string) {
62-
return validator.isCreditCard(creditCard)
95+
return validator.helpers.isCreditCard(creditCard)
6396
}
6497

6598
export function isISBN(isbn: string) {
66-
return validator.isISBN(isbn)
99+
return v.isISBN(isbn)
67100
}
68101

69102
export function isIP(ip: string) {
70-
return validator.isIP(ip)
103+
return validator.helpers.isIP(ip)
71104
}
72105

73106
export function isIPRange(ip: string) {
74-
return validator.isIPRange(ip)
107+
return v.isIPRange(ip)
75108
}
76109

77110
export function isMACAddress(macAddress: string) {
78-
return validator.isMACAddress(macAddress)
111+
return v.isMACAddress(macAddress)
79112
}
80113

81114
export function isLatLong(latitude: string) {
82-
return validator.isLatLong(latitude)
115+
return validator.helpers.isLatLong(latitude)
83116
}
84117

85118
export function isLatitude(longitude: string) {
86-
return validator.isLatLong(longitude)
119+
return validator.helpers.isLatLong(longitude)
87120
}
88121

89122
export function isLongitude(longitude: string) {
90-
return validator.isLatLong(longitude)
123+
return validator.helpers.isLatLong(longitude)
91124
}
92125

93126
export function isCurrency(currency: string) {
94-
return validator.isCurrency(currency)
127+
return v.isCurrency(currency)
95128
}
96129

97130
export function isDataURI(dataURI: string) {
98-
return validator.isDataURI(dataURI)
131+
return v.isDataURI(dataURI)
99132
}
100133

101134
export function isMimeType(mimeType: string) {
102-
return validator.isMimeType(mimeType)
135+
return v.isMimeType(mimeType)
103136
}
104137

105138
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)
111140
}
112141

113142
export function isAscii(ascii: string) {
114-
return validator.isAscii(ascii)
143+
return validator.helpers.isAscii(ascii)
115144
}
116145

117146
export function isBase32(base32: string) {
118-
return validator.isBase32(base32)
147+
return v.isBase32(base32)
119148
}
120149

121150
export function isByteLength(byteLength: string) {
122-
return validator.isByteLength(byteLength)
151+
return v.isByteLength(byteLength)
123152
}
124153

125154
export function isFQDN(fqdn: string) {
126-
return validator.isFQDN(fqdn)
155+
return v.isFQDN(fqdn)
127156
}
128157

129158
export function isFullWidth(fullWidth: string) {
130-
return validator.isFullWidth(fullWidth)
159+
return v.isFullWidth(fullWidth)
131160
}
132161

133162
export function isHalfWidth(halfWidth: string) {
134-
return validator.isHalfWidth(halfWidth)
163+
return v.isHalfWidth(halfWidth)
135164
}
136165

137166
export function isHash(hash: string, algorithm: HashAlgorithm) {
138-
return validator.isHash(hash, algorithm)
167+
return v.isHash(hash, algorithm)
139168
}
140169

141170
export function isHSL(hsl: string) {
142-
return validator.isHSL(hsl)
171+
return v.isHSL(hsl)
143172
}
144173

145174
export function isIBAN(iban: string) {
146-
return validator.isIBAN(iban)
175+
return v.isIBAN(iban)
147176
}
148177

149178
export function isIdentityCard(identityCard: string) {
150-
return validator.isIdentityCard(identityCard)
179+
return v.isIdentityCard(identityCard)
151180
}
152181

153182
export function isISIN(isin: string) {
154-
return validator.isISIN(isin)
183+
return v.isISIN(isin)
155184
}
156185

157186
export function isISO8601(iso8601: string) {
158-
return validator.isISO8601(iso8601)
187+
return v.isISO8601(iso8601)
159188
}
160189

161190
export function isISRC(isrc: string) {
162-
return validator.isISRC(isrc)
191+
return v.isISRC(isrc)
163192
}
164193

165194
export function isISSN(issn: string) {
166-
return validator.isISSN(issn)
195+
return v.isISSN(issn)
167196
}
168197

169198
export function isISO31661Alpha2(iso31661Alpha2: string) {
170-
return validator.isISO31661Alpha2(iso31661Alpha2)
199+
return v.isISO31661Alpha2(iso31661Alpha2)
171200
}
172201

173202
export function isISO31661Alpha3(iso31661Alpha3: string) {
174-
return validator.isISO31661Alpha3(iso31661Alpha3)
203+
return v.isISO31661Alpha3(iso31661Alpha3)
175204
}
176205

177206
export function isDef<T = any>(val?: T): val is T {

0 commit comments

Comments
 (0)