zod-typeorm 1.0.0
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 +21 -0
- package/README.md +0 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +2 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/decorators/zod-property.d.ts +27 -0
- package/dist/decorators/zod-property.d.ts.map +1 -0
- package/dist/decorators/zod-property.js +41 -0
- package/dist/decorators/zod-property.js.map +1 -0
- package/dist/decorators/zod-schema.d.ts +14 -0
- package/dist/decorators/zod-schema.d.ts.map +1 -0
- package/dist/decorators/zod-schema.js +13 -0
- package/dist/decorators/zod-schema.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/schema.d.ts +52 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +63 -0
- package/dist/schema.js.map +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Martin Dagarin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,WAAW,EACX,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GACzB,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,WAAW,GAGZ,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const ZOD_SCHEMA_PROPERTY: unique symbol;
|
|
4
|
+
export interface ZodPropertyOptions {
|
|
5
|
+
schema: z.ZodType | (() => z.ZodType);
|
|
6
|
+
/**
|
|
7
|
+
* Make property optional when generating schema for certain variants
|
|
8
|
+
*/
|
|
9
|
+
optionalForVariants?: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Skip property when generating schema for certain variants (blacklist)
|
|
12
|
+
*/
|
|
13
|
+
skipForVariants?: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Include property only when generating schema for specified variants (whitelist)
|
|
16
|
+
*/
|
|
17
|
+
includeForVariants?: string[];
|
|
18
|
+
}
|
|
19
|
+
export interface ZodPropertyMetadata {
|
|
20
|
+
propertyKey: string | symbol;
|
|
21
|
+
schema: z.ZodType | (() => z.ZodType);
|
|
22
|
+
optionalForVariants: string[];
|
|
23
|
+
skipForVariants: string[];
|
|
24
|
+
includeForVariants: string[];
|
|
25
|
+
}
|
|
26
|
+
export declare function ZodProperty(options: ZodPropertyOptions | z.ZodType | (() => z.ZodType)): PropertyDecorator;
|
|
27
|
+
//# sourceMappingURL=zod-property.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-property.d.ts","sourceRoot":"","sources":["../../src/decorators/zod-property.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,mBAAmB,eAAgC,CAAC;AAGjE,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,CAAC,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC;IAEtC;;OAEG;IACH,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAE/B;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,EAAE,CAAC;IAE3B;;OAEG;IACH,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;CAC/B;AAGD,MAAM,WAAW,mBAAmB;IAClC,WAAW,EAAE,MAAM,GAAG,MAAM,CAAC;IAC7B,MAAM,EAAE,CAAC,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC;IACtC,mBAAmB,EAAE,MAAM,EAAE,CAAC;IAC9B,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,kBAAkB,EAAE,MAAM,EAAE,CAAC;CAC9B;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,CAAC,CAAC,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,iBAAiB,CAyC1G"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export const ZOD_SCHEMA_PROPERTY = Symbol('zod:schema:property');
|
|
4
|
+
export function ZodProperty(options) {
|
|
5
|
+
return (target, propertyKey) => {
|
|
6
|
+
const ctor = target.constructor;
|
|
7
|
+
let existingMetadata = Reflect.getOwnMetadata(ZOD_SCHEMA_PROPERTY, ctor) ?? [];
|
|
8
|
+
// First time copy metadata from base class if anything exists
|
|
9
|
+
if (!existingMetadata || existingMetadata.length < 1) {
|
|
10
|
+
existingMetadata = [...(Reflect.getMetadata(ZOD_SCHEMA_PROPERTY, ctor) ?? [])];
|
|
11
|
+
}
|
|
12
|
+
// Prevent duplicate metadata registration
|
|
13
|
+
const index = existingMetadata.findIndex(m => m.propertyKey === propertyKey);
|
|
14
|
+
// Prepare new record
|
|
15
|
+
const metadata = {
|
|
16
|
+
propertyKey: propertyKey,
|
|
17
|
+
...((options instanceof z.ZodType || typeof options === 'function')
|
|
18
|
+
? {
|
|
19
|
+
schema: options,
|
|
20
|
+
optionalForVariants: [],
|
|
21
|
+
skipForVariants: [],
|
|
22
|
+
includeForVariants: [],
|
|
23
|
+
}
|
|
24
|
+
: {
|
|
25
|
+
schema: options.schema,
|
|
26
|
+
optionalForVariants: options.optionalForVariants ?? [],
|
|
27
|
+
skipForVariants: options.skipForVariants ?? [],
|
|
28
|
+
includeForVariants: options.includeForVariants ?? [],
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
if (index === -1) { // No existing metadata found => add new record
|
|
32
|
+
existingMetadata.push(metadata);
|
|
33
|
+
}
|
|
34
|
+
else { // Found existing metadata => overwrite
|
|
35
|
+
existingMetadata[index] = metadata;
|
|
36
|
+
}
|
|
37
|
+
// Set updated metadata
|
|
38
|
+
Reflect.defineMetadata(ZOD_SCHEMA_PROPERTY, existingMetadata, ctor);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMapping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mBAAmB,IAAI,EAAE;oBACtD,eAAe,EAAE,OAAO,CAAC,eAAe,IAAI,EAAE;oBAC9C,kBAAkB,EAAE,OAAO,CAAC,kBAAkB,IAAI,EAAE;iBACrD,CAAC;SACL,CAAC;QAEF,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,+CAA+C;YACjE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;aAAM,CAAC,CAAC,uCAAuC;YAC9C,gBAAgB,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC;QACrC,CAAC;QAED,uBAAuB;QACvB,OAAO,CAAC,cAAc,CAAC,mBAAmB,EAAE,gBAAgB,EAAE,IAAI,CAAC,CAAC;IACtE,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
export declare const ZOD_SCHEMA: unique symbol;
|
|
3
|
+
export interface ZodSchemaOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Reject unknown properties
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
strict?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface ZodSchemaMetadata {
|
|
11
|
+
strict: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare function ZodSchema(options?: ZodSchemaOptions): ClassDecorator;
|
|
14
|
+
//# sourceMappingURL=zod-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-schema.d.ts","sourceRoot":"","sources":["../../src/decorators/zod-schema.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAI1B,eAAO,MAAM,UAAU,eAAuB,CAAC;AAG/C,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAGlB;AAGD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,cAAc,CASxE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export const ZOD_SCHEMA = Symbol('zod:schema');
|
|
4
|
+
export function ZodSchema(options = {}) {
|
|
5
|
+
return (target) => {
|
|
6
|
+
// Prepare new record
|
|
7
|
+
const metadata = {
|
|
8
|
+
strict: options.strict ?? false,
|
|
9
|
+
};
|
|
10
|
+
Reflect.defineMetadata(ZOD_SCHEMA, metadata, target);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=zod-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-schema.js","sourceRoot":"","sources":["../../src/decorators/zod-schema.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAkB/C,MAAM,UAAU,SAAS,CAAC,OAAO,GAAqB,EAAE;IACtD,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,qBAAqB;QACrB,MAAM,QAAQ,GAAsB;YAClC,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,KAAK;SAChC,CAAC;QAEF,OAAO,CAAC,cAAc,CAAC,UAAU,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IACvD,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,uBAAuB,CAAC;AAEtC,OAAO,EACL,mCAAmC,EACnC,yBAAyB,EACzB,0BAA0B,EAC1B,KAAK,sBAAsB,GAC5B,MAAM,aAAa,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,uBAAuB,CAAC;AAEtC,OAAO,EACL,mCAAmC,EACnC,yBAAyB,EACzB,0BAA0B,GAE3B,MAAM,aAAa,CAAC"}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export interface CreateZodSchemaOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Create strict schema
|
|
6
|
+
*/
|
|
7
|
+
strict?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Make fields optional
|
|
10
|
+
*/
|
|
11
|
+
optionalFields?: (string | Symbol)[];
|
|
12
|
+
/**
|
|
13
|
+
* Skip additional fields
|
|
14
|
+
*/
|
|
15
|
+
skipFields?: (string | Symbol)[];
|
|
16
|
+
/**
|
|
17
|
+
* Add additional field transformation
|
|
18
|
+
*/
|
|
19
|
+
transformFields?: Record<string | symbol, (schema: z.ZodType) => z.ZodType>;
|
|
20
|
+
/**
|
|
21
|
+
* Additional schema transformation
|
|
22
|
+
*/
|
|
23
|
+
transformSchema?: (schema: z.ZodObject<z.ZodRawShape>) => z.ZodObject<z.ZodRawShape>;
|
|
24
|
+
/**
|
|
25
|
+
* Add additional schema transformation for certain schemas
|
|
26
|
+
*/
|
|
27
|
+
transformSchemaForVariants?: Record<string, (schema: z.ZodObject<z.ZodRawShape>) => z.ZodObject<z.ZodRawShape>>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generate schema from decorated class for variant
|
|
31
|
+
* @param entityClass Entity class
|
|
32
|
+
* @param variantName Variant name
|
|
33
|
+
* @param options Options
|
|
34
|
+
* @returns z.ZodObject
|
|
35
|
+
*/
|
|
36
|
+
export declare function createZodSchemaFromEntityForVariant<T>(entityClass: new () => T, variantName: string, options?: CreateZodSchemaOptions): z.ZodObject<z.ZodRawShape>;
|
|
37
|
+
/**
|
|
38
|
+
* Generate schema from decorated class for 'default' variant
|
|
39
|
+
* @param entityClass Entity class
|
|
40
|
+
* @param options Options
|
|
41
|
+
* @returns z.ZodObject
|
|
42
|
+
*/
|
|
43
|
+
export declare function createZodSchemaFromEntity<T>(entityClass: new () => T, options?: CreateZodSchemaOptions): z.ZodObject<z.ZodRawShape>;
|
|
44
|
+
/**
|
|
45
|
+
* Generate schema from decorated class for list of variants
|
|
46
|
+
* @param entityClass Entity class
|
|
47
|
+
* @param variantNames Variant names
|
|
48
|
+
* @param options Options
|
|
49
|
+
* @returns Record<string, z.ZodObject>
|
|
50
|
+
*/
|
|
51
|
+
export declare function createZodSchemasFromEntity<T>(entityClass: new () => T, variantNames: string[], options?: CreateZodSchemaOptions): Record<string, z.ZodObject<z.ZodRawShape>>;
|
|
52
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,cAAc,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAErC;;OAEG;IACH,UAAU,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAEjC;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC;IAE5E;;OAEG;IACH,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;IAErF;;OAEG;IACH,0BAA0B,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC;CACjH;AAED;;;;;;GAMG;AACH,wBAAgB,mCAAmC,CAAC,CAAC,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,GAAE,sBAA2B,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAiCtK;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,OAAO,GAAE,sBAA2B,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAEvI;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAAE,WAAW,EAAE,UAAU,CAAC,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,sBAA2B,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAKhL"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { ZOD_SCHEMA_PROPERTY } from './decorators/zod-property.js';
|
|
4
|
+
/**
|
|
5
|
+
* Generate schema from decorated class for variant
|
|
6
|
+
* @param entityClass Entity class
|
|
7
|
+
* @param variantName Variant name
|
|
8
|
+
* @param options Options
|
|
9
|
+
* @returns z.ZodObject
|
|
10
|
+
*/
|
|
11
|
+
export function createZodSchemaFromEntityForVariant(entityClass, variantName, options = {}) {
|
|
12
|
+
// Get metadata for properties
|
|
13
|
+
const propertyMetadata = Reflect.getMetadata(ZOD_SCHEMA_PROPERTY, entityClass) ?? [];
|
|
14
|
+
if (propertyMetadata.length <= 0)
|
|
15
|
+
throw new Error(`No Zod metadata found on Entity ${entityClass.name} (Use @ZodProperty)`);
|
|
16
|
+
// Build set of properties for zod schema
|
|
17
|
+
const shape = {};
|
|
18
|
+
for (const item of propertyMetadata) { // Go over each property decorator metadata
|
|
19
|
+
// Check if field needs to be skipped (schema skip, skip field because of variant) or if not in include list
|
|
20
|
+
if (options.skipFields?.includes(item.propertyKey) || item.skipForVariants.includes(variantName) || (item.includeForVariants.length > 0 && !item.includeForVariants.includes(variantName)))
|
|
21
|
+
continue;
|
|
22
|
+
const propertyKey = String(item.propertyKey);
|
|
23
|
+
shape[propertyKey] =
|
|
24
|
+
typeof item.schema === 'function'
|
|
25
|
+
? item.schema()
|
|
26
|
+
: item.schema;
|
|
27
|
+
if (item.propertyKey && options.transformFields && item.propertyKey in options.transformFields && options.transformFields[item.propertyKey]) // Transform field
|
|
28
|
+
shape[propertyKey] = options.transformFields[item.propertyKey](shape[propertyKey]);
|
|
29
|
+
if (options.optionalFields?.includes(item.propertyKey) || item.optionalForVariants.includes(variantName)) // Make field optional
|
|
30
|
+
shape[propertyKey] = shape[propertyKey].optional();
|
|
31
|
+
}
|
|
32
|
+
let schema = z.object(shape);
|
|
33
|
+
if (options.transformSchemaForVariants && variantName in options.transformSchemaForVariants && options.transformSchemaForVariants[variantName]) // Per-variant transformation
|
|
34
|
+
schema = options.transformSchemaForVariants[variantName](schema);
|
|
35
|
+
if (options.transformSchema) // Single (everytime) transformation
|
|
36
|
+
schema = options.transformSchema(schema);
|
|
37
|
+
if (options.strict)
|
|
38
|
+
schema = schema.strict();
|
|
39
|
+
return schema;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Generate schema from decorated class for 'default' variant
|
|
43
|
+
* @param entityClass Entity class
|
|
44
|
+
* @param options Options
|
|
45
|
+
* @returns z.ZodObject
|
|
46
|
+
*/
|
|
47
|
+
export function createZodSchemaFromEntity(entityClass, options = {}) {
|
|
48
|
+
return createZodSchemaFromEntityForVariant(entityClass, 'default', options);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Generate schema from decorated class for list of variants
|
|
52
|
+
* @param entityClass Entity class
|
|
53
|
+
* @param variantNames Variant names
|
|
54
|
+
* @param options Options
|
|
55
|
+
* @returns Record<string, z.ZodObject>
|
|
56
|
+
*/
|
|
57
|
+
export function createZodSchemasFromEntity(entityClass, variantNames, options = {}) {
|
|
58
|
+
const schemaVariants = {};
|
|
59
|
+
for (let name of variantNames)
|
|
60
|
+
schemaVariants[name] = createZodSchemaFromEntityForVariant(entityClass, name, options);
|
|
61
|
+
return schemaVariants;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,mBAAmB,EAA4B,MAAM,8BAA8B,CAAC;AAkC7F;;;;;;GAMG;AACH,MAAM,UAAU,mCAAmC,CAAI,WAAwB,EAAE,WAAmB,EAAE,OAAO,GAA2B,EAAE;IACxI,8BAA8B;IAC9B,MAAM,gBAAgB,GAA0B,OAAO,CAAC,WAAW,CAAC,mBAAmB,EAAE,WAAW,CAAC,IAAI,EAAE,CAAC;IAC5G,IAAI,gBAAgB,CAAC,MAAM,IAAI,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,mCAAmC,WAAW,CAAC,IAAI,qBAAqB,CAAC,CAAC;IAE5F,yCAAyC;IACzC,MAAM,KAAK,GAA8B,EAAE,CAAC;IAE5C,KAAK,MAAM,IAAI,IAAI,gBAAgB,EAAE,CAAC,CAAC,2CAA2C;QAChF,4GA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AA6B;QAC3K,MAAM,GAAG,OAAO,CAAC,0BAA0B,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;IACnE,IAAI,OAAO,CAAC,eAAe,EAAE,oCAAoC;QAC/D,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IAC3C,IAAI,OAAO,CAAC,MAAM;QAChB,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;IAC3B,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CAAI,WAAwB,EAAE,OAAO,GAA2B,EAAE;IACzG,OAAO,mCAAmC,CAAC,WAAW,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC9E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B,CAAI,WAAwB,EAAE,YAAsB,EAAE,OAAO,GAA2B,EAAE;IAClI,MAAM,cAAc,GAA+C,EAAE,CAAC;IACtE,KAAK,IAAI,IAAI,IAAI,YAAY;QAC3B,cAAc,CAAC,IAAI,CAAC,GAAG,mCAAmC,CAAC,WAAW,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACzF,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zod-typeorm",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeORM Zod utililities",
|
|
5
|
+
"keywords": ["database", "orm", "schema", "typeorm", "validation", "zod"],
|
|
6
|
+
"author": "Martin Dagarin",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/SloCompTech/zod-typeorm#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/SloCompTech/zod-typeorm/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/SloCompTech/zod-typeorm.git"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"files": ["dist", "LICENSE", "README.md"],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.test.json --noEmit",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"check-format": "npx @biomejs/biome check",
|
|
25
|
+
"format": "npx @biomejs/biome check --write",
|
|
26
|
+
"publish-dry-run": "npm pack --dry-run"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@biomejs/biome": "2.5.14",
|
|
30
|
+
"@types/node": "^22.20.2",
|
|
31
|
+
"typescript": "^7.0.2",
|
|
32
|
+
"vitest": "^5.0.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"reflect-metadata": "^0.2.2",
|
|
36
|
+
"typeorm": "^1.1.1",
|
|
37
|
+
"zod": "^4.6.2"
|
|
38
|
+
}
|
|
39
|
+
}
|