hono-ban 0.2.0 → 0.2.4

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.
@@ -4,54 +4,85 @@
4
4
  */
5
5
 
6
6
  import { z } from "@hono/zod-openapi";
7
- import type {
8
- ValidationParam,
9
- ConstraintViolation,
10
- ProblemDetails,
11
- ProblemErrorData,
12
- } from "../../types";
13
7
 
14
8
  /**
15
- * Schema for validation error parameters
9
+ * Schema for RFC 7807 validation error parameters
16
10
  */
17
- export const ValidationParamSchema = z
18
- .object({
19
- name: z.string(),
20
- reason: z.string(),
21
- })
22
- .openapi("ValidationParam");
11
+ export const RFC7807ValidationParamSchema = z.object({
12
+ name: z.string().openapi({
13
+ example: "username",
14
+ description: "The field name that failed validation",
15
+ }),
16
+ reason: z.string().openapi({
17
+ example: "String must contain at least 3 character(s)",
18
+ description: "The reason for validation failure",
19
+ }),
20
+ });
23
21
 
24
22
  /**
25
- * Schema for constraint violations
23
+ * Schema for RFC 7807 constraint violations
26
24
  */
27
- export const ConstraintViolationSchema = z
28
- .object({
29
- name: z.string(),
30
- reason: z.string(),
31
- resource: z.string(),
32
- constraint: z.string(),
33
- })
34
- .openapi("ConstraintViolation");
25
+ export const RFC7807ConstraintViolationSchema = z.object({
26
+ name: z.string().openapi({
27
+ example: "email",
28
+ description: "The field name that violated a constraint",
29
+ }),
30
+ reason: z.string().openapi({
31
+ example: "Email already exists",
32
+ description: "The reason for the constraint violation",
33
+ }),
34
+ resource: z.string().openapi({
35
+ example: "user",
36
+ description: "The resource type that contains the constraint",
37
+ }),
38
+ constraint: z.string().openapi({
39
+ example: "unique",
40
+ description: "The type of constraint that was violated",
41
+ }),
42
+ });
35
43
 
36
44
  /**
37
- * Schema for additional error data in problem details
45
+ * Schema for additional error data in RFC 7807 problem details
38
46
  */
