@stacksjs/validation 0.64.6 → 0.67.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.d.ts +6 -0
- package/dist/index.js +97 -47
- package/dist/is.d.ts +28 -0
- package/dist/reporter.d.ts +8 -0
- package/dist/rules.d.ts +79 -0
- package/dist/schema.d.ts +16 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/validator.d.ts +15 -0
- package/package.json +11 -22
- package/dist/index.js.map +0 -133
- package/src/index.ts +0 -6
- package/src/is.ts +0 -118
- package/src/reporter.ts +0 -15
- package/src/rules.ts +0 -92
- package/src/schema.ts +0 -22
- package/src/types/index.ts +0 -6
- package/src/validator.ts +0 -86
package/src/index.ts
DELETED
package/src/is.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { toString } from '@stacksjs/strings'
|
|
2
|
-
import { getTypeName } from '@stacksjs/types'
|
|
3
|
-
|
|
4
|
-
export function isDef<T = any>(val?: T): val is T {
|
|
5
|
-
return typeof val !== 'undefined'
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function isBoolean(val: any): val is boolean {
|
|
9
|
-
return typeof val === 'boolean'
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function isFunction<T extends Function>(val: any): val is T {
|
|
13
|
-
return typeof val === 'function'
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function isNumber(val: any): val is number {
|
|
17
|
-
return typeof val === 'number'
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function isString(val: unknown): val is string {
|
|
21
|
-
return typeof val === 'string'
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function isObject(val: any): val is object {
|
|
25
|
-
return toString(val) === '[object Object]'
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function isWindow(val: any): boolean {
|
|
29
|
-
return typeof window !== 'undefined' && toString(val) === '[object Window]'
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const isBrowser = typeof window !== 'undefined'
|
|
33
|
-
|
|
34
|
-
export const isServer = typeof document === 'undefined' // https://remix.run/docs/en/v1/pages/gotchas#typeof-window-checks
|
|
35
|
-
|
|
36
|
-
export function isMap(val: any): val is Map<any, any> {
|
|
37
|
-
return toString(val) === '[object Map]'
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function isSet(val: any): val is Set<any> {
|
|
41
|
-
return toString(val) === '[object Set]'
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function isPromise<T = any>(val: any): val is Promise<T> {
|
|
45
|
-
return toString(val) === '[object Promise]'
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function isUndefined(v: any) {
|
|
49
|
-
return getTypeName(v) === 'undefined'
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function isNull(v: any) {
|
|
53
|
-
return getTypeName(v) === 'null'
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function isSymbol(v: any) {
|
|
57
|
-
return getTypeName(v) === 'symbol'
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function isDate(v: any) {
|
|
61
|
-
return getTypeName(v) === 'date'
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function isRegExp(v: any) {
|
|
65
|
-
return getTypeName(v) === 'regexp'
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function isArray(v: any) {
|
|
69
|
-
return getTypeName(v) === 'array'
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function isPrimitive(v: any) {
|
|
73
|
-
const type = getTypeName(v)
|
|
74
|
-
return (
|
|
75
|
-
type === 'null' ||
|
|
76
|
-
type === 'undefined' ||
|
|
77
|
-
type === 'string' ||
|
|
78
|
-
type === 'number' ||
|
|
79
|
-
type === 'boolean' ||
|
|
80
|
-
type === 'symbol'
|
|
81
|
-
)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function isInteger(v: any) {
|
|
85
|
-
return isNumber(v) && Number.isInteger(v)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function isFloat(v: any) {
|
|
89
|
-
return isNumber(v) && !Number.isInteger(v)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function isPositive(v: any) {
|
|
93
|
-
return isNumber(v) && v > 0
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function isNegative(v: any) {
|
|
97
|
-
return isNumber(v) && v < 0
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function isEven(v: any) {
|
|
101
|
-
return isNumber(v) && v % 2 === 0
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function isOdd(v: any) {
|
|
105
|
-
return isNumber(v) && v % 2 !== 0
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function isEvenOrOdd(v: any) {
|
|
109
|
-
return isNumber(v) && (v % 2 === 0 ? 'even' : 'odd')
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function isPositiveOrNegative(v: any) {
|
|
113
|
-
return isNumber(v) && (v > 0 ? 'positive' : 'negative')
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function isIntegerOrFloat(v: any) {
|
|
117
|
-
return isNumber(v) && (Number.isInteger(v) ? 'integer' : 'float')
|
|
118
|
-
}
|
package/src/reporter.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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/rules.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Thanks to VineJS for the following types:
|
|
3
|
-
*
|
|
4
|
-
* The context shared with the entire validation pipeline.
|
|
5
|
-
* Each field gets its own context object.
|
|
6
|
-
*/
|
|
7
|
-
export interface FieldContext {
|
|
8
|
-
/**
|
|
9
|
-
* Field value
|
|
10
|
-
*/
|
|
11
|
-
value: unknown
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The data property is the top-level object under validation.
|
|
15
|
-
*/
|
|
16
|
-
data: any
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Shared metadata across the entire validation lifecycle. It can be
|
|
20
|
-
* used to pass data between validation rules
|
|
21
|
-
*/
|
|
22
|
-
meta: Record<string, any>
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Mutate the value of field under validation.
|
|
26
|
-
*/
|
|
27
|
-
mutate: (newValue: any, field: FieldContext) => void
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Report error to the error reporter
|
|
31
|
-
*/
|
|
32
|
-
report: ErrorReporterContract['report']
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Is this field valid. Default: true
|
|
36
|
-
*/
|
|
37
|
-
isValid: boolean
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Is this field has value defined.
|
|
41
|
-
*/
|
|
42
|
-
isDefined: boolean
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Wildcard path for the field. The value is a nested
|
|
46
|
-
* pointer to the field under validation.
|
|
47
|
-
*
|
|
48
|
-
* In case of arrays, the `*` wildcard is used.
|
|
49
|
-
*/
|
|
50
|
-
wildCardPath: string
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* The parent property is the parent of the field. It could be an
|
|
54
|
-
* array or an object.
|
|
55
|
-
*/
|
|
56
|
-
parent: any
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Name of the field under validation. In case of an array, the field
|
|
60
|
-
* name will be a number
|
|
61
|
-
*/
|
|
62
|
-
name: string | number
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Is this field an array member
|
|
66
|
-
*/
|
|
67
|
-
isArrayMember: boolean
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Thanks to VineJS for the following types:
|
|
72
|
-
*
|
|
73
|
-
* The error reporter is used to report errors during the validation
|
|
74
|
-
* process.
|
|
75
|
-
*/
|
|
76
|
-
export interface ErrorReporterContract {
|
|
77
|
-
/**
|
|
78
|
-
* A boolean to known if there are one or more
|
|
79
|
-
* errors.
|
|
80
|
-
*/
|
|
81
|
-
hasErrors: boolean
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Creates an instance of an exception to throw
|
|
85
|
-
*/
|
|
86
|
-
createError: () => Error
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Report error for a field
|
|
90
|
-
*/
|
|
91
|
-
report: (message: string, rule: string, field: FieldContext, args?: Record<string, any>) => any
|
|
92
|
-
}
|
package/src/schema.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import schema from '@vinejs/vine'
|
|
2
|
-
import { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine'
|
|
3
|
-
import rule from 'validator'
|
|
4
|
-
|
|
5
|
-
export { schema, rule, SimpleMessagesProvider, VineError }
|
|
6
|
-
|
|
7
|
-
type SchemaString = string
|
|
8
|
-
type SchemaNumber = number
|
|
9
|
-
type SchemaBoolean = boolean
|
|
10
|
-
type SchemaEnum = string[]
|
|
11
|
-
|
|
12
|
-
export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum
|
|
13
|
-
|
|
14
|
-
export type { Infer } from '@vinejs/vine/types'
|
|
15
|
-
export { VineString, VineBoolean, VineEnum, VineNumber, VineDate } from '@vinejs/vine'
|
|
16
|
-
|
|
17
|
-
export const validate = {
|
|
18
|
-
string: (defaultValue = ''): SchemaString => defaultValue,
|
|
19
|
-
number: (defaultValue = 1): SchemaNumber => defaultValue,
|
|
20
|
-
boolean: (defaultValue = true): SchemaBoolean => defaultValue,
|
|
21
|
-
enum: (values: string[]): SchemaEnum => values,
|
|
22
|
-
}
|
package/src/types/index.ts
DELETED
package/src/validator.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { path } from '@stacksjs/path'
|
|
2
|
-
import { snakeCase } from '@stacksjs/strings'
|
|
3
|
-
import type { Model } from '@stacksjs/types'
|
|
4
|
-
import type { VineType } from '@stacksjs/types'
|
|
5
|
-
import type { SchemaTypes } from '@vinejs/vine/types'
|
|
6
|
-
import { SimpleMessagesProvider, VineError, reportError, schema } from './'
|
|
7
|
-
|
|
8
|
-
interface RequestData {
|
|
9
|
-
[key: string]: any
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface ValidationField {
|
|
13
|
-
rule: VineType
|
|
14
|
-
message: Record<string, string>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface CustomAttributes {
|
|
18
|
-
[key: string]: ValidationField
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function isObjectNotEmpty(obj: object): boolean {
|
|
22
|
-
return Object.keys(obj).length > 0
|
|
23
|
-
}
|
|
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
|
|
27
|
-
const attributes = model.attributes
|
|
28
|
-
|
|
29
|
-
const ruleObject: Record<string, SchemaTypes> = {}
|
|
30
|
-
const messageObject: Record<string, string> = {}
|
|
31
|
-
|
|
32
|
-
for (const key in attributes) {
|
|
33
|
-
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
|
|
34
|
-
ruleObject[snakeCase(key)] = attributes[key]?.validation?.rule
|
|
35
|
-
const validatorMessages = attributes[key]?.validation?.message
|
|
36
|
-
|
|
37
|
-
for (const validatorMessageKey in validatorMessages) {
|
|
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)
|
|
52
|
-
|
|
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 (Object.prototype.hasOwnProperty.call(attributes, key)) {
|
|
63
|
-
const rule = attributes[key]?.rule
|
|
64
|
-
if (rule) ruleObject[key] = rule as SchemaTypes
|
|
65
|
-
|
|
66
|
-
const validatorMessages = attributes[key]?.message
|
|
67
|
-
|
|
68
|
-
for (const validatorMessageKey in validatorMessages) {
|
|
69
|
-
const validatorMessageString = `${key}.${validatorMessageKey}`
|
|
70
|
-
messageObject[validatorMessageString] = attributes[key]?.message[validatorMessageKey] || ''
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
schema.messagesProvider = new SimpleMessagesProvider(messageObject)
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
const vineSchema = schema.object(ruleObject)
|
|
79
|
-
const validator = schema.compile(vineSchema)
|
|
80
|
-
await validator.validate(params)
|
|
81
|
-
} catch (error: any) {
|
|
82
|
-
if (error instanceof VineError.E_VALIDATION_ERROR) reportError(error.messages)
|
|
83
|
-
|
|
84
|
-
throw { status: 422, errors: error.messages }
|
|
85
|
-
}
|
|
86
|
-
}
|