@rexeus/typeweaver-types 0.10.0 → 0.10.2

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.
@@ -3,7 +3,7 @@ import type {
3
3
  IRequestValidator,
4
4
  SafeRequestValidationResult,
5
5
  } from "@rexeus/typeweaver-core";
6
- import { Validator } from "./Validator";
6
+ import { Validator } from "./Validator.js";
7
7
 
8
8
  export declare abstract class RequestValidator
9
9
  extends Validator
@@ -1,19 +1,25 @@
1
- import { Validator } from "./Validator";
2
1
  /**
3
- * Abstract base class for HTTP request validation.
4
- *
5
- * This class provides the foundation for request validators that:
6
- * - Validate headers, body, query parameters, and path parameters
7
- * - Support both safe (non-throwing) and unsafe (throwing) validation
8
- * - Integrate with Zod schemas for runtime validation
9
- * - Provide detailed error information for debugging
10
- *
11
- * Implementations should validate all request components and either:
12
- * - Return validated data (for `validate`)
13
- * - Return success/error result (for `safeValidate`)
14
- */
2
+ * This file was automatically generated by typeweaver.
3
+ * DO NOT EDIT. Instead, modify the source definition file and generate again.
4
+ *
5
+ * @generated by @rexeus/typeweaver
6
+ */
7
+ import { Validator } from "./Validator.js";
8
+ /**
9
+ * Abstract base class for HTTP request validation.
10
+ *
11
+ * This class provides the foundation for request validators that:
12
+ * - Validate headers, body, query parameters, and path parameters
13
+ * - Support both safe (non-throwing) and unsafe (throwing) validation
14
+ * - Integrate with Zod schemas for runtime validation
15
+ * - Provide detailed error information for debugging
16
+ *
17
+ * Implementations should validate all request components and either:
18
+ * - Return validated data (for `validate`)
19
+ * - Return success/error result (for `safeValidate`)
20
+ */
15
21
  export class RequestValidator extends Validator {
16
- constructor() {
17
- super();
18
- }
22
+ constructor() {
23
+ super();
24
+ }
19
25
  }
@@ -7,7 +7,7 @@ import type {
7
7
  ResponseValidationError,
8
8
  SafeResponseValidationResult,
9
9
  } from "@rexeus/typeweaver-core";
10
- import { Validator } from "./Validator";
10
+ import { Validator } from "./Validator.js";
11
11
 
