@stacksjs/validation 0.61.24 → 0.63.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/validation",
3
3
  "type": "module",
4
- "version": "0.61.24",
4
+ "version": "0.63.0",
5
5
  "description": "The Stacks Validation ways.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
package/src/rules.ts CHANGED
@@ -1,7 +1,3 @@
1
- import { USD } from '@dinero.js/currencies'
2
- import { dinero as currency } from 'dinero.js'
3
- import { schema } from './schema'
4
-
5
1
  /**
6
2
  * Thanks to VineJS for the following types:
7
3
  *
@@ -94,29 +90,3 @@ export interface ErrorReporterContract {
94
90
  */
95
91
  report: (message: string, rule: string, field: FieldContext, args?: Record<string, any>) => any
96
92
  }
97
-
98
- export const isMoney = schema.createRule((value: unknown, _, field: FieldContext) => {
99
- /**
100
- * Convert string representation of a number to a JavaScript
101
- * Number data type.
102
- */
103
- const numericValue = schema.helpers.asNumber(value)
104
-
105
- /**
106
- * Report error, if the value is NaN post-conversion
107
- */
108
- if (Number.isNaN(numericValue)) {
109
- field.report('The {{ field }} field value must be a number', 'money', field)
110
- return
111
- }
112
-
113
- /**
114
- * Create amount type
115
- */
116
- const amount = currency({ amount: numericValue, currency: USD })
117
-
118
- /**
119
- * Mutate the field's value
120
- */
121
- field.mutate(amount, field)
122
- })
package/src/schema.ts CHANGED
@@ -1,13 +1,9 @@
1
- import schema, { Vine } from '@vinejs/vine'
1
+ import schema from '@vinejs/vine'
2
2
  import { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine'
3
3
  import rule from 'validator'
4
- import { MoneyValidator } from './types/money'
5
4
 
6
5
  export { schema, rule, SimpleMessagesProvider, VineError }
7
6
 
8
- // @ts-expect-error - investigate why this is not working
9
- Vine.macro('money', () => new MoneyValidator())
10
-
11
7
  type SchemaString = string
12
8
  type SchemaNumber = number
13
9
  type SchemaBoolean = boolean
@@ -1,4 +1,3 @@
1
- export * from './money'
2
1
  export {
3
2
  VineEnum as ValidationEnum,
4
3
  VineBoolean as ValidationBoolean,
package/src/validator.ts CHANGED
@@ -1,32 +1,72 @@
1
- import { log } from '@stacksjs/cli'
2
1
  import { path } from '@stacksjs/path'
3
- import { Model } from '@stacksjs/types'
4
- import { SimpleMessagesProvider, VineError, reportError, schema } from '@stacksjs/validation'
2
+ import { snakeCase } from '@stacksjs/strings'
3
+ import type { Model } from '@stacksjs/types'
4
+ import type { VineType } from '@stacksjs/types'
5
5
  import type { SchemaTypes } from '@vinejs/vine/types'
6
+ import { SimpleMessagesProvider, VineError, reportError, schema } from './'
6
7
 
7
8
  interface RequestData {
8
9
  [key: string]: string | number | null | undefined | boolean
9
10
  }
10
11
 
11
- export async function validateField(modelFile: string, params: RequestData): Promise<void> {
12
- const model = (await import(path.userModelsPath(modelFile))).default
12
+ interface ValidationType {
13
+ rule: VineType
14
+ message: { [key: string]: string }
15
+ }
16
+
17
+ interface ValidationField {
18
+ [key: string]: string | ValidationType
19
+ validation: ValidationType
20
+ }
13
21
 
22
+ interface CustomAttributes {
23
+ [key: string]: ValidationField
24
+ }
25
+
26
+ export async function validateField(modelFile: string, params: RequestData): Promise<any> {
27
+ const model = (await import(/* @vite-ignore */ path.userModelsPath(`${modelFile}.ts`))).default as Model
14
28
  const attributes = model.attributes
15
29
 
16
30
  const ruleObject: Record<string, SchemaTypes> = {}
17
31
  const messageObject: Record<string, string> = {}
18
32
 
19
33
  for (const key in attributes) {
20
- // biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
21
- if (attributes.hasOwnProperty(key)) {
22
- ruleObject[key] = model.attributes[key].validator.rule
23
-
24
- const validatorMessages = model.attributes[key].validator.message
34
+ if (Object.prototype.hasOwnProperty.call(attributes, key)) {
35
+ ruleObject[snakeCase(key)] = attributes[key]?.validation?.rule
36
+ const validatorMessages = attributes[key]?.validation?.message
25
37
 
26
38
  for (const validatorMessageKey in validatorMessages) {
27
39
  const validatorMessageString = `${key}.${validatorMessageKey}`
40
+ messageObject[validatorMessageString] = attributes[key]?.validation?.message[validatorMessageKey] || ''
41
+ }
42
+ }
43
+ }
44
+
45
+ schema.messagesProvider = new SimpleMessagesProvider(messageObject)
46
+
47
+ try {
48
+ const vineSchema = schema.object(ruleObject)
49
+ const validator = schema.compile(vineSchema)
50
+ await validator.validate(params)
51
+ } catch (error: any) {
52
+ if (error instanceof VineError.E_VALIDATION_ERROR) reportError(error.messages)
53
+
54
+ throw { status: 422, errors: error.messages }
55
+ }
56
+ }
28
57
 
29
- messageObject[validatorMessageString] = model.attributes[key].validator.message[validatorMessageKey]
58
+ export async function customValidate(attributes: CustomAttributes, params: RequestData): Promise<any> {
59
+ const ruleObject: Record<string, SchemaTypes> = {}
60
+ const messageObject: Record<string, string> = {}
61
+
62
+ for (const key in attributes) {
63
+ if (Object.prototype.hasOwnProperty.call(attributes, key)) {
64
+ ruleObject[key] = attributes[key]?.validation?.rule
65
+ const validatorMessages = attributes[key]?.validation?.message
66
+
67
+ for (const validatorMessageKey in validatorMessages) {
68
+ const validatorMessageString = `${key}.${validatorMessageKey}`
69
+ messageObject[validatorMessageString] = attributes[key]?.validation?.message[validatorMessageKey] || ''
30
70
  }
31
71
  }
32
72
  }
@@ -40,7 +80,6 @@ export async function validateField(modelFile: string, params: RequestData): Pro
40
80
  } catch (error: any) {
41
81
  if (error instanceof VineError.E_VALIDATION_ERROR) reportError(error.messages)
42
82
 
43
- log.info(error.message)
44
- throw error
83
+ throw { status: 422, errors: error.messages }
45
84
  }
46
85
  }
@@ -1,14 +0,0 @@
1
- import type { Money } from '@stacksjs/types'
2
- import { BaseLiteralType } from '@vinejs/vine'
3
- import type { FieldOptions, Validation } from '@vinejs/vine/types'
4
- import { isMoney } from '../rules'
5
-
6
- export class MoneyValidator extends BaseLiteralType<string, Money, Money> {
7
- constructor(options?: FieldOptions, validations?: Validation<any>[]) {
8
- super(options, validations || [isMoney()])
9
- }
10
-
11
- clone() {
12
- return new MoneyValidator(this.cloneOptions(), this.cloneValidations()) as this
13
- }
14
- }