-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: generate zod schema from types (#1798)
- Loading branch information
1 parent
081cbc5
commit 93c6fba
Showing
10 changed files
with
763 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
"author": "Steve Korshakov <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"gen:config": "ts-to-zod -k --skipValidation src/config/config.ts src/config/config.zod.ts", | ||
"gen:grammar:old": "ohm generateBundles --withTypes src/grammar/prev/*.ohm", | ||
"gen:grammar:new": "pgen src/grammar/next/grammar.gg -o src/grammar/next/grammar.ts", | ||
"gen:grammar": "yarn gen:grammar:old && yarn gen:grammar:new", | ||
|
@@ -94,8 +95,8 @@ | |
"@types/node": "^22.5.0", | ||
"@typescript-eslint/eslint-plugin": "^8.21.0", | ||
"@typescript-eslint/parser": "^8.21.0", | ||
"cli-table3": "^0.6.5", | ||
"chalk": "4.1.2", | ||
"cli-table3": "^0.6.5", | ||
"cross-env": "^7.0.3", | ||
"cspell": "^8.8.3", | ||
"diff": "^7.0.0", | ||
|
@@ -110,6 +111,7 @@ | |
"prettier": "^3.2.5", | ||
"ts-jest": "^29.0.3", | ||
"ts-node": "^10.9.1", | ||
"ts-to-zod": "^3.15.0", | ||
"typescript": "~5.6.2" | ||
}, | ||
"publishConfig": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
export type SafetyOptions = { | ||
/** | ||
* If set to `true`, enables run-time null checks for the `!!` operator. Default is `true`. | ||
*/ | ||
readonly nullChecks?: boolean; | ||
}; | ||
|
||
export type ExperimentalOptions = { | ||
/** | ||
* If set to true, enables inlining of all functions in contracts. | ||
* This can reduce gas usage at the cost of bigger contracts. | ||
*/ | ||
readonly inline?: boolean; | ||
}; | ||
|
||
/** | ||
* Per-project configuration options | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects | ||
*/ | ||
export type Options = { | ||
/** | ||
* If set to true, enables debug output of a contract and allows usage of `dump()` function, | ||
* which is useful for debugging purposes. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/debug | ||
*/ | ||
readonly debug?: boolean; | ||
/** | ||
* If set to true, enables support of external message receivers. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/external | ||
*/ | ||
readonly external?: boolean; | ||
/** | ||
* If set to true, enables generation of a getter with IPFS links describing the contract's ABI. | ||
* | ||
* Read more: https://docs.tact-lang.org/ref/evolution/otp-003 | ||
*/ | ||
readonly ipfsAbiGetter?: boolean; | ||
/** | ||
* If set to true, enables generation of a getter with a list of interfaces provided by the contract. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/contracts#interfaces | ||
*/ | ||
readonly interfacesGetter?: boolean; | ||
/** | ||
* If set to "new", uses new parser. If set to "old", uses legacy parser. Default is "old". | ||
*/ | ||
readonly parser?: "new" | "old"; | ||
/** | ||
* Experimental options that might be removed in the future. Use with caution! | ||
*/ | ||
readonly experimental?: ExperimentalOptions; | ||
/** | ||
* Safety options for the contract. | ||
*/ | ||
readonly safety?: SafetyOptions; | ||
/** | ||
* If set to true, enables generation of `lazy_deployment_completed()` getter. | ||
*/ | ||
readonly enableLazyDeploymentCompletedGetter?: boolean; | ||
}; | ||
|
||
export type Mode = "fullWithDecompilation" | "full" | "funcOnly" | "checkOnly"; | ||
|
||
/** | ||
* Per-project configuration options | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects | ||
*/ | ||
export type Project = { | ||
/** | ||
* Name of the project. All generated files are prefixed with it. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-name | ||
*/ | ||
name: string; | ||
/** | ||
* Path to the project's Tact file. You can only specify one Tact file per project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-path | ||
*/ | ||
path: string; | ||
/** | ||
* Path to the directory where all generated files will be placed. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-output | ||
*/ | ||
output: string; | ||
/** | ||
* Compilation options for the project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-options | ||
*/ | ||
options?: Options; | ||
/** | ||
* Compilation mode of the project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-mode | ||
*/ | ||
mode?: Mode; | ||
|
||
/** | ||
* Set verbosity level (higher = more details), default: 1 | ||
*/ | ||
verbose?: number; | ||
}; | ||
|
||
/** | ||
* Compiler configuration schema | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config | ||
*/ | ||
export type Config = { | ||
/** | ||
* A property for specifying a path or URL to the JSON schema of tact.config.json | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#schema | ||
*/ | ||
$schema?: string; | ||
/** | ||
* List of Tact projects with respective compilation options. Each .tact file represents its own Tact project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects | ||
*/ | ||
projects: readonly Project[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Generated by ts-to-zod | ||
import { z } from "zod"; | ||
import * as C from "./config"; | ||
|
||
export const safetyOptionsSchema: z.ZodType<C.SafetyOptions> = z.object({ | ||
nullChecks: z.boolean().optional(), | ||
}); | ||
|
||
/** | ||
* Per-project configuration options | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects | ||
*/ | ||
export const optionsSchema: z.ZodType<C.Options> = z.object({ | ||
/** | ||
* If set to true, enables debug output of a contract and allows usage of `dump()` function, | ||
* which is useful for debugging purposes. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/debug | ||
*/ | ||
debug: z.boolean().optional(), | ||
/** | ||
* If set to true, enables support of external message receivers. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/external | ||
*/ | ||
external: z.boolean().optional(), | ||
/** | ||
* If set to true, enables generation of a getter with IPFS links describing the contract's ABI. | ||
* | ||
* Read more: https://docs.tact-lang.org/ref/evolution/otp-003 | ||
*/ | ||
ipfsAbiGetter: z.boolean().optional(), | ||
/** | ||
* If set to true, enables generation of a getter with a list of interfaces provided by the contract. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/contracts#interfaces | ||
*/ | ||
interfacesGetter: z.boolean().optional(), | ||
/** | ||
* If set to "new", uses new parser. If set to "old", uses legacy parser. Default is "old". | ||
*/ | ||
parser: z.union([z.literal("new"), z.literal("old")]).optional(), | ||
/** | ||
* Experimental options that might be removed in the future. Use with caution! | ||
*/ | ||
experimental: z | ||
.object({ | ||
/** | ||
* If set to true, enables inlining of all functions in contracts. | ||
* This can reduce gas usage at the cost of bigger contracts. | ||
*/ | ||
inline: z.boolean().optional(), | ||
}) | ||
.optional(), | ||
/** | ||
* Safety options for the contract. | ||
*/ | ||
safety: safetyOptionsSchema.optional(), | ||
/** | ||
* If set to true, enables generation of `lazy_deployment_completed()` getter. | ||
*/ | ||
enableLazyDeploymentCompletedGetter: z.boolean().optional(), | ||
}); | ||
|
||
export const modeSchema: z.ZodType<C.Mode> = z.union([ | ||
z.literal("fullWithDecompilation"), | ||
z.literal("full"), | ||
z.literal("funcOnly"), | ||
z.literal("checkOnly"), | ||
]); | ||
|
||
/** | ||
* Per-project configuration options | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects | ||
*/ | ||
export const projectSchema: z.ZodType<C.Project> = z.object({ | ||
/** | ||
* Name of the project. All generated files are prefixed with it. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-name | ||
*/ | ||
name: z.string(), | ||
/** | ||
* Path to the project's Tact file. You can only specify one Tact file per project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-path | ||
*/ | ||
path: z.string(), | ||
/** | ||
* Path to the directory where all generated files will be placed. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-output | ||
*/ | ||
output: z.string(), | ||
/** | ||
* Compilation options for the project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-options | ||
*/ | ||
options: optionsSchema.optional(), | ||
/** | ||
* Compilation mode of the project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects-mode | ||
*/ | ||
mode: modeSchema.optional(), | ||
/** | ||
* Set verbosity level (higher = more details), default: 1 | ||
*/ | ||
verbose: z.number().optional(), | ||
}); | ||
|
||
/** | ||
* Compiler configuration schema | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config | ||
*/ | ||
export const configSchema: z.ZodType<C.Config> = z.object({ | ||
/** | ||
* A property for specifying a path or URL to the JSON schema of tact.config.json | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#schema | ||
*/ | ||
$schema: z.string().optional(), | ||
/** | ||
* List of Tact projects with respective compilation options. Each .tact file represents its own Tact project. | ||
* | ||
* Read more: https://docs.tact-lang.org/book/config#projects | ||
*/ | ||
projects: z.array(projectSchema), | ||
}); |
Oops, something went wrong.