@carno.js/core 1.0.2 → 1.0.3

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.
@@ -1,69 +1,69 @@
1
- /**
2
- * Validation result type.
3
- */
4
- export interface ValidationResult<T = any> {
5
- success: boolean;
6
- data?: T;
7
- errors?: ValidationError[];
8
- }
9
-
10
- export interface ValidationError {
11
- path: string;
12
- message: string;
13
- }
14
-
15
- /**
16
- * Base interface for validation adapters.
17
- * Adapters provide validation capabilities for different libraries.
18
- */
19
- export interface ValidatorAdapter {
20
- /**
21
- * Validator name for debugging.
22
- */
23
- readonly name: string;
24
-
25
- /**
26
- * Check if a target has validation schema.
27
- */
28
- hasValidation(target: any): boolean;
29
-
30
- /**
31
- * Validate and transform a value.
32
- * Returns result object instead of throwing for better performance.
33
- */
34
- validate<T>(target: any, value: unknown): ValidationResult<T>;
35
-
36
- /**
37
- * Validate and transform, throwing on error.
38
- * Used when you want to short-circuit on failure.
39
- */
40
- validateOrThrow<T>(target: any, value: unknown): T;
41
- }
42
-
43
- /**
44
- * Validation configuration for Turbo.
45
- */
46
- export interface ValidationConfig {
47
- adapter: ValidatorAdapter;
48
- }
49
-
50
- /**
51
- * Symbol for storing validation schema on DTOs.
52
- */
53
- export const VALIDATION_SCHEMA = Symbol('turbo:validation');
54
-
55
- /**
56
- * Decorator to attach a validation schema to a DTO class.
57
- */
58
- export function Schema(schema: any): ClassDecorator {
59
- return (target) => {
60
- Reflect.defineMetadata(VALIDATION_SCHEMA, schema, target);
61
- };
62
- }
63
-
64
- /**
65
- * Get the validation schema from a DTO class.
66
- */
67
- export function getSchema(target: any): any | undefined {
68
- return Reflect.getMetadata(VALIDATION_SCHEMA, target);
69
- }
1
+ /**
2
+ * Validation result type.
3
+ */
4
+ export interface ValidationResult<T = any> {
5
+ success: boolean;
6
+ data?: T;
7
+ errors?: ValidationError[];
8
+ }
9
+
10
+ export interface ValidationError {
11
+ path: string;
12
+ message: string;
13
+ }
14
+
15
+ /**
16
+ * Base interface for validation adapters.
17
+ * Adapters provide validation capabilities for different libraries.
18
+ */
19
+ export interface ValidatorAdapter {
20
+ /**
21
+ * Validator name for debugging.
22
+ */
23
+ readonly name: string;
24
+
25
+ /**
26
+ * Check if a target has validation schema.
27
+ */
28
+ hasValidation(target: any): boolean;
29
+
30
+ /**
31
+ * Validate and transform a value.
32
+ * Returns result object instead of throwing for better performance.
33
+ */
34
+ validate<T>(target: any, value: unknown): ValidationResult<T>;
35
+
36
+ /**
37
+ * Validate and transform, throwing on error.
38
+ * Used when you want to short-circuit on failure.
39
+ */
40
+ validateOrThrow<T>(target: any, value: unknown): T;
41
+ }
42
+
43
+ /**
44
+ * Validation configuration for Turbo.
45
+ */
46
+ export interface ValidationConfig {
47
+ adapter: ValidatorAdapter;
48
+ }
49
+
50
+ /**
51
+ * Symbol for storing validation schema on DTOs.
52
+ */
53
+ export const VALIDATION_SCHEMA = Symbol('turbo:validation');
54
+
55
+ /**
56
+ * Decorator to attach a validation schema to a DTO class.
57
+ */
58
+ export function Schema(schema: any): ClassDecorator {
59
+ return (target) => {
60
+ Reflect.defineMetadata(VALIDATION_SCHEMA, schema, target);
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Get the validation schema from a DTO class.
66
+ */
67
+ export function getSchema(target: any): any | undefined {
68
+ return Reflect.getMetadata(VALIDATION_SCHEMA, target);
69
+ }
@@ -1,102 +1,102 @@
1
- import type { ValidatorAdapter, ValidationResult, ValidationError } from './ValidatorAdapter';
2
- import { VALIDATION_SCHEMA, getSchema } from './ValidatorAdapter';
3
-
4
- /**
5
- * Zod Adapter - Default validation adapter for Turbo.
6
- *
7
- * Usage:
8
- * ```typescript
9
- * import { z } from 'zod';
10
- *
11
- * @Schema(z.object({
12
- * name: z.string().min(1),
13
- * email: z.string().email()
14
- * }))
15
- * class CreateUserDto {
16
- * name: string;
17
- * email: string;
18
- * }
19
- * ```
20
- */
21
- export class ZodAdapter implements ValidatorAdapter {
22
- readonly name = 'ZodAdapter';
23
-
24
- // Cache parsed schemas for performance
25
- private schemaCache = new Map<any, any>();
26
-
27
- hasValidation(target: any): boolean {
28
- return getSchema(target) !== undefined;
29
- }
30
-
31
- validate<T>(target: any, value: unknown): ValidationResult<T> {
32
- const schema = this.getOrCacheSchema(target);
33
-
34
- if (!schema) {
35
- return { success: true, data: value as T };
36
- }
37
-
38
- const result = schema.safeParse(value);
39
-
40
- if (result.success) {
41
- return { success: true, data: result.data };
42
- }
43
-
44
- return {
45
- success: false,
46
- errors: this.formatErrors(result.error)
47
- };
48
- }
49
-
50
- validateOrThrow<T>(target: any, value: unknown): T {
51
- const schema = this.getOrCacheSchema(target);
52
-
53
- if (!schema) {
54
- return value as T;
55
- }
56
-
57
- const result = schema.safeParse(value);
58
-
59
- if (result.success) {
60
- return result.data;
61
- }
62
-
63
- const errors = this.formatErrors(result.error);
64
- throw new ValidationException(errors);
65
- }
66
-
67
- private getOrCacheSchema(target: any): any {
68
- let schema = this.schemaCache.get(target);
69
-
70
- if (schema === undefined) {
71
- schema = getSchema(target) ?? null;
72
- this.schemaCache.set(target, schema);
73
- }
74
-
75
- return schema;
76
- }
77
-
78
- private formatErrors(zodError: any): ValidationError[] {
79
- return zodError.issues.map((issue: any) => ({
80
- path: issue.path.join('.'),
81
- message: issue.message
82
- }));
83
- }
84
- }
85
-
86
- /**
87
- * Validation exception thrown when validation fails.
88
- */
89
- export class ValidationException extends Error {
90
- constructor(public readonly errors: ValidationError[]) {
91
- super(`Validation failed: ${errors.map(e => `${e.path}: ${e.message}`).join(', ')}`);
92
- this.name = 'ValidationException';
93
- }
94
-
95
- toResponse(): Response {
96
- return Response.json({
97
- statusCode: 400,
98
- message: 'Validation failed',
99
- errors: this.errors
100
- }, { status: 400 });
101
- }
102
- }
1
+ import type { ValidatorAdapter, ValidationResult, ValidationError } from './ValidatorAdapter';
2
+ import { VALIDATION_SCHEMA, getSchema } from './ValidatorAdapter';
3
+
4
+ /**
5
+ * Zod Adapter - Default validation adapter for Turbo.
6
+ *
7
+ * Usage:
8
+ * ```typescript
9
+ * import { z } from 'zod';
10
+ *
11
+ * @Schema(z.object({
12
+ * name: z.string().min(1),
13
+ * email: z.string().email()
14
+ * }))
15
+ * class CreateUserDto {
16
+ * name: string;
17
+ * email: string;
18
+ * }
19
+ * ```
20
+ */
21
+ export class ZodAdapter implements ValidatorAdapter {
22
+ readonly name = 'ZodAdapter';
23
+
24
+ // Cache parsed schemas for performance
25
+ private schemaCache = new Map<any, any>();
26
+
27
+ hasValidation(target: any): boolean {
28
+ return getSchema(target) !== undefined;
29
+ }
30
+
31
+ validate<T>(target: any, value: unknown): ValidationResult<T> {
32
+ const schema = this.getOrCacheSchema(target);
33
+
34
+ if (!schema) {
35
+ return { success: true, data: value as T };
36
+ }
37
+
38
+ const result = schema.safeParse(value);
39
+
40
+ if (result.success) {
41
+ return { success: true, data: result.data };
42
+ }
43
+
44
+ return {
45
+ success: false,
46
+ errors: this.formatErrors(result.error)
47
+ };
48
+ }
49
+
50
+ validateOrThrow<T>(target: any, value: unknown): T {
51
+ const schema = this.getOrCacheSchema(target);
52
+
53
+ if (!schema) {
54
+ return value as T;
55
+ }
56
+
57
+ const result = schema.safeParse(value);
58
+
59
+ if (result.success) {
60
+ return result.data;
61
+ }
62
+
63
+ const errors = this.formatErrors(result.error);
64
+ throw new ValidationException(errors);
65
+ }
66
+
67
+ private getOrCacheSchema(target: any): any {
68
+ let schema = this.schemaCache.get(target);
69
+
70
+ if (schema === undefined) {
71
+ schema = getSchema(target) ?? null;
72
+ this.schemaCache.set(target, schema);
73
+ }
74
+
75
+ return schema;
76
+ }
77
+
78
+ private formatErrors(zodError: any): ValidationError[] {
79
+ return zodError.issues.map((issue: any) => ({
80
+ path: issue.path.join('.'),
81
+ message: issue.message
82
+ }));
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Validation exception thrown when validation fails.
88
+ */
89
+ export class ValidationException extends Error {
90
+ constructor(public readonly errors: ValidationError[]) {
91
+ super(`Validation failed: ${errors.map(e => `${e.path}: ${e.message}`).join(', ')}`);
92
+ this.name = 'ValidationException';
93
+ }
94
+
95
+ toResponse(): Response {
96
+ return Response.json({
97
+ statusCode: 400,
98
+ message: 'Validation failed',
99
+ errors: this.errors
100
+ }, { status: 400 });
101
+ }
102
+ }