vona-module-a-openapiutils 5.0.16 → 5.1.1
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/LICENSE +0 -0
- package/dist/.metadata/index.d.ts +1 -0
- package/dist/.metadata/index.d.ts.map +1 -0
- package/dist/.metadata/locales.d.ts +1 -0
- package/dist/.metadata/locales.d.ts.map +1 -0
- package/dist/.metadata/this.d.ts +1 -0
- package/dist/.metadata/this.d.ts.map +1 -0
- package/dist/config/locale/en-us.d.ts +1 -0
- package/dist/config/locale/en-us.d.ts.map +1 -0
- package/dist/config/locale/zh-cn.d.ts +1 -0
- package/dist/config/locale/zh-cn.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +145 -81
- package/dist/index.js.map +1 -0
- package/dist/lib/const/database.d.ts +1 -0
- package/dist/lib/const/database.d.ts.map +1 -0
- package/dist/lib/const/decorator.d.ts +4 -0
- package/dist/lib/const/decorator.d.ts.map +1 -0
- package/dist/lib/const/index.d.ts +1 -0
- package/dist/lib/const/index.d.ts.map +1 -0
- package/dist/lib/decorator/api.d.ts +4 -3
- package/dist/lib/decorator/api.d.ts.map +1 -0
- package/dist/lib/decorator/field.d.ts +1 -0
- package/dist/lib/decorator/field.d.ts.map +1 -0
- package/dist/lib/decorator/index.d.ts +2 -0
- package/dist/lib/decorator/index.d.ts.map +1 -0
- package/dist/lib/decorator/resource.d.ts +2 -0
- package/dist/lib/decorator/resource.d.ts.map +1 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/schema/bodySchemaWrapper.d.ts +1 -0
- package/dist/lib/schema/bodySchemaWrapper.d.ts.map +1 -0
- package/dist/lib/schema/index.d.ts +2 -0
- package/dist/lib/schema/index.d.ts.map +1 -0
- package/dist/lib/schema/makeMetadata.d.ts +4 -0
- package/dist/lib/schema/makeMetadata.d.ts.map +1 -0
- package/dist/lib/schema/makeSchemaLikes.d.ts +3 -2
- package/dist/lib/schema/makeSchemaLikes.d.ts.map +1 -0
- package/dist/lib/schema/schemaDynamic.d.ts +1 -0
- package/dist/lib/schema/schemaDynamic.d.ts.map +1 -0
- package/dist/lib/schema/v/component.d.ts +5 -0
- package/dist/lib/schema/v/component.d.ts.map +1 -0
- package/dist/lib/schema/v/filter.d.ts +1 -0
- package/dist/lib/schema/v/filter.d.ts.map +1 -0
- package/dist/lib/schema/v/helpers.d.ts +1 -2
- package/dist/lib/schema/v/helpers.d.ts.map +1 -0
- package/dist/lib/schema/v/openapi.d.ts +1 -0
- package/dist/lib/schema/v/openapi.d.ts.map +1 -0
- package/dist/lib/schema/v/rest.d.ts +4 -4
- package/dist/lib/schema/v/rest.d.ts.map +1 -0
- package/dist/lib/schema/v/serializer.d.ts +1 -0
- package/dist/lib/schema/v/serializer.d.ts.map +1 -0
- package/dist/lib/schema/v/system.d.ts +2 -1
- package/dist/lib/schema/v/system.d.ts.map +1 -0
- package/dist/lib/schema/v/utils.d.ts +4 -0
- package/dist/lib/schema/v/utils.d.ts.map +1 -0
- package/dist/lib/schema/v/zod.d.ts +1 -0
- package/dist/lib/schema/v/zod.d.ts.map +1 -0
- package/dist/lib/schema/v.d.ts +7 -5
- package/dist/lib/schema/v.d.ts.map +1 -0
- package/dist/lib/utils.d.ts +4 -1
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/web.d.ts +2 -2
- package/dist/lib/web.d.ts.map +1 -0
- package/dist/types/api.d.ts +4 -4
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/component.d.ts +12 -0
- package/dist/types/component.d.ts.map +1 -0
- package/dist/types/decorator.d.ts +1 -1
- package/dist/types/decorator.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/order.d.ts +1 -0
- package/dist/types/order.d.ts.map +1 -0
- package/package.json +21 -16
- package/src/.metadata/index.ts +35 -0
- package/src/.metadata/locales.ts +13 -0
- package/src/.metadata/this.ts +2 -0
- package/src/config/locale/en-us.ts +3 -0
- package/src/config/locale/zh-cn.ts +3 -0
- package/src/index.ts +4 -0
- package/src/lib/const/database.ts +10 -0
- package/src/lib/const/decorator.ts +6 -0
- package/src/lib/const/index.ts +2 -0
- package/src/lib/decorator/api.ts +107 -0
- package/src/lib/decorator/field.ts +19 -0
- package/src/lib/decorator/index.ts +2 -0
- package/src/lib/decorator/resource.ts +10 -0
- package/src/lib/index.ts +6 -0
- package/src/lib/schema/bodySchemaWrapper.ts +9 -0
- package/src/lib/schema/index.ts +5 -0
- package/src/lib/schema/makeMetadata.ts +12 -0
- package/src/lib/schema/makeSchemaLikes.ts +107 -0
- package/src/lib/schema/schemaDynamic.ts +17 -0
- package/src/lib/schema/v/component.ts +78 -0
- package/src/lib/schema/v/filter.tsx +40 -0
- package/src/lib/schema/v/helpers.ts +110 -0
- package/src/lib/schema/v/openapi.ts +29 -0
- package/src/lib/schema/v/rest.ts +50 -0
- package/src/lib/schema/v/serializer.ts +86 -0
- package/src/lib/schema/v/system.ts +72 -0
- package/src/lib/schema/v/utils.ts +6 -0
- package/src/lib/schema/v/zod.ts +36 -0
- package/src/lib/schema/v.ts +88 -0
- package/src/lib/utils.ts +100 -0
- package/src/lib/web.ts +11 -0
- package/src/types/api.ts +99 -0
- package/src/types/component.ts +19 -0
- package/src/types/decorator.ts +7 -0
- package/src/types/index.ts +4 -0
- package/src/types/order.ts +9 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Constructable } from 'vona';
|
|
2
|
+
import type { ISchemaObjectOptions, TypeOpenapiMetadata } from 'vona-module-a-openapi';
|
|
3
|
+
|
|
4
|
+
import { isClass } from '@cabloy/utils';
|
|
5
|
+
import { appMetadata, appResource, cast } from 'vona';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
|
|
8
|
+
import type { SchemaLike, SchemaLikeCreate } from '../../types/decorator.ts';
|
|
9
|
+
|
|
10
|
+
import { SymbolDecoratorRule } from '../const/decorator.ts';
|
|
11
|
+
import { prepareClassType } from '../utils.ts';
|
|
12
|
+
import { SymbolSchemaDynamicRefId } from './schemaDynamic.ts';
|
|
13
|
+
|
|
14
|
+
export function $makeSchema<T>(...schemaLikes: SchemaLike<T>[]): z.ZodType<T> {
|
|
15
|
+
return makeSchemaLikes(schemaLikes, undefined);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function makeSchemaLikes<T>(schemaLikes: SchemaLike<T> | SchemaLike<T>[], typeInit: any): z.ZodType<T> {
|
|
19
|
+
if (!Array.isArray(schemaLikes)) schemaLikes = [schemaLikes];
|
|
20
|
+
// default schema
|
|
21
|
+
let argSchema: z.ZodType<T> = $schema(typeInit);
|
|
22
|
+
// loop
|
|
23
|
+
for (let index = schemaLikes.length - 1; index >= 0; index--) {
|
|
24
|
+
const schemaLike = schemaLikes[index];
|
|
25
|
+
argSchema = makeSchemaLike(schemaLike, argSchema);
|
|
26
|
+
}
|
|
27
|
+
return argSchema;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function makeSchemaLike<T>(schemaLike: SchemaLike<T> | undefined, schemaPrevious: z.ZodType<T>): z.ZodType<T> {
|
|
31
|
+
if (!schemaLike) return schemaPrevious;
|
|
32
|
+
if (Object.prototype.hasOwnProperty.call(schemaLike, 'parseAsync')) {
|
|
33
|
+
// schema
|
|
34
|
+
return schemaLike as z.ZodType<T>;
|
|
35
|
+
} else if (isClass(schemaLike) || ['String', 'Number', 'Boolean', 'Date', 'BigInt', 'Array'].includes(cast<Function>(schemaLike).name)) {
|
|
36
|
+
// class
|
|
37
|
+
return $schema(cast<Constructable>(schemaLike)) as z.ZodType<T>;
|
|
38
|
+
} else {
|
|
39
|
+
// function
|
|
40
|
+
return cast<SchemaLikeCreate>(schemaLike)(schemaPrevious);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function $schema<T>(schemaLike: z.ZodType<T>): z.ZodType<T>;
|
|
45
|
+
export function $schema(classType: StringConstructor): z.ZodString;
|
|
46
|
+
export function $schema(classType: NumberConstructor): z.ZodNumber;
|
|
47
|
+
export function $schema(classType: BooleanConstructor): z.ZodBoolean;
|
|
48
|
+
export function $schema(classType: DateConstructor): z.ZodDate;
|
|
49
|
+
export function $schema(classType: BigIntConstructor): z.ZodBigInt;
|
|
50
|
+
export function $schema(classType: ArrayConstructor): z.ZodArray<z.ZodAny>;
|
|
51
|
+
export function $schema<T>(classType: Constructable<T>, options?: ISchemaObjectOptions): z.ZodType<T>;
|
|
52
|
+
export function $schema(classType: any, options?: ISchemaObjectOptions): any {
|
|
53
|
+
if (!classType) return z.any();
|
|
54
|
+
if (classType.parseAsync) return classType;
|
|
55
|
+
if (classType.name === 'String') return z.string();
|
|
56
|
+
if (classType.name === 'Number') return z.number();
|
|
57
|
+
if (classType.name === 'Boolean') return z.boolean();
|
|
58
|
+
if (classType.name === 'Date') return z.date();
|
|
59
|
+
if (classType.name === 'BigInt') return z.bigint();
|
|
60
|
+
if (classType.name === 'Array') return z.array(z.any());
|
|
61
|
+
// check if object
|
|
62
|
+
const rules = classType.prototype ? appMetadata.getMetadata(SymbolDecoratorRule, classType.prototype) : undefined;
|
|
63
|
+
if (!rules) {
|
|
64
|
+
// not object
|
|
65
|
+
return z.any();
|
|
66
|
+
}
|
|
67
|
+
// object
|
|
68
|
+
let schema = _createSchemaObject(rules, options);
|
|
69
|
+
// refId
|
|
70
|
+
const schemaDynamicRefId = classType[SymbolSchemaDynamicRefId];
|
|
71
|
+
if (schemaDynamicRefId) {
|
|
72
|
+
// dynamic
|
|
73
|
+
schema = schema.openapi(schemaDynamicRefId);
|
|
74
|
+
} else {
|
|
75
|
+
// static
|
|
76
|
+
const beanOptions = appResource.getBean(classType);
|
|
77
|
+
if (beanOptions) {
|
|
78
|
+
const pipes: SchemaLike | SchemaLike[] = cast(beanOptions.options)?.pipes;
|
|
79
|
+
if (pipes) {
|
|
80
|
+
schema = makeSchemaLikes(pipes, schema) as any;
|
|
81
|
+
}
|
|
82
|
+
const openapi: TypeOpenapiMetadata = cast(beanOptions.options)?.openapi;
|
|
83
|
+
schema = schema.openapi(beanOptions.beanFullName, openapi);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return schema as any;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function $schemaLazy<T>(...schemaLikes: SchemaLike<T>[]): z.ZodType<T> {
|
|
90
|
+
return z.lazy(() => {
|
|
91
|
+
return _createSchemaLazy(schemaLikes);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function _createSchemaLazy<T>(schemaLikes: SchemaLike<T>[]): z.ZodType<T> {
|
|
96
|
+
const classType = schemaLikes[schemaLikes.length - 1];
|
|
97
|
+
schemaLikes = schemaLikes.slice(0, schemaLikes.length - 1);
|
|
98
|
+
const classType2 = prepareClassType(classType as any);
|
|
99
|
+
return makeSchemaLikes(schemaLikes, $schema(classType2));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function _createSchemaObject(rules: {}, options?: ISchemaObjectOptions) {
|
|
103
|
+
let schema = z.object(rules as z.ZodRawShape);
|
|
104
|
+
if (options?.loose) schema = z.looseObject(schema.shape) as any;
|
|
105
|
+
if (options?.strict) schema = z.strictObject(schema.shape) as any;
|
|
106
|
+
return schema;
|
|
107
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Constructable } from 'vona';
|
|
2
|
+
|
|
3
|
+
const __schemasDynamic: Record<string, Constructable> = {};
|
|
4
|
+
|
|
5
|
+
export const SymbolSchemaDynamicRefId = Symbol('SymbolSchemaDynamicRefId');
|
|
6
|
+
|
|
7
|
+
export function addSchemaDynamic(dynamicName: string, classType: Constructable) {
|
|
8
|
+
__schemasDynamic[dynamicName] = classType;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function getSchemaDynamic(dynamicName: string) {
|
|
12
|
+
return __schemasDynamic[dynamicName];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getSchemasDynamic() {
|
|
16
|
+
return __schemasDynamic;
|
|
17
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { CurrencyOptions } from '@zhennann/currency';
|
|
2
|
+
import type {
|
|
3
|
+
ICaptchaOptions,
|
|
4
|
+
IResourcePickerOptions,
|
|
5
|
+
ISelectOptions,
|
|
6
|
+
ITextareaOptions,
|
|
7
|
+
IToggleOptions,
|
|
8
|
+
TypeDateFormat,
|
|
9
|
+
TypeSchemaScene,
|
|
10
|
+
} from 'vona-module-a-openapi';
|
|
11
|
+
import type z from 'zod';
|
|
12
|
+
|
|
13
|
+
import type { ISchemaComponentRecord } from '../../../types/component.ts';
|
|
14
|
+
|
|
15
|
+
import { _generalSchemaRest } from './utils.ts';
|
|
16
|
+
|
|
17
|
+
const __schemaComponents = {
|
|
18
|
+
currency: schemaCurrency,
|
|
19
|
+
date: schemaDate,
|
|
20
|
+
captcha: schemaCaptcha,
|
|
21
|
+
toggle: schemaToggle,
|
|
22
|
+
select: schemaSelect,
|
|
23
|
+
resourcePicker: schemaResourcePicker,
|
|
24
|
+
textarea: schemaTextarea,
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
export function schemaComponent<K extends keyof ISchemaComponentRecord>(name: K, options?: ISchemaComponentRecord[K], scene?: TypeSchemaScene) {
|
|
28
|
+
return __schemaComponents[name](options as any, scene);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function schemaCurrency<T extends z.ZodType>(currency?: CurrencyOptions, scene?: TypeSchemaScene) {
|
|
32
|
+
return function (schema: T): T {
|
|
33
|
+
const options = currency !== undefined ? { render: 'currency', currency } : { render: 'currency' };
|
|
34
|
+
return _generalSchemaRest(schema, options, scene);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function schemaDate<T extends z.ZodType>(dateFormat?: TypeDateFormat, scene?: TypeSchemaScene) {
|
|
39
|
+
return function (schema: T): T {
|
|
40
|
+
const options = dateFormat !== undefined ? { render: 'date', dateFormat } : { render: 'date' };
|
|
41
|
+
return _generalSchemaRest(schema, options, scene);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function schemaCaptcha<T extends z.ZodType>(captcha?: ICaptchaOptions, scene?: TypeSchemaScene) {
|
|
46
|
+
return function (schema: T): T {
|
|
47
|
+
const options = captcha !== undefined ? { render: 'captcha', captcha } : { render: 'captcha' };
|
|
48
|
+
return _generalSchemaRest(schema, options, scene);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function schemaToggle<T extends z.ZodType>(toggle?: IToggleOptions, scene?: TypeSchemaScene) {
|
|
53
|
+
return function (schema: T): T {
|
|
54
|
+
const options = toggle !== undefined ? { render: 'toggle', toggle } : { render: 'toggle' };
|
|
55
|
+
return _generalSchemaRest(schema, options, scene);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function schemaSelect<T extends z.ZodType>(select?: ISelectOptions, scene?: TypeSchemaScene) {
|
|
60
|
+
return function (schema: T): T {
|
|
61
|
+
const options = select !== undefined ? { render: 'select', select } : { render: 'select' };
|
|
62
|
+
return _generalSchemaRest(schema, options, scene);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function schemaResourcePicker<T extends z.ZodType>(resourcePicker?: IResourcePickerOptions, scene?: TypeSchemaScene) {
|
|
67
|
+
return function (schema: T): T {
|
|
68
|
+
const options = resourcePicker !== undefined ? { render: 'resourcePicker', resourcePicker } : { render: 'resourcePicker' };
|
|
69
|
+
return _generalSchemaRest(schema, options, scene);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function schemaTextarea<T extends z.ZodType>(textarea?: ITextareaOptions, scene?: TypeSchemaScene) {
|
|
74
|
+
return function (schema: T): T {
|
|
75
|
+
const options = textarea !== undefined ? { render: 'textarea', textarea } : { render: 'textarea' };
|
|
76
|
+
return _generalSchemaRest(schema, options, scene);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { IFilterTransformOptionsDateRange, IFilterTransformRecord, ISchemaObjectExtensionFieldFilter } from 'vona-module-a-web';
|
|
2
|
+
import type z from 'zod';
|
|
3
|
+
|
|
4
|
+
import React from 'react';
|
|
5
|
+
|
|
6
|
+
export function schemaFilter(options: ISchemaObjectExtensionFieldFilter) {
|
|
7
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
8
|
+
return schema.openapi({
|
|
9
|
+
filter: options,
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function schemaFilterTransform<T extends keyof IFilterTransformRecord>(filterTransformName: T, options?: Partial<IFilterTransformRecord[T]>) {
|
|
15
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
16
|
+
return schema.openapi({
|
|
17
|
+
filter: {
|
|
18
|
+
transform: [filterTransformName, options],
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function schemaFilterDateRange(options?: Partial<IFilterTransformOptionsDateRange>) {
|
|
25
|
+
const separator = options?.separator;
|
|
26
|
+
const DateRange = 'dateRange' as any;
|
|
27
|
+
const filterRender = separator ? <DateRange separator={separator}></DateRange> : 'dateRange';
|
|
28
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
29
|
+
return schema.openapi({
|
|
30
|
+
filter: {
|
|
31
|
+
transform: ['a-web:dateRange', options],
|
|
32
|
+
},
|
|
33
|
+
rest: {
|
|
34
|
+
filter: {
|
|
35
|
+
render: filterRender,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ILocaleMagic } from 'vona';
|
|
2
|
+
|
|
3
|
+
import { useApp } from 'vona';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
import { normalizeErrorParams } from '../../utils.ts';
|
|
7
|
+
|
|
8
|
+
export function schemaEmail(params?: string | ILocaleMagic | z.core.$ZodEmailParams) {
|
|
9
|
+
return function (_schema: z.ZodString): z.ZodEmail {
|
|
10
|
+
return z.email(normalizeErrorParams(params));
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function schemaUrl(params?: string | ILocaleMagic | z.core.$ZodURLParams) {
|
|
15
|
+
return function (_schema: z.ZodString): z.ZodURL {
|
|
16
|
+
return z.url(normalizeErrorParams(params));
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function schemaUuid(params?: string | ILocaleMagic | z.core.$ZodUUIDParams) {
|
|
21
|
+
return function (_schema: z.ZodString): z.ZodUUID {
|
|
22
|
+
return z.uuid(normalizeErrorParams(params));
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function schemaIPv4(params?: string | ILocaleMagic | z.core.$ZodIPv4Params) {
|
|
27
|
+
return function (_schema: z.ZodString): z.ZodIPv4 {
|
|
28
|
+
return z.ipv4(normalizeErrorParams(params));
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function schemaIPv6(params?: string | ILocaleMagic | z.core.$ZodIPv6Params) {
|
|
33
|
+
return function (_schema: z.ZodString): z.ZodIPv6 {
|
|
34
|
+
return z.ipv6(normalizeErrorParams(params));
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function schemaMin(min: number, params?: string | ILocaleMagic | z.core.$ZodCheckMinLengthParams | z.core.$ZodCheckGreaterThanParams) {
|
|
39
|
+
return function (schema: z.ZodString | z.ZodNumber): z.ZodString | z.ZodNumber {
|
|
40
|
+
if (schema.type === 'string') {
|
|
41
|
+
return schema.min(min, normalizeErrorParams(params) as string | z.core.$ZodCheckMinLengthParams);
|
|
42
|
+
} else {
|
|
43
|
+
return schema.min(min, normalizeErrorParams(params) as string | z.core.$ZodCheckGreaterThanParams);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function schemaMax(max: number, params?: string | ILocaleMagic | z.core.$ZodCheckMaxLengthParams | z.core.$ZodCheckLessThanParams) {
|
|
49
|
+
return function (schema: z.ZodString | z.ZodNumber): z.ZodString | z.ZodNumber {
|
|
50
|
+
if (schema.type === 'string') {
|
|
51
|
+
return schema.max(max, normalizeErrorParams(params) as string | z.core.$ZodCheckMaxLengthParams);
|
|
52
|
+
} else {
|
|
53
|
+
return schema.max(max, normalizeErrorParams(params) as string | z.core.$ZodCheckLessThanParams);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function schemaTrim() {
|
|
59
|
+
return function (schema: z.ZodString): z.ZodString {
|
|
60
|
+
return schema.trim();
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function schemaToLowerCase() {
|
|
65
|
+
return function (schema: z.ZodString): z.ZodString {
|
|
66
|
+
return schema.toLowerCase();
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function schemaToUpperCase() {
|
|
71
|
+
return function (schema: z.ZodString): z.ZodString {
|
|
72
|
+
return schema.toUpperCase();
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function schemaLowercase(params?: string | ILocaleMagic | z.core.$ZodCheckLowerCaseParams) {
|
|
77
|
+
return function (schema: z.ZodString): z.ZodString {
|
|
78
|
+
return schema.lowercase(normalizeErrorParams(params));
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function schemaUppercase(params?: string | ILocaleMagic | z.core.$ZodCheckUpperCaseParams) {
|
|
83
|
+
return function (schema: z.ZodString): z.ZodString {
|
|
84
|
+
return schema.uppercase(normalizeErrorParams(params));
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function schemaRegex(regex: RegExp, params?: string | ILocaleMagic | z.core.$ZodCheckRegexParams) {
|
|
89
|
+
return function (schema: z.ZodString): z.ZodString {
|
|
90
|
+
return schema.regex(regex, normalizeErrorParams(params));
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function schemaTableIdentity() {
|
|
95
|
+
return function (_schema?: any): z.ZodType {
|
|
96
|
+
const schema = z.union([z.number(), z.string()]);
|
|
97
|
+
return schema.transform(value => {
|
|
98
|
+
const app = useApp();
|
|
99
|
+
const ormConfig = app.util.getModuleConfigRaw('a-orm');
|
|
100
|
+
const _identityType = ormConfig?.table?.identityType ?? 'bigint';
|
|
101
|
+
if (_identityType === 'number') {
|
|
102
|
+
return Number.parseInt(value as any);
|
|
103
|
+
} else if (_identityType === 'bigint') {
|
|
104
|
+
return String(value);
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error('not support');
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ILocaleMagic } from 'vona';
|
|
2
|
+
import type { TypeOpenapiMetadata } from 'vona-module-a-openapi';
|
|
3
|
+
import type { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
export function schemaOpenapi<T extends z.ZodType>(metadata: TypeOpenapiMetadata<T>): T;
|
|
6
|
+
export function schemaOpenapi<T extends z.ZodType>(refId: string, metadata?: TypeOpenapiMetadata<T>): T;
|
|
7
|
+
export function schemaOpenapi<T extends z.ZodType>(refId: any, metadata?: any) {
|
|
8
|
+
return function (schema: T): T {
|
|
9
|
+
return schema.openapi(refId, metadata);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function schemaTitle<T extends z.ZodType>(title?: string | ILocaleMagic) {
|
|
14
|
+
return function (schema: T): T {
|
|
15
|
+
return schema.openapi({ title });
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function schemaDescription<T extends z.ZodType>(description?: string | ILocaleMagic) {
|
|
20
|
+
return function (schema: T): T {
|
|
21
|
+
return schema.openapi({ description });
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function schemaExample<T extends z.ZodType>(example?: any) {
|
|
26
|
+
return function (schema: T): T {
|
|
27
|
+
return schema.openapi({ example });
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ISchemaObjectExtensionFieldRest,
|
|
3
|
+
ISchemaObjectExtensionFieldRestScene,
|
|
4
|
+
TypeRenderComponent,
|
|
5
|
+
TypeSchemaScene,
|
|
6
|
+
} from 'vona-module-a-openapi';
|
|
7
|
+
import type z from 'zod';
|
|
8
|
+
|
|
9
|
+
import type { TypeSchemaOrderLevel } from '../../../types/order.ts';
|
|
10
|
+
|
|
11
|
+
import { $order } from '../../utils.ts';
|
|
12
|
+
import { _generalSchemaRest } from './utils.ts';
|
|
13
|
+
|
|
14
|
+
export function schemaRest<T extends z.ZodType>(
|
|
15
|
+
rest?: ISchemaObjectExtensionFieldRestScene | ISchemaObjectExtensionFieldRest,
|
|
16
|
+
scene?: TypeSchemaScene,
|
|
17
|
+
) {
|
|
18
|
+
return function (schema: T): T {
|
|
19
|
+
return _generalSchemaRest(schema, rest, scene);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function schemaCustomKey<T extends z.ZodType>(customKey: string, scene?: TypeSchemaScene) {
|
|
24
|
+
return function (schema: T): T {
|
|
25
|
+
const options = { customKey };
|
|
26
|
+
return _generalSchemaRest(schema, options, scene);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function schemaOrder<T extends z.ZodType>(order: number, level?: TypeSchemaOrderLevel, scene?: TypeSchemaScene) {
|
|
31
|
+
const orderReal = $order(order, level);
|
|
32
|
+
return function (schema: T): T {
|
|
33
|
+
const options = { order: orderReal };
|
|
34
|
+
return _generalSchemaRest(schema, options, scene);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function schemaRender<T extends z.ZodType>(render: TypeRenderComponent, scene?: TypeSchemaScene) {
|
|
39
|
+
return function (schema: T): T {
|
|
40
|
+
const options = { render };
|
|
41
|
+
return _generalSchemaRest(schema, options, scene);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function schemaVisible<T extends z.ZodType>(visible?: boolean, scene?: TypeSchemaScene) {
|
|
46
|
+
return function (schema: T): T {
|
|
47
|
+
const options = { visible };
|
|
48
|
+
return _generalSchemaRest(schema, options, scene);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ISerializerTransformOptionsCustom,
|
|
3
|
+
ISerializerTransformOptionsExclude,
|
|
4
|
+
ISerializerTransformOptionsGetter,
|
|
5
|
+
ISerializerTransformRecord,
|
|
6
|
+
TypeSerializerTransformCustom,
|
|
7
|
+
TypeSerializerTransformGetter,
|
|
8
|
+
} from 'vona-module-a-serialization';
|
|
9
|
+
import type z from 'zod';
|
|
10
|
+
|
|
11
|
+
export function schemaSerializerTransform<T extends keyof ISerializerTransformRecord>(
|
|
12
|
+
serializerTransformName: T,
|
|
13
|
+
options?: Partial<ISerializerTransformRecord[T]>,
|
|
14
|
+
) {
|
|
15
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
16
|
+
return schema.openapi({
|
|
17
|
+
serializerTransforms: {
|
|
18
|
+
[serializerTransformName]: options,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function schemaSerializerExclude(options: Partial<ISerializerTransformOptionsExclude>);
|
|
25
|
+
export function schemaSerializerExclude(exclude?: boolean);
|
|
26
|
+
export function schemaSerializerExclude(param?: boolean | Partial<ISerializerTransformOptionsExclude>) {
|
|
27
|
+
let options: Partial<ISerializerTransformOptionsExclude>;
|
|
28
|
+
if (!param || typeof param === 'boolean') {
|
|
29
|
+
options = { exclude: param };
|
|
30
|
+
} else {
|
|
31
|
+
options = param;
|
|
32
|
+
}
|
|
33
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
34
|
+
return schema.openapi({
|
|
35
|
+
serializerTransforms: {
|
|
36
|
+
'a-serialization:exclude': options,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function schemaSerializerReplace(options: ISerializerTransformRecord['a-serialization:replace']) {
|
|
43
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
44
|
+
return schema.openapi({
|
|
45
|
+
serializerTransforms: {
|
|
46
|
+
'a-serialization:replace': options,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function schemaSerializerGetter(options: Partial<ISerializerTransformOptionsGetter>);
|
|
53
|
+
export function schemaSerializerGetter(getter: TypeSerializerTransformGetter);
|
|
54
|
+
export function schemaSerializerGetter(param: TypeSerializerTransformGetter | Partial<ISerializerTransformOptionsGetter>) {
|
|
55
|
+
let options: Partial<ISerializerTransformOptionsGetter>;
|
|
56
|
+
if (typeof param === 'function') {
|
|
57
|
+
options = { getter: param };
|
|
58
|
+
} else {
|
|
59
|
+
options = param;
|
|
60
|
+
}
|
|
61
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
62
|
+
return schema.openapi({
|
|
63
|
+
serializerTransforms: {
|
|
64
|
+
'a-serialization:getter': options,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function schemaSerializerCustom(options: Partial<ISerializerTransformOptionsCustom>);
|
|
71
|
+
export function schemaSerializerCustom(getter: TypeSerializerTransformCustom);
|
|
72
|
+
export function schemaSerializerCustom(param: TypeSerializerTransformCustom | Partial<ISerializerTransformOptionsCustom>) {
|
|
73
|
+
let options: Partial<ISerializerTransformOptionsCustom>;
|
|
74
|
+
if (typeof param === 'function') {
|
|
75
|
+
options = { custom: param };
|
|
76
|
+
} else {
|
|
77
|
+
options = param;
|
|
78
|
+
}
|
|
79
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
80
|
+
return schema.openapi({
|
|
81
|
+
serializerTransforms: {
|
|
82
|
+
'a-serialization:custom': options,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Constructable, ILocaleMagic } from 'vona';
|
|
2
|
+
import type { ISchemaObjectOptions } from 'vona-module-a-openapi';
|
|
3
|
+
|
|
4
|
+
import { isNil } from '@cabloy/utils';
|
|
5
|
+
import { coerceWithNil } from '@cabloy/zod-query';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
|
|
8
|
+
import type { SchemaLike } from '../../../types/decorator.ts';
|
|
9
|
+
|
|
10
|
+
import { $locale } from '../../../.metadata/locales.ts';
|
|
11
|
+
import { normalizeErrorParams } from '../../utils.ts';
|
|
12
|
+
import { $schema, $schemaLazy, makeSchemaLike } from '../makeSchemaLikes.ts';
|
|
13
|
+
|
|
14
|
+
export function schemaDefault(defaultValue: any | Function) {
|
|
15
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
16
|
+
return schema.default(defaultValue);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function schemaOptional() {
|
|
21
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
22
|
+
return schema.optional();
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function schemaLazy<T>(...schemaLikes: SchemaLike<T>[]) {
|
|
27
|
+
return function (_schema?: z.ZodType): z.ZodType<T> {
|
|
28
|
+
return $schemaLazy(...schemaLikes);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function schemaObject<T>(classType: Constructable<T>, options?: ISchemaObjectOptions) {
|
|
33
|
+
return function (_schema?: z.ZodType): z.ZodType<T> {
|
|
34
|
+
return $schema(classType, options);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function schemaArray(schemaLike?: SchemaLike, params?: z.core.$ZodArrayParams & { separator?: string }) {
|
|
39
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
40
|
+
return z.preprocess(
|
|
41
|
+
val => {
|
|
42
|
+
val = coerceWithNil(val);
|
|
43
|
+
if (isNil(val)) return val;
|
|
44
|
+
if (typeof val !== 'string') return val;
|
|
45
|
+
if (val.startsWith('[') && val.endsWith(']')) return JSON.parse(val);
|
|
46
|
+
return val.split(params?.separator ?? ',');
|
|
47
|
+
},
|
|
48
|
+
z.array(makeSchemaLike(schemaLike ?? schema, z.any()), params),
|
|
49
|
+
// z.array(makeSchemaLike(schemaLike, schema), params),
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function schemaStrictObject() {
|
|
55
|
+
return function (schema: z.ZodObject): z.ZodObject {
|
|
56
|
+
return z.strictObject(schema.shape);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function schemaLooseObject() {
|
|
61
|
+
return function (schema: z.ZodObject): z.ZodObject {
|
|
62
|
+
return z.looseObject(schema.shape);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function schemaRequired(params?: string | ILocaleMagic | z.core.$ZodStringParams) {
|
|
67
|
+
const errorDefault = { error: () => $locale('ZodErrorRequired') };
|
|
68
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
69
|
+
schema._zod.def.error = normalizeErrorParams(params, errorDefault).error;
|
|
70
|
+
return schema;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TypeSchemaScene } from 'vona-module-a-openapi';
|
|
2
|
+
import type z from 'zod';
|
|
3
|
+
|
|
4
|
+
export function _generalSchemaRest<T extends z.ZodType>(schema: T, options: any, scene?: TypeSchemaScene) {
|
|
5
|
+
return schema.openapi(scene ? { rest: { [scene]: options } } : { rest: options });
|
|
6
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { IZodRefineExecute, IZodRefineRecord, IZodTransformExecute, IZodTransformRecord } from 'vona-module-a-zod';
|
|
2
|
+
import type z from 'zod';
|
|
3
|
+
|
|
4
|
+
import { beanFullNameFromOnionName, useApp } from 'vona';
|
|
5
|
+
|
|
6
|
+
export function schemaZodRefine<T extends keyof IZodRefineRecord>(zodRefineName: T, options?: Partial<IZodRefineRecord[T]>) {
|
|
7
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
8
|
+
return schema.superRefine(async (value, refinementCtx) => {
|
|
9
|
+
const app = useApp();
|
|
10
|
+
const options2 = app.bean.onion.zodRefine.getOnionOptionsDynamic(zodRefineName, options);
|
|
11
|
+
// execute
|
|
12
|
+
const beanFullName = beanFullNameFromOnionName(zodRefineName, 'zodRefine');
|
|
13
|
+
const beanInstance = app.bean._getBean(beanFullName) as unknown as IZodRefineExecute;
|
|
14
|
+
if (!beanInstance) {
|
|
15
|
+
throw new Error(`zodRefine bean not found: ${beanFullName}`);
|
|
16
|
+
}
|
|
17
|
+
return await beanInstance.execute(value, refinementCtx, options2);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function schemaZodTransform<T extends keyof IZodTransformRecord>(zodTransformName: T, options?: Partial<IZodTransformRecord[T]>) {
|
|
23
|
+
return function (schema: z.ZodType): z.ZodType {
|
|
24
|
+
return schema.transform(async value => {
|
|
25
|
+
const app = useApp();
|
|
26
|
+
const options2 = app.bean.onion.zodTransform.getOnionOptionsDynamic(zodTransformName, options);
|
|
27
|
+
// execute
|
|
28
|
+
const beanFullName = beanFullNameFromOnionName(zodTransformName, 'zodTransform');
|
|
29
|
+
const beanInstance = app.bean._getBean(beanFullName) as IZodTransformExecute;
|
|
30
|
+
if (!beanInstance) {
|
|
31
|
+
throw new Error(`zodTransform bean not found: ${beanFullName}`);
|
|
32
|
+
}
|
|
33
|
+
return await beanInstance.execute(value, options2);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
}
|