@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.
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/lib/RequestValidator.d.ts +1 -1
- package/dist/lib/RequestValidator.js +22 -16
- package/dist/lib/ResponseValidator.d.ts +1 -1
- package/dist/lib/ResponseValidator.js +98 -97
- package/dist/lib/Validator.js +193 -180
- package/dist/lib/definitionLookup.js +16 -10
- package/dist/lib/index.ts +4 -4
- package/dist/templates/RequestValidator.ejs +1 -1
- package/dist/templates/ResponseValidator.ejs +1 -1
- package/package.json +7 -8
|
@@ -1,19 +1,25 @@
|
|
|
1
|
-
import { Validator } from "./Validator";
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* -
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
}
|
|
19
25
|
}
|
|
@@ -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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
}
|