@hg-ts/validation 0.5.17 → 0.5.18
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/package.json +6 -6
- package/src/dto.ts +35 -0
- package/src/extensions.ts +87 -0
- package/src/index.ts +11 -0
- package/src/schemas.ts +15 -0
- package/src/validation.exception.ts +30 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hg-ts/validation",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.18",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"lint:ts:fix": "lint-ts --fix"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@hg-ts-config/typescript": "0.5.
|
|
20
|
-
"@hg-ts/exception": "0.5.
|
|
21
|
-
"@hg-ts/linter": "0.5.
|
|
22
|
-
"@hg-ts/types": "0.5.
|
|
19
|
+
"@hg-ts-config/typescript": "0.5.18",
|
|
20
|
+
"@hg-ts/exception": "0.5.18",
|
|
21
|
+
"@hg-ts/linter": "0.5.18",
|
|
22
|
+
"@hg-ts/types": "0.5.18",
|
|
23
23
|
"@types/node": "22.19.1",
|
|
24
24
|
"eslint": "9.18.0",
|
|
25
25
|
"reflect-metadata": "0.2.2",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typescript": "5.7.3"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@hg-ts/exception": "0.5.
|
|
31
|
+
"@hg-ts/exception": "0.5.18",
|
|
32
32
|
"reflect-metadata": "*",
|
|
33
33
|
"tslib": "*"
|
|
34
34
|
},
|
package/src/dto.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
infer as ZodInfer,
|
|
3
|
+
ZodSchema,
|
|
4
|
+
ZodType,
|
|
5
|
+
} from 'zod';
|
|
6
|
+
|
|
7
|
+
export type ZodDto<TOutput = any, TInput = TOutput> = {
|
|
8
|
+
new (): TOutput;
|
|
9
|
+
isZodDto: true;
|
|
10
|
+
schema: ZodSchema<TOutput, TInput>;
|
|
11
|
+
create(input: unknown): TOutput;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createZodDto<T extends ZodType>(
|
|
15
|
+
schema: T,
|
|
16
|
+
): ZodDto<ZodInfer<T>> {
|
|
17
|
+
abstract class AugmentedZodDto {
|
|
18
|
+
public static isZodDto = true;
|
|
19
|
+
public static schema = schema;
|
|
20
|
+
|
|
21
|
+
public static create(input: unknown): AugmentedZodDto {
|
|
22
|
+
return this.schema.parse(input) as AugmentedZodDto;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return AugmentedZodDto as unknown as ZodDto<ZodInfer<T>>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isZodDto(value: unknown): value is ZodDto {
|
|
30
|
+
if (!value || typeof value !== 'function') {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return 'isZodDto' in value && value.isZodDto === true;
|
|
35
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import zod, {
|
|
2
|
+
core,
|
|
3
|
+
infer as ZodInfer,
|
|
4
|
+
ZodBoolean,
|
|
5
|
+
ZodPipe,
|
|
6
|
+
ZodTransform,
|
|
7
|
+
ZodType,
|
|
8
|
+
} from 'zod';
|
|
9
|
+
import 'zod-openapi';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
createZodDto,
|
|
13
|
+
ZodDto,
|
|
14
|
+
} from './dto.js';
|
|
15
|
+
import { ValidationException } from './validation.exception.js';
|
|
16
|
+
|
|
17
|
+
declare module 'zod' {
|
|
18
|
+
// eslint-disable-next-line @typescript/consistent-type-definitions
|
|
19
|
+
interface ZodType<Output = unknown, Input = unknown> {
|
|
20
|
+
enforceEnv<T extends ZodType<Output, Input>>(
|
|
21
|
+
this: T,
|
|
22
|
+
name: string
|
|
23
|
+
): ZodPipe<T, ZodTransform<Awaited<ZodInfer<T> | string>>>;
|
|
24
|
+
transformBooleanString<T extends ZodType<Output, Input>>(
|
|
25
|
+
this: T,
|
|
26
|
+
): ZodPipe<ZodPipe<T, ZodTransform<boolean, core.output<T>>>, ZodBoolean>;
|
|
27
|
+
toClass<T extends ZodType<Output, Input>>(this: T): ZodDto<ZodInfer<T>>;
|
|
28
|
+
describe(description: string): this;
|
|
29
|
+
example(example: Input): this;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
zod.ZodType.prototype.enforceEnv = function<T extends ZodType>(
|
|
34
|
+
this: T,
|
|
35
|
+
name: string,
|
|
36
|
+
): ZodPipe<T, ZodTransform<Awaited<ZodInfer<T> | string>>> {
|
|
37
|
+
return this.transform(value => process.env[name] ?? value);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
zod.ZodType.prototype.parse = function<
|
|
41
|
+
T extends ZodType<Output, Input>,
|
|
42
|
+
Input = unknown,
|
|
43
|
+
Output = unknown,
|
|
44
|
+
>(
|
|
45
|
+
this: T,
|
|
46
|
+
value: unknown,
|
|
47
|
+
): Output {
|
|
48
|
+
const { success, error, data } = this.safeParse(value);
|
|
49
|
+
|
|
50
|
+
if (success) {
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
throw new ValidationException(error);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
zod.ZodType.prototype.parseAsync = async function<
|
|
58
|
+
T extends ZodType<Output, Input>,
|
|
59
|
+
Input = unknown,
|
|
60
|
+
Output = unknown,
|
|
61
|
+
>(
|
|
62
|
+
this: T,
|
|
63
|
+
value: unknown,
|
|
64
|
+
): Promise<Output> {
|
|
65
|
+
const { success, error, data } = await this.safeParseAsync(value);
|
|
66
|
+
|
|
67
|
+
if (success) {
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
throw new ValidationException(error);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
zod.ZodType.prototype.transformBooleanString = function<T extends ZodType>(
|
|
76
|
+
this: T,
|
|
77
|
+
): ZodPipe<ZodPipe<T, ZodTransform<boolean, core.output<T>>>, ZodBoolean> {
|
|
78
|
+
return this
|
|
79
|
+
.transform(value => (typeof value === 'boolean' ? value : value === 'true'))
|
|
80
|
+
.pipe(zod.boolean());
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
zod.ZodType.prototype.toClass = function<T extends ZodType>(
|
|
84
|
+
this: T,
|
|
85
|
+
): ZodDto<ZodInfer<T>> {
|
|
86
|
+
return createZodDto(this);
|
|
87
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import './extensions.js';
|
|
2
|
+
import zod from 'zod';
|
|
3
|
+
|
|
4
|
+
export * from 'zod';
|
|
5
|
+
export { isZodDto, ZodDto } from './dto.js';
|
|
6
|
+
export * from './validation.exception.js';
|
|
7
|
+
export * from './schemas.js';
|
|
8
|
+
|
|
9
|
+
export { createSchema } from 'zod-openapi';
|
|
10
|
+
|
|
11
|
+
export default zod;
|
package/src/schemas.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const baseExceptionSchema = z.object({
|
|
4
|
+
name: z.string(),
|
|
5
|
+
code: z.number().nullable(),
|
|
6
|
+
message: z.string(),
|
|
7
|
+
stack: z.array(z.string()),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export const bufferSchema = z
|
|
11
|
+
.instanceof(Buffer)
|
|
12
|
+
.meta({
|
|
13
|
+
type: 'string',
|
|
14
|
+
format: 'binary',
|
|
15
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseException,
|
|
3
|
+
JSONBaseException,
|
|
4
|
+
} from '@hg-ts/exception';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
ZodError,
|
|
8
|
+
ZodIssue,
|
|
9
|
+
} from 'zod';
|
|
10
|
+
|
|
11
|
+
export type JSONValidationException = JSONBaseException & {
|
|
12
|
+
issues: ZodIssue[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class ValidationException extends BaseException {
|
|
16
|
+
private readonly issues: ZodIssue[];
|
|
17
|
+
|
|
18
|
+
public constructor(originalError: ZodError) {
|
|
19
|
+
super('Validation failed');
|
|
20
|
+
|
|
21
|
+
this.issues = originalError.issues;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public override toJSON(): JSONValidationException {
|
|
25
|
+
return {
|
|
26
|
+
...super.toJSON(),
|
|
27
|
+
issues: this.issues.map(item => ({ ...item })),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|