@stacksjs/validation 0.65.0 → 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/src/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './is'
2
- export * from './reporter'
3
- export * from './rules'
4
- export * from './schema'
5
- export * from './types'
6
- export * from './validator'
package/src/is.ts DELETED
@@ -1,119 +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
- // eslint-disable-next-line ts/no-unsafe-function-type
13
- export function isFunction<T extends Function>(val: any): val is T {
14
- return typeof val === 'function'
15
- }
16
-
17
- export function isNumber(val: any): val is number {
18
- return typeof val === 'number'
19
- }
20
-
21
- export function isString(val: unknown): val is string {
22
- return typeof val === 'string'
23
- }
24
-
25
- export function isObject(val: any): val is object {
26
- return toString(val) === '[object Object]'
27
- }
28
-
29
- export function isWindow(val: any): boolean {
30
- return typeof window !== 'undefined' && toString(val) === '[object Window]'
31
- }
32
-
33
- export const isBrowser: boolean = typeof window !== 'undefined'
34
-
35
- export const isServer: boolean = typeof document === 'undefined' // https://remix.run/docs/en/v1/pages/gotchas#typeof-window-checks
36
-
37
- export function isMap(val: any): val is Map<any, any> {
38
- return toString(val) === '[object Map]'
39
- }
40
-
41
- export function isSet(val: any): val is Set<any> {
42
- return toString(val) === '[object Set]'
43
- }
44
-
45
- export function isPromise<T = any>(val: any): val is Promise<T> {
46
- return toString(val) === '[object Promise]'
47
- }
48
-
49
- export function isUndefined(v: any): boolean {
50
- return getTypeName(v) === 'undefined'
51
- }
52
-
53
- export function isNull(v: any): boolean {
54
- return getTypeName(v) === 'null'
55
- }
56
-
57
- export function isSymbol(v: any): boolean {
58
- return getTypeName(v) === 'symbol'
59
- }
60
-
61
- export function isDate(v: any): boolean {
62
- return getTypeName(v) === 'date'
63
- }
64
-
65
- export function isRegExp(v: any): boolean {
66
- return getTypeName(v) === 'regexp'
67
- }
68
-
69
- export function isArray(v: any): boolean {
70
- return getTypeName(v) === 'array'
71
- }
72
-
73
- export function isPrimitive(v: any): boolean {
74
- const type = getTypeName(v)
75
- return (
76
- type === 'null'
77
- || type === 'undefined'
78
- || type === 'string'
79
- || type === 'number'
80
- || type === 'boolean'
81
- || type === 'symbol'
82
- )
83
- }
84
-
85
- export function isInteger(v: any): boolean {
86
- return isNumber(v) && Number.isInteger(v)
87
- }
88
-
89
- export function isFloat(v: any): boolean {
90
- return isNumber(v) && !Number.isInteger(v)
91
- }
92
-
93
- export function isPositive(v: any): boolean {
94
- return isNumber(v) && v > 0
95
- }
96
-
97
- export function isNegative(v: any): boolean {
98
- return isNumber(v) && v < 0
99
- }
100
-
101
- export function isEven(v: any): boolean {
102
- return isNumber(v) && v % 2 === 0
103
- }
104
-
105
- export function isOdd(v: any): boolean {
106
- return isNumber(v) && v % 2 !== 0
107
- }
108
-
109
- export function isEvenOrOdd(v: any): 'even' | 'odd' {
110
- return isNumber(v) ? (v % 2 === 0 ? 'even' : 'odd') : 'odd'
111
- }
112
-
113
- export function isPositiveOrNegative(v: any): 'positive' | 'negative' {
114
- return isNumber(v) ? (v > 0 ? 'positive' : 'negative') : 'negative'
115
- }
116
-
117
- export function isIntegerOrFloat(v: any): 'integer' | 'float' {
118
- return isNumber(v) ? (Number.isInteger(v) ? 'integer' : 'float') : 'float'
119
- }
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,21 +0,0 @@
1
- import schema, { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine'
2
- import rule from 'validator'
3
-
4
- export { rule, schema, SimpleMessagesProvider, VineError }
5
-
6
- type SchemaString = string
7
- type SchemaNumber = number
8
- type SchemaBoolean = boolean
9
- type SchemaEnum = string[]
10
-
11
- export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum
12
-
13
- export { VineBoolean, VineDate, VineEnum, VineNumber, VineString } from '@vinejs/vine'
14
- export type { Infer } from '@vinejs/vine/types'
15
-
16
- export const validate = {
17
- string: (defaultValue = ''): SchemaString => defaultValue,
18
- number: (defaultValue = 1): SchemaNumber => defaultValue,
19
- boolean: (defaultValue = true): SchemaBoolean => defaultValue,
20
- enum: (values: string[]): SchemaEnum => values,
21
- }
@@ -1,6 +0,0 @@
1
- export {
2
- VineBoolean as ValidationBoolean,
3
- VineEnum as ValidationEnum,
4
- VineNumber as ValidationNumber,
5
- VineString as ValidationString,
6
- } from '@vinejs/vine'
package/src/validator.ts DELETED
@@ -1,93 +0,0 @@
1
- import type { Model, VineType } from '@stacksjs/types'
2
- import type { SchemaTypes } from '@vinejs/vine/types'
3
- import { path } from '@stacksjs/path'
4
- import { snakeCase } from '@stacksjs/strings'
5
- import { reportError, schema, SimpleMessagesProvider, VineError } from './'
6
-
7
- interface RequestData {
8
- [key: string]: any
9
- }
10
-
11
- interface ValidationField {
12
- rule: VineType
13
- message: Record<string, string>
14
- }
15
-
16
- interface CustomAttributes {
17
- [key: string]: ValidationField
18
- }
19
-
20
- export function isObjectNotEmpty(obj: object | undefined): boolean {
21
- if (obj === undefined)
22
- return false
23
-
24
- return Object.keys(obj).length > 0
25
- }
26
-
27
- export async function validateField(modelFile: string, params: RequestData): Promise<any> {
28
- const model = (await import(/* @vite-ignore */ path.userModelsPath(`${modelFile}.ts`))).default as Model
29
- const attributes = model.attributes
30
-
31
- const ruleObject: Record<string, SchemaTypes> = {}
32
- const messageObject: Record<string, string> = {}
33
-
34
- for (const key in attributes) {
35
- if (Object.prototype.hasOwnProperty.call(attributes, key)) {
36
- ruleObject[snakeCase(key)] = attributes[key]?.validation?.rule
37
- const validatorMessages = attributes[key]?.validation?.message
38
-
39
- for (const validatorMessageKey in validatorMessages) {
40
- const validatorMessageString = `${key}.${validatorMessageKey}`
41
- messageObject[validatorMessageString] = attributes[key]?.validation?.message[validatorMessageKey] || ''
42
- }
43
- }
44
- }
45
-
46
- schema.messagesProvider = new SimpleMessagesProvider(messageObject)
47
-
48
- try {
49
- const vineSchema = schema.object(ruleObject)
50
- const validator = schema.compile(vineSchema)
51
- await validator.validate(params)
52
- }
53
- catch (error: any) {
54
- if (error instanceof VineError.E_VALIDATION_ERROR)
55
- reportError(error.messages)
56
-
57
- throw { status: 422, errors: error.messages }
58
- }
59
- }
60
-
61
- export async function customValidate(attributes: CustomAttributes, params: RequestData): Promise<any> {
62
- const ruleObject: Record<string, SchemaTypes> = {}
63
- const messageObject: Record<string, string> = {}
64
-
65
- for (const key in attributes) {
66
- if (Object.prototype.hasOwnProperty.call(attributes, key)) {
67
- const rule = attributes[key]?.rule
68
- if (rule)
69
- ruleObject[key] = rule as SchemaTypes
70
-
71
- const validatorMessages = attributes[key]?.message
72
-
73
- for (const validatorMessageKey in validatorMessages) {
74
- const validatorMessageString = `${key}.${validatorMessageKey}`
75
- messageObject[validatorMessageString] = attributes[key]?.message[validatorMessageKey] || ''
76
- }
77
- }
78
- }
79
-
80
- schema.messagesProvider = new SimpleMessagesProvider(messageObject)
81
-
82
- try {
83
- const vineSchema = schema.object(ruleObject)
84
- const validator = schema.compile(vineSchema)
85
- await validator.validate(params)
86
- }
87
- catch (error: any) {
88
- if (error instanceof VineError.E_VALIDATION_ERROR)
89
- reportError(error.messages)
90
-
91
- throw { status: 422, errors: error.messages }
92
- }
93
- }