39
- export const ProblemErrorDataSchema = z.object({
40
- "invalid-params": z.array(ValidationParamSchema).optional(),
41
- violations: z.array(ConstraintViolationSchema).optional(),
47
+ export const RFC7807ErrorDataSchema = z.object({
48
+ "invalid-params": z.array(RFC7807ValidationParamSchema).optional(),
49
+ violations: z.array(RFC7807ConstraintViolationSchema).optional(),
42
50
  });
43
51
 
44
52
  /**
45
53
  * Schema for complete RFC 7807 problem details
46
54
  */
47
- export const ProblemDetailsSchema = z
55
+ export const RFC7807DetailsSchema = z
48
56
  .object({
49
- type: z.string().url(),
50
- title: z.string(),
51
- status: z.number().int().min(400).max(599),
52
- detail: z.string(),
53
- instance: z.string(),
54
- timestamp: z.string().datetime(),
57
+ type: z.string().url().openapi({
58
+ example: "https://api.example.com/problems/validation-error",
59
+ description: "A URI reference that identifies the problem type",
60
+ }),
61
+ title: z.string().openapi({
62
+ example: "Validation Failed",
63
+ description: "A short, human-readable summary of the problem type",
64
+ }),
65
+ status: z.number().int().min(400).max(599).openapi({
66
+ example: 400,
67
+ description: "The HTTP status code",
68
+ }),
69
+
70
+ // Optional fields per RFC7807
71
+ detail: z.string().optional().openapi({
72
+ example: "The request contains invalid fields",
73
+ description:
74
+ "A human-readable explanation specific to this occurrence of the problem",
75
+ }),
76
+ instance: z.string().url().optional().openapi({
77
+ example: "urn:uuid:6b56944d-5e89-4b4d-9ca7-c1be3d1f0e3f",
78
+ description:
79
+ "A URI reference that identifies the specific occurrence of the problem",
80
+ }),
81
+
82
+ // Extensions for validation errors
83
+ timestamp: z.string().datetime().optional().openapi({
84
+ example: "2025-02-26T12:34:56.789Z",
85
+ description: "When the error occurred",
86
+ }),
55
87
  })
56
- .merge(ProblemErrorDataSchema)
57
- .openapi("ProblemDetails");
88
+ .merge(RFC7807ErrorDataSchema);
@@ -9,82 +9,75 @@
9
9
  * @see {@link https://datatracker.ietf.org/doc/html/rfc7807} RFC 7807 Problem Details
10
10
  */
11
11
 
12
+ import { z } from "@hono/zod-openapi";
13
+ import {
14
+ RFC7807ValidationParamSchema,
15
+ RFC7807ConstraintViolationSchema,
16
+ RFC7807ErrorDataSchema,
17
+ RFC7807DetailsSchema,
18
+ } from "../formatters/rfc7807/schemas";
19
+
12
20
  /**
13
21
  * Represents a validation error parameter in an RFC 7807 problem details object.
14
22
  * Used to indicate specific validation failures in request parameters.
15
23
  *
16
- * @interface ValidationParam
24
+ * @type RFC7807ValidationParam
17
25
  * @property {string} name - The name of the parameter that failed validation
18
26
  * @property {string} reason - The reason why the parameter failed validation
19
27
  */
20
- export interface ValidationParam {
21
- name: string;
22
- reason: string;
23
- }
28
+ export type RFC7807ValidationParam = z.infer<
29
+ typeof RFC7807ValidationParamSchema
30
+ >;
24
31
 
25
32
  /**
26
33
  * Represents a constraint violation in an RFC 7807 problem details object.
27
34
  * Used to indicate violations of business rules or data constraints.
28
35
  *
29
- * @interface ConstraintViolation
36
+ * @type RFC7807ConstraintViolation
30
37
  * @property {string} name - The name of the violated constraint
31
38
  * @property {string} reason - The reason why the constraint was violated
32
39
  * @property {string} resource - The resource or entity where the violation occurred
33
40
  * @property {string} constraint - The specific constraint that was violated
34
41
  */
35
- export interface ConstraintViolation {
36
- name: string;
37
- reason: string;
38
- resource: string;
39
- constraint: string;
40
- }
42
+ export type RFC7807ConstraintViolation = z.infer<
43
+ typeof RFC7807ConstraintViolationSchema
44
+ >;
41
45
 
42
46
  /**
43
47
  * Additional error data that can be included in an RFC 7807 problem details object.
44
48
  * This interface extends the standard problem details with validation and constraint information.
45
49
  *
46
- * @interface ProblemErrorData
47
- * @property {ValidationParam[]} [invalid-params] - Array of validation errors
48
- * @property {ConstraintViolation[]} [violations] - Array of constraint violations
50
+ * @type RFC7807ErrorData
51
+ * @property {RFC7807ValidationParam[]} [invalid-params] - Array of validation errors
52
+ * @property {RFC7807ConstraintViolation[]} [violations] - Array of constraint violations
49
53
  */
50
- export interface ProblemErrorData {
51
- "invalid-params"?: ValidationParam[];
52
- violations?: ConstraintViolation[];
53
- }
54
+ export type RFC7807ErrorData = z.infer<typeof RFC7807ErrorDataSchema>;
54
55
 
55
56
  /**
56
57
  * Complete RFC 7807 problem details object structure.
57
58
  * This interface represents the full problem details format as defined in RFC 7807,
58
59
  * with additional properties for validation and constraint violation data.
59
60
  *
60
- * @interface ProblemDetails
61
- * @extends ProblemErrorData
61
+ * @type RFC7807Details
62
62
  * @property {string} type - URI reference that identifies the problem type
63
63
  * @property {string} title - Short, human-readable summary of the problem
64
64
  * @property {number} status - HTTP status code (400-599)
65
- * @property {string} detail - Human-readable explanation specific to this occurrence
66
- * @property {string} instance - URI reference that identifies the specific occurrence
67
- * @property {string} timestamp - ISO 8601 datetime when the error occurred
65
+ * @property {string} [detail] - Human-readable explanation specific to this occurrence
66
+ * @property {string} [instance] - URI reference that identifies the specific occurrence
67
+ * @property {string} [timestamp] - ISO 8601 datetime when the error occurred
68
68
  */
69
- export interface ProblemDetails extends ProblemErrorData {
70
- type: string;
71
- title: string;
72
- status: number;
73
- detail: string;
74
- instance: string;
75
- timestamp: string;
76
- }
69
+ export type RFC7807Details = z.infer<typeof RFC7807DetailsSchema>;
77
70
 
78
71
  /**
79
72
  * Hook function for customizing RFC 7807 problem details before formatting.
80
73
  * This type represents a function that can modify the problem details object
81
74
  * before it is serialized into the final response.
82
75
  *
83
- * @callback ProblemDetailsHook
84
- * @param {ProblemDetails} details - The problem details object to modify
85
- * @returns {ProblemDetails} The modified problem details object
76
+ * @callback RFC7807DetailsHook
77
+ * @param {RFC7807Details} details - The problem details object to modify
78
+ * @returns {RFC7807Details} The modified problem details object
86
79
  */
87
- export type ProblemDetailsHook = (details: ProblemDetails) => ProblemDetails;
80
+ export type RFC7807DetailsHook = (details: RFC7807Details) => RFC7807Details;
88
81
 
89
82
  /**
90
83
  * Configuration options for the RFC 7807 formatter.
@@ -92,9 +85,9 @@ export type ProblemDetailsHook = (details: ProblemDetails) => ProblemDetails;
92
85
  *
93
86
  * @interface RFC7807FormatterOptions
94
87
  * @property {string} [baseUrl="https://api.example.com/problems"] - Base URL for problem type URIs
95
- * @property {ProblemDetailsHook[]} [hooks=[]] - Array of hooks for customizing problem details
88
+ * @property {RFC7807DetailsHook[]} [hooks=[]] - Array of hooks for customizing problem details
96
89
  */
97
90
  export interface RFC7807FormatterOptions {
98
91
  baseUrl?: string;
99
- hooks?: ProblemDetailsHook[];
92
+ hooks?: RFC7807DetailsHook[];
100
93
  }