@stacksjs/validation 0.64.5 → 0.65.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 +17 -17
- package/dist/index.js.map +7 -7
- 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 -21
- package/src/is.ts +28 -27
- package/src/schema.ts +3 -4
- package/src/types/index.ts +1 -1
- package/src/validator.ts +17 -10
package/dist/is.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare function isDef<T = any>(val?: T): val is T;
|
|
2
|
+
export declare function isBoolean(val: any): val is boolean;
|
|
3
|
+
export declare function isFunction<T extends Function>(val: any): val is T;
|
|
4
|
+
export declare function isNumber(val: any): val is number;
|
|
5
|
+
export declare function isString(val: unknown): val is string;
|
|
6
|
+
export declare function isObject(val: any): val is object;
|
|
7
|
+
export declare function isWindow(val: any): boolean;
|
|
8
|
+
export declare const isBrowser: boolean;
|
|
9
|
+
export declare const isServer: boolean;
|
|
10
|
+
export declare function isMap(val: any): val is Map<any, any>;
|
|
11
|
+
export declare function isSet(val: any): val is Set<any>;
|
|
12
|
+
export declare function isPromise<T = any>(val: any): val is Promise<T>;
|
|
13
|
+
export declare function isUndefined(v: any): boolean;
|
|
14
|
+
export declare function isNull(v: any): boolean;
|
|
15
|
+
export declare function isSymbol(v: any): boolean;
|
|
16
|
+
export declare function isDate(v: any): boolean;
|
|
17
|
+
export declare function isRegExp(v: any): boolean;
|
|
18
|
+
export declare function isArray(v: any): boolean;
|
|
19
|
+
export declare function isPrimitive(v: any): boolean;
|
|
20
|
+
export declare function isInteger(v: any): boolean;
|
|
21
|
+
export declare function isFloat(v: any): boolean;
|
|
22
|
+
export declare function isPositive(v: any): boolean;
|
|
23
|
+
export declare function isNegative(v: any): boolean;
|
|
24
|
+
export declare function isEven(v: any): boolean;
|
|
25
|
+
export declare function isOdd(v: any): boolean;
|
|
26
|
+
export declare function isEvenOrOdd(v: any): "even" | "odd";
|
|
27
|
+
export declare function isPositiveOrNegative(v: any): "positive" | "negative";
|
|
28
|
+
export declare function isIntegerOrFloat(v: any): "integer" | "float";
|
package/dist/rules.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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
|
+
* The data property is the top-level object under validation.
|
|
14
|
+
*/
|
|
15
|
+
data: any;
|
|
16
|
+
/**
|
|
17
|
+
* Shared metadata across the entire validation lifecycle. It can be
|
|
18
|
+
* used to pass data between validation rules
|
|
19
|
+
*/
|
|
20
|
+
meta: Record<string, any>;
|
|
21
|
+
/**
|
|
22
|
+
* Mutate the value of field under validation.
|
|
23
|
+
*/
|
|
24
|
+
mutate: (newValue: any, field: FieldContext) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Report error to the error reporter
|
|
27
|
+
*/
|
|
28
|
+
report: ErrorReporterContract["report"];
|
|
29
|
+
/**
|
|
30
|
+
* Is this field valid. Default: true
|
|
31
|
+
*/
|
|
32
|
+
isValid: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Is this field has value defined.
|
|
35
|
+
*/
|
|
36
|
+
isDefined: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Wildcard path for the field. The value is a nested
|
|
39
|
+
* pointer to the field under validation.
|
|
40
|
+
*
|
|
41
|
+
* In case of arrays, the `*` wildcard is used.
|
|
42
|
+
*/
|
|
43
|
+
wildCardPath: string;
|
|
44
|
+
/**
|
|
45
|
+
* The parent property is the parent of the field. It could be an
|
|
46
|
+
* array or an object.
|
|
47
|
+
*/
|
|
48
|
+
parent: any;
|
|
49
|
+
/**
|
|
50
|
+
* Name of the field under validation. In case of an array, the field
|
|
51
|
+
* name will be a number
|
|
52
|
+
*/
|
|
53
|
+
name: string | number;
|
|
54
|
+
/**
|
|
55
|
+
* Is this field an array member
|
|
56
|
+
*/
|
|
57
|
+
isArrayMember: boolean;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Thanks to VineJS for the following types:
|
|
61
|
+
*
|
|
62
|
+
* The error reporter is used to report errors during the validation
|
|
63
|
+
* process.
|
|
64
|
+
*/
|
|
65
|
+
export interface ErrorReporterContract {
|
|
66
|
+
/**
|
|
67
|
+
* A boolean to known if there are one or more
|
|
68
|
+
* errors.
|
|
69
|
+
*/
|
|
70
|
+
hasErrors: boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Creates an instance of an exception to throw
|
|
73
|
+
*/
|
|
74
|
+
createError: () => Error;
|
|
75
|
+
/**
|
|
76
|
+
* Report error for a field
|
|
77
|
+
*/
|
|
78
|
+
report: (message: string, rule: string, field: FieldContext, args?: Record<string, any>) => any;
|
|
79
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import schema, { SimpleMessagesProvider, errors as VineError } from "@vinejs/vine";
|
|
2
|
+
import rule from "validator";
|
|
3
|
+
export { rule, schema, SimpleMessagesProvider, VineError };
|
|
4
|
+
type SchemaString = string;
|
|
5
|
+
type SchemaNumber = number;
|
|
6
|
+
type SchemaBoolean = boolean;
|
|
7
|
+
type SchemaEnum = string[];
|
|
8
|
+
export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum;
|
|
9
|
+
export { VineBoolean, VineDate, VineEnum, VineNumber, VineString } from "@vinejs/vine";
|
|
10
|
+
export type { Infer } from "@vinejs/vine/types";
|
|
11
|
+
export declare const validate: {
|
|
12
|
+
string: (defaultValue?: string) => SchemaString;
|
|
13
|
+
number: (defaultValue?: number) => SchemaNumber;
|
|
14
|
+
boolean: (defaultValue?: boolean) => SchemaBoolean;
|
|
15
|
+
enum: (values: string[]) => SchemaEnum;
|
|
16
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { VineBoolean as ValidationBoolean, VineEnum as ValidationEnum, VineNumber as ValidationNumber, VineString as ValidationString } from "@vinejs/vine";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { VineType } from "@stacksjs/types";
|
|
2
|
+
interface RequestData {
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
}
|
|
5
|
+
interface ValidationField {
|
|
6
|
+
rule: VineType;
|
|
7
|
+
message: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
interface CustomAttributes {
|
|
10
|
+
[key: string]: ValidationField;
|
|
11
|
+
}
|
|
12
|
+
export declare function isObjectNotEmpty(obj: object | undefined): boolean;
|
|
13
|
+
export declare function validateField(modelFile: string, params: RequestData): Promise<any>;
|
|
14
|
+
export declare function customValidate(attributes: CustomAttributes, params: RequestData): Promise<any>;
|
|
15
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/validation",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.65.0",
|
|
5
5
|
"description": "The Stacks Validation ways.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
|
+
"contributors": ["Chris Breuer <chris@stacksjs.org>"],
|
|
7
8
|
"license": "MIT",
|
|
8
9
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
9
10
|
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/validation#readme",
|
|
@@ -15,16 +16,11 @@
|
|
|
15
16
|
"bugs": {
|
|
16
17
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
17
18
|
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"vinejs",
|
|
20
|
-
"validation",
|
|
21
|
-
"utilities",
|
|
22
|
-
"functions",
|
|
23
|
-
"stacks"
|
|
24
|
-
],
|
|
19
|
+
"keywords": ["vinejs", "validation", "utilities", "functions", "stacks"],
|
|
25
20
|
"exports": {
|
|
26
21
|
".": {
|
|
27
22
|
"bun": "./src/index.ts",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
28
24
|
"import": "./dist/index.js"
|
|
29
25
|
},
|
|
30
26
|
"./*": {
|
|
@@ -34,25 +30,19 @@
|
|
|
34
30
|
},
|
|
35
31
|
"main": "dist/index.cjs",
|
|
36
32
|
"module": "dist/index.js",
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
],
|
|
40
|
-
"files": [
|
|
41
|
-
"README.md",
|
|
42
|
-
"dist",
|
|
43
|
-
"src"
|
|
44
|
-
],
|
|
33
|
+
"types": "dist/index.d.ts",
|
|
34
|
+
"files": ["README.md", "dist", "src"],
|
|
45
35
|
"scripts": {
|
|
46
|
-
"build": "bun
|
|
47
|
-
"typecheck": "bun
|
|
36
|
+
"build": "bun build.ts",
|
|
37
|
+
"typecheck": "bun tsc --noEmit",
|
|
48
38
|
"prepublishOnly": "bun run build"
|
|
49
39
|
},
|
|
50
40
|
"dependencies": {
|
|
51
|
-
"@stacksjs/strings": "
|
|
52
|
-
"@stacksjs/types": "
|
|
41
|
+
"@stacksjs/strings": "0.64.6",
|
|
42
|
+
"@stacksjs/types": "0.64.6",
|
|
53
43
|
"@vinejs/vine": "^2.1.0"
|
|
54
44
|
},
|
|
55
45
|
"devDependencies": {
|
|
56
|
-
"@stacksjs/development": "
|
|
46
|
+
"@stacksjs/development": "0.64.6"
|
|
57
47
|
}
|
|
58
48
|
}
|
package/src/is.ts
CHANGED
|
@@ -9,6 +9,7 @@ export function isBoolean(val: any): val is boolean {
|
|
|
9
9
|
return typeof val === 'boolean'
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
// eslint-disable-next-line ts/no-unsafe-function-type
|
|
12
13
|
export function isFunction<T extends Function>(val: any): val is T {
|
|
13
14
|
return typeof val === 'function'
|
|
14
15
|
}
|
|
@@ -29,9 +30,9 @@ export function isWindow(val: any): boolean {
|
|
|
29
30
|
return typeof window !== 'undefined' && toString(val) === '[object Window]'
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export const isBrowser = typeof window !== 'undefined'
|
|
33
|
+
export const isBrowser: boolean = typeof window !== 'undefined'
|
|
33
34
|
|
|
34
|
-
export const isServer = typeof document === 'undefined' // https://remix.run/docs/en/v1/pages/gotchas#typeof-window-checks
|
|
35
|
+
export const isServer: boolean = typeof document === 'undefined' // https://remix.run/docs/en/v1/pages/gotchas#typeof-window-checks
|
|
35
36
|
|
|
36
37
|
export function isMap(val: any): val is Map<any, any> {
|
|
37
38
|
return toString(val) === '[object Map]'
|
|
@@ -45,74 +46,74 @@ export function isPromise<T = any>(val: any): val is Promise<T> {
|
|
|
45
46
|
return toString(val) === '[object Promise]'
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
export function isUndefined(v: any) {
|
|
49
|
+
export function isUndefined(v: any): boolean {
|
|
49
50
|
return getTypeName(v) === 'undefined'
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
export function isNull(v: any) {
|
|
53
|
+
export function isNull(v: any): boolean {
|
|
53
54
|
return getTypeName(v) === 'null'
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
export function isSymbol(v: any) {
|
|
57
|
+
export function isSymbol(v: any): boolean {
|
|
57
58
|
return getTypeName(v) === 'symbol'
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
export function isDate(v: any) {
|
|
61
|
+
export function isDate(v: any): boolean {
|
|
61
62
|
return getTypeName(v) === 'date'
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
export function isRegExp(v: any) {
|
|
65
|
+
export function isRegExp(v: any): boolean {
|
|
65
66
|
return getTypeName(v) === 'regexp'
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
export function isArray(v: any) {
|
|
69
|
+
export function isArray(v: any): boolean {
|
|
69
70
|
return getTypeName(v) === 'array'
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
export function isPrimitive(v: any) {
|
|
73
|
+
export function isPrimitive(v: any): boolean {
|
|
73
74
|
const type = getTypeName(v)
|
|
74
75
|
return (
|
|
75
|
-
type === 'null'
|
|
76
|
-
type === 'undefined'
|
|
77
|
-
type === 'string'
|
|
78
|
-
type === 'number'
|
|
79
|
-
type === 'boolean'
|
|
80
|
-
type === 'symbol'
|
|
76
|
+
type === 'null'
|
|
77
|
+
|| type === 'undefined'
|
|
78
|
+
|| type === 'string'
|
|
79
|
+
|| type === 'number'
|
|
80
|
+
|| type === 'boolean'
|
|
81
|
+
|| type === 'symbol'
|
|
81
82
|
)
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
export function isInteger(v: any) {
|
|
85
|
+
export function isInteger(v: any): boolean {
|
|
85
86
|
return isNumber(v) && Number.isInteger(v)
|
|
86
87
|
}
|
|
87
88
|
|
|
88
|
-
export function isFloat(v: any) {
|
|
89
|
+
export function isFloat(v: any): boolean {
|
|
89
90
|
return isNumber(v) && !Number.isInteger(v)
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
export function isPositive(v: any) {
|
|
93
|
+
export function isPositive(v: any): boolean {
|
|
93
94
|
return isNumber(v) && v > 0
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
export function isNegative(v: any) {
|
|
97
|
+
export function isNegative(v: any): boolean {
|
|
97
98
|
return isNumber(v) && v < 0
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
export function isEven(v: any) {
|
|
101
|
+
export function isEven(v: any): boolean {
|
|
101
102
|
return isNumber(v) && v % 2 === 0
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
export function isOdd(v: any) {
|
|
105
|
+
export function isOdd(v: any): boolean {
|
|
105
106
|
return isNumber(v) && v % 2 !== 0
|
|
106
107
|
}
|
|
107
108
|
|
|
108
|
-
export function isEvenOrOdd(v: any) {
|
|
109
|
-
return isNumber(v)
|
|
109
|
+
export function isEvenOrOdd(v: any): 'even' | 'odd' {
|
|
110
|
+
return isNumber(v) ? (v % 2 === 0 ? 'even' : 'odd') : 'odd'
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
export function isPositiveOrNegative(v: any) {
|
|
113
|
-
return isNumber(v)
|
|
113
|
+
export function isPositiveOrNegative(v: any): 'positive' | 'negative' {
|
|
114
|
+
return isNumber(v) ? (v > 0 ? 'positive' : 'negative') : 'negative'
|
|
114
115
|
}
|
|
115
116
|
|
|
116
|
-
export function isIntegerOrFloat(v: any) {
|
|
117
|
-
return isNumber(v)
|
|
117
|
+
export function isIntegerOrFloat(v: any): 'integer' | 'float' {
|
|
118
|
+
return isNumber(v) ? (Number.isInteger(v) ? 'integer' : 'float') : 'float'
|
|
118
119
|
}
|
package/src/schema.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import schema from '@vinejs/vine'
|
|
2
|
-
import { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine'
|
|
1
|
+
import schema, { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine'
|
|
3
2
|
import rule from 'validator'
|
|
4
3
|
|
|
5
|
-
export {
|
|
4
|
+
export { rule, schema, SimpleMessagesProvider, VineError }
|
|
6
5
|
|
|
7
6
|
type SchemaString = string
|
|
8
7
|
type SchemaNumber = number
|
|
@@ -11,8 +10,8 @@ type SchemaEnum = string[]
|
|
|
11
10
|
|
|
12
11
|
export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum
|
|
13
12
|
|
|
13
|
+
export { VineBoolean, VineDate, VineEnum, VineNumber, VineString } from '@vinejs/vine'
|
|
14
14
|
export type { Infer } from '@vinejs/vine/types'
|
|
15
|
-
export { VineString, VineBoolean, VineEnum, VineNumber, VineDate } from '@vinejs/vine'
|
|
16
15
|
|
|
17
16
|
export const validate = {
|
|
18
17
|
string: (defaultValue = ''): SchemaString => defaultValue,
|
package/src/types/index.ts
CHANGED
package/src/validator.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import type { Model, VineType } from '@stacksjs/types'
|
|
2
|
+
import type { SchemaTypes } from '@vinejs/vine/types'
|
|
1
3
|
import { path } from '@stacksjs/path'
|
|
2
4
|
import { snakeCase } from '@stacksjs/strings'
|
|
3
|
-
import
|
|
4
|
-
import type { VineType } from '@stacksjs/types'
|
|
5
|
-
import type { SchemaTypes } from '@vinejs/vine/types'
|
|
6
|
-
import { SimpleMessagesProvider, VineError, reportError, schema } from './'
|
|
5
|
+
import { reportError, schema, SimpleMessagesProvider, VineError } from './'
|
|
7
6
|
|
|
8
7
|
interface RequestData {
|
|
9
8
|
[key: string]: any
|
|
@@ -18,7 +17,10 @@ interface CustomAttributes {
|
|
|
18
17
|
[key: string]: ValidationField
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
export function isObjectNotEmpty(obj: object): boolean {
|
|
20
|
+
export function isObjectNotEmpty(obj: object | undefined): boolean {
|
|
21
|
+
if (obj === undefined)
|
|
22
|
+
return false
|
|
23
|
+
|
|
22
24
|
return Object.keys(obj).length > 0
|
|
23
25
|
}
|
|
24
26
|
|
|
@@ -47,8 +49,10 @@ export async function validateField(modelFile: string, params: RequestData): Pro
|
|
|
47
49
|
const vineSchema = schema.object(ruleObject)
|
|
48
50
|
const validator = schema.compile(vineSchema)
|
|
49
51
|
await validator.validate(params)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
+
}
|
|
53
|
+
catch (error: any) {
|
|
54
|
+
if (error instanceof VineError.E_VALIDATION_ERROR)
|
|
55
|
+
reportError(error.messages)
|
|
52
56
|
|
|
53
57
|
throw { status: 422, errors: error.messages }
|
|
54
58
|
}
|
|
@@ -61,7 +65,8 @@ export async function customValidate(attributes: CustomAttributes, params: Reque
|
|
|
61
65
|
for (const key in attributes) {
|
|
62
66
|
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
|
|
63
67
|
const rule = attributes[key]?.rule
|
|
64
|
-
if (rule)
|
|
68
|
+
if (rule)
|
|
69
|
+
ruleObject[key] = rule as SchemaTypes
|
|
65
70
|
|
|
66
71
|
const validatorMessages = attributes[key]?.message
|
|
67
72
|
|
|
@@ -78,8 +83,10 @@ export async function customValidate(attributes: CustomAttributes, params: Reque
|
|
|
78
83
|
const vineSchema = schema.object(ruleObject)
|
|
79
84
|
const validator = schema.compile(vineSchema)
|
|
80
85
|
await validator.validate(params)
|
|
81
|
-
}
|
|
82
|
-
|
|
86
|
+
}
|
|
87
|
+
catch (error: any) {
|
|
88
|
+
if (error instanceof VineError.E_VALIDATION_ERROR)
|
|
89
|
+
reportError(error.messages)
|
|
83
90
|
|
|
84
91
|
throw { status: 422, errors: error.messages }
|
|
85
92
|
}
|