@stacksjs/validation 0.61.18 → 0.61.20

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/validation",
3
3
  "type": "module",
4
- "version": "0.61.18",
4
+ "version": "0.61.20",
5
5
  "description": "The Stacks Validation ways.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export * from './is'
2
+ export * from './reporter'
2
3
  export * from './rules'
3
4
  export * from './schema'
4
5
  export * from './types'
6
+ export * from './validator'
@@ -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())
@@ -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
+ }