@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.
@@ -6,3 +6,4 @@ export * from "./wrap-sync-rule.js";
6
6
  export * from "./flatten-rules.js";
7
7
  export * from "./validation-rule.js";
8
8
  export * from "./validation-rule-builder.js";
9
+ export * from "./validation-schema-error.js";
@@ -6,3 +6,4 @@ export * from "./wrap-sync-rule.js";
6
6
  export * from "./flatten-rules.js";
7
7
  export * from "./validation-rule.js";
8
8
  export * from "./validation-rule-builder.js";
9
+ export * from "./validation-schema-error.js";
@@ -1,5 +1,4 @@
1
- import type { ValidationRule } from "./validation-rule.js";
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<TResponse<null>>;
4
+ }): Promise<void>;
@@ -1,4 +1,5 @@
1
- import { newFailureResponse, newSuccessResponse, } from "@pogodisco/response";
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
- return newFailureResponse("Validation error", {
19
- errors: validationErrors,
20
- });
19
+ throw new ValidationSchemaError(validationErrors);
21
20
  }
22
21
  }
23
22
  }
24
23
  if (Object.keys(validationErrors).length > 0) {
25
- return newFailureResponse("Validation error", { errors: validationErrors });
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",
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
  }