@stacksjs/validation 0.59.11 → 0.61.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 +6208 -29
- package/package.json +4 -5
- package/src/index.ts +1 -1
- package/src/is.ts +8 -1
- package/src/rules.ts +5 -9
- package/src/schema.ts +25 -0
- package/src/types/money.ts +2 -5
- package/dist/index.d.ts +0 -4
- package/dist/is.d.ts +0 -28
- package/dist/rules.d.ts +0 -80
- package/dist/types/index.d.ts +0 -2
- package/dist/types/money.d.ts +0 -7
- package/dist/validate.d.ts +0 -21
- package/src/validate.ts +0 -33
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.61.1",
|
|
5
5
|
"description": "The Stacks Validation ways.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -50,15 +50,14 @@
|
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@stacksjs/strings": "latest",
|
|
52
52
|
"@stacksjs/types": "latest",
|
|
53
|
-
"@vinejs/vine": "^
|
|
53
|
+
"@vinejs/vine": "^2.0.0"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@stacksjs/strings": "latest",
|
|
57
57
|
"@stacksjs/types": "latest",
|
|
58
|
-
"@vinejs/vine": "^
|
|
58
|
+
"@vinejs/vine": "^2.0.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@stacksjs/development": "latest"
|
|
62
|
-
"@stacksjs/vite": "latest"
|
|
61
|
+
"@stacksjs/development": "latest"
|
|
63
62
|
}
|
|
64
63
|
}
|
package/src/index.ts
CHANGED
package/src/is.ts
CHANGED
|
@@ -71,7 +71,14 @@ export function isArray(v: any) {
|
|
|
71
71
|
|
|
72
72
|
export function isPrimitive(v: any) {
|
|
73
73
|
const type = getTypeName(v)
|
|
74
|
-
return
|
|
74
|
+
return (
|
|
75
|
+
type === 'null' ||
|
|
76
|
+
type === 'undefined' ||
|
|
77
|
+
type === 'string' ||
|
|
78
|
+
type === 'number' ||
|
|
79
|
+
type === 'boolean' ||
|
|
80
|
+
type === 'symbol'
|
|
81
|
+
)
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
export function isInteger(v: any) {
|
package/src/rules.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { USD } from '@dinero.js/currencies'
|
|
2
2
|
import { dinero as currency } from 'dinero.js'
|
|
3
|
-
import {
|
|
3
|
+
import { schema } from './schema'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Thanks to VineJS for the following types:
|
|
@@ -95,22 +95,18 @@ export interface ErrorReporterContract {
|
|
|
95
95
|
report: (message: string, rule: string, field: FieldContext, args?: Record<string, any>) => any
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
export const isMoney =
|
|
98
|
+
export const isMoney = schema.createRule((value: unknown, _, field: FieldContext) => {
|
|
99
99
|
/**
|
|
100
100
|
* Convert string representation of a number to a JavaScript
|
|
101
101
|
* Number data type.
|
|
102
102
|
*/
|
|
103
|
-
const numericValue =
|
|
103
|
+
const numericValue = schema.helpers.asNumber(value)
|
|
104
104
|
|
|
105
105
|
/**
|
|
106
106
|
* Report error, if the value is NaN post-conversion
|
|
107
107
|
*/
|
|
108
108
|
if (Number.isNaN(numericValue)) {
|
|
109
|
-
field.report(
|
|
110
|
-
'The {{ field }} field value must be a number',
|
|
111
|
-
'money',
|
|
112
|
-
field,
|
|
113
|
-
)
|
|
109
|
+
field.report('The {{ field }} field value must be a number', 'money', field)
|
|
114
110
|
return
|
|
115
111
|
}
|
|
116
112
|
|
|
@@ -123,4 +119,4 @@ export const isMoney = validator.createRule((value: unknown, _, field: FieldCont
|
|
|
123
119
|
* Mutate the field's value
|
|
124
120
|
*/
|
|
125
121
|
field.mutate(amount, field)
|
|
126
|
-
})
|
|
122
|
+
})
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import schema, { Vine } from '@vinejs/vine'
|
|
2
|
+
import rule from 'validator'
|
|
3
|
+
import { MoneyValidator } from './types/money'
|
|
4
|
+
|
|
5
|
+
export { schema, rule }
|
|
6
|
+
|
|
7
|
+
// @ts-expect-error - investigate why this is not working
|
|
8
|
+
Vine.macro('money', () => new MoneyValidator())
|
|
9
|
+
|
|
10
|
+
type SchemaString = string
|
|
11
|
+
type SchemaNumber = number
|
|
12
|
+
type SchemaBoolean = boolean
|
|
13
|
+
type SchemaEnum = string[]
|
|
14
|
+
|
|
15
|
+
export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum
|
|
16
|
+
|
|
17
|
+
export type { Infer } from '@vinejs/vine/types'
|
|
18
|
+
export { VineString, VineBoolean, VineEnum, VineNumber, VineDate } from '@vinejs/vine'
|
|
19
|
+
|
|
20
|
+
export const validate = {
|
|
21
|
+
string: (defaultValue = ''): SchemaString => defaultValue,
|
|
22
|
+
number: (defaultValue = 1): SchemaNumber => defaultValue,
|
|
23
|
+
boolean: (defaultValue = true): SchemaBoolean => defaultValue,
|
|
24
|
+
enum: (values: string[]): SchemaEnum => values,
|
|
25
|
+
}
|
package/src/types/money.ts
CHANGED
|
@@ -3,15 +3,12 @@ import { BaseLiteralType } from '@vinejs/vine'
|
|
|
3
3
|
import type { FieldOptions, Validation } from '@vinejs/vine/types'
|
|
4
4
|
import { isMoney } from '../rules'
|
|
5
5
|
|
|
6
|
-
export class MoneyValidator extends BaseLiteralType<Money, Money> {
|
|
6
|
+
export class MoneyValidator extends BaseLiteralType<string, Money, Money> {
|
|
7
7
|
constructor(options?: FieldOptions, validations?: Validation<any>[]) {
|
|
8
8
|
super(options, validations || [isMoney()])
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
clone() {
|
|
12
|
-
return new MoneyValidator(
|
|
13
|
-
this.cloneOptions(),
|
|
14
|
-
this.cloneValidations(),
|
|
15
|
-
) as this
|
|
12
|
+
return new MoneyValidator(this.cloneOptions(), this.cloneValidations()) as this
|
|
16
13
|
}
|
|
17
14
|
}
|
package/dist/index.d.ts
DELETED
package/dist/is.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
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): false | "even" | "odd";
|
|
27
|
-
export declare function isPositiveOrNegative(v: any): false | "positive" | "negative";
|
|
28
|
-
export declare function isIntegerOrFloat(v: any): false | "integer" | "float";
|
package/dist/rules.d.ts
DELETED
|
@@ -1,80 +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
|
-
* 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
|
-
}
|
|
80
|
-
export declare const isMoney: any;
|
package/dist/types/index.d.ts
DELETED
package/dist/types/money.d.ts
DELETED
|
@@ -1,7 +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
|
-
export declare class MoneyValidator extends BaseLiteralType<Money, Money> {
|
|
5
|
-
constructor(options?: FieldOptions, validations?: Validation<any>[]);
|
|
6
|
-
clone(): this;
|
|
7
|
-
}
|
package/dist/validate.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Vine as Validator } from '@vinejs/vine';
|
|
2
|
-
export declare const validator: Validator;
|
|
3
|
-
type SchemaString = string;
|
|
4
|
-
type SchemaNumber = number;
|
|
5
|
-
type SchemaBoolean = boolean;
|
|
6
|
-
type SchemaEnum = string[];
|
|
7
|
-
export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum;
|
|
8
|
-
export declare const validate: {
|
|
9
|
-
string: (defaultValue?: string) => SchemaString;
|
|
10
|
-
number: (defaultValue?: number) => SchemaNumber;
|
|
11
|
-
boolean: (defaultValue?: boolean) => SchemaBoolean;
|
|
12
|
-
enum: (values: string[]) => SchemaEnum;
|
|
13
|
-
};
|
|
14
|
-
export declare const RuleString: Validator['string'];
|
|
15
|
-
export declare const RuleNumber: Validator['number'];
|
|
16
|
-
export declare const RuleBoolean: Validator['boolean'];
|
|
17
|
-
export declare const RuleArray: Validator['array'];
|
|
18
|
-
export declare const RuleObject: Validator['object'];
|
|
19
|
-
export declare const RuleAny: Validator['any'];
|
|
20
|
-
export type { Infer } from '@vinejs/vine/types';
|
|
21
|
-
export { VineString, VineBoolean, VineEnum, VineNumber } from '@vinejs/vine';
|
package/src/validate.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import v, { Vine as Validator } from '@vinejs/vine'
|
|
2
|
-
import { MoneyValidator } from './types/money'
|
|
3
|
-
|
|
4
|
-
Validator.macro('money', () => {
|
|
5
|
-
return new MoneyValidator()
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
export const validator: Validator = v
|
|
9
|
-
// validator.string().email()
|
|
10
|
-
type SchemaString = string
|
|
11
|
-
type SchemaNumber = number
|
|
12
|
-
type SchemaBoolean = boolean
|
|
13
|
-
type SchemaEnum = string[]
|
|
14
|
-
|
|
15
|
-
export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum
|
|
16
|
-
|
|
17
|
-
export const validate = {
|
|
18
|
-
string: (defaultValue: string = ''): SchemaString => defaultValue,
|
|
19
|
-
number: (defaultValue: number = 1): SchemaNumber => defaultValue,
|
|
20
|
-
boolean: (defaultValue: boolean = true): SchemaBoolean => defaultValue,
|
|
21
|
-
enum: (values: string[]): SchemaEnum => values,
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const RuleString: Validator['string'] = (...args) => validator.string(...args)
|
|
25
|
-
export const RuleNumber: Validator['number'] = (...args) => validator.number(...args)
|
|
26
|
-
export const RuleBoolean: Validator['boolean'] = (...args) => validator.boolean(...args)
|
|
27
|
-
export const RuleArray: Validator['array'] = (...args) => validator.array(...args)
|
|
28
|
-
export const RuleObject: Validator['object'] = (...args) => validator.object(...args)
|
|
29
|
-
export const RuleAny: Validator['any'] = (...args) => validator.any(...args)
|
|
30
|
-
// const email = () => validator.string().email()
|
|
31
|
-
|
|
32
|
-
export type { Infer } from '@vinejs/vine/types'
|
|
33
|
-
export { VineString, VineBoolean, VineEnum, VineNumber } from '@vinejs/vine'
|