@pogodisco/val 0.1.0 → 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/validate.js
CHANGED
|
@@ -5,9 +5,7 @@ export async function validate({ rules, }) {
|
|
|
5
5
|
for (const rule of rules) {
|
|
6
6
|
if (!(await rule.shouldRun()))
|
|
7
7
|
continue;
|
|
8
|
-
const
|
|
9
|
-
const isAppResponse = typeof result === "object" && result !== null && "ok" in result;
|
|
10
|
-
const isValid = isAppResponse ? result?.ok : result;
|
|
8
|
+
const isValid = await rule.rule();
|
|
11
9
|
if (!isValid) {
|
|
12
10
|
for (const [key, message] of Object.entries(rule.errors)) {
|
|
13
11
|
validationErrors[key] = [
|
|
@@ -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)) {
|