hekireki 0.0.1 → 0.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.
- package/README.md +1 -1
- package/dist/common/format/index.js +6 -6
- package/dist/common/helper/get-camel-case-schema-name-helper.d.ts +3 -1
- package/dist/common/helper/get-camel-case-schema-name-helper.js +2 -2
- package/dist/common/helper/get-pascal-case-schema-name-helper.d.ts +3 -1
- package/dist/common/helper/get-pascal-case-schema-name-helper.js +2 -2
- package/dist/common/helper/get-variable-name-helper.d.ts +4 -1
- package/dist/common/helper/get-variable-name-helper.js +3 -3
- package/dist/common/helper/get-variable-schema-name-helper.d.ts +4 -1
- package/dist/common/helper/get-variable-schema-name-helper.js +7 -3
- package/dist/common/helper/group-by-model-helper.d.ts +3 -1
- package/dist/common/helper/group-by-model-helper.js +7 -7
- package/dist/common/text/capitalize.js +1 -1
- package/dist/common/text/decapitalize.js +1 -1
- package/dist/common/type/index.d.ts +8 -8
- package/dist/common/validator/is-fields-validation.d.ts +9 -7
- package/dist/common/validator/is-fields-validation.js +1 -3
- package/dist/generator/mermaid/generator/generate-er-content.js +14 -8
- package/dist/generator/mermaid/generator/generate-model-fields.js +21 -16
- package/dist/generator/mermaid/generator/generate-model-info.js +5 -5
- package/dist/generator/mermaid/generator/generate-relation-line.js +5 -5
- package/dist/generator/mermaid/index.d.ts +2 -2
- package/dist/generator/mermaid/index.js +52 -52
- package/dist/generator/mermaid/output/index.js +17 -13
- package/dist/generator/mermaid/type/index.d.ts +30 -6
- package/dist/generator/mermaid/validator/exclude-many-to-one-relations.d.ts +3 -1
- package/dist/generator/mermaid/validator/exclude-many-to-one-relations.js +1 -1
- package/dist/generator/mermaid/validator/extract-relations.js +15 -13
- package/dist/generator/mermaid/validator/is-relation.js +25 -25
- package/dist/generator/mermaid/validator/parse-relation.js +17 -16
- package/dist/generator/mermaid/validator/remove-duplicate-relations.d.ts +3 -1
- package/dist/generator/mermaid/validator/remove-duplicate-relations.js +1 -1
- package/dist/generator/valibot/generator/generate-valibot-infer-input.d.ts +4 -1
- package/dist/generator/valibot/generator/generate-valibot-infer-input.js +10 -3
- package/dist/generator/valibot/generator/generate-valibot-properties.d.ts +10 -7
- package/dist/generator/valibot/generator/generate-valibot-properties.js +21 -15
- package/dist/generator/valibot/generator/generate-valibot-schema.d.ts +5 -1
- package/dist/generator/valibot/generator/generate-valibot-schema.js +6 -2
- package/dist/generator/valibot/generator/generate-valibot-schemas.d.ts +10 -7
- package/dist/generator/valibot/generator/generate-valibot-schemas.js +14 -7
- package/dist/generator/valibot/generator/generate-valibot.d.ts +4 -1
- package/dist/generator/valibot/generator/generate-valibot.js +51 -36
- package/dist/generator/valibot/index.d.ts +6 -6
- package/dist/generator/valibot/index.js +46 -40
- package/dist/generator/valibot/validator/is-valibot-documentation.d.ts +3 -1
- package/dist/generator/valibot/validator/is-valibot-documentation.js +8 -7
- package/dist/generator/valibot/validator/is-valibot-validation.d.ts +3 -1
- package/dist/generator/valibot/validator/is-valibot-validation.js +3 -4
- package/dist/generator/zod/generator/generate-zod-infer.d.ts +4 -1
- package/dist/generator/zod/generator/generate-zod-infer.js +10 -3
- package/dist/generator/zod/generator/generate-zod-properties.d.ts +10 -7
- package/dist/generator/zod/generator/generate-zod-properties.js +21 -15
- package/dist/generator/zod/generator/generate-zod-schema.d.ts +5 -1
- package/dist/generator/zod/generator/generate-zod-schema.js +6 -2
- package/dist/generator/zod/generator/generate-zod-schemas.d.ts +10 -7
- package/dist/generator/zod/generator/generate-zod-schemas.js +14 -7
- package/dist/generator/zod/generator/generate-zod.d.ts +4 -1
- package/dist/generator/zod/generator/generate-zod.js +51 -36
- package/dist/generator/zod/index.d.ts +6 -6
- package/dist/generator/zod/index.js +46 -40
- package/dist/generator/zod/validator/is-zod-documentation.d.ts +3 -1
- package/dist/generator/zod/validator/is-zod-documentation.js +8 -7
- package/dist/generator/zod/validator/is-zod-validation.js +3 -4
- package/package.json +2 -2
|
@@ -6,4 +6,8 @@ import type { Config } from "..";
|
|
|
6
6
|
* @param config - The configuration for the generator
|
|
7
7
|
* @returns The generated Zod schema
|
|
8
8
|
*/
|
|
9
|
-
export declare function generateZodSchema(
|
|
9
|
+
export declare function generateZodSchema(
|
|
10
|
+
modelName: string,
|
|
11
|
+
fields: string,
|
|
12
|
+
config: Config,
|
|
13
|
+
): string;
|
|
@@ -10,6 +10,10 @@ const get_variable_schema_name_helper_1 = require("../../../common/helper/get-va
|
|
|
10
10
|
* @returns The generated Zod schema
|
|
11
11
|
*/
|
|
12
12
|
function generateZodSchema(modelName, fields, config) {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
const schemaName = (0,
|
|
14
|
+
get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(
|
|
15
|
+
modelName,
|
|
16
|
+
config,
|
|
17
|
+
);
|
|
18
|
+
return `export const ${schemaName} = z.object({\n${fields}\n})`;
|
|
15
19
|
}
|
|
@@ -5,10 +5,13 @@ import type { Config } from "..";
|
|
|
5
5
|
* @param config - The configuration for the generator
|
|
6
6
|
* @returns The generated Zod schemas
|
|
7
7
|
*/
|
|
8
|
-
export declare function generateZodSchemas(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
export declare function generateZodSchemas(
|
|
9
|
+
modelFields: {
|
|
10
|
+
documentation: string;
|
|
11
|
+
modelName: string;
|
|
12
|
+
fieldName: string;
|
|
13
|
+
validation: string | null;
|
|
14
|
+
comment: string[];
|
|
15
|
+
}[],
|
|
16
|
+
config: Config,
|
|
17
|
+
): string;
|
|
@@ -10,11 +10,18 @@ const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
|
10
10
|
* @returns The generated Zod schemas
|
|
11
11
|
*/
|
|
12
12
|
function generateZodSchemas(modelFields, config) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
const modelName = modelFields[0].modelName;
|
|
14
|
+
const modelDoc = modelFields[0].documentation || "";
|
|
15
|
+
const fields = (0, generate_zod_properties_1.generateZodProperties)(
|
|
16
|
+
modelFields,
|
|
17
|
+
config,
|
|
18
|
+
);
|
|
19
|
+
if (!(modelDoc || !config?.comment)) {
|
|
20
|
+
return (0, generate_zod_schema_1.generateZodSchema)(
|
|
21
|
+
modelName,
|
|
22
|
+
fields,
|
|
23
|
+
config,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return `${(0, generate_zod_schema_1.generateZodSchema)(modelName, fields, config)}`;
|
|
20
27
|
}
|
|
@@ -6,4 +6,7 @@ import type { Model } from "../../../common/type";
|
|
|
6
6
|
* @param config - The configuration for the generator
|
|
7
7
|
* @returns The generated Zod schemas and types
|
|
8
8
|
*/
|
|
9
|
-
export declare function generateZod(
|
|
9
|
+
export declare function generateZod(
|
|
10
|
+
models: readonly Model[],
|
|
11
|
+
config: Config,
|
|
12
|
+
): string;
|
|
@@ -15,40 +15,55 @@ const ZOD_IMPORT = `import { z } from 'zod'\n`;
|
|
|
15
15
|
* @returns The generated Zod schemas and types
|
|
16
16
|
*/
|
|
17
17
|
function generateZod(models, config) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
18
|
+
const modelInfos = models.map((model) => {
|
|
19
|
+
return {
|
|
20
|
+
documentation: model.documentation ?? "",
|
|
21
|
+
name: model.name,
|
|
22
|
+
fields: model.fields,
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
const modelFields = modelInfos.map((model) => {
|
|
26
|
+
const fields = model.fields.map((field) => ({
|
|
27
|
+
documentation: model.documentation,
|
|
28
|
+
modelName: model.name,
|
|
29
|
+
fieldName: field.name,
|
|
30
|
+
comment: (0, is_zod_documentation_1.isZodDocumentValidation)(
|
|
31
|
+
field.documentation,
|
|
32
|
+
),
|
|
33
|
+
validation: (0, is_zod_validation_1.isZodValidation)(field.documentation),
|
|
34
|
+
}));
|
|
35
|
+
return fields;
|
|
36
|
+
});
|
|
37
|
+
// null exclude
|
|
38
|
+
const validFields = (0, is_fields_validation_1.isFieldsValidation)(
|
|
39
|
+
modelFields,
|
|
40
|
+
);
|
|
41
|
+
// group by model
|
|
42
|
+
const groupedByModel = (0, group_by_model_helper_1.groupByModelHelper)(
|
|
43
|
+
validFields,
|
|
44
|
+
);
|
|
45
|
+
const zods = Object.values(groupedByModel).map((fields) => {
|
|
46
|
+
return {
|
|
47
|
+
generateZodSchema: (0, generate_zod_schemas_1.generateZodSchemas)(
|
|
48
|
+
fields,
|
|
49
|
+
config,
|
|
50
|
+
),
|
|
51
|
+
generateZodInfer:
|
|
52
|
+
config.type === "true"
|
|
53
|
+
? (0, generate_zod_infer_1.generateZodInfer)(
|
|
54
|
+
fields[0].modelName,
|
|
55
|
+
config,
|
|
56
|
+
)
|
|
57
|
+
: "",
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
return [
|
|
61
|
+
ZOD_IMPORT,
|
|
62
|
+
"",
|
|
63
|
+
zods
|
|
64
|
+
.flatMap(({ generateZodSchema, generateZodInfer }) =>
|
|
65
|
+
[generateZodSchema, generateZodInfer].filter(Boolean),
|
|
66
|
+
)
|
|
67
|
+
.join("\n\n"),
|
|
68
|
+
].join("\n");
|
|
54
69
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { GeneratorOptions } from "@prisma/generator-helper";
|
|
2
2
|
export type Config = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
output?: string;
|
|
4
|
+
file?: string | string[];
|
|
5
|
+
schemaName?: "PascalCase" | "camelCase" | string | string[];
|
|
6
|
+
typeName?: "PascalCase" | "camelCase" | string | string[];
|
|
7
|
+
type?: boolean | string | string[];
|
|
8
|
+
comment?: boolean | string | string[];
|
|
9
9
|
};
|
|
10
10
|
export declare function main(options: GeneratorOptions): Promise<void>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
exports.main = main;
|
|
7
9
|
const generator_helper_1 = require("@prisma/generator-helper");
|
|
@@ -9,45 +11,49 @@ const generate_zod_1 = require("./generator/generate-zod");
|
|
|
9
11
|
const prettier_1 = require("prettier");
|
|
10
12
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
13
|
const DEFAULT_CONFIG = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
output: "./zod",
|
|
15
|
+
file: "index.ts",
|
|
16
|
+
schemaName: "PascalCase",
|
|
17
|
+
typeName: "PascalCase",
|
|
18
|
+
type: false,
|
|
19
|
+
comment: false,
|
|
18
20
|
};
|
|
19
21
|
async function main(options) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
22
|
+
const config = {
|
|
23
|
+
output: options.generator.output?.value ?? DEFAULT_CONFIG.output,
|
|
24
|
+
file: options.generator.config?.file ?? DEFAULT_CONFIG.file,
|
|
25
|
+
schemaName:
|
|
26
|
+
options.generator.config?.schemaName ?? DEFAULT_CONFIG.schemaName,
|
|
27
|
+
typeName: options.generator.config?.typeName ?? DEFAULT_CONFIG.typeName,
|
|
28
|
+
type: options.generator.config?.type ?? DEFAULT_CONFIG.type,
|
|
29
|
+
comment: options.generator.config?.comment === "true",
|
|
30
|
+
};
|
|
31
|
+
const content = (0, generate_zod_1.generateZod)(
|
|
32
|
+
options.dmmf.datamodel.models,
|
|
33
|
+
config,
|
|
34
|
+
);
|
|
35
|
+
const code = await (0, prettier_1.format)(content, {
|
|
36
|
+
parser: "typescript",
|
|
37
|
+
printWidth: 100,
|
|
38
|
+
singleQuote: true,
|
|
39
|
+
semi: false,
|
|
40
|
+
});
|
|
41
|
+
if (!config.output) {
|
|
42
|
+
throw new Error("output is required");
|
|
43
|
+
}
|
|
44
|
+
if (!node_fs_1.default.existsSync(config.output)) {
|
|
45
|
+
node_fs_1.default.mkdirSync(config.output, { recursive: true });
|
|
46
|
+
}
|
|
47
|
+
const file = config.file ?? "index.ts";
|
|
48
|
+
const filePath = `${config.output}/${file}`;
|
|
49
|
+
node_fs_1.default.writeFileSync(filePath, code);
|
|
44
50
|
}
|
|
45
51
|
(0, generator_helper_1.generatorHandler)({
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
onManifest() {
|
|
53
|
+
return {
|
|
54
|
+
defaultOutput: "./zod/",
|
|
55
|
+
prettyName: "Hekireki-Zod",
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
onGenerate: main,
|
|
53
59
|
});
|
|
@@ -3,4 +3,6 @@
|
|
|
3
3
|
* @param documentation - The documentation of the field
|
|
4
4
|
* @returns The Zod documentation
|
|
5
5
|
*/
|
|
6
|
-
export declare function isZodDocumentValidation(
|
|
6
|
+
export declare function isZodDocumentValidation(
|
|
7
|
+
documentation?: string,
|
|
8
|
+
): string[];
|
|
@@ -8,11 +8,12 @@ const VALIDATION_PATTERNS = ["@z."];
|
|
|
8
8
|
* @returns The Zod documentation
|
|
9
9
|
*/
|
|
10
10
|
function isZodDocumentValidation(documentation) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
if (!documentation) return [];
|
|
12
|
+
return documentation
|
|
13
|
+
.split("\n")
|
|
14
|
+
.filter(
|
|
15
|
+
(line) => !VALIDATION_PATTERNS.some((pattern) => line.includes(pattern)),
|
|
16
|
+
)
|
|
17
|
+
.map((line) => line.trim())
|
|
18
|
+
.filter(Boolean);
|
|
18
19
|
}
|
|
@@ -7,8 +7,7 @@ exports.isZodValidation = isZodValidation;
|
|
|
7
7
|
* @returns The Zod validation
|
|
8
8
|
*/
|
|
9
9
|
function isZodValidation(documentation) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return match ? match[1].trim() : null;
|
|
10
|
+
if (!documentation) return null;
|
|
11
|
+
const match = documentation.match(/@z\.(.+?)(?:\n|$)/);
|
|
12
|
+
return match ? match[1].trim() : null;
|
|
14
13
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hekireki",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Hekireki is a tool that generates validation schemas for Zod and Valibot, as well as ER diagrams, from Prisma schemas annotated with comments.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"prisma",
|
|
8
8
|
"zod",
|