zod-typeorm 1.0.0 → 1.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/README.md +166 -0
- package/package.json +1 -1
- package/dist/decorators/index.d.ts +0 -2
- package/dist/decorators/index.d.ts.map +0 -1
- package/dist/decorators/index.js +0 -2
- package/dist/decorators/index.js.map +0 -1
- package/dist/decorators/zod-property.d.ts +0 -27
- package/dist/decorators/zod-property.d.ts.map +0 -1
- package/dist/decorators/zod-property.js +0 -41
- package/dist/decorators/zod-property.js.map +0 -1
- package/dist/decorators/zod-schema.d.ts +0 -14
- package/dist/decorators/zod-schema.d.ts.map +0 -1
- package/dist/decorators/zod-schema.js +0 -13
- package/dist/decorators/zod-schema.js.map +0 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -6
- package/dist/index.js.map +0 -1
- package/dist/schema.d.ts +0 -52
- package/dist/schema.d.ts.map +0 -1
- package/dist/schema.js +0 -63
- package/dist/schema.js.map +0 -1
package/README.md
CHANGED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# zod-typeorm
|
|
2
|
+
|
|
3
|
+
[zod-typeorm](https://github.com/SloCompTech/zod-typeorm) aims to be simple helper library to help developers create Zod schemas from TypeORM entites without assumptions about development style.
|
|
4
|
+
|
|
5
|
+
## Brief description
|
|
6
|
+
|
|
7
|
+
- Works with class inheritance (no base class pollution)
|
|
8
|
+
- Currently **no property name conflict resolution**
|
|
9
|
+
- Currently no integration with *@Column* decorator from TypeORM
|
|
10
|
+
- Supports schema variants (no predefined schema)
|
|
11
|
+
|
|
12
|
+
## Quick start
|
|
13
|
+
|
|
14
|
+
Define schema for each property with *@ZodProperty*.
|
|
15
|
+
|
|
16
|
+
- Argument can be schema diretly like `@ZodProperty(z.number().min(0))`
|
|
17
|
+
- Or it can be options with *schema* property `@ZodProperty({ schema: z.number().min(0) })`
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
@Entity()
|
|
21
|
+
export class TestEntity {
|
|
22
|
+
@PrimaryGeneratedColumn({ unsigned: true })
|
|
23
|
+
@ZodProperty(z.number().min(0))
|
|
24
|
+
id: number;
|
|
25
|
+
|
|
26
|
+
@Column()
|
|
27
|
+
@ZodProperty(z.string().min(1).max(255))
|
|
28
|
+
name: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Create schema from entity
|
|
32
|
+
const entitySchema = createZodSchemaFromEntity(TestEntity);
|
|
33
|
+
// included schema properties: id, name
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install zod-typeorm
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Inheritance support
|
|
43
|
+
|
|
44
|
+
Library supports object inheritance with limition that property names should **not be duplicated** between classes.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
export class EntityBase {
|
|
48
|
+
@Column()
|
|
49
|
+
@ZodProperty({ schema: z.string().min(1).max(255) })
|
|
50
|
+
baseProperty: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Entity()
|
|
54
|
+
export class TestEntity extends EntityBase {
|
|
55
|
+
@PrimaryGeneratedColumn({ unsigned: true })
|
|
56
|
+
@ZodProperty({ schema: z.number().min(0) })
|
|
57
|
+
id: number;
|
|
58
|
+
|
|
59
|
+
@Column()
|
|
60
|
+
@ZodProperty({ schema: z.string().min(1).max(255) })
|
|
61
|
+
name: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Create schema from entity
|
|
65
|
+
const entitySchema = createZodSchemaFromEntity(TestEntity);
|
|
66
|
+
// included schema properties: baseProperty, id, name
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Schema variants
|
|
70
|
+
|
|
71
|
+
To create different schema from the same Entity based on your use case, there is support for schema variants.
|
|
72
|
+
|
|
73
|
+
- Variants can have arbitrary name (except *default* is reserved), variant names don't need to be defined anywhere, just use logic below
|
|
74
|
+
- Using *includeForVariants* in *@ZodProperty* you can select variants for which property should be included
|
|
75
|
+
- Using *optionalForVariants* in *@ZodProperty* you can make property optional for variants
|
|
76
|
+
- Using *skipForVariants* in *@ZodProperty* you can exclude property from variants
|
|
77
|
+
- Using *transformForVariants* in *@ZodProperty* you can transform schema additionaly for variants
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
@Entity()
|
|
81
|
+
export class TestEntity {
|
|
82
|
+
@PrimaryGeneratedColumn({ unsigned: true })
|
|
83
|
+
@ZodProperty({ schema: z.number().min(0) })
|
|
84
|
+
id: number;
|
|
85
|
+
|
|
86
|
+
@Column()
|
|
87
|
+
@ZodProperty({ schema: z.string().min(1).max(255) })
|
|
88
|
+
name: string;
|
|
89
|
+
|
|
90
|
+
@Column({ unsigned: true })
|
|
91
|
+
@ZodProperty({ schema: z.number().min(0), includeForVariants: ['variant1'] })
|
|
92
|
+
iProperty: number;
|
|
93
|
+
|
|
94
|
+
@Column({ unsigned: true })
|
|
95
|
+
@ZodProperty({ schema: z.number().min(0), includeForVariants: ['variant2'] })
|
|
96
|
+
sProperty: number;
|
|
97
|
+
|
|
98
|
+
@Column({ unsigned: true })
|
|
99
|
+
@ZodProperty({ schema: z.number().min(0), skipForVariants: ['variant2'], optionalForVariants: ['variant3'] })
|
|
100
|
+
pProperty: number;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Create schemas from entity
|
|
104
|
+
const schemas = createZodSchemasFromEntity(TestEntity, ['variant1', 'variant2', 'variant3']);
|
|
105
|
+
schemas.variant1 // variant1: includes properties id, name, iProperty, pProperty
|
|
106
|
+
schemas.variant2 // variant2: includes properties id, name, sProperty
|
|
107
|
+
schemas.variant3 // variant3: includes properties id, name, pProperty?
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## Schema generation options
|
|
112
|
+
|
|
113
|
+
While creating schemas additional options can be specified.
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const schemas = createZodSchemasFromEntity(TestEntity, ['variant1', 'variant2', 'variant3'], {
|
|
117
|
+
strict: true, // Make schema strict (see Zod doc)
|
|
118
|
+
optionalFields: [], // Make fields optional for all variants
|
|
119
|
+
skipFields: [], // Skip fields for all variants,
|
|
120
|
+
transformFields: {
|
|
121
|
+
variant1: (schema: z.ZodType) => schema, // Transform certain fields
|
|
122
|
+
},
|
|
123
|
+
transformSchema: (schema: z.ZodObject<z.ZodRawShape>) => schema, // Additionaly transform generated schema
|
|
124
|
+
transformSchemaForVariants: {
|
|
125
|
+
variant1: (schema: z.ZodObject<z.ZodRawShape>) => schema, // Transform schema for variant
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Type Inference
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
type Variant1 = z.infer<typeof schemas.variant1>;
|
|
134
|
+
type Variant2 = z.infer<typeof schemas.variant2>;
|
|
135
|
+
type Variant3 = z.infer<typeof schemas.variant3>;
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## API reference
|
|
139
|
+
|
|
140
|
+
Decorators:
|
|
141
|
+
|
|
142
|
+
- `@ZodProperty(schema)`
|
|
143
|
+
- `@ZodProperty({ schema, includeForVariants?, optionalForVariants?, skipForVariants?, transformForVariants? })`
|
|
144
|
+
|
|
145
|
+
Schema creation:
|
|
146
|
+
|
|
147
|
+
- `createZodSchemaFromEntity(entityClass, variantName, options)`
|
|
148
|
+
- `createZodSchemasFromEntity(entityClass, [variantNames], options)`
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
## Requirements
|
|
152
|
+
|
|
153
|
+
- TypeORM >= 1.1.1
|
|
154
|
+
- Zod >= 4.0.0
|
|
155
|
+
- TypeScript
|
|
156
|
+
- *reflect-metadata* package
|
|
157
|
+
|
|
158
|
+
## Contribution
|
|
159
|
+
|
|
160
|
+
Contributions are welcome, before commiting changes please run:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
npm run typecheck
|
|
164
|
+
npm run test
|
|
165
|
+
npm run check-format
|
|
166
|
+
```
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/decorators/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,WAAW,GAGZ,MAAM,mBAAmB,CAAC"}
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,41 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=zod-property.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"zod-property.js","sourceRoot":"","sources":["../../src/decorators/zod-property.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CA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,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"}
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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
|
package/dist/schema.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
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
|
package/dist/schema.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,4GAA4G;QAC5G,IAAI,OAAO,CAAC,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAA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