@pogodisco/val 0.0.3 → 0.1.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/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,4 +1,5 @@
|
|
|
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) {
|
|
@@ -15,14 +16,11 @@ export async function validate({ rules, }) {
|
|
|
15
16
|
];
|
|
16
17
|
}
|
|
17
18
|
if (rule.bail ?? true) {
|
|
18
|
-
|
|
19
|
-
errors: validationErrors,
|
|
20
|
-
});
|
|
19
|
+
throw new ValidationSchemaError(validationErrors);
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
if (Object.keys(validationErrors).length > 0) {
|
|
25
|
-
|
|
24
|
+
throw new ValidationSchemaError(validationErrors);
|
|
26
25
|
}
|
|
27
|
-
return newSuccessResponse(null);
|
|
28
26
|
}
|
|
@@ -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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pogodisco/val",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
}
|