@stacksjs/validation 0.70.22 → 0.70.25

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.
@@ -1,7 +1,7 @@
1
+ export declare function reportError(errors: MessageObject[]): void;
2
+ export declare function getErrors(): MessageObject[];
1
3
  declare interface MessageObject {
2
4
  message: string
3
- rule: string
5
+ value: string
4
6
  field: string
5
7
  }
6
- export declare function reportError(errors: MessageObject[]): void;
7
- export declare function getErrors(): MessageObject[];
package/dist/schema.d.ts CHANGED
@@ -1,21 +1,2 @@
1
- import rule from 'validator';
2
- import schema, { SimpleMessagesProvider, errors as VineError } from '@vinejs/vine';
3
-
4
- export type { Infer } from '@vinejs/vine/types'
5
- declare type SchemaString = string
6
- type SchemaNumber = number
7
- type SchemaBoolean = boolean
8
- type SchemaEnum = string[]
9
-
10
- export type SchemaType = SchemaString | SchemaNumber | SchemaBoolean | SchemaEnum
11
-
12
-
13
- export const validate = {
14
- string: (defaultValue = ''): SchemaString => defaultValue,
15
- number: (defaultValue = 1): SchemaNumber => defaultValue,
16
- boolean: (defaultValue = true): SchemaBoolean => defaultValue,
17
- enum: (values: string[]): SchemaEnum => values,
18
- }
19
-
20
- export { rule, schema, SimpleMessagesProvider, VineError }
21
- export { VineBoolean, VineDate, VineEnum, VineNumber, VineString } from '@vinejs/vine'
1
+ import type { ValidationInstance } from '@stacksjs/ts-validation';
2
+ export declare const schema: ValidationInstance;
@@ -1,15 +1,78 @@
1
- import type { VineType } from '@stacksjs/types';
2
-
1
+ import type { Validator } from '@stacksjs/ts-validation';
2
+ export declare function isObjectNotEmpty(obj: object | undefined): boolean;
3
+ export declare function validateField(modelFile: string, params: RequestData): Promise<any>;
4
+ /**
5
+ * Register a custom validation rule that can be referenced by name.
6
+ */
7
+ export declare function registerRule(name: string, rule: AsyncValidator): void;
8
+ /**
9
+ * Get a registered custom rule by name.
10
+ */
11
+ export declare function getCustomRule(name: string): AsyncValidator | undefined;
12
+ /**
13
+ * Built-in async rule: checks that a value is unique in a database table.
14
+ */
15
+ export declare function unique(table: string, column: string, exceptId?: number): AsyncValidator;
16
+ /**
17
+ * Built-in async rule: checks that a value exists in a database table.
18
+ */
19
+ export declare function exists(table: string, column: string): AsyncValidator;
20
+ /**
21
+ * Validate fields with both sync rules (from model) and async rules.
22
+ * Runs sync validation first, then async rules, combining all errors.
23
+ */
24
+ export declare function validateFieldAsync(modelFile: string, params: RequestData, asyncRules?: Record<string, AsyncValidator>): Promise<any>;
25
+ /**
26
+ * Action-side validation shortcut. Returns the validated input typed as
27
+ * `T` so callers can drop the post-validate type-cast they used to need.
28
+ *
29
+ * Throws `HttpError(422, 'Validation failed', { errors })` on the first
30
+ * failing field — the router's error handler turns that into a JSON
31
+ * 422 response automatically.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { validate, schema } from '@stacksjs/validation'
36
+ *
37
+ * type Payload = { email: string; age: number }
38
+ *
39
+ * const data = await validate<Payload>(req, {
40
+ * email: schema.string().email(),
41
+ * age: schema.number().min(0),
42
+ * })
43
+ *
44
+ * // `data.email` is `string`, `data.age` is `number` — no cast needed.
45
+ * ```
46
+ */
47
+ export declare function validate<T = Record<string, unknown>>(request: RequestLike | Record<string, unknown>, rules: ValidationRules): Promise<T>;
48
+ export declare function customValidate(attributes: CustomAttributes, params: RequestData): Promise<any>;
3
49
  declare interface RequestData {
4
50
  [key: string]: any
5
51
  }
6
52
  declare interface ValidationField {
7
- rule: VineType
53
+ rule: ValidationType
8
54
  message: Record<string, string>
9
55
  }
10
56
  declare interface CustomAttributes {
11
57
  [key: string]: ValidationField
12
58
  }