12
12
  export type ResponseEntry = {
13
13
  readonly name: string;
@@ -1,101 +1,102 @@
1
+ /**
2
+ * This file was automatically generated by typeweaver.
3
+ * DO NOT EDIT. Instead, modify the source definition file and generate again.
4
+ *
5
+ * @generated by @rexeus/typeweaver
6
+ */
1
7
  import { ResponseValidationError } from "@rexeus/typeweaver-core";
2
- import { Validator } from "./Validator";
8
+ import { Validator } from "./Validator.js";
3
9
  /**
4
- * Abstract base class for HTTP response validation.
5
- *
6
- * This class provides the foundation for response validators that:
7
- * - Validate response status codes match expected values
8
- * - Validate response headers and body against schemas
9
- * - Support both safe (non-throwing) and unsafe (throwing) validation
10
- * - Integrate with Zod schemas for runtime validation
11
- *
12
- * Response validators are typically used in API clients to ensure
13
- * responses match the expected format before processing.
14
- *
15
- * Subclasses provide response metadata via `responseEntries` and
16
- * `expectedStatusCodes`. All validation logic lives in this base class.
17
- */
10
+ * Abstract base class for HTTP response validation.
11
+ *
12
+ * This class provides the foundation for response validators that:
13
+ * - Validate response status codes match expected values
14
+ * - Validate response headers and body against schemas
15
+ * - Support both safe (non-throwing) and unsafe (throwing) validation
16
+ * - Integrate with Zod schemas for runtime validation
17
+ *
18
+ * Response validators are typically used in API clients to ensure
19
+ * responses match the expected format before processing.
20
+ *
21
+ * Subclasses provide response metadata via `responseEntries` and
22
+ * `expectedStatusCodes`. All validation logic lives in this base class.
23
+ */
18
24
  export class ResponseValidator extends Validator {
19
- /**
20
- * Validates a response without throwing errors.
21
- *
22
- * @param response - The HTTP response to validate
23
- * @returns A result object containing either the validated response or error details
24
- */
25
- safeValidate(response) {
26
- const error = new ResponseValidationError(response.statusCode);
27
- for (const entry of this.responseEntries) {
28
- if (response.statusCode === entry.statusCode) {
29
- const result = this.validateResponseType(entry.name, entry.headerSchema, entry.bodySchema)(response, error);
30
- if (result.isValid) return result;
31
- }
32
- }
33
- if (!error.hasResponseIssues()) {
34
- error.addStatusCodeIssue([...this.expectedStatusCodes]);
35
- }
36
- return {
37
- isValid: false,
38
- error
39
- };
40
- }
41
- /**
42
- * Validates a response and throws if validation fails.
43
- *
44
- * @param response - The HTTP response to validate
45
- * @returns The validated response with proper typing
46
- * @throws {ResponseValidationError} If response structure fails validation
47
- */
48
- validate(response) {
49
- const result = this.safeValidate(response);
50
- if (!result.isValid) throw result.error;
51
- return result.data;
52
- }
53
- /**
54
- * Validates a single response variant against its header and body schemas.
55
- *
56
- * @param responseName - Name of the response type for error reporting
57
- * @param headerSchema - Zod schema for header validation (optional)
58
- * @param bodySchema - Zod schema for body validation (optional)
59
- * @returns Function that validates response and returns result
60
- */
61
- validateResponseType(responseName, headerSchema, bodySchema) {
62
- return (response, error) => {
63
- let isValid = true;
64
- const validatedResponse = {
65
- type: responseName,
66
- statusCode: response.statusCode,
67
- header: undefined,
68
- body: undefined
69
- };
70
- if (bodySchema) {
71
- const validateBodyResult = bodySchema.safeParse(response.body);
72
- if (!validateBodyResult.success) {
73
- error.addBodyIssues(responseName, validateBodyResult.error.issues);
74
- isValid = false;
75
- } else {
76
- validatedResponse.body = validateBodyResult.data;
77
- }
78
- }
79
- if (headerSchema) {
80
- const coercedHeader = this.coerceHeaderToSchema(response.header, this.getSchema(headerSchema));
81
- const validateHeaderResult = headerSchema.safeParse(coercedHeader);
82
- if (!validateHeaderResult.success) {
83
- error.addHeaderIssues(responseName, validateHeaderResult.error.issues);
84
- isValid = false;
85
- } else {
86
- validatedResponse.header = validateHeaderResult.data;
87
- }
88
- }
89
- if (!isValid) {
90
- return {
91
- isValid: false,
92
- error
93
- };
94
- }
95
- return {
96
- isValid: true,
97
- data: validatedResponse
98
- };
99
- };
100
- }
25
+ /**
26
+ * Validates a response without throwing errors.
27
+ *
28
+ * @param response - The HTTP response to validate
29
+ * @returns A result object containing either the validated response or error details
30
+ */
31
+ safeValidate(response) {
32
+ const error = new ResponseValidationError(response.statusCode);
33
+ for (const entry of this.responseEntries) {
34
+ if (response.statusCode === entry.statusCode) {
35
+ const result = this.validateResponseType(entry.name, entry.headerSchema, entry.bodySchema)(response, error);
36
+ if (result.isValid)
37
+ return result;
38
+ }
39
+ }
40
+ if (!error.hasResponseIssues()) {
41
+ error.addStatusCodeIssue([...this.expectedStatusCodes]);
42
+ }
43
+ return { isValid: false, error };
44
+ }
45
+ /**
46
+ * Validates a response and throws if validation fails.
47
+ *
48
+ * @param response - The HTTP response to validate
49
+ * @returns The validated response with proper typing
50
+ * @throws {ResponseValidationError} If response structure fails validation
51
+ */
52
+ validate(response) {
53
+ const result = this.safeValidate(response);
54
+ if (!result.isValid)
55
+ throw result.error;
56
+ return result.data;
57
+ }
58
+ /**
59
+ * Validates a single response variant against its header and body schemas.
60
+ *
61
+ * @param responseName - Name of the response type for error reporting
62
+ * @param headerSchema - Zod schema for header validation (optional)
63
+ * @param bodySchema - Zod schema for body validation (optional)
64
+ * @returns Function that validates response and returns result
65
+ */
66
+ validateResponseType(responseName, headerSchema, bodySchema) {
67
+ return (response, error) => {
68
+ let isValid = true;
69
+ const validatedResponse = {
70
+ type: responseName,
71
+ statusCode: response.statusCode,
72
+ header: undefined,
73
+ body: undefined,
74
+ };
75
+ if (bodySchema) {
76
+ const validateBodyResult = bodySchema.safeParse(response.body);
77
+ if (!validateBodyResult.success) {
78
+ error.addBodyIssues(responseName, validateBodyResult.error.issues);
79
+ isValid = false;
80
+ }
81
+ else {
82
+ validatedResponse.body = validateBodyResult.data;
83
+ }
84
+ }
85
+ if (headerSchema) {
86
+ const coercedHeader = this.coerceHeaderToSchema(response.header, this.getSchema(headerSchema));
87
+ const validateHeaderResult = headerSchema.safeParse(coercedHeader);
88
+ if (!validateHeaderResult.success) {
89
+ error.addHeaderIssues(responseName, validateHeaderResult.error.issues);
90
+ isValid = false;
91
+ }
92
+ else {
93
+ validatedResponse.header = validateHeaderResult.data;
94
+ }
95
+ }
96
+ if (!isValid) {
97
+ return { isValid: false, error };
98
+ }
99
+ return { isValid: true, data: validatedResponse };
100
+ };
101
+ }
101
102
  }