Skip to content

Commit

Permalink
feat: handle defaults (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyichv authored Sep 21, 2024
1 parent 91e3725 commit 316e685
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-guests-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"arke-zod": patch
---

feat: handle defaults
18 changes: 18 additions & 0 deletions src/core/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function buildSchemaString(arke: TUnit, parameters: Parameter[]) {
parseType(param),
parseMinMax(param),
parseRequired(param),
parseDefault(param),
].join("");
return `${param.id}: ${zodStr},`;
})
Expand Down Expand Up @@ -92,3 +93,20 @@ function parseMinMax(parameter: Parameter) {

return result;
}

function parseDefault(parameter: Parameter) {
let defaultValue: string | null = parameter.default;
if (
defaultValue !== undefined &&
defaultValue !== null &&
(typeof defaultValue !== "object" || Object.keys(defaultValue).length > 0)
) {
if (parameter.type === "string") {
defaultValue = `"${defaultValue}"`;
}

return `.default(${defaultValue})`;
}

return "";
}
1 change: 1 addition & 0 deletions src/utils/temp-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type BaseParameter = {
| "boolean"
| "binary";
required: boolean;
default: null;
};

export type DynamicParameter = BaseParameter & {
Expand Down

0 comments on commit 316e685

Please sign in to comment.