@stacksjs/validation 0.61.17 → 0.61.19
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 +13367 -124
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/reporter.ts +15 -0
- package/src/schema.ts +2 -1
- package/src/validator.ts +45 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/reporter.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface MessageObject {
|
|
2
|
+
message: string
|
|
3
|
+
rule: string
|
|
4
|
+
field: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
let messages: MessageObject[] = [] // Initialize as an empty array
|
|
8
|
+
|
|
9
|
+
export function reportError(errors: MessageObject[]): void {
|
|
10
|
+
messages = errors
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getErrors(): MessageObject[] {
|
|
14
|
+
return messages
|
|
15
|
+
}
|
package/src/schema.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import schema, { Vine } from '@vinejs/vine'
|
|
2
|
+
import { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine'
|
|
2
3
|
import rule from 'validator'
|
|
3
4
|
import { MoneyValidator } from './types/money'
|
|
4
5
|
|
|
5
|
-
export { schema, rule }
|
|
6
|
+
export { schema, rule, SimpleMessagesProvider, VineError }
|
|
6
7
|
|
|
7
8
|
// @ts-expect-error - investigate why this is not working
|
|
8
9
|
Vine.macro('money', () => new MoneyValidator())
|
package/src/validator.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { log } from '@stacksjs/cli'
|
|
2
|
+
import { path } from '@stacksjs/path'
|
|
3
|
+
import { SimpleMessagesProvider, VineError, reportError, schema } from '@stacksjs/validation'
|
|
4
|
+
import type { SchemaTypes } from '@vinejs/vine/types'
|
|
5
|
+
|
|
6
|
+
interface RequestData {
|
|
7
|
+
[key: string]: string | number | null | undefined | boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function validateField(modelFile: string, params: RequestData): Promise<void> {
|
|
11
|
+
const model = (await import(path.userModelsPath(modelFile))).default
|
|
12
|
+
|
|
13
|
+
const attributes = model.attributes
|
|
14
|
+
|
|
15
|
+
const ruleObject: Record<string, SchemaTypes> = {}
|
|
16
|
+
const messageObject: Record<string, string> = {}
|
|
17
|
+
|
|
18
|
+
for (const key in attributes) {
|
|
19
|
+
// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
|
|
20
|
+
if (attributes.hasOwnProperty(key)) {
|
|
21
|
+
ruleObject[key] = model.attributes[key].validator.rule
|
|
22
|
+
|
|
23
|
+
const validatorMessages = model.attributes[key].validator.message
|
|
24
|
+
|
|
25
|
+
for (const validatorMessageKey in validatorMessages) {
|
|
26
|
+
const validatorMessageString = `${key}.${validatorMessageKey}`
|
|
27
|
+
|
|
28
|
+
messageObject[validatorMessageString] = model.attributes[key].validator.message[validatorMessageKey]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
schema.messagesProvider = new SimpleMessagesProvider(messageObject)
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const vineSchema = schema.object(ruleObject)
|
|
37
|
+
const validator = schema.compile(vineSchema)
|
|
38
|
+
await validator.validate(params)
|
|
39
|
+
} catch (error: any) {
|
|
40
|
+
if (error instanceof VineError.E_VALIDATION_ERROR) reportError(error.messages)
|
|
41
|
+
|
|
42
|
+
log.info(error.message)
|
|
43
|
+
throw error
|
|
44
|
+
}
|
|
45
|
+
}
|