@pogodisco/val 0.0.3 → 0.1.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/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/validate.d.ts +2 -3
- package/dist/utils/validate.js +5 -9
- package/dist/utils/validation-schema-error.d.ts +11 -0
- package/dist/utils/validation-schema-error.js +30 -0
- package/dist/utils/wrap-async-rule.d.ts +1 -3
- package/dist/utils/wrap-async-rule.js +4 -4
- package/package.json +1 -5
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
package/dist/utils/validate.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { type TResponse } from "@pogodisco/response";
|
|
1
|
+
import { ValidationRule } from "./validation-rule.js";
|
|
3
2
|
export declare function validate({ rules, }: {
|
|
4
3
|
rules: ValidationRule[];
|
|
5
|
-
}): Promise<
|
|
4
|
+
}): Promise<void>;
|
package/dist/utils/validate.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ValidationSchemaError } from "./validation-schema-error.js";
|
|
2
|
+
// validate.ts
|
|
2
3
|
export async function validate({ rules, }) {
|
|
3
4
|
const validationErrors = {};
|
|
4
5
|
for (const rule of rules) {
|
|
5
6
|
if (!(await rule.shouldRun()))
|
|
6
7
|
continue;
|
|
7
|
-
const
|
|
8
|
-
const isAppResponse = typeof result === "object" && result !== null && "ok" in result;
|
|
9
|
-
const isValid = isAppResponse ? result?.ok : result;
|
|
8
|
+
const isValid = await rule.rule();
|
|
10
9
|
if (!isValid) {
|
|
11
10
|
for (const [key, message] of Object.entries(rule.errors)) {
|
|
12
11
|
validationErrors[key] = [
|
|
@@ -15,14 +14,11 @@ export async function validate({ rules, }) {
|
|
|
15
14
|
];
|
|
16
15
|
}
|
|
17
16
|
if (rule.bail ?? true) {
|
|
18
|
-
|
|
19
|
-
errors: validationErrors,
|
|
20
|
-
});
|
|
17
|
+
throw new ValidationSchemaError(validationErrors);
|
|
21
18
|
}
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
21
|
if (Object.keys(validationErrors).length > 0) {
|
|
25
|
-
|
|
22
|
+
throw new ValidationSchemaError(validationErrors);
|
|
26
23
|
}
|
|
27
|
-
return newSuccessResponse(null);
|
|
28
24
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class ValidationSchemaError extends Error {
|
|
2
|
+
readonly errors: Record<string, string[]>;
|
|
3
|
+
constructor(errors: Record<string, string[]>);
|
|
4
|
+
toJSON(): {
|
|
5
|
+
name: string;
|
|
6
|
+
message: string;
|
|
7
|
+
errors: Record<string, string[]>;
|
|
8
|
+
};
|
|
9
|
+
static fromJSON(json: any): ValidationSchemaError;
|
|
10
|
+
}
|
|
11
|
+
export declare function isValidationSchemaError(error: unknown): error is ValidationSchemaError;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export class ValidationSchemaError extends Error {
|
|
2
|
+
constructor(errors) {
|
|
3
|
+
// Store a readable message without stringifying
|
|
4
|
+
super(`Validation failed with ${Object.keys(errors).length} error(s)`);
|
|
5
|
+
this.name = "ValidationSchemaError";
|
|
6
|
+
this.errors = errors;
|
|
7
|
+
// Maintain proper prototype chain
|
|
8
|
+
Object.setPrototypeOf(this, ValidationSchemaError.prototype);
|
|
9
|
+
}
|
|
10
|
+
// Optional: for when you need to serialize/deserialize
|
|
11
|
+
toJSON() {
|
|
12
|
+
return {
|
|
13
|
+
name: this.name,
|
|
14
|
+
message: this.message,
|
|
15
|
+
errors: this.errors,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
static fromJSON(json) {
|
|
19
|
+
return new ValidationSchemaError(json.errors);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function isValidationSchemaError(error) {
|
|
23
|
+
return (typeof error === "object" &&
|
|
24
|
+
error !== null &&
|
|
25
|
+
(error instanceof ValidationSchemaError ||
|
|
26
|
+
("name" in error &&
|
|
27
|
+
error.name === "ValidationSchemaError" &&
|
|
28
|
+
"errors" in error &&
|
|
29
|
+
typeof error.errors === "object")));
|
|
30
|
+
}
|
|
@@ -1,4 +1,2 @@
|
|
|
1
1
|
import { ValidationRuleBuilder } from "./validation-rule-builder.js";
|
|
2
|
-
export declare function wrapAsyncRule<T extends any[]>(fn: (...args: T) => Promise<
|
|
3
|
-
ok: boolean;
|
|
4
|
-
}>): (...args: T) => ValidationRuleBuilder;
|
|
2
|
+
export declare function wrapAsyncRule<T extends any[]>(fn: (...args: T) => Promise<boolean>): (...args: T) => ValidationRuleBuilder;
|
|
@@ -2,21 +2,21 @@ import { ValidationRuleBuilder } from "./validation-rule-builder.js";
|
|
|
2
2
|
export function wrapAsyncRule(fn) {
|
|
3
3
|
return (...args) => {
|
|
4
4
|
const ruleFn = async () => {
|
|
5
|
-
const
|
|
6
|
-
return
|
|
5
|
+
const ok = await fn(...args);
|
|
6
|
+
return ok;
|
|
7
7
|
};
|
|
8
8
|
const builder = new ValidationRuleBuilder(ruleFn);
|
|
9
9
|
const ruleName = fn.name;
|
|
10
10
|
if (ruleName === "isOptional") {
|
|
11
11
|
builder.runIf(async () => {
|
|
12
12
|
const result = await fn(...args);
|
|
13
|
-
return result
|
|
13
|
+
return result;
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
if (ruleName === "isTrue" || ruleName === "isFalse") {
|
|
17
17
|
builder.runIf(async () => {
|
|
18
18
|
const result = await fn(...args);
|
|
19
|
-
return result
|
|
19
|
+
return result;
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
22
|
if (["requiredIf", "requiredUnless", "requiredIfElseEmpty"].includes(ruleName)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pogodisco/val",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -28,10 +28,6 @@
|
|
|
28
28
|
"prepublishOnly": "npm run build"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@pogodisco/response": "~0.0.1",
|
|
32
31
|
"typescript": "^5.0.0"
|
|
33
|
-
},
|
|
34
|
-
"devDependencies": {
|
|
35
|
-
"@pogodisco/response": "~0.0.1"
|
|
36
32
|
}
|
|
37
33
|
}
|