@stacksjs/validation 0.61.23 → 0.62.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/dist/index.js +774 -753
- package/package.json +1 -1
- package/src/validator.ts +49 -11
package/package.json
CHANGED
package/src/validator.ts
CHANGED
|
@@ -1,32 +1,71 @@
|
|
|
1
1
|
import { log } from '@stacksjs/cli'
|
|
2
2
|
import { path } from '@stacksjs/path'
|
|
3
|
-
import { Model } from '@stacksjs/types'
|
|
3
|
+
import type { Model } from '@stacksjs/types'
|
|
4
|
+
import type { VineType } from '@stacksjs/types'
|
|
4
5
|
import { SimpleMessagesProvider, VineError, reportError, schema } from '@stacksjs/validation'
|
|
5
6
|
import type { SchemaTypes } from '@vinejs/vine/types'
|
|
6
|
-
|
|
7
7
|
interface RequestData {
|
|
8
8
|
[key: string]: string | number | null | undefined | boolean
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
interface ValidationType {
|
|
12
|
+
rule: VineType
|
|
13
|
+
message: { [key: string]: string }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface ValidationField {
|
|
17
|
+
[key: string]: string | ValidationType
|
|
18
|
+
validation: ValidationType
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface CustomAttributes {
|
|
22
|
+
[key: string]: ValidationField
|
|
23
|
+
}
|
|
13
24
|
|
|
25
|
+
export async function validateField(modelFile: string, params: RequestData): Promise<any> {
|
|
26
|
+
const model = (await import(/* @vite-ignore */ path.userModelsPath(`${modelFile}.ts`))).default as Model
|
|
14
27
|
const attributes = model.attributes
|
|
15
28
|
|
|
16
29
|
const ruleObject: Record<string, SchemaTypes> = {}
|
|
17
30
|
const messageObject: Record<string, string> = {}
|
|
18
31
|
|
|
19
32
|
for (const key in attributes) {
|
|
20
|
-
// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
|
|
21
33
|
if (attributes.hasOwnProperty(key)) {
|
|
22
|
-
ruleObject[key] =
|
|
23
|
-
|
|
24
|
-
const validatorMessages = model.attributes[key].validator.message
|
|
34
|
+
ruleObject[key] = attributes[key]?.validation?.rule
|
|
35
|
+
const validatorMessages = attributes[key]?.validation?.message
|
|
25
36
|
|
|
26
37
|
for (const validatorMessageKey in validatorMessages) {
|
|
27
38
|
const validatorMessageString = `${key}.${validatorMessageKey}`
|
|
39
|
+
messageObject[validatorMessageString] = attributes[key]?.validation?.message[validatorMessageKey] || ''
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
schema.messagesProvider = new SimpleMessagesProvider(messageObject)
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const vineSchema = schema.object(ruleObject)
|
|
48
|
+
const validator = schema.compile(vineSchema)
|
|
49
|
+
await validator.validate(params)
|
|
50
|
+
} catch (error: any) {
|
|
51
|
+
if (error instanceof VineError.E_VALIDATION_ERROR) reportError(error.messages)
|
|
28
52
|
|
|
29
|
-
|
|
53
|
+
throw { status: 422, errors: error.messages }
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function customValidate(attributes: CustomAttributes, params: RequestData): Promise<any> {
|
|
58
|
+
const ruleObject: Record<string, SchemaTypes> = {}
|
|
59
|
+
const messageObject: Record<string, string> = {}
|
|
60
|
+
|
|
61
|
+
for (const key in attributes) {
|
|
62
|
+
if (attributes.hasOwnProperty(key)) {
|
|
63
|
+
ruleObject[key] = attributes[key]?.validation?.rule
|
|
64
|
+
const validatorMessages = attributes[key]?.validation?.message
|
|
65
|
+
|
|
66
|
+
for (const validatorMessageKey in validatorMessages) {
|
|
67
|
+
const validatorMessageString = `${key}.${validatorMessageKey}`
|
|
68
|
+
messageObject[validatorMessageString] = attributes[key]?.validation?.message[validatorMessageKey] || ''
|
|
30
69
|
}
|
|
31
70
|
}
|
|
32
71
|
}
|
|
@@ -40,7 +79,6 @@ export async function validateField(modelFile: string, params: RequestData): Pro
|
|
|
40
79
|
} catch (error: any) {
|
|
41
80
|
if (error instanceof VineError.E_VALIDATION_ERROR) reportError(error.messages)
|
|
42
81
|
|
|
43
|
-
|
|
44
|
-
throw error
|
|
82
|
+
throw { status: 422, errors: error.messages }
|
|
45
83
|
}
|
|
46
84
|
}
|