13
- export declare function isObjectNotEmpty(obj: object | undefined): boolean;
14
- export declare function validateField(modelFile: string, params: RequestData): Promise<any>;
15
- export declare function customValidate(attributes: CustomAttributes, params: RequestData): Promise<any>;
59
+ declare interface RequestLike {
60
+ all?: () => Record<string, unknown> | Promise<Record<string, unknown>>
61
+ jsonBody?: Record<string, unknown>
62
+ formBody?: Record<string, unknown>
63
+ query?: Record<string, unknown>
64
+ params?: Record<string, unknown>
65
+ url?: string
66
+ }
67
+ /**
68
+ * Async validation rule type.
69
+ * Return true if valid, or a string error message if invalid.
70
+ */
71
+ // eslint-disable-next-line pickier/no-unused-vars
72
+ export type AsyncValidator = (value: unknown, params: RequestData) => Promise<boolean | string>;
73
+ /**
74
+ * Shape of a validation rule set understood by `validate()`. Each key
75
+ * maps to either a raw `Validator<T>` (from `@stacksjs/ts-validation`)
76
+ * or a `{ rule, message }` object that mirrors the model-attribute form.
77
+ */
78
+ export type ValidationRules = Record<string, Validator<any> | { rule: Validator<any>, message?: string | Record<string, string> }>;
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@stacksjs/validation",
3
3
  "type": "module",
4
- "version": "0.70.22",
4
+ "version": "0.70.25",
5
5
  "description": "The Stacks Validation ways.",
6
6
  "author": "Chris Breuer",
7
- "contributors": ["Chris Breuer <chris@stacksjs.org>"],
7
+ "contributors": [
8
+ "Chris Breuer <chris@stacksjs.com>"
9
+ ],
8
10
  "license": "MIT",
9
11
  "funding": "https://github.com/sponsors/chrisbbreuer",
10
12
  "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/validation#readme",
@@ -16,9 +18,15 @@
16
18
  "bugs": {
17
19
  "url": "https://github.com/stacksjs/stacks/issues"
18
20
  },
19
- "keywords": ["vinejs", "validation", "utilities", "functions", "stacks"],
21
+ "keywords": [
22
+ "validation",
23
+ "utilities",
24
+ "functions",
25
+ "stacks"
26
+ ],
20
27
  "exports": {
21
28
  ".": {
29
+ "bun": "./src/index.ts",
22
30
  "types": "./dist/index.d.ts",
23
31
  "import": "./dist/index.js"
24
32
  },
@@ -30,16 +38,21 @@
30
38
  "main": "dist/index.cjs",
31
39
  "module": "dist/index.js",
32
40
  "types": "dist/index.d.ts",
33
- "files": ["README.md", "dist"],
41
+ "files": [
42
+ "README.md",
43
+ "dist"
44
+ ],
34
45
  "scripts": {
35
46
  "build": "bun build.ts",
36
47
  "typecheck": "bun tsc --noEmit",
37
48
  "prepublishOnly": "bun run build"
38
49
  },
50
+ "dependencies": {
51
+ "@stacksjs/ts-validation": "^0.4.9"
52
+ },
39
53
  "devDependencies": {
40
- "@stacksjs/development": "0.70.18",
41
- "@stacksjs/strings": "0.70.18",
42
- "@stacksjs/types": "0.70.18",
43
- "@vinejs/vine": "^3.0.1"
54
+ "better-dx": "^0.2.12",
55
+ "@stacksjs/strings": "0.70.23",
56
+ "@stacksjs/types": "0.70.23"
44
57
  }
45
58
  }
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): '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 DELETED
@@ -1,30 +0,0 @@
1
- export declare interface FieldContext {
2
- value: unknown
3
-
4
- data: any
5
-
6
- meta: Record<string, any>
7
-
8
- mutate: (newValue: any, field: FieldContext) => void
9
-
10
- report: ErrorReporterContract['report']
11
-
12
- isValid: boolean
13
-
14
- isDefined: boolean
15
-
16
- wildCardPath: string
17
-
18
- parent: any
19
-
20
- name: string | number
21
-
22
- isArrayMember: boolean
23
- }
24
- export declare interface ErrorReporterContract {
25
- hasErrors: boolean
26
-
27
- createError: () => Error
28
-
29
- report: (message: string, rule: string, field: FieldContext, args?: Record<string, any>) => any
30
- }