@stacksjs/validation 0.62.0 → 0.63.1
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 +104 -19631
- package/dist/index.js.map +133 -0
- package/package.json +1 -6
- package/src/rules.ts +0 -30
- package/src/schema.ts +1 -5
- package/src/types/index.ts +0 -1
- package/src/validator.ts +6 -5
- package/src/types/money.ts +0 -14
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/validation",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.63.1",
|
|
5
5
|
"description": "The Stacks Validation ways.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -47,11 +47,6 @@
|
|
|
47
47
|
"typecheck": "bun --bun tsc --noEmit",
|
|
48
48
|
"prepublishOnly": "bun run build"
|
|
49
49
|
},
|
|
50
|
-
"peerDependencies": {
|
|
51
|
-
"@stacksjs/strings": "latest",
|
|
52
|
-
"@stacksjs/types": "latest",
|
|
53
|
-
"@vinejs/vine": "^2.1.0"
|
|
54
|
-
},
|
|
55
50
|
"dependencies": {
|
|
56
51
|
"@stacksjs/strings": "latest",
|
|
57
52
|
"@stacksjs/types": "latest",
|
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
|
|
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
|
package/src/types/index.ts
CHANGED
package/src/validator.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { log } from '@stacksjs/cli'
|
|
2
1
|
import { path } from '@stacksjs/path'
|
|
2
|
+
import { snakeCase } from '@stacksjs/strings'
|
|
3
3
|
import type { Model } from '@stacksjs/types'
|
|
4
4
|
import type { VineType } from '@stacksjs/types'
|
|
5
|
-
import { SimpleMessagesProvider, VineError, reportError, schema } from '@stacksjs/validation'
|
|
6
5
|
import type { SchemaTypes } from '@vinejs/vine/types'
|
|
6
|
+
import { SimpleMessagesProvider, VineError, reportError, schema } from './'
|
|
7
|
+
|
|
7
8
|
interface RequestData {
|
|
8
9
|
[key: string]: string | number | null | undefined | boolean
|
|
9
10
|
}
|
|
@@ -30,8 +31,8 @@ export async function validateField(modelFile: string, params: RequestData): Pro
|
|
|
30
31
|
const messageObject: Record<string, string> = {}
|
|
31
32
|
|
|
32
33
|
for (const key in attributes) {
|
|
33
|
-
if (
|
|
34
|
-
ruleObject[key] = attributes[key]?.validation?.rule
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
|
|
35
|
+
ruleObject[snakeCase(key)] = attributes[key]?.validation?.rule
|
|
35
36
|
const validatorMessages = attributes[key]?.validation?.message
|
|
36
37
|
|
|
37
38
|
for (const validatorMessageKey in validatorMessages) {
|
|
@@ -59,7 +60,7 @@ export async function customValidate(attributes: CustomAttributes, params: Reque
|
|
|
59
60
|
const messageObject: Record<string, string> = {}
|
|
60
61
|
|
|
61
62
|
for (const key in attributes) {
|
|
62
|
-
if (
|
|
63
|
+
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
|
|
63
64
|
ruleObject[key] = attributes[key]?.validation?.rule
|
|
64
65
|
const validatorMessages = attributes[key]?.validation?.message
|
|
65
66
|
|
package/src/types/money.ts
DELETED
|
@@ -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
|
-
}